-
Notifications
You must be signed in to change notification settings - Fork 2
/
Pesapal.php
239 lines (225 loc) · 9.41 KB
/
Pesapal.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
/**
* @package Pesapal for HikaShop Joomla!
* @version 1.0
* @author twitter.com/patric_mutwiri
* @copyright (C) 2010-2014 GBC SOFTWARE. All rights reserved.
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
defined('_JEXEC') or die('Restricted access');
?><?php
//define("ENCRYPTION_KEY", "!@#$%^&*");
class plgHikashoppaymentPesapal extends hikashopPaymentPlugin
{
var $accepted_currencies = array( "KES", "USD" );
var $multiple = true;
var $name = 'Pesapal';
var $pluginConfig = array(
'key' => array("Consumer Key",'input'), //User's secret on the payment platform
'secret' => array("Consumer Secret",'input'), //User's key on the payment platform
//'iframelink' => array("Iframe Link",'input'), //User's key on the payment platform
'pesapal_link'=> array("Use https://www.pesapal.com/API/PostPesapalDirectOrderV4 for live (demo: http://demo.pesapal.com/api/PostPesapalDirectOrderV4) |the link that is passed to the iframe pointing to the PesaPal server|",'input'),
//http://demo.pesapal.com/api/PostPesapalDirectOrderV4 demo
'notification' => array('ALLOW_NOTIFICATIONS_FROM_X', 'boolean','0'),
'payment_url' => array("Payment URL | Callback (index.php?option=com_hikashop&ctrl=checkout&task=after_end) ",'input'),
'debug' => array('DEBUG', 'boolean','0'),
'cancel_url' => array('Cancel Url | index.php?option=com_hikashop&ctrl=order&task=cancel_order','input'),
'return_url_gateway' => array('RETURN_URL_DEFINE', 'html',''),
'return_url' => array('Return Url (This is the full url pointing to the page the iframe redirects to after processing the order on pesapal.com) | index.php?option=com_hikashop&ctrl=checkout&task=after_end', 'input'),
'notify_url' => array('Notification Url | index.php?option=com_hikashop&ctrl=checkout&task=notify&notif_payment=Pesapal&tmpl=component','input'),
'invalid_status' => array('INVALID_STATUS', 'orderstatus'),
'verified_status' => array('VERIFIED_STATUS', 'orderstatus')
);
function __construct(&$subject, $config)
{
$this->pluginConfig['notification'][0] = JText::sprintf('ALLOW_NOTIFICATIONS_FROM_X','Pesapal');
$this->pluginConfig['cancel_url'][2] = HIKASHOP_LIVE."index.php?option=com_hikashop&ctrl=order&task=cancel_order";
$this->pluginConfig['return_url'][2] = HIKASHOP_LIVE."index.php?option=com_hikashop&ctrl=checkout&task=after_end";
$this->pluginConfig['notify_url'][2] = HIKASHOP_LIVE.'index.php?option=com_hikashop&ctrl=checkout&task=notify&notif_payment='.$this->name.'&tmpl=component';
return parent::__construct($subject, $config);
}
function onAfterOrderConfirm(&$order,&$methods,$method_id)
{
$order_id = $order->order_id;
$user = JFactory::getUser();
parent::onAfterOrderConfirm($order,$methods,$method_id);
if (empty($this->payment_params->secret))
{
$this->app->enqueueMessage('You have to configure a secret key for the Pesapal plugin payment first : check your plugin\'s parameters, on your website backend','error');
return false;
}
elseif (empty($this->payment_params->key))
{
$this->app->enqueueMessage('You have to configure a plugin key for the Pesapal plugin payment first : check your plugin\'s parameters, on your website backend','error');
return false;
}
elseif (empty($this->payment_params->payment_url))
{
$this->app->enqueueMessage('You have to configure a payment url for the Pesapal plugin payment first : check your plugin\'s parameters, on your website backend','error');
return false;
}
else
{
$address_type = 'billing_address';
$address = $this->app->getUserState(HIKASHOP_COMPONENT . '.' . $address_type);
$user = JFactory::getUser();
$amout = round($order->cart->full_total->prices[0]->price_value_with_tax,2);
//retun HIKASHOP_LIVE."index.php?option=com_hikashop&ctrl=checkout&task=after_end";
// HIKASHOP_LIVE."index.php?option=com_hikashop&ctrl=checkout&task=notify¬if_payment=Pesapal&tmpl=component";
// HIKASHOP_LIVE."index.php?option=com_hikashop&ctrl=order&task=cancel_order";
// HIKASHOP_LIVE."index.php?option=com_hikashop&ctrl=checkout&task=after_end";
$this->payment_params->return_url = HIKASHOP_LIVE.'index.php?option=com_hikashop&ctrl=checkout&task=after_end&order_id='.$order_id.$this->url_itemid;
$vars = array(
'EMAIL' => $user->email,
'TYPE' => "MERCHANT",
'DESCRIPTION' => "My Order",
'TYPE' => "MERCHANT",
'REFERENCE' => $order->order_number,
'FIRST_NAME' => $order->cart->$address_type->address_firstname,
'LAST_NAME'=> $order->cart->$address_type->address_lastname,
'IFRAMELINK'=> $this->payment_params->pesapal_link,
'CALLBACK'=> $this->payment_params->return_url,
//'CX1' => plgHikashoppaymentPesapal::encrypt($this->payment_params->keyx, ENCRYPTION_KEY),
//'CX2' => plgHikashoppaymentPesapal::encrypt($this->payment_params->secret, ENCRYPTION_KEY),
//'IDENTIFIER' => $this->payment_params->secret, //User's identifier on the payment platform
'CLIENTIDENT' => $order->order_user_id,
'DESCRIPTION' => "order number : ".$order->order_number,
'ORDERID' => $order->order_id,
'VERSION' => 2.0,
'AMOUNT' => $amout
);
//self
$this->payment_params->return_urlx = HIKASHOP_LIVE.'index.php?option=com_hikashop&ctrl=checkout&task=after_end&order_id='.$order_id.$this->url_itemid;
$this->payment_params->ref = $order->order_number;
$this->payment_params->firstname = $order->cart->$address_type->address_firstname;
$this->payment_params->lastname = $order->cart->$address_type->address_lastname;
$this->payment_params->amount = $amout;
//endself
$vars['HASH'] = $this->pesapal_signature($this->payment_params->secret,$vars);
$this->vars = $vars;
?>
<!----------------->
<form style="display:none" id="hikashop_pesapal_form" name="hikashop_pesapal_form" action="<?php echo JURI::base() ?>plugins/hikashoppayment/Pesapal/pesapal_end.php" method="post">
<div id="hikashop_pesapal_end_image" class="hikashop_pesapal_end_image">
<input id="hikashop_pesapal_button" type="submit" class="btn btn-primary" value="<?php echo JText::_('PAY_NOW');?>" name="" alt="<?php echo JText::_('PAY_NOW');?>" />
</div>
<?php
foreach($this->vars as $name => $value ) {
echo '<input type="hidden" name="'.$name.'" value="'.htmlspecialchars((string)$value).'" />';
}
JRequest::setVar('noform',1); ?>
</form>
<script type="text/javascript">
$(function(){
$.post('<?php echo JURI::root() ?>index.php?option=com_hikashop&ctrl=checkout&task=step&step=1',$('#hikashop_pesapal_form').serialize(), function(){
});
});
//document.getElementById('hikashop_pesapal_form').submit();
</script>
<!------------------->
<?php
return $this->showPage('end');
}
}
function getPaymentDefaultValues(&$element)
{
$element->payment_name='Pesapal';
$element->payment_description='You can pay by visa/mobile money using this payment method';
$element->payment_images='Pesapal';
$element->payment_params->address_type="billing";
$element->payment_params->notification=1;
$element->payment_params->invalid_status='cancelled';
$element->payment_params->verified_status='confirmed';
}
function onPaymentNotification(&$statuses)
{
$user = JFactory::getUser();
$config = JFactory::getConfig();
$vars = array();
$filter = JFilterInput::getInstance();
foreach($_REQUEST as $key => $value)
{
$key = $filter->clean($key);
$value = JRequest::getString($key);
$vars[$key]=$value;
}
$order_id = (int)@$vars['ORDERID'];
$dbOrder = $this->getOrder($order_id);
$this->loadPaymentParams($dbOrder);
if(empty($this->payment_params))
return false;
$this->loadOrderData($dbOrder);
$hash = $this->pesapal_signature($this->payment_params->key,$vars,false,true);
if($this->payment_params->debug) //Debug mode activated or not
{
echo print_r($vars,true)."\n\n\n";
echo print_r($dbOrder,true)."\n\n\n";
echo print_r($hash,true)."\n\n\n";
}
if (strcasecmp($hash,$vars['HASH'])!=0)
{
if($this->payment_params->debug)
echo 'Hash error '.$vars['HASH'].' - '.$hash."\n\n\n";
return false;
}
elseif($vars['EXECCODE']!='0000')
{
if($this->payment_params->debug)
echo 'payment '.$vars['MESSAGE']."\n\n\n";
$this->modifyOrder($order_id, $this->payment_params->invalid_status, true, true);
return false;
}
else
{
$this->modifyOrder($order_id, $this->payment_params->verified_status, true, true);
//$this->app->redirect($return_url);
return true;
}
}
function pesapal_signature($secret, $parameters, $debug=false, $decode=false)
{
ksort($parameters);
$clear_string = $secret;
$expectedKey = array (
'IDENTIFIER',
'TRANSACTIONID',
'CLIENTIDENT',
'CLIENTEMAIL',
'ORDERID',
'VERSION',
'LANGUAGE',
'CURRENCY',
'EXTRADATA',
'CARDCODE',
'CARDCOUNTRY',
'EXECCODE',
'MESSAGE',
'DESCRIPTOR',
'ALIAS',
'3DSECURE',
'AMOUNT',
);
foreach ($parameters as $key => $value)
{
if ($decode)
{
if (in_array($key,$expectedKey))
$clear_string .= $key . '=' . $value . $secret;
}
else
$clear_string .= $key . '=' . $value . $secret;
}
if (PHP_VERSION_ID < 50102)
{
$this->app->enqueueMessage('The Pesapal payment plugin requires at least the PHP 5.1.2 version to work, but it seems that it is not available on your server. Please contact your web hosting to set it up.','error');
return false;
}
else
{
if ($debug)
return $clear_string;
else
return hash('sha256', $clear_string);
}
}
}