Skip to content

Commit

Permalink
Front-end OpenCart 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Quoc Dat Pham committed Aug 30, 2017
1 parent 61653a9 commit 7b5ed77
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,26 @@ public function index()
// If POST request => validate the data before saving
if ($this->request->server['REQUEST_METHOD'] == 'POST' && $this->validate()) {
// Edit settings
$this->model_setting_setting->editSetting('lemonway', $this->request->post);
$this->model_setting_setting->editSetting($this->prefix() . 'lemonway', $this->request->post);
}

// Load default layout
$this->variables['header'] = $this->load->controller('common/header');
$this->variables['column_left'] = $this->load->controller('common/column_left');
$this->variables['footer'] = $this->load->controller('common/footer');

if (isset($this->session->data['user_token'])) { // OpenCart 3
$this->variables['cancel_link'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true);
} else { // OpenCart 2
$this->variables['cancel_link'] = $this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=payment', true);
}

$this->variables['cancel_link'] = (version_compare(VERSION, '3.0', '>=')) ? $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true) : $this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=payment', true);

// Load setting values
$this->variables['lemonway_api_login'] = $this->model_setting_setting->getSettingValue('lemonway_api_login');
$this->variables['lemonway_api_password'] = $this->model_setting_setting->getSettingValue('lemonway_api_password');
$this->variables['lemonway_is_test_mode'] = $this->model_setting_setting->getSettingValue('lemonway_is_test_mode');
$this->variables['lemonway_css_url'] = $this->model_setting_setting->getSettingValue('lemonway_css_url');
$this->variables['lemonway_debug'] = $this->model_setting_setting->getSettingValue('lemonway_debug');
$this->variables['lemonway_environment_name'] = $this->model_setting_setting->getSettingValue('lemonway_environment_name');
$this->variables['lemonway_custom_wallet'] = $this->model_setting_setting->getSettingValue('lemonway_custom_wallet');
$this->variables['lemonway_status'] = $this->model_setting_setting->getSettingValue('lemonway_status');
$this->variables['lemonway_oneclick_enabled'] = $this->model_setting_setting->getSettingValue('lemonway_oneclick_enabled');
$this->variables['lemonway_api_login'] = $this->model_setting_setting->getSettingValue($this->prefix() . 'lemonway_api_login');
$this->variables['lemonway_api_password'] = $this->model_setting_setting->getSettingValue($this->prefix() . 'lemonway_api_password');
$this->variables['lemonway_is_test_mode'] = $this->model_setting_setting->getSettingValue($this->prefix() . 'lemonway_is_test_mode');
$this->variables['lemonway_css_url'] = $this->model_setting_setting->getSettingValue($this->prefix() . 'lemonway_css_url');
$this->variables['lemonway_debug'] = $this->model_setting_setting->getSettingValue($this->prefix() . 'lemonway_debug');
$this->variables['lemonway_environment_name'] = $this->model_setting_setting->getSettingValue($this->prefix() . 'lemonway_environment_name');
$this->variables['lemonway_custom_wallet'] = $this->model_setting_setting->getSettingValue($this->prefix() . 'lemonway_custom_wallet');
$this->variables['lemonway_status'] = $this->model_setting_setting->getSettingValue($this->prefix() . 'lemonway_status');
$this->variables['lemonway_oneclick_enabled'] = $this->model_setting_setting->getSettingValue($this->prefix() . 'lemonway_oneclick_enabled');

// Alerts
$this->variables['no_permission'] = false;
Expand Down Expand Up @@ -81,6 +77,10 @@ public function index()
$this->response->setOutput($this->load->view('extension/payment/lemonway', $this->variables));
}

private function prefix() {
return (version_compare(VERSION, '3.0', '>=')) ? 'payment_' : '';
}

private function validate()
{
$error = false;
Expand Down Expand Up @@ -130,6 +130,11 @@ private function validate()
$this->request->post['lemonway_oneclick_enabled'] = 0;
}

foreach ($this->request->post as $key => $value) {
unset($this->request->post[$key]);
$this->request->post[$this->prefix() . $key] = $value; //concatinate your existing array with new one
}

return !$error; // If no error => validated
}

Expand All @@ -142,11 +147,11 @@ private function testConfig()
// TEST
if ($this->variables['lemonway_is_test_mode']) {
// DIRECTKIT TEST URL
$dkUrl = $this->model_setting_setting->getSettingValue('lemonway_directkit_url_test');
$dkUrl = $this->model_setting_setting->getSettingValue($this->prefix() . 'lemonway_directkit_url_test');
} //PROD
else {
// DIRECTKIT URL
$dkUrl = $this->model_setting_setting->getSettingValue('lemonway_directkit_url');
$dkUrl = $this->model_setting_setting->getSettingValue($this->prefix() . 'lemonway_directkit_url');
}

require_once DIR_SYSTEM . '/library/lemonway/LemonWayService.php';
Expand All @@ -172,7 +177,7 @@ private function testConfig()
if (empty($this->variables['lemonway_environment_name'])) {
// If lwecommerce, get wallet
if (isset($res->WALLET->ID)) {
$this->model_setting_setting->editSettingValue('lemonway', 'lemonway_wallet', $res->WALLET->ID);
$this->model_setting_setting->editSettingValue($this->prefix() . 'lemonway', $this->prefix() . 'lemonway_wallet', $res->WALLET->ID);
}
}

Expand All @@ -192,13 +197,13 @@ public function install()

// Default settings
$this->load->model('setting/setting');
$this->model_setting_setting->editSetting('lemonway', [
'lemonway_status' => 1,
'lemonway_css_url' => self::CSS_URL_DEFAULT,
'lemonway_directkit_url' => sprintf(self::LEMONWAY_DIRECTKIT_FORMAT_URL_PROD, self::LEMONWAY_ENVIRONMENT_DEFAULT),
'lemonway_webkit_url' => sprintf(self::LEMONWAY_WEBKIT_FORMAT_URL_PROD, self::LEMONWAY_ENVIRONMENT_DEFAULT),
'lemonway_directkit_url_test' => sprintf(self::LEMONWAY_DIRECTKIT_FORMAT_URL_TEST, self::LEMONWAY_ENVIRONMENT_DEFAULT),
'lemonway_webkit_url_test' => sprintf(self::LEMONWAY_WEBKIT_FORMAT_URL_TEST, self::LEMONWAY_ENVIRONMENT_DEFAULT)
$this->model_setting_setting->editSetting($this->prefix() . 'lemonway', [
$this->prefix() . 'lemonway_status' => 1,
$this->prefix() . 'lemonway_css_url' => self::CSS_URL_DEFAULT,
$this->prefix() . 'lemonway_directkit_url' => sprintf(self::LEMONWAY_DIRECTKIT_FORMAT_URL_PROD, self::LEMONWAY_ENVIRONMENT_DEFAULT),
$this->prefix() . 'lemonway_webkit_url' => sprintf(self::LEMONWAY_WEBKIT_FORMAT_URL_PROD, self::LEMONWAY_ENVIRONMENT_DEFAULT),
$this->prefix() . 'lemonway_directkit_url_test' => sprintf(self::LEMONWAY_DIRECTKIT_FORMAT_URL_TEST, self::LEMONWAY_ENVIRONMENT_DEFAULT),
$this->prefix() . 'lemonway_webkit_url_test' => sprintf(self::LEMONWAY_WEBKIT_FORMAT_URL_TEST, self::LEMONWAY_ENVIRONMENT_DEFAULT)
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class ControllerExtensionPaymentLemonWay extends Controller

private $money_in_trans_details;

private function prefix() {
return (version_compare(VERSION, '3.0', '>=')) ? 'payment_' : '';
}

/*
Check if there are GET params
*/
Expand Down Expand Up @@ -60,22 +64,22 @@ private function getLemonWayConfig()
{
$config = array();

if ($this->config->get('lemonway_is_test_mode')) {
if ($this->config->get($this->prefix() . 'lemonway_is_test_mode')) {
// TEST
$config['dkURL'] = $this->config->get('lemonway_directkit_url_test'); //DIRECT KIT URL TEST
$config['wkURL'] = $this->config->get('lemonway_webkit_url_test'); //WEB KIT URL TEST
$config['dkURL'] = $this->config->get($this->prefix() . 'lemonway_directkit_url_test'); //DIRECT KIT URL TEST
$config['wkURL'] = $this->config->get($this->prefix() . 'lemonway_webkit_url_test'); //WEB KIT URL TEST
} else {
// PROD
$config['dkURL'] = $this->config->get('lemonway_directkit_url'); // DIRECT KIT URL PROD
$config['wkURL'] = $this->config->get('lemonway_webkit_url'); // WEBKIT URL PROD
$config['dkURL'] = $this->config->get($this->prefix() . 'lemonway_directkit_url'); // DIRECT KIT URL PROD
$config['wkURL'] = $this->config->get($this->prefix() . 'lemonway_webkit_url'); // WEBKIT URL PROD
}

$config['login'] = $this->config->get('lemonway_api_login');
$config['pass'] = $this->config->get('lemonway_api_password');
$config['wallet'] = empty($this->config->get('lemonway_environment_name')) ?
$this->config->get('lemonway_wallet') : $this->config->get('lemonway_custom_wallet');
$config['cssURL'] = $this->config->get('lemonway_css_url');
$config['autoCommission'] = (int)!empty($this->config->get('lemonway_environment_name'));
$config['login'] = $this->config->get($this->prefix() . 'lemonway_api_login');
$config['pass'] = $this->config->get($this->prefix() . 'lemonway_api_password');
$config['wallet'] = empty($this->config->get($this->prefix() . 'lemonway_environment_name')) ?
$this->config->get($this->prefix() . 'lemonway_wallet') : $this->config->get($this->prefix() . 'lemonway_custom_wallet');
$config['cssURL'] = $this->config->get($this->prefix() . 'lemonway_css_url');
$config['autoCommission'] = (int)!empty($this->config->get($this->prefix() . 'lemonway_environment_name'));
// Autocom = 0 if lwecommerce, 1 if custom environment

return $config;
Expand All @@ -95,7 +99,7 @@ private function getMoneyInTransDetails($wkToken)
$config['login'],
$config['pass'],
substr($this->language->get('code'), 0, 2),
$this->config->get('lemonway_debug')
$this->config->get($this->prefix() . 'lemonway_debug')
);

$params = array(
Expand Down Expand Up @@ -163,7 +167,7 @@ public function index()

$data['text_card'] = $this->language->get('text_card');
$data['link_checkout'] = $this->url->link('extension/payment/lemonway/checkout', '', true);
$data['lemonway_oneclick_enabled'] = $this->config->get('lemonway_oneclick_enabled');
$data['lemonway_oneclick_enabled'] = $this->config->get($this->prefix() . 'lemonway_oneclick_enabled');
$data['customerId'] = empty($this->customer->getId()) ? 0 : $this->customer->getId();
// A guest customer has no Id, we consider it 0

Expand Down Expand Up @@ -225,7 +229,7 @@ public function checkout()
$config['login'],
$config['pass'],
substr($this->language->get('code'), 0, 2),
$this->config->get('lemonway_debug')
$this->config->get($this->prefix() . 'lemonway_debug')
);

$params = array();
Expand All @@ -241,15 +245,15 @@ public function checkout()
$customerId = empty($this->customer->getId()) ? 0 : $this->customer->getId(); // A guest customer has no Id, we consider it 0

$useCard = (
$this->config->get('lemonway_oneclick_enabled') &&
$this->config->get($this->prefix() . 'lemonway_oneclick_enabled') &&
$customerId &&
$this->postValue('lemonway_oneclick') === 'use_card'
);

if (!$useCard) { // If the client use a new card => MoneyInWebInit
// Whether the client save a card
$registerCard = (int)(
$this->config->get('lemonway_oneclick_enabled') &&
$this->config->get($this->prefix() . 'lemonway_oneclick_enabled') &&
$customerId &&
$this->postValue('lemonway_oneclick') === 'register_card'
);
Expand Down Expand Up @@ -406,7 +410,7 @@ public function checkoutReturn()

if ($this->isGet()) { // If redirection
// GET params
if ($this->config->get('lemonway_debug')) {
if ($this->config->get($this->prefix() . 'lemonway_debug')) {
$debug_log = new Log('lemonway_debug.log');
$debug_log->write('GET params: ' . print_r($this->request->get, true));
}
Expand Down Expand Up @@ -461,7 +465,7 @@ public function checkoutReturn()
}
} elseif ($this->isPost()) { // If IPN
// Get response by IPN
if ($this->config->get('lemonway_debug')) {
if ($this->config->get($this->prefix() . 'lemonway_debug')) {
$debug_log = new Log('lemonway_debug.log');
$debug_log->write('IPN: ' . print_r($this->request->post, true));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
<?php
class ModelExtensionPaymentLemonway extends Model
{
{
private function prefix() {
return (version_compare(VERSION, '3.0', '>=')) ? 'payment_' : '';
}

/*
This function is required for OpenCart to show the method in the checkout page
*/
public function getMethod($address, $total)
{
$this->load->language('extension/payment/lemonway');

$mode = $this->config->get('lemonway_is_test_mode') ? " (Test)" : "";
$mode = $this->config->get($this->prefix() . 'lemonway_is_test_mode') ? " (Test)" : "";

$method_data = array(
'code' => 'lemonway',
'title' => $this->language->get('text_card') . $mode,
'terms' => '',
'sort_order' => '' /*TODO: $this->config->get('lemonway_cc_sort_order')*/
'sort_order' => '' /*TODO: $this->config->get($this->prefix() . 'lemonway_cc_sort_order')*/
);

return $method_data;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<div>
<h3 class="lemonway-method-title">
{{ text_card }}
</h3>
<div>
<form action="{{ link_checkout }}" method="POST">
{% if lemonway_oneclick_enabled and customerId %} {# Hide One-click form from guest #}
{% if card %} {# User already have a saved card. He can choose to use it or not #}
<div>
<div class="radio">
<label>
<input type="radio" class="hide_cards" name="lemonway_oneclick" value="use_card" checked />
{{ text_use_card }} (<em>{{ card['card_num'] }}</em> - <em>{{ card['card_exp'] }}</em>)
</label>
</div>
<div class="radio">
<label>
<input type="radio" class="show_cards" name="lemonway_oneclick" value="register_card" />
{{ text_save_new_card }}
</label>
</div>
<div class="radio">
<label>
<input type="radio" class="show_cards" name="lemonway_oneclick" value="no_use_card" />
{{ text_not_use_card }}
</label>
</div>
</div>
{% endif %}
{% endif %}

<!-- Choose a card type -->
<!-- If the user choose to use a saved card, he doesn't need to choose a card type -->
<div id="card_choosing_container" style="{{ (lemonway_oneclick_enabled and customerId and card) ? 'display: none' : '' }}">
<div class="row">
<div class="col-md-1 text-center">
<label>
<img class="img-responsive" alt="CB" src="catalog/view/theme/default/image/lemonway_cb.gif">
<input type="radio" name="cc_type" value="CB" checked />
</label>
</div>
<div class="col-md-1 text-center">
<label>
<img class="img-responsive" alt="Visa" src="catalog/view/theme/default/image/lemonway_visa.gif">
<input type="radio" name="cc_type" value="VISA" />
</label>
</div>
<div class="col-md-1 text-center">
<label>
<img class="img-responsive" alt="MasterCard" src="catalog/view/theme/default/image/lemonway_mastercard.gif">
<input type="radio" name="cc_type" value="MASTERCARD" />
</label>
</div>
</div>
</div>

<div class="buttons">
<div class="pull-right">
{% if lemonway_oneclick_enabled and customerId %} {# Hide One-click form from guest #}
{% if not card %} {# If no saved card => Ask to save the card #}
<!-- User can choose to save his card -->
<label>
{{ text_save_card }}
<input type="checkbox" name="lemonway_oneclick" value="register_card" />
</label>
{% endif %}
{% endif %}
<button type="submit" data-toggle="tooltip" class="btn btn-primary" data-loading-text="{{ text_loading }}">
{{ button_continue }}
</button>
</div>
</div>
</form>
</div>
</div>

<script>
$(function()
{
// If using a saved card, hide the card choices
$(".hide_cards").click(function()
{
$('#card_choosing_container').hide(300);
});
// If not using a saved card, show the card choices
$(".show_cards").click(function()
{
$('#card_choosing_container').show(300);
})
});
</script>

0 comments on commit 7b5ed77

Please sign in to comment.