public function send() { $this->load->model('checkout/order'); $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']); $params = array("sellerId" => $this->config->get('twocheckout_account'), "merchantOrderId" => $this->session->data['order_id'], "token" => $this->request->get['token'], "currency" => $order_info['currency_code'], "total" => $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false), "billingAddr" => array("name" => $order_info['payment_firstname'] . ' ' . $order_info['payment_lastname'], "addrLine1" => $order_info['payment_address_1'], "addrLine2" => $order_info['payment_address_2'], "city" => $order_info['payment_city'], "state" => $order_info['payment_iso_code_2'] == 'US' || $order_info['payment_iso_code_2'] == 'CA' ? $order_info['payment_zone'] : 'XX', "zipCode" => $order_info['payment_postcode'], "country" => $order_info['payment_country'], "email" => $order_info['email'], "phoneNumber" => $order_info['telephone'])); if ($this->cart->hasShipping()) { $shipping = array("shippingAddr" => array("name" => $order_info['shipping_firstname'] . ' ' . $order_info['shipping_lastname'], "addrLine1" => $order_info['shipping_address_1'], "addrLine2" => $order_info['shipping_address_2'], "city" => $order_info['shipping_city'], "state" => $order_info['shipping_zone'], "zipCode" => $order_info['shipping_postcode'], "country" => $order_info['shipping_country'], "email" => $order_info['email'], "phoneNumber" => $order_info['telephone'])); $params = array_merge($params, $shipping); } try { if ($this->config->get('twocheckout_test')) { TwocheckoutApi::setCredentials($this->config->get('twocheckout_account'), $this->config->get('twocheckout_private_key'), 'sandbox'); } else { TwocheckoutApi::setCredentials($this->config->get('twocheckout_account'), $this->config->get('twocheckout_private_key')); } $charge = Twocheckout_Charge::auth($params); $this->model_checkout_order->confirm($this->session->data['order_id'], $this->config->get('config_order_status_id')); $message = '2Checkout Order: ' . $charge['response']['orderNumber']; $this->model_checkout_order->update($this->session->data['order_id'], $this->config->get('twocheckout_order_status_id'), $message, false); $charge['oc_redirect'] = $this->url->link('checkout/success', '', 'SSL'); $this->response->setOutput(json_encode($charge)); } catch (Twocheckout_Error $e) { $error = array("error" => $e->getMessage()); $this->response->setOutput(json_encode($error)); } }
static function setCredentials($sid, $privateKey, $mode = '') { self::$sid = $sid; self::$privateKey = $privateKey; if ($mode == 'sandbox') { self::$apiUrl = 'https://sandbox.2checkout.com/checkout/api/1/' . $sid . '/rs/authService'; } else { self::$apiUrl = 'https://www.2checkout.com/checkout/api/1/' . $sid . '/rs/authService'; } }
/** * Process the payment and return the result * * @access public * @param int $order_id * @return array */ function process_payment($order_id) { global $woocommerce; $order = new WC_Order($order_id); if ('yes' == $this->debug) { $this->log('Generating payment form for order ' . $order->get_order_number() . '. Notify URL: ' . $this->notify_url); } // 2Checkout Args $twocheckout_args = array('token' => $_POST['token'], 'sellerId' => $this->seller_id, 'currency' => get_woocommerce_currency(), 'total' => $order->get_total(), 'merchantOrderId' => $order->get_order_number(), "billingAddr" => array('name' => $order->billing_first_name . ' ' . $order->billing_last_name, 'addrLine1' => $order->billing_address_1, 'addrLine2' => $order->billing_address_2, 'city' => $order->billing_city, 'state' => $order->billing_state, 'zipCode' => $order->billing_postcode, 'country' => $order->billing_country, 'email' => $order->billing_email, 'phoneNumber' => $order->billing_phone)); try { if ($this->sandbox == 'yes') { TwocheckoutApi::setCredentials($this->seller_id, $this->private_key, 'sandbox'); } else { TwocheckoutApi::setCredentials($this->seller_id, $this->private_key); } $charge = Twocheckout_Charge::auth($twocheckout_args); if ($charge['response']['responseCode'] == 'APPROVED') { $order->payment_complete(); return array('result' => 'success', 'redirect' => $this->get_return_url($order)); } } catch (Twocheckout_Error $e) { wc_add_notice($e->getMessage(), $notice_type = 'error'); return; } }
function processPayment($token) { include dirname(__FILE__) . '/lib/Twocheckout/TwocheckoutApi.php'; $cart = $this->context->cart; $user = $this->context->customer; $delivery = new Address(intval($cart->id_address_delivery)); $invoice = new Address(intval($cart->id_address_invoice)); $customer = new Customer(intval($cart->id_customer)); $currencies = Currency::getCurrencies(); $authorized_currencies = array_flip(explode(',', $this->currencies)); $currencies_used = array(); foreach ($currencies as $key => $currency) { if (isset($authorized_currencies[$currency['id_currency']])) { $currencies_used[] = $currencies[$key]; } } foreach ($currencies_used as $currency) { if ($currency['id_currency'] == $cart->id_currency) { $order_currency = $currency['iso_code']; } } try { $params = array("sellerId" => Configuration::get('TWOCHECKOUT_SID'), "merchantOrderId" => $cart->id, "token" => $token, "currency" => $order_currency, "total" => number_format($cart->getOrderTotal(true, 3), 2, '.', ''), "billingAddr" => array("name" => $invoice->firstname . ' ' . $invoice->lastname, "addrLine1" => $invoice->address1, "addrLine2" => $invoice->address2, "city" => $invoice->city, "state" => $invoice->country == "United States" || $invoice->country == "Canada" ? State::getNameById($invoice->id_state) : 'XX', "zipCode" => $invoice->postcode, "country" => $invoice->country, "email" => $customer->email, "phoneNumber" => $invoice->phone)); if ($delivery) { $shippingAddr = array("name" => $delivery->firstname . ' ' . $delivery->lastname, "addrLine1" => $delivery->address1, "addrLine2" => $delivery->address2, "city" => $delivery->city, "state" => (Validate::isLoadedObject($delivery) and $delivery->id_state) ? new State(intval($delivery->id_state)) : 'XX', "zipCode" => $delivery->postcode, "country" => $delivery->country); array_merge($shippingAddr, $params); } if (Configuration::get('TWOCHECKOUT_SANDBOX')) { TwocheckoutApi::setCredentials(Configuration::get('TWOCHECKOUT_SID'), Configuration::get('TWOCHECKOUT_PRIVATE'), 'sandbox'); } else { TwocheckoutApi::setCredentials(Configuration::get('TWOCHECKOUT_SID'), Configuration::get('TWOCHECKOUT_PRIVATE')); } $charge = Twocheckout_Charge::auth($params); } catch (Twocheckout_Error $e) { $message = 'Payment Authorization Failed'; Tools::redirect('index.php?controller=order&step=3&twocheckouterror=' . $message); } if (isset($charge['response']['responseCode'])) { $order_status = (int) Configuration::get('TWOCHECKOUT_ORDER_STATUS'); $message = $charge['response']['responseMsg']; $this->validateOrder((int) $this->context->cart->id, _PS_OS_PAYMENT_, $charge['response']['total'], $this->displayName, $message, array(), null, false, $this->context->customer->secure_key); Tools::redirect('index.php?controller=order-confirmation?key=' . $user->secure_key . '&id_cart=' . (int) $cart->id . '&id_module=' . (int) $this->module->id . '&id_order=' . (int) $this->module->currentOrder); } else { $message = 'Payment Authorization Failed'; Tools::redirect('index.php?controller=order&step=3&twocheckouterror=' . $message); } }