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