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