/** * Gets all digital downloads for the pricelist * @param null $image_id * @return mixed */ function get_digital_downloads($image_id = NULL) { // Find digital download items $mapper = C_Pricelist_Item_Mapper::get_instance(); $conditions = array(array("pricelist_id = %d", $this->object->id()), array("source IN %s", array(NGG_PRO_DIGITAL_DOWNLOADS_SOURCE))); $items = $mapper->select()->where($conditions)->order_by('ID', 'ASC')->run_query(); // Filter by image resolutions if ($image_id) { $image = is_object($image_id) ? $image_id : C_Image_Mapper::get_instance()->find($image_id); if ($image) { $retval = array(); $storage = C_Gallery_Storage::get_instance(); foreach ($items as $item) { $source_width = $image->meta_data['width']; $source_height = $image->meta_data['height']; // the downloads themselves come from the backup as source so if possible only filter images // whose backup file doesn't have sufficient dimensions $backup_abspath = $storage->get_backup_abspath($image); if (@file_exists($backup_abspath)) { $dimensions = @getimagesize($backup_abspath); $source_width = $dimensions[0]; $source_height = $dimensions[1]; } if (isset($item->resolution) && $item->resolution >= 0 && ($source_height >= $item->resolution or $source_width >= $item->resolution)) { $retval[] = $item; } } $items = $retval; } } return $items; }
function destroy_items($pricelist_id, $ids = array()) { global $wpdb; // If no ids have been provided, then delete all items for the given pricelist if (!$ids) { // Ensure we have the pricelist id if (!is_int($pricelist_id)) { $pricelist_id = $pricelist_id->ID; } // Find all item ids $item_mapper = C_Pricelist_Item_Mapper::get_instance(); $ids = array(); $results = $item_mapper->select("ID, post_parent")->where(array('pricelist_id = %d', $pricelist_id))->run_query(); foreach ($results as $row) { $ids[] = $row->ID; if ($row->post_parent) { $ids[] = $row->post_parent; } } } // Get unique ids $ids = array_unique($ids); // Delete all posts and post meta for the item ids $sql = array(); $sql[] = "DELETE FROM {$wpdb->posts} WHERE ID IN (" . implode(',', $ids) . ')'; $sql[] = "DELETE FROM {$wpdb->postmeta} WHERE post_id IN (" . implode(',', $ids) . ')'; foreach ($sql as $query) { $wpdb->query($query); } return TRUE; }
function cheque_checkout_action() { $retval = array(); $items = $this->param('items'); if (!$items) { return array('error' => __('Your cart is empty', 'nggallery')); } $customer = array('name' => $this->param('customer_name'), 'email' => $this->param('customer_email'), 'address' => $this->param('customer_address'), 'city' => $this->param('customer_city'), 'state' => $this->param('customer_state'), 'postal' => $this->param('customer_postal'), 'country' => $this->param('customer_country')); $retval['customer'] = $customer; // Presently we only do basic field validation: ensure that each field is filled and that // the country selected exists in C_NextGen_Pro_Currencies::$countries foreach ($customer as $key => $val) { if (empty($val)) { $retval['error'] = __('Please fill all fields and try again', 'nggallery'); break; } } // No error yet? if (!isset($retval['error'])) { if (empty(C_NextGen_Pro_Currencies::$countries[$customer['country']])) { return array('error' => __('Invalid country selected, please try again.', 'nggallery')); } else { $customer['country'] = C_NextGen_Pro_Currencies::$countries[$customer['country']]['name']; } $checkout = new C_NextGen_Pro_Checkout(); $cart = new C_NextGen_Pro_Cart(); $settings = C_NextGen_Settings::get_instance(); $currency = C_NextGen_Pro_Currencies::$currencies[$settings->ecommerce_currency]; foreach ($items as $image_id => $image_items) { if ($image = C_Image_Mapper::get_instance()->find($image_id)) { $cart->add_image($image_id, $image); foreach ($image_items as $item_id => $quantity) { if ($item = C_Pricelist_Item_Mapper::get_instance()->find($item_id)) { $item->quantity = $quantity; $cart->add_item($image_id, $item_id, $item); } } } } // Calculate the total $use_home_country = intval($this->param('use_home_country')); $order_total = $cart->get_total($use_home_country); // Create the order if (!$cart->has_items()) { return array('error' => __('Your cart is empty', 'nggallery')); } $order = $checkout->create_order($cart->to_array(), $customer['name'], $customer['email'], $order_total, 'cheque', $customer['address'], $customer['city'], $customer['state'], $customer['postal'], $customer['country'], $use_home_country, 'unverified'); $order->status = 'unverified'; $order->gateway_admin_note = __('Payment was successfully made via Check. Once you have received payment, you can click “Verify” in the View Orders page and a confirmation email will be sent to the user.'); C_Order_Mapper::get_instance()->save($order); $checkout->send_email_notification($order->hash); $retval['order'] = $order->hash; $retval['redirect'] = $checkout->get_thank_you_page_url($order->hash, TRUE); } return $retval; }
function add_item($image_id, $item_id, $item_props = array()) { // Treat an object as if it were an array if (is_object($item_props)) { $item_props = get_object_vars($item_props); } // Find the item $item = C_Pricelist_Item_Mapper::get_instance()->find($item_id); // Find the image if ($image = C_Image_Mapper::get_instance()->find($image_id) and $item) { // Ensure that the image has been added if (!isset($this->_state[$image_id])) { $image->items = array(); $this->_state[$image_id] = $image; } else { $image = $this->_state[$image_id]; } // Ensure that the image has an items array if (!isset($image->items)) { $image->items = array(); } // Ensure that the items source key exists as an array if (!isset($image->items[$item->source])) { $image->items[$item->source] = array(); } // Ensure that the item's pricelist id exists as a key in the array if (!isset($image->items[$item->source][$item->pricelist_id])) { $image->items[$item->source][$item->pricelist_id] = array(); } // Has the item already been added? If so, increment it's quantity if (isset($image->items[$item->source][$item->pricelist_id][$item_id])) { $previous_quantity = intval($image->items[$item->source][$item->pricelist_id][$item_id]->quantity); $image->items[$item->source][$item->pricelist_id][$item_id]->quantity = $previous_quantity + intval($item_props['quantity']); } else { $item->quantity = isset($item_props['quantity']) ? intval($item_props['quantity']) : 1; $image->items[$item->source][$item->pricelist_id][$item_id] = $item; } } else { unset($this->_state[$image_id]); } }
function paypal_standard_order_action() { $retval = array(); if ($items = $this->param('items')) { $checkout = new C_NextGen_Pro_Checkout(); $cart = new C_NextGen_Pro_Cart(); $settings = C_NextGen_Settings::get_instance(); $currency = C_NextGen_Pro_Currencies::$currencies[$settings->ecommerce_currency]; foreach ($items as $image_id => $image_items) { if ($image = C_Image_Mapper::get_instance()->find($image_id)) { $cart->add_image($image_id, $image); foreach ($image_items as $item_id => $quantity) { if ($item = C_Pricelist_Item_Mapper::get_instance()->find($item_id)) { $item->quantity = $quantity; $cart->add_item($image_id, $item_id, $item); } } } } // Calculate the total $use_home_country = intval($this->param('use_home_country')); $order_total = $cart->get_total($use_home_country); // Create the order if ($cart->has_items()) { $order = $checkout->create_order($cart->to_array(), __('PayPal Customer', 'nggallery'), 'Unknown', $order_total, 'paypal_standard'); $order->status = 'unverified'; $order->use_home_country = $use_home_country; $order->gateway_admin_note = __('Payment was successfully made via PayPal Standard, with no further payment action required.'); C_Order_Mapper::get_instance()->save($order); $retval['order'] = $order->hash; } else { $retval['error'] = __('Your cart is empty', 'nggallery'); } } return $retval; }
function set_express_checkout() { $router = C_Router::get_instance(); $settings = C_NextGen_Settings::get_instance(); $image_mapper = C_Image_Mapper::get_instance(); $item_mapper = C_Pricelist_Item_Mapper::get_instance(); $return_url = site_url('/?ngg_ppxc_rtn=1'); $cancel_url = site_url('/?ngg_ppxc_ccl=1'); $notify_url = site_url('/?ngg_ppxc_nfy=1'); $cart = new C_NextGen_Pro_Cart(); $currency = C_NextGen_Pro_Currencies::$currencies[$settings->ecommerce_currency]; // Set up request data $data = array('RETURNURL' => $return_url, 'CANCELURL' => $cancel_url, 'CALLBACKTIMEOUT' => 6, 'NOSHIPPING' => 0, 'CALLBACKVERSION' => 61.0, 'PAYMENTREQUEST_0_NOTIFYURL' => $notify_url, 'PAYMENTREQUEST_0_PAYMENTREASON' => 'None', 'PAYMENTREQUEST_0_CURRENCYCODE' => $this->_get_paypal_currency_code(), 'PAYMENTREQUEST_0_CUSTOM' => $this->object->param('ship_to')); if ($settings->paypal_page_style) { $data['PAGESTYLE'] = $settings->paypal_page_style; } // Add items if ($cart_items = $this->param('items')) { $item_number = 0; foreach ($cart_items as $image_id => $items) { if ($image = $image_mapper->find($image_id)) { $cart->add_image($image_id, $image); foreach ($items as $item_id => $quantity) { if ($item = $item_mapper->find($item_id)) { $item->quantity = $quantity; $cart->add_item($image_id, $item_id, $item); $data['L_PAYMENTREQUEST_0_NAME' . $item_number] = $item->title . ' / ' . $image->alttext; $data['L_PAYMENTREQUEST_0_DESC' . $item_number] = $image->filename; $data['L_PAYMENTREQUEST_0_AMT' . $item_number] = sprintf("%.{$currency['exponent']}f", $item->price); $data['L_PAYMENTREQUEST_0_NUMBER' . $item_number] = "{$image_id}-{$item_id}"; $data['L_PAYMENTREQUEST_0_QTY' . $item_number] = intval($quantity); $data['L_PAYMENTREQUEST_0_ITEMCATEGORY' . $item_number] = 'Physical'; $item_number += 1; } else { $data['NOT_FOUND' . $item_number] = $item_id; } } } } } // Totals, Shipping & Taxes $subtotal = $cart->get_subtotal(); if ($this->param('ship_to') === '1') { $local = TRUE; } else { $local = FALSE; } $shipping = $cart->get_shipping($local); $data['PAYMENTREQUEST_0_SHIPPINGAMT'] = sprintf("%.{$currency['exponent']}f", $shipping); $data['PAYMENTREQUEST_0_ITEMAMT'] = sprintf("%.{$currency['exponent']}f", $subtotal); $data['PAYMENTREQUEST_0_AMT'] = sprintf("%.{$currency['exponent']}f", bcadd($subtotal, $shipping, $currency['exponent'])); // Submit the PayPal request $response = $this->_paypal_request('SetExpressCheckout', $data); if (isset($response['token'])) { if ($settings->ecommerce_paypal_sandbox) { $url = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token='; } else { $url = 'https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token='; } $response['redirect'] = $url . $response['token']; } if (isset($response['l_longmessage0'])) { $response['error'] = $response['l_longmessage0']; } if (isset($response['ERROR'])) { $response['error'] = $response['ERROR']; unset($response['ERROR']); } return $response; }
function save_action() { $retval = FALSE; // Do I need to check security token? $pricelist = $this->get_model(); // disable caching or the changes we're about to save() won't be displayed $mapper = C_Pricelist_Mapper::get_instance(); $mapper->_use_cache = FALSE; if ($pricelist->save($_REQUEST['pricelist'])) { // Reset the pricelist object $this->pricelist = $pricelist; // Create price list items $item_mapper = C_Pricelist_Item_Mapper::get_instance(); foreach ($_POST['pricelist_item'] as $id => $updates) { // Set the pricelist associated to each item $updates['pricelist_id'] = $pricelist->id(); if (strpos($id, 'new-') !== FALSE) { $item = $item_mapper->create($updates); $item->save(); } else { $item = $item_mapper->find($id, TRUE); $item->save($updates); } } if (!isset($_REQUEST['id'])) { wp_redirect(admin_url("edit.php?post_type=ngg_pricelist&id=" . $pricelist->id() . '&message=saved')); } } if (isset($_REQUEST['deleted_items'])) { $pricelist->destroy_items($_REQUEST['deleted_items']); } return $retval; }
function duplicate_pricelist() { if (isset($_REQUEST['ngg_duplicate']) && current_user_can('NextGEN Change options')) { $pricelist_mapper = C_Pricelist_Mapper::get_instance(); if ($pricelist = $pricelist_mapper->find($_REQUEST['id'], TRUE)) { // Get items for the pricelist $items = $pricelist->get_items(); // Find the unique post title $results = $pricelist_mapper->select()->where(array('post_title LIKE %s', $pricelist->post_title . '%'))->run_query(); $i = 0; foreach ($results as $p) { $number = intval(trim(str_replace($pricelist->post_title, '', $p->post_title))); if ($number > $i) { $i = $number; } } $i++; // Create new pricelist $pricelist->ID = NULL; $pricelist->post_date = $pricelist->post_date_gmt = $pricelist->post_modified = $pricelist->post_modified_gmt = NULL; $pricelist->post_title = $pricelist->title = "{$pricelist->title} {$i}"; $pricelist_mapper->save($pricelist); // Duplicate pricelist items $item_mapper = C_Pricelist_Item_Mapper::get_instance(); foreach ($items as $item) { $item->ID = NULL; $item->pricelist_id = $pricelist->ID; $item_mapper->save($item); } wp_redirect(admin_url("/edit.php?post_type={$_REQUEST['post_type']}")); } } }
function create_stripe_charge() { $retval = array(); $total = 0.0; // Include the SDK if another plugin hasn't already done so if (!class_exists('Stripe')) { include_once 'stripe-sdk/lib/Stripe.php'; } // Get Stripe input params if (($stripe = $this->param('stripe')) && isset($stripe['token']) && $this->param('items')) { $stripe = array_merge($stripe, $this->get_stripe_vars(TRUE)); // Set Stripe API key Stripe::setApiKey($stripe['private_key']); // Ensure we have sufficient data returned from Stripe Checkout $req_fields = array('customer_name', 'email', 'shipping_street_address', 'shipping_city', 'shipping_state', 'shipping_zip', 'shipping_country'); $missing_fields = array(); foreach ($req_fields as $field) { if (!isset($stripe[$field])) { $missing_fields[] = $field; } } if ($missing_fields) { $retval['error'] = __("Invalid request"); } else { $image_mapper = C_Image_Mapper::get_instance(); $item_mapper = C_Pricelist_Item_Mapper::get_instance(); $cart = new C_NextGen_Pro_Cart(); $use_home_country = $this->object->param('ship_to'); // Calculate the order total foreach ($this->param('items') as $image_id => $items) { if ($image = $image_mapper->find($image_id)) { $cart->add_image($image_id, $image); foreach ($items as $item_id => $quantity) { if ($item = $item_mapper->find($item_id)) { $item->quantity = $quantity; $cart->add_item($image_id, $item_id, $item); } } } } $subtotal = $cart->get_subtotal(); $shipping = $cart->get_shipping($use_home_country); $total = $cart->get_total($use_home_country); // Create order $order = $this->create_order($cart->to_array(), $stripe['customer_name'], $stripe['email'], $total, 'stripe_checkout', $stripe['shipping_street_address'], $stripe['shipping_city'], $stripe['shipping_state'], $stripe['shipping_zip'], $stripe['shipping_country'], $use_home_country); $order->gateway_admin_note = __('Payment was successfully made via Stripe, with no further payment action required.'); $order->save(); try { $charge_params = array('amount' => round($total, 2) * 100, 'currency' => $stripe['currency'], 'card' => $stripe['token'], 'metadata' => array('order_id' => $order->ID(), 'description' => sprintf(__('Order from %s for %s (%s)'), $stripe['site_name'], $stripe['customer_name'], $stripe['email']))); $charge = Stripe_Charge::create($charge_params); $order->stripe_data = get_object_vars($charge); if ($order->save()) { $retval['redirect'] = site_url('/?ngg_stripe_rtn=1&order=' . $order->hash); } } catch (Stripe_Error $ex) { $retval['request'] = $charge_params; $retval['error'] = $ex->getMessage(); $order->destroy(); } } } else { $retval['error'] = __('Invalid request'); } return $retval; }