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 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 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 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; }