-
Notifications
You must be signed in to change notification settings - Fork 0
/
kpay.php
446 lines (377 loc) · 19.2 KB
/
kpay.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<?php
/**
* @package KPay for HikaShop
* @version 1.0.0
* @author www.k-ict.org
* @copyright (C) 2017 Konsortium ICT Pantai Timur. All rights reserved.
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
defined('_JEXEC') or die('Restricted access');
?><?php
class plgHikashoppaymentKpay extends hikashopPaymentPlugin
{ # Class plgHikashoppaymentKpay start.
var $accepted_currencies=array(
'MYR',
);
var $multiple=true;
var $name='kpay';
var $doc_form='kpay';
function __construct(&$subject,$config)
{ # The class constructor start.
if(!file_exists(JPATH_SITE.'/media/com_hikashop/images/payment/FPX.jpg'))
rename(JPATH_PLUGINS.'/hikashoppayment/kpay/FPX.jpg',JPATH_ROOT.'/media/com_hikashop/images/payment/FPX.jpg');
parent::__construct($subject,$config);
$this->KPay_name='KPay';
$this->KPay_provider="https://www.k-ict.org/";
$this->KPay_url=$this->KPay_provider."kpg/";
$this->KPay_payment_url=$this->KPay_url."payment.php";
$this->KPay_receipt_url=$this->KPay_url."receipt.php";
$this->API_url=$this->KPay_url."API.php";
$this->API_client_name="KPG";
$this->API_client_type="APIclient";
$this->API_client_version="v1.1";
$this->API_user_agent=$this->API_client_name." ".$this->API_client_type." ".$this->API_client_version;
if(!(JFactory::getApplication()->input->post->get('PortalKey')))
{ # Do process on normal request by excluding the POST portalKey from KPG server start.
# Check all orders with 'Created' and 'Pending' status for KPay payment method start.
$db=JFactory::getDbo();
$query=$db->getQuery(true);
$query->select('order_id');
$query->from($db->quoteName('#__hikashop_order'));
$query->where("(".strtolower($db->quoteName('order_status'))."='created' or ".strtolower($db->quoteName('order_status'))."='pending') and ".$db->quoteName('order_payment_method')."='".strtolower($this->KPay_name)."'");
$db->setQuery($query);
$records=$db->loadColumn();
foreach($records as $records_index=>$records_value)
{ # Loop each result and proceed with the checking start.
$dbOrder=$this->getOrder((int)@$records_value);
$this->loadPaymentParams($dbOrder);
$order_id=$dbOrder->order_id;
# Perform remote checking at KPG server start.
# Define the API data.
$KPay_API_data=array(
"UserLoginID"=>$this->payment_params->kpay_user_loginid,
"UserPassword"=>$this->payment_params->kpay_user_password,
"Category"=>"getTransactionDetailsByOrderNumber",
"PortalKey"=>$this->payment_params->kpay_portal_key,
"OrderNumber"=>$order_id,
);
# Perform API operations.
$KPay_API_operations=$this->KPay_API_operations($KPay_API_data);
$KPay_API_operations_result=$KPay_API_operations['Result'];
$KPay_order_transaction_status=$KPay_API_operations['TransactionStatus'];
if($KPay_order_transaction_status=='Successful' || $KPay_order_transaction_status=='Unsuccessful')
{ # Do process transactions those are not pending start.
# Define the current order status.
$order_status_current=$dbOrder->order_status;
if($KPay_order_transaction_status=='Successful')
$order_status_new=$this->payment_params->verified_status;
else
$order_status_new=$this->payment_params->invalid_status;
if($order_status_current!=$order_status_new)
{ # Do update order if the status changed start.
$history->notified=1;
$this->modifyOrder($order_id,$order_status_new,true);
} # Do update order if the status changed end.
# Do nothing if the status was unchanged.
} # Do process transactions those are not pending end.
# Otherwise, do not process pending transaction.
# Perform remote checking at KPG server end.
} # Loop each result and proceed with the checking end.
# Check all orders with 'Created' and 'Pending' status for KPay payment method end.
} # Do process on normal request by excluding the POST portalKey from KPG server end.
} # The class constructor end.
function onBeforeOrderCreate(&$order,&$do)
{ # Method to be executed before creating an order start.
if(parent::onBeforeOrderCreate($order,$do)===true)
return true;
if(empty($this->payment_params->kpay_user_loginid) || empty($this->payment_params->kpay_user_password) || empty($this->payment_params->kpay_portal_key))
{ # Check for the current KPay configuration error start.
$this->app->enqueueMessage('<div style="color:#a00;padding:10px;background:#fff;border-radius:5px;">Error notes : NULL_KPay_Config<br>Sorry, there was a problem with '.$this->KPay_name.' plugin configuration.<br>If you are the shopper, please contact the seller.<br>Otherwise please check your plugin configuration.</div>');
$do=false;
} # Check for the current KPay configuration error end.
else
{ # Security features : Check for the supplied configuration validity from the KPG server start.
# Define the API data.
$KPay_API_data=array(
"UserLoginID"=>$this->payment_params->kpay_user_loginid,
"UserPassword"=>$this->payment_params->kpay_user_password,
"Category"=>"getSellerDetails",
"PortalKey"=>$this->payment_params->kpay_portal_key,
);
# Perform API operations.
$KPay_API_operations=$this->KPay_API_operations($KPay_API_data);
$KPay_API_operations_result=$KPay_API_operations['Result'];
if($KPay_API_operations_result=='Error')
{ # Display a friendly error message start.
$this->app->enqueueMessage('<div style="color:#a00;padding:10px;background:#fff;border-radius:5px;">Error notes : Error_KPay_API_Response<br>Sorry, invalid seller information detected.<br>If you are the shopper, please contact the seller.<br>Otherwise please check your plugin configuration.</div>');
$do=false;
} # Display a friendly error message end.
} # Security features : Check for the supplied configuration validity from the KPG server end.
} # Method to be executed before creating an order end.
function onAfterOrderConfirm(&$order,&$methods,$method_id)
{ # Method to be executed during the order submission to KPay start.
parent::onAfterOrderConfirm($order,$methods,$method_id);
if($this->currency->currency_locale['int_frac_digits']>2)
$this->currency->currency_locale['int_frac_digits']=2;
$notify_url=HIKASHOP_LIVE.'index.php?option=com_hikashop&ctrl=checkout&task=notify¬if_payment='.$this->name.'&tmpl=component&lang='.$this->locale.$this->url_itemid;
$return_url=HIKASHOP_LIVE.'index.php?option=com_hikashop&ctrl=checkout&task=after_end&order_id='.$order->order_id.$this->url_itemid;
$cancel_url=HIKASHOP_LIVE.'index.php?option=com_hikashop&ctrl=order&task=cancel_order&order_id='.$order->order_id.$this->url_itemid;
$tax_total='';
$discount_total='';
$vars=array(
'currency_code'=>$this->currency->currency_code,
'charset'=>'utf-8',
);
$this->payment_params->url=$this->KPay_payment_url;
$vars=array(
"portal_key"=>$this->payment_params->kpay_portal_key,
"order_no"=>preg_replace("/[^0-9]/","",$order->order_number),
"amount"=>number_format($order->order_full_price,2,'.',''),
"description"=>"Order no. ".$order->order_number,
"buyer_name"=>$order->customer->name,
"buyer_tel"=>$order->cart->billing_address->address_telephone,
"buyer_email"=>$order->customer->email,
);
$this->vars=$vars;
return $this->showPage('end');
} # Method to be executed during the order submission to KPay end.
function onPaymentNotification(&$statuses)
{ # Method to be executed when listening for the transaction status responses from KPay start.
$vars=array();
$data=array();
$filter=JFilterInput::getInstance();
foreach(JFactory::getApplication()->input->post->getArray() as $key=>$value)
{
$key=$filter->clean($key);
$vars[$key]=$value;
$data[]=$key.'='.$value;
}
$dbOrder=$this->getOrder((int)@$vars['orderNo']);
$this->loadPaymentParams($dbOrder);
# Security features : Attempt to check this order at KPay server start.
if($vars['portalKey']==$this->payment_params->kpay_portal_key)
{ # Proceed only on matched portalKey start.
# Define the API data.
$KPay_API_data=array(
"UserLoginID"=>$this->payment_params->kpay_user_loginid,
"UserPassword"=>$this->payment_params->kpay_user_password,
"Category"=>"getTransactionDetailsByOrderNumber",
"PortalKey"=>$this->payment_params->kpay_portal_key,
"OrderNumber"=>$vars['orderNo'],
);
# Perform API operations.
$KPay_API_operations=$this->KPay_API_operations($KPay_API_data);
$KPay_API_operations_result=$KPay_API_operations['Result'];
} # Proceed only on matched portalKey end.
# Security features : Attempt to check this order at KPay server end.
if($KPay_API_operations_result=='OK')
{ # Do only proceed if the API result was OK start.
$this->loadOrderData($dbOrder);
$order_id=$dbOrder->order_id;
if(!empty($KPay_API_operations['TransactionStatus']))
$KPay_order_transaction_status=$KPay_API_operations['TransactionStatus'];
else
$KPay_order_transaction_status=null;
if($KPay_order_transaction_status!='Pending')
{ # Do process transactions those are not pending start.
# Define the current order status.
$order_status_current=$dbOrder->order_status;
if($KPay_order_transaction_status=='Successful')
{
$order_status_new=$this->payment_params->verified_status;
$this->removeCart=true;
}
else
$order_status_new=$this->payment_params->invalid_status;
if($order_status_current!=$order_status_new)
{ # Do update order if the status changed start.
$history->notified=1;
$this->modifyOrder($order_id,$order_status_new,true);
} # Do update order if the status changed end.
# Do nothing if the status was unchanged.
# Redirect to the KPay transaction receipt page.
header('location:'.$this->KPay_receipt_url.'?txnId='.$vars['txnId'].'&txnEx='.$vars['txnEx']);
return true;
} # Do process transactions those are not pending end.
else
{ # Do not process pending transaction start.
$order_status_new=$this->payment_params->pending_status;
return false;
} # Do not process pending transaction end.
return true;
} # Do only proceed if the API result was OK end.
else
{ # Display error message if the API result was Error start.
echo 'Portal Key : '.$_REQUEST['portalKey'].'<br>Order Number : '.$_REQUEST['orderNo'].'<br>Transaction ID : '.$_REQUEST['txnEx'].'<hr>';
echo '<div style="font-weight:bold;color:#a00;">Error notes : '.$this->KPay_name.'_API_result_error<br>Sorry, an error has occurred.<br>If you are the visitor, please contact your seller.<br>Otherwise please take a screenshot and email to the <a href="mailto:support@k-ict.org?Subject='.$this->KPay_name.'_API_result_error-'.$_REQUEST['portalKey'].'">developer</a>.<br>We are sorry for the inconvenience caused.</div>';
exit();
return false;
} # Display error message if the API result was Error end.
} # Method to be executed when listening for the transaction status responses from KPay end.
function onPaymentConfiguration(&$element)
{ # Method to be executed in the back-end configuration pages start.
$subtask=JFactory::getApplication()->input->post->get('subtask','');
parent::onPaymentConfiguration($element);
} # Method to be executed in the back-end configuration pages end.
function onPaymentConfigurationSave(&$element)
{ # Method to be executed when saving the configuration start.
if(!empty($element->payment_params->ips))
$element->payment_params->ips=explode(',',$element->payment_params->ips);
if(empty($element->payment_params->kpay_user_loginid))
{
$app=JFactory::getApplication();
$app->enqueueMessage('<div style="color:#a00;">Please fill in your '.$this->KPay_name.' User Login ID.</div>');
return false;
}
if(empty($element->payment_params->kpay_user_password))
{
$app=JFactory::getApplication();
$app->enqueueMessage('<div style="color:#a00;">Please fill in your '.$this->KPay_name.' User Password.</div>');
return false;
}
if(empty($element->payment_params->kpay_portal_key))
{
$app=JFactory::getApplication();
$app->enqueueMessage('<div style="color:#a00;">Please fill in your '.$this->KPay_name.' Portal Key.</div>');
return false;
}
return true;
} # Method to be executed when saving the configuration end.
function getPaymentDefaultValues(&$element)
{ # Method to set the default parameters value start.
$element->payment_name='FPX';
$element->payment_description='You can pay via Malaysia Internet Banking.';
$element->payment_images=$element->payment_name;
$element->payment_params->url=$this->KPay_payment_url;
$element->payment_params->notification=1;
$element->payment_params->ips='';
$element->payment_params->details=1;
$element->payment_params->invalid_status='cancelled';
$element->payment_params->pending_status='created';
$element->payment_params->verified_status='confirmed';
$element->payment_params->address_override=1;
} # Method to set the default parameters value end.
public function KPay_API_cURL($KPay_API_data)
{ # Function to connect to KPay API start.
# Fetch the configuration data.
$KPay_API_url=$this->API_url;
$KPay_API_client_name=$this->API_client_name;
$KPay_API_client_type=$this->API_client_type;
$KPay_API_client_version=$this->API_client_version;
$KPay_API_user_agent=$this->API_user_agent;
# Fetch the API data.
$KPay_login_id=$KPay_API_data["UserLoginID"];
$KPay_password=$KPay_API_data["UserPassword"];
$KPay_API_category=$KPay_API_data["Category"];
$KPay_portal_key=$KPay_API_data["PortalKey"];
$KPay_API_order_number=$KPay_API_data["OrderNumber"];
# Use API call getTransactionDetailsByOrderNumber start.
$KPay_API_data=array(
"UserLoginID"=>rawurlencode($KPay_login_id),
"UserPassword"=>rawurlencode($KPay_password),
"Category"=>rawurlencode($KPay_API_category),
"PortalKey"=>rawurlencode($KPay_portal_key),
"OrderNumber"=>rawurlencode($KPay_API_order_number),
);
# Use API call getTransactionDetailsByOrderNumber end.
# Count number of data to be POSTed.
$KPay_API_data_count=count($KPay_API_data);
$KPay_API_data_fields=""; # Initialize the data to be POSTed.
foreach($KPay_API_data as $KPay_API_data_key=>$KPay_API_data_value)
$KPay_API_data_fields.=$KPay_API_data_key.'='.$KPay_API_data_value.'&';
rtrim($KPay_API_data_fields,'&');
# cURL section start.
$KPay_curl_output="";
$KPay_curl=curl_init();
curl_setopt($KPay_curl,CURLOPT_URL,$KPay_API_url);
curl_setopt($KPay_curl,CURLOPT_USERAGENT,$KPay_API_user_agent);
curl_setopt($KPay_curl,CURLOPT_POST,true);
curl_setopt($KPay_curl,CURLOPT_POSTFIELDS,$KPay_API_data_fields);
curl_setopt($KPay_curl,CURLOPT_RETURNTRANSFER,true);
$KPay_curl_output=curl_exec($KPay_curl);
curl_close($KPay_curl);
# cURL section end.
return $KPay_curl_output;
} # Function to connect to KPay API end.
public function KPay_API_cURL_response($KPay_curl_output)
{ # Function to fetch the KPay API response start.
# Decode JSON output to PHP object.
$KPay_curl_output_object=json_decode($KPay_curl_output);
# Initialize the output variables.
$KPay_curl_output_reason="";
$KPay_transaction_id="";
$KPay_transaction_status="";
$KPay_transaction_description="";
$KPay_FPX_transaction_id="";
$KPay_curl_output_result="";
$KPay_curl_output_data_mode="";
$KPay_curl_seller_name="";
foreach($KPay_curl_output_object as $KPay_curl_output_object_data=>$KPay_curl_output_object_value)
{ # Loop through each object start.
if(is_object($KPay_curl_output_object_value))
{ # If the return value is sub-object, loop through each sub-object start.
foreach($KPay_curl_output_object_value as $KPay_curl_output_data=>$KPay_curl_output_value)
{ # Fetch specific API response data start.
if($KPay_curl_output_data=="Reason")
$KPay_curl_output_reason=$KPay_curl_output_value;
if($KPay_curl_output_data=="OrderNumber")
$KPay_order_number=$KPay_curl_output_value;
if($KPay_curl_output_data=="TransactionID")
$KPay_transaction_id=$KPay_curl_output_value;
if($KPay_curl_output_data=="TransactionStatus")
$KPay_transaction_status=$KPay_curl_output_value;
if($KPay_curl_output_data=="TransactionDescription")
$KPay_transaction_description=$KPay_curl_output_value;
if($KPay_curl_output_data=="FPXTransactionID")
$KPay_FPX_transaction_id=$KPay_curl_output_value;
if($KPay_curl_output_data=="BusinessName")
$KPay_curl_seller_name=$KPay_curl_output_value;
} # Fetch specific API response data end.
} # If the return value is sub-object, loop through each sub-object end.
else
{ # Display normal object output start.
if($KPay_curl_output_object_data=="Result")
$KPay_curl_output_result=$KPay_curl_output_object_value;
if($KPay_curl_output_object_data=="DataMode")
$KPay_curl_output_data_mode=$KPay_curl_output_object_value;
} # Display normal object output end.
} # Loop through each object end.
# Prepare the output to be returned.
$KPay_curl_output_response_array=array(
"Result"=>$KPay_curl_output_result,
"Reason"=>$KPay_curl_output_reason,
"DataMode"=>$KPay_curl_output_data_mode,
"OrderNumber"=>$KPay_order_number,
"TransactionID"=>$KPay_transaction_id,
"TransactionStatus"=>$KPay_transaction_status,
"TransactionDescription"=>$KPay_transaction_description,
"FPXTransactionID"=>$KPay_FPX_transaction_id,
"BusinessName"=>urldecode($KPay_curl_seller_name),
);
return $KPay_curl_output_response_array;
} # Function to fetch the KPay API response end.
public function KPay_API_operations($KPay_API_data)
{ # Function to communicate with KPay API start.
# Request for the latest transaction response from the server (API request).
$KPay_curl_output=$this->KPay_API_cURL($KPay_API_data);
# Fetch the API response.
$KPay_curl_output_response=$this->KPay_API_cURL_response($KPay_curl_output);
# Translate the API response.
$KPay_curl_output_result=$KPay_curl_output_response["Result"];
$KPay_curl_output_reason=$KPay_curl_output_response["Reason"];
$KPay_curl_output_data_mode=$KPay_curl_output_response["DataMode"];
if($KPay_curl_output_response["OrderNumber"])
$KPay_order_number=$KPay_curl_output_response["OrderNumber"];
if($KPay_curl_output_response["TransactionID"])
$KPay_transaction_id=$KPay_curl_output_response["TransactionID"];
if($KPay_curl_output_response["TransactionStatus"])
$KPay_transaction_status=$KPay_curl_output_response["TransactionStatus"];
if($KPay_curl_output_response["TransactionDescription"])
$KPay_transaction_description=$KPay_curl_output_response["TransactionDescription"];
if($KPay_curl_output_response["FPXTransactionID"])
$KPay_FPX_transaction_id=$KPay_curl_output_response["FPXTransactionID"];
if($KPay_curl_output_response["BusinessName"])
$KPay_curl_seller_name=$KPay_curl_output_response["BusinessName"];
return $KPay_curl_output_response;
} # Function to communicate with KPay API end.
} # Class plgHikashoppaymentKpay end.