function validate_order($order_hash, $total, $customer_name, $email, $shipping_street_address, $shipping_city, $shipping_state, $shipping_zip, $shipping_country, $phone)
 {
     $retval = FALSE;
     $order_mapper = C_Order_Mapper::get_instance();
     if ($order = $order_mapper->find_by_hash($order_hash)) {
         // Has fraud been detected?
         $cart = new C_NextGen_Pro_Cart($order->cart);
         if ($cart->get_total($order->use_home_country) == $total) {
             $order->customer_name = $customer_name;
             $order->email = $email;
             $order->shipping_street_address = $shipping_street_address;
             $order->shipping_city = $shipping_city;
             $order->shipping_state = $shipping_state;
             $order->shipping_zip = $shipping_zip;
             $order->shipping_country = $shipping_country;
             $retval = $order;
         }
         // Fraud detected
         $order->status = 'fraud';
     }
     return $retval;
 }
 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 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 get_shipping_amount_action()
 {
     $cart = new C_NextGen_Pro_Cart($this->param('cart'));
     return array('shipping' => $cart->get_shipping($this->param('use_home_country')));
 }
 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 output_order_column($column_name, $post_id)
 {
     global $post;
     $order_mapper = C_Order_Mapper::get_instance();
     $entity = $order_mapper->unserialize($post->post_content);
     switch ($column_name) {
         case 'order_gateway':
             $str = '';
             switch ($entity['payment_gateway']) {
                 case 'cheque':
                     $str = __('Cheque', 'nextgen-gallery-pro');
                     break;
                 case 'paypal_standard':
                 case 'paypal_express_checkout':
                     $str = __('PayPal', 'nextgen-gallery-pro');
                     break;
                 case 'stripe_checkout':
                     $str = __('Stripe', 'nextgen-gallery-pro');
                     break;
                 case 'test_gateway':
                     $str = __('Test', 'nextgen-gallery-pro');
                     break;
                 default:
                     break;
             }
             echo $str;
             break;
         case 'order_total':
             $cart = new C_NextGen_Pro_Cart($entity['cart']);
             $use_home_country = isset($entity['use_home_country']) ? $entity['use_home_country'] : FALSE;
             echo $this->get_formatted_price($cart->get_total($use_home_country));
             break;
         case 'order_status':
             echo esc_html($entity['status']);
             break;
         case 'order_hash':
             echo esc_html($post_id);
             break;
         case 'order_customer':
             $checkout = C_NextGen_Pro_Checkout::get_instance();
             $url = esc_attr($checkout->get_thank_you_page_url($entity['hash']));
             $name = esc_html($entity['customer_name']);
             echo "<a href='{$url}' target='_blank'>{$name}</a>";
             break;
     }
 }
 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;
 }