Skip to content

Commit

Permalink
2Checkout fixes
Browse files Browse the repository at this point in the history
1. fixed SSL links in admin
2. fixed USD conversion. Cause 2Checkout works only with USD
3. fixed digital products checkout. They don't require shipping address now on 2Checkout gateway page
4. Fixed redirects
5. Updated form field names according to latest 2Checkout documentation
  • Loading branch information
stalker780 committed Feb 9, 2018
1 parent 1206416 commit a884575
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 34 deletions.
12 changes: 6 additions & 6 deletions upload/admin/controller/extension/payment/twocheckout.php
Expand Up @@ -14,7 +14,7 @@ public function index() {

$this->session->data['success'] = $this->language->get('text_success');

$this->response->redirect($this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment'));
$this->response->redirect($this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true));
}

if (isset($this->error['warning'])) {
Expand All @@ -39,22 +39,22 @@ public function index() {

$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
);

$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_extension'),
'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment')
'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true)
);

$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('extension/payment/twocheckout', 'user_token=' . $this->session->data['user_token'])
'href' => $this->url->link('extension/payment/twocheckout', 'user_token=' . $this->session->data['user_token'], true)
);

$data['action'] = $this->url->link('extension/payment/twocheckout', 'user_token=' . $this->session->data['user_token']);
$data['action'] = $this->url->link('extension/payment/twocheckout', 'user_token=' . $this->session->data['user_token'], true);

$data['cancel'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment');
$data['cancel'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true);

if (isset($this->request->post['payment_twocheckout_account'])) {
$data['payment_twocheckout_account'] = $this->request->post['payment_twocheckout_account'];
Expand Down
84 changes: 62 additions & 22 deletions upload/catalog/controller/extension/payment/twocheckout.php
@@ -1,5 +1,8 @@
<?php
class ControllerExtensionPaymentTwoCheckout extends Controller {

// https://www.2checkout.com/documentation/checkout/inline-checkout

public function index() {
$data['button_confirm'] = $this->language->get('button_confirm');

Expand All @@ -10,9 +13,11 @@ public function index() {
$data['action'] = 'https://www.2checkout.com/checkout/purchase';

$data['sid'] = $this->config->get('payment_twocheckout_account');
$data['currency_code'] = $order_info['currency_code'];
$data['total'] = $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false);
$data['cart_order_id'] = $this->session->data['order_id'];
//$data['currency_code'] = $order_info['currency_code'];
$data['currency_code'] = 'USD';
//$data['total'] = $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false);
$data['total'] = $this->currency->format($order_info['total'], 'USD', $this->currency->getValue('USD'), false);
$data['merchant_order_id'] = $this->session->data['order_id'];
$data['card_holder_name'] = $order_info['payment_firstname'] . ' ' . $order_info['payment_lastname'];
$data['street_address'] = $order_info['payment_address_1'];
$data['city'] = $order_info['payment_city'];
Expand All @@ -28,7 +33,9 @@ public function index() {
$data['email'] = $order_info['email'];
$data['phone'] = $order_info['telephone'];

if ($this->cart->hasShipping()) {
$data['shipping'] = (int)$this->cart->hasShipping();

if ($data['shipping']) {
$data['ship_street_address'] = $order_info['shipping_address_1'];
$data['ship_city'] = $order_info['shipping_city'];
$data['ship_state'] = $order_info['shipping_zone'];
Expand All @@ -52,7 +59,8 @@ public function index() {
'name' => $product['name'],
'description' => $product['name'],
'quantity' => $product['quantity'],
'price' => $this->currency->format($product['price'], $order_info['currency_code'], $order_info['currency_value'], false)
'price' => $this->currency->format($product['price'], $order_info['currency_code'], $order_info['currency_value'], false),
'shipping' => !empty($product['shipping']) ? 'Y' : 'N'
);
}

Expand All @@ -68,17 +76,36 @@ public function index() {
$data['display'] = '';
}

$data['lang'] = $this->config->get('config_language');
$data['lang'] = $this->session->data['language'];

$data['return_url'] = $this->url->link('extension/payment/twocheckout/callback', 'language=' . $this->config->get('config_language'));
$data['return_url'] = $this->url->link('extension/payment/twocheckout/callback', '', true);

return $this->load->view('extension/payment/twocheckout', $data);
}

public function callback() {
$this->load->model('checkout/order');

$order_info = $this->model_checkout_order->getOrder($this->request->post['cart_order_id']);
$redirect = $this->url->link('checkout/checkout');

$order_info = array();

if (empty($this->request->post)) {
echo 'Redirecting...<br><br> If you see this message more than one minute, something went wrong with your payment, contact website administrator.';
return false;
}

if (!empty($this->request->post['merchant_order_id'])) {
$order_info = $this->model_checkout_order->getOrder($this->request->post['merchant_order_id']);
}

if (empty($order_info)) {
$this->log->write('2CO error: wrong or empty merchant_order_id');
echo 'Wrong parameters! Contact website administrator!';
return false;
} else {
$redirect = $this->url->link('checkout/success');
}

if (!$this->config->get('payment_twocheckout_test')) {
$order_number = $this->request->post['order_number'];
Expand All @@ -87,28 +114,41 @@ public function callback() {
}

if (strtoupper(md5($this->config->get('payment_twocheckout_secret') . $this->config->get('payment_twocheckout_account') . $order_number . $this->request->post['total'])) == $this->request->post['key']) {
$comment = '';

ksort($this->request->post);

foreach ($this->request->post as $param => $value) {
$comment .= $param . ': ' . print_r($value, true) . "\n";
}

$comment = trim($comment);

if ($this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false) == $this->request->post['total']) {
$this->model_checkout_order->addOrderHistory($this->request->post['cart_order_id'], $this->config->get('payment_twocheckout_order_status_id'));
$this->model_checkout_order->addOrderHistory($this->request->post['merchant_order_id'], $this->config->get('payment_twocheckout_order_status_id'), $comment, false);
} else {
$this->model_checkout_order->addOrderHistory($this->request->post['cart_order_id'], $this->config->get('config_order_status_id'));// Ugh. Some one've faked the sum. What should we do? Probably drop a mail to the shop owner?
$this->model_checkout_order->addOrderHistory($this->request->post['merchant_order_id'], $this->config->get('config_order_status_id'), $comment, false);// Ugh. Some one've faked the sum. What should we do? Probably drop a mail to the shop owner?
$this->log->write('2CO: Total value is different from order total' . $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false) . ' => ' . $this->request->post['total']);
}

//exit();
} else {
echo 'The response from 2checkout.com can\'t be parsed. Contact site administrator, please!';
return false;
}

// We can't use $this->response->redirect() here, because of 2CO behavior. It fetches this page
// on behalf of the user and thus user (and his browser) see this as located at 2checkout.com
// domain. So user's cookies are not here and he will see empty basket and probably other
// weird things.

echo '<html>' . "\n";
echo '<head>' . "\n";
echo ' <meta http-equiv="Refresh" content="0; url=' . $this->url->link('checkout/success', 'language=' . $this->config->get('config_language')) . '">' . "\n";
echo '</head>' . "\n";
echo '<body>' . "\n";
echo ' <p>Please follow <a href="' . $this->url->link('checkout/success', 'language=' . $this->config->get('config_language')) . '">link</a>!</p>' . "\n";
echo '</body>' . "\n";
echo '</html>' . "\n";
exit();
} else {
echo 'The response from 2checkout.com can\'t be parsed. Contact site administrator, please!';
}
echo '<html>' . "\n";
echo '<head>' . "\n";
echo ' <meta http-equiv="Refresh" content="0; url=' . $redirect . '">' . "\n";
echo '</head>' . "\n";
echo '<body>' . "\n";
echo ' <p>Please follow <a href="' . $redirect . '">link</a>!</p>' . "\n";
echo '</body>' . "\n";
echo '</html>' . "\n";
}
}
@@ -1,7 +1,8 @@
<form action="{{ action }}" method="post">
<input type="hidden" name="sid" value="{{ sid }}" />
<input type="hidden" name="mode" value="2CO" />
<input type="hidden" name="merchant_order_id" value="{{ merchant_order_id }}" />
<input type="hidden" name="total" value="{{ total }}" />
<input type="hidden" name="cart_order_id" value="{{ cart_order_id }}" />
<input type="hidden" name="card_holder_name" value="{{ card_holder_name }}" />
<input type="hidden" name="street_address" value="{{ street_address }}" />
<input type="hidden" name="city" value="{{ city }}" />
Expand All @@ -10,17 +11,23 @@
<input type="hidden" name="country" value="{{ country }}" />
<input type="hidden" name="email" value="{{ email }}" />
<input type="hidden" name="phone" value="{{ phone }}" />
{% if shipping %}
<input type="hidden" name="ship_name" value="{{ card_holder_name }}" />
<input type="hidden" name="ship_street_address" value="{{ ship_street_address }}" />
<input type="hidden" name="ship_city" value="{{ ship_city }}" />
<input type="hidden" name="ship_state" value="{{ ship_state }}" />
<input type="hidden" name="ship_zip" value="{{ ship_zip }}" />
<input type="hidden" name="ship_country" value="{{ ship_country }}" />
{% endif %}
{% set i = 0 %}
{% for product in products %}
<input type="hidden" name="c_prod_{{ i }}" value="{{ product.product_id }},{{ product.quantity }}" />
<input type="hidden" name="c_name_{{ i }}" value="{{ product.name }}" />
<input type="hidden" name="c_description_{{ i }}" value="{{ product.description }}" />
<input type="hidden" name="c_price_{{ i }}" value="{{ product.price }}" />
<input type="hidden" name="li_{{ i }}_product_id" value="{{ product.product_id }}" />
<input type="hidden" name="li_{{ i }}_type" value="product" />
<input type="hidden" name="li_{{ i }}_name" value="{{ product.name }}" />
{#<input type="hidden" name="li_{{ i }}_description" value="{{ product.description }}" />#}
<input type="hidden" name="li_{{ i }}_quantity" value="{{ product.quantity }}" />
<input type="hidden" name="li_{{ i }}_price" value="{{ product.price }}" />
<input type="hidden" name="li_{{ i }}_tangible" value="{{ product.shipping }}" />
{% set i = i + 1 %}
{% endfor %}
<input type="hidden" name="id_type" value="1" />
Expand All @@ -30,8 +37,11 @@
<input type="hidden" name="lang" value="{{ lang }}" />
<input type="hidden" name="return_url" value="{{ return_url }}" />
<div class="buttons">
<div class="pull-left">
<img src="https://www.2checkout.com/static/checkout/images/powered-by-2co.png">
</div>
<div class="pull-right">
<input type="submit" value="{{ button_confirm }}" class="btn btn-primary" />
</div>
</div>
</form>
</form>

0 comments on commit a884575

Please sign in to comment.