if ($this->config->get('amazon_checkout_mode') == 'sandbox') {
$amazon_payment_js = 'https://static-eu.payments-amazon.com/cba/js/gb/sandbox/PaymentWidgets.js';
} elseif ($this->config->get('amazon_checkout_mode') == 'live') {
$amazon_payment_js = 'https://static-eu.payments-amazon.com/cba/js/gb/PaymentWidgets.js';
}
$this->document->addScript($amazon_payment_js);
$this->load->library('cba');
$this->load->model('checkout/order');
$this->load->model('checkout/coupon');
$this->load->model('setting/extension');
$this->load->model('account/order');
$this->load->model('payment/amazon_checkout');
$this->language->load('payment/amazon_checkout');
if (!isset($this->session->data['cba']['order_id'])) {
$this->redirect($this->url->link('common/home'));
}
if (isset($this->session->data['coupon'])) {
$coupon = $this->model_checkout_coupon->getCoupon($this->session->data['coupon']);
} else {
$coupon = array();
}
$order = $this->model_checkout_order->getOrder($this->session->data['cba']['order_id']);
$parameters_items = array();
$cba_marketplace = $this->config->get('amazon_checkout_marketplace');
switch ($cba_marketplace) {
case 'uk':
$currency_code = 'GBP';
break;
case 'de':
$currency_code = 'EUR';
break;
}
$ordered_products = $this->model_account_order->getOrderProducts($order['order_id']);
$total = 0;
$free_shipping = $this->model_payment_amazon_checkout->hasFreeShipping($order['order_id']);
$shipping_cost = $this->model_payment_amazon_checkout->getShippingPrice($order['order_id']);
if (!$free_shipping) {
$total += $shipping_cost;
}
foreach ($ordered_products as $product) {
$parameters_items['products'][] = array(
'title' => html_entity_decode($product['name'], ENT_QUOTES, 'UTF-8'),
'model' => $product['order_product_id'],
'quantity' => $product['quantity'],
'price' => $this->currency->format($product['price'] + $product['tax'], $currency_code, '', false),
);
$total += ($product['price'] + $product['tax']) * $product['quantity'];
}
$order_totals = $this->model_payment_amazon_checkout->getAdditionalCharges($order['order_id']);
foreach ($order_totals as $order_total) {
$parameters_items['products'][] = array(
'title' => $order_total['title'],
'model' => 'ot_' . $order_total['order_total_id'],
'quantity' => 1,
'price' => $this->currency->format($order_total['price'], $currency_code, '', false),
);
$total += $order_total['price'];
}
$parameters_items['currency'] = $currency_code;
$parameters_items['contract_id'] = $this->session->data['cba']['contract_id'];
$cba = new CBA($this->config->get('amazon_checkout_merchant_id'), $this->config->get('amazon_checkout_access_key'),
$this->config->get('amazon_checkout_access_secret'));
$cba->setMode($this->config->get('amazon_checkout_mode'));
if ($cba->setPurchaseItems($parameters_items) !== true) {
$this->redirect($this->url->link('payment/amazon_checkout/failure', '', 'SSL'));
}
$order_discount = $order['total'] - $total;
$parameters_charges = array();
$parameters_charges['contract_id'] = $this->session->data['cba']['contract_id'];
$parameters_charges['currency'] = $currency_code;
if ($free_shipping) {
$parameters_charges['shipping_price'] = '0.00';
} else {
$parameters_charges['shipping_price'] = $this->currency->format($shipping_cost, $currency_code, '', false);
}
if ($order_discount < 0) {
$parameters_charges['discount'] = $this->currency->format(-$order_discount, $currency_code, '', false);
}
if (!$cba->setContractCharges($parameters_charges)) {
$this->redirect($this->url->link('payment/amazon_checkout/failure', '', 'SSL'));
}
$complete_parameters = array();
$complete_parameters['contract_id'] = $this->session->data['cba']['contract_id'];
$amazon_order_ids = $cba->completePurchaseContracts($complete_parameters);
unset($this->session->data['cba']);
if ($amazon_order_ids) {
$this->model_payment_amazon_checkout->addAmazonOrderId($order['order_id'], $amazon_order_ids[0]);
} else {
$this->redirect($this->url->link('payment/amazon_checkout/failure', '', 'SSL'));
}
$this->model_checkout_order->confirm($order['order_id'], $this->config->get('amazon_checkout_order_default_status'));
$this->redirect($this->url->link('payment/amazon_checkout/success', 'amazon_order_id=' . $amazon_order_ids[0], 'SSL'));