Skip to content

Commit 1b1b94c

Browse files
authored
Merge pull request #6 from steveamaza/feature/enable-GHS
Pass in currency variable when initializing payment
2 parents e05c54b + 897f769 commit 1b1b94c

File tree

2 files changed

+131
-133
lines changed

2 files changed

+131
-133
lines changed

controllers/front/payment.php

Lines changed: 85 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -4,95 +4,98 @@
44

55
class PrestaPaystackPaymentModuleFrontController extends ModuleFrontController
66
{
7-
public $display_column_left = false;
8-
9-
public $ssl = true;
10-
11-
public function initContent()
12-
{
13-
// Call parent init content method
14-
parent::initContent();
15-
16-
$this->display_column_left = false;
17-
$this->display_column_right = false;
18-
$this->context->smarty->assign(array(
19-
'nb_products' => $this->context->cart->nbProducts(),
20-
'cart_currency' => $this->context->cart->id_currency,
21-
'currencies' => $this->module->getCurrency((int)$this->context->cart->id_currency),
22-
'total_amount' =>$this->context->cart->getOrderTotal(true, Cart::BOTH),
23-
'path' => $this->module->getPathUri(),
24-
));
25-
$test_publickey = Configuration::get('PAYSTACK_TEST_PUBLICKEY');
26-
$live_publickey = Configuration::get('PAYSTACK_LIVE_PUBLICKEY');
27-
$mode = Configuration::get('PAYSTACK_MODE');
28-
$style = Configuration::get('PAYSTACK_STYLE');
29-
if ($mode == 'test') {
30-
$key = $test_publickey;
31-
}else{
32-
$key = $live_publickey;
7+
public $display_column_left = false;
8+
9+
public $ssl = true;
10+
11+
public function initContent()
12+
{
13+
// Call parent init content method
14+
parent::initContent();
15+
16+
$this->display_column_left = false;
17+
$this->display_column_right = false;
18+
$this->context->smarty->assign(
19+
array(
20+
'nb_products' => $this->context->cart->nbProducts(),
21+
'cart_currency' => $this->context->cart->id_currency,
22+
'currencies' => $this->module->getCurrency((int)$this->context->cart->id_currency),
23+
'total_amount' =>$this->context->cart->getOrderTotal(true, Cart::BOTH),
24+
'path' => $this->module->getPathUri(),
25+
)
26+
);
27+
$test_publickey = Configuration::get('PAYSTACK_TEST_PUBLICKEY');
28+
$live_publickey = Configuration::get('PAYSTACK_LIVE_PUBLICKEY');
29+
$mode = Configuration::get('PAYSTACK_MODE');
30+
$style = Configuration::get('PAYSTACK_STYLE');
31+
if ($mode == 'test') {
32+
$key = $test_publickey;
33+
} else {
34+
$key = $live_publickey;
35+
}
36+
$key = str_replace(' ', '', $key);
37+
38+
$this->context->smarty->assign('key', $key);
39+
$this->context->smarty->assign('style', $style);
40+
$cart = $this->context->cart;
41+
$cart_id = $cart->id;
42+
43+
$pcode = $this->getPaystackcode($cart_id);
44+
// die($cart_id);
45+
if ($cart->id_customer == 0 || $cart->id_address_delivery == 0 || $cart->id_address_invoice == 0 ||!$this->module->active) {
46+
Tools::redirect('index.php?controller=order&step=1');
47+
}
48+
/////
49+
$authorized = false;
50+
foreach (Module::getPaymentModules() as $module) {
51+
if ($module['name'] == $this->module->name) {
52+
$authorized = true;
53+
}
54+
}
55+
if (!$authorized) {
56+
die('This payment method is not available.');
57+
}
58+
$customer = new Customer($cart->id_customer);
59+
if (!Validate::isLoadedObject($customer)) {
60+
Tools::redirect('index.php?controller=order&step=1');
61+
}
62+
//////////
63+
$all_products = self::$cart->getProducts();
64+
$this->context->smarty->assign(
65+
array(
66+
'nbProducts' => $cart->nbProducts(),
67+
'email' => $this->context->customer->email,
68+
'code' => $pcode->code,
69+
'products' => $all_products,
70+
)
71+
);
72+
73+
$this->setTemplate('payment.tpl');
3374
}
34-
$key = str_replace(' ', '', $key);
3575

36-
$this->context->smarty->assign('key', $key);
37-
$this->context->smarty->assign('style', $style);
38-
$cart = $this->context->cart;
39-
$cart_id = $cart->id;
76+
private function getPaystackcode($cart_id) {
77+
$o_exist = Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'paystack_txncodes` WHERE `cart_id` = "'.$cart_id.'"');
4078

41-
$pcode = $this->getPaystackcode($cart_id);
42-
// die($cart_id);
43-
if ($cart->id_customer == 0 || $cart->id_address_delivery == 0 || $cart->id_address_invoice == 0 ||!$this->module->active){
44-
Tools::redirect('index.php?controller=order&step=1');
45-
}
46-
/////
47-
$authorized = false;
48-
foreach (Module::getPaymentModules() as $module){
49-
if ($module['name'] == $this->module->name){
50-
$authorized = true;
51-
}
52-
}
53-
if (!$authorized){
54-
die('This payment method is not available.');
55-
}
56-
$customer = new Customer($cart->id_customer);
57-
if (!Validate::isLoadedObject($customer)){
58-
Tools::redirect('index.php?controller=order&step=1');
59-
}
60-
//////////
61-
$all_products = self::$cart->getProducts();
62-
$this->context->smarty->assign(array(
63-
'nbProducts' => $cart->nbProducts(),
64-
'email' => $this->context->customer->email,
65-
'code' => $pcode->code,
66-
'products' => $all_products,
67-
68-
));
69-
70-
$this->setTemplate('payment.tpl');
71-
}
79+
if (count($o_exist) == 0) {
80+
$pcode = new Paystackcode();
81+
$pcode->id = null;
82+
$pcode->cart_id = (int)$cart_id;
83+
$pcode->code = $pcode->generate_code();
84+
$pcode->add();
7285

73-
private function getPaystackcode($cart_id){
74-
$o_exist = Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'paystack_txncodes` WHERE `cart_id` = "'.$cart_id.'"');
86+
} else {
87+
$acode = new Paystackcode();
88+
$newcode = $acode->generate_code();
89+
// Db::getInstance()->execute($sql);
90+
$newref = @Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'paystack_txncodes` SET `code` = "'.$newcode.'" WHERE `cart_id` = "'.$cart_id.'"');
91+
//SELECT * FROM `'._DB_PREFIX_.'paystack_txncodes` WHERE `code` = "'.$code.'"');//Rproduct::where('code', '=', $code)->
7592

76-
if (count($o_exist) == 0) {
77-
$pcode = new Paystackcode();
78-
$pcode->id = NULL;
79-
$pcode->cart_id = (int)$cart_id;
80-
$pcode->code = $pcode->generate_code();
81-
$pcode->add();
93+
$pcode = @new Paystackcode((int)$o_exist[0][id]);
94+
// print_r($o_exist);
8295

83-
} else {
84-
$acode = new Paystackcode();
85-
$newcode = $acode->generate_code();
86-
// Db::getInstance()->execute($sql);
87-
$newref = @Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'paystack_txncodes` SET `code` = "'.$newcode.'" WHERE `cart_id` = "'.$cart_id.'"');
88-
//SELECT * FROM `'._DB_PREFIX_.'paystack_txncodes` WHERE `code` = "'.$code.'"');//Rproduct::where('code', '=', $code)->
96+
}
8997

90-
$pcode = @new Paystackcode((int)$o_exist[0][id]);
91-
// print_r($o_exist);
9298

99+
return $pcode;
93100
}
94-
95-
96-
return $pcode;
97-
}
98101
}

views/templates/front/payment.tpl

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
{include file="$tpl_dir./order-steps.tpl"}
1212

1313
{if $nb_products <= 0}
14-
<p class="alert alert-warning">
15-
{l s='Your shopping cart is empty.' mod='prestapaystack'}
16-
</p>
14+
<p class="alert alert-warning">
15+
{l s='Your shopping cart is empty.' mod='prestapaystack'}
16+
</p>
1717
{else}
18-
<form action="{$link->getModuleLink('prestapaystack', 'confirm', [], true)|escape:'html'}" id="paystack_form" method="post">
18+
<form action="{$link->getModuleLink('prestapaystack', 'confirm', [], true)|escape:'html'}" id="paystack_form" method="post">
1919
<div class="box cheque-box">
2020
<h3 class="page-subheading">
2121
{l s='PAYSTACK Payment' mod='prestapaystack'}
@@ -24,38 +24,36 @@
2424
<strong class="dark">
2525
{l s='You have chosen to pay with Paystack.' mod='prestapaystack'} {l s='Here is a short summary of your order:' mod='prestapaystack'}
2626
</strong>
27-
2827
</p>
2928
<p>
3029
- {l s='The total amount of your order is' mod='prestapaystack'}
3130
<span id="amount" class="price">{displayPrice price=$total_amount}</span>
32-
{if $use_taxes == 1}
33-
{l s='(tax incl.)' mod='prestapaystack'}
34-
{/if}
31+
{if $use_taxes == 1}
32+
{l s='(tax incl.)' mod='prestapaystack'}
33+
{/if}
3534
</p>
36-
<p>
35+
<p>
3736
-
38-
{if $currencies|@count > 1}
39-
{l s='We allow several currencies to be sent via MyMod Payment.' mod='prestapaystack'}
37+
{if $currencies|@count > 1}
38+
{l s='We allow several currencies to be sent via MyMod Payment.' mod='prestapaystack'}
4039
<div class="form-group">
4140
<label>{l s='Choose one of the following:' mod='prestapaystack'}</label>
4241
<select id="currency_payment" class="form-control" name="currency_payment">
43-
{foreach from=$currencies item=currency}
42+
{foreach from=$currencies item=currency}
4443
<option value="{$currency.id_currency}" {if $currency.id_currency == $cart_currency}selected="selected"{/if}>
45-
{$currency.name}
44+
{$currency.name}
4645
</option>
47-
{/foreach}
46+
{/foreach}
4847
</select>
49-
</div>
50-
{else}
51-
{l s='We allow the following currency to be sent via Paystack:' mod='prestapaystack'}&nbsp;<b>{$currencies.0.name}</b>
52-
<input type="hidden" name="currency_payment" value="{$currencies.0.id_currency}" />
53-
54-
{/if}
55-
56-
<input type="hidden" name="amounttotal" value="{$total_amount}" />
57-
<input type="hidden" name="email" value="{$email}" />
58-
<input type="hidden" name="txn_code" value="{$code}" />
48+
</div>
49+
{else}
50+
{l s='We allow the following currency to be sent via Paystack:' mod='prestapaystack'}&nbsp;<b>{$currencies.0.name}</b>
51+
<input type="hidden" name="currency_payment" value="{$currencies.0.id_currency}" />
52+
{/if}
53+
54+
<input type="hidden" name="amounttotal" value="{$total_amount}" />
55+
<input type="hidden" name="email" value="{$email}" />
56+
<input type="hidden" name="txn_code" value="{$code}" />
5957
</p>
6058
<br />
6159
ITEMS:
@@ -67,17 +65,17 @@
6765
{/foreach}
6866
</div>
6967

70-
<p class="cart_navigation clearfix" id="cart_navigation" style="display:inline-block;">
71-
<a
72-
class="button-exclusive btn btn-default"
73-
href="{$link->getPageLink('order', true, NULL, "step=3")|escape:'html':'UTF-8'}">
74-
<i class="icon-chevron-left"></i>{l s='Other payment methods' mod='prestapaystack'}
75-
</a>
68+
<p class="cart_navigation clearfix" id="cart_navigation" style="display:inline-block;">
69+
<a
70+
class="button-exclusive btn btn-default"
71+
href="{$link->getPageLink('order', true, NULL, "step=3")|escape:'html':'UTF-8'}">
72+
<i class="icon-chevron-left"></i>{l s='Other payment methods' mod='prestapaystack'}
73+
</a>
7674
</p>
7775
{if $style == 'inline'}
7876
<script src="https://js.paystack.co/v1/inline.js"></script>
7977

80-
<input class="btn btn-default pull-right" type="button" name="save_settings" id="paystack_button" value="Pay Now" />
78+
<input class="btn btn-default pull-right" type="button" name="save_settings" id="paystack_button" value="Pay Now" />
8179

8280
{else}
8381
<script
@@ -89,9 +87,6 @@
8987
</script>
9088
{/if}
9189

92-
93-
94-
9590
</form>
9691
<style>
9792
.paystack-trigger-btn{
@@ -100,23 +95,23 @@
10095
</style>
10196
{if $style == 'inline'}
10297
<script>
103-
$('#paystack_button').on('click', function (e) {
104-
// e.preventDefault();
105-
$("#paystack_form").unbind("submit");
106-
var handler = PaystackPop.setup({
107-
key: '{$key}',
108-
email: '{$email}',
109-
amount: '{$total_amount*100}',
110-
ref: '{$code}',
111-
callback: function(response){
112-
$( "#paystack_form" ).submit();
113-
},
114-
onClose: function(){
115-
116-
}
117-
});
118-
handler.openIframe();
119-
98+
$('#paystack_button').on('click', function (e) {
99+
// e.preventDefault();
100+
$("#paystack_form").unbind("submit");
101+
var handler = PaystackPop.setup({
102+
key: '{$key}',
103+
email: '{$email}',
104+
amount: '{$total_amount*100}',
105+
currency: '{$cart_currency}',
106+
ref: '{$code}',
107+
callback: function(response){
108+
$( "#paystack_form" ).submit();
109+
},
110+
onClose: function(){
111+
112+
}
113+
});
114+
handler.openIframe();
120115
});
121116
</script>
122117

0 commit comments

Comments
 (0)