-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdotpay.php
446 lines (401 loc) · 13.1 KB
/
dotpay.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
class plgHikashoppaymentDotpay extends hikashopPaymentPlugin
{
protected $autoloadLanguage = true;
private $statusMessage;
var $accepted_currencies = array( "PLN","EUR", "USD", "GBP", "JPY","CZK", "SEK" );
var $multiple = true;
var $name = 'dotpay';
var $pluginConfig = array(
'identifier' => array("DOTPAY_ID",'input'),
'pin' => array("DOTPAY_PIN",'input'),
'dotpay_mode' => array('DOTPAY_MODE', 'list',array(
'test' => 'TEST',
'production' => 'PRODUCTION'
)),
'invalid_status' => array('INVALID_STATUS', 'orderstatus'),
'pending_status' => array('PENDING_STATUS', 'orderstatus'),
'verified_status' => array('VERIFIED_STATUS', 'orderstatus')
);
const API_VERSION = 'dev';
const PAYMENT_TYPE = '0';
const ADDRESS_TYPE = 'billing_address';
const TRUSTED_IP = '195.150.9.37';
const DOTPAY_URL = 'https://ssl.dotpay.pl/';
const DOTPAY_TEST_URL = 'https://ssl.dotpay.pl/test_payment/';
public function __construct(&$subject, $config)
{
$this->pluginConfig['notify_url'][2] = HIKASHOP_LIVE.'index.php?option=com_hikashop&ctrl=checkout&task=notify¬if_payment='.$this->name.'&tmpl=component';
return parent::__construct($subject, $config);
}
/**
* This method is internal hikashop method check out:
* http://www.hikashop.com/support/documentation/62-hikashop-developer-documentation.html#payment
*
* Is triggered after customer place an order
*
* @param $order
* @param $methods
* @param $method_id
* @return bool
*/
public function onAfterOrderConfirm(&$order,&$methods,$method_id)
{
parent::onAfterOrderConfirm($order,$methods,$method_id);
if (!$this->isPaymentParametersValidate()){
return false;
}
$this->vars = $this->prepareVarsArray($order);
return $this->showPage('end');
}
/**
* This is internal hikashop method it is invoke to move image
*
* Moveing image is necessary because uninstal cause issue. For more details
* read
* http://www.hikashop.com/forum/development/878700-uninstall-custom-payment-plugin-image.html
* @param $element
*/
public function onPaymentConfiguration(&$element)
{
$imagePath = HIKASHOP_MEDIA.'images/';
$this->moveImage($imagePath);
parent::onPaymentConfiguration($element);
}
/**
* Move image from folder /image/payment/dotpay/ to /image/payment
*
* @param $imagePath
*/
private function moveImage($imagePath)
{
$src = $imagePath . 'payment'.DS.'dotpay/dotpay.png';
$destination = $imagePath.'payment/dotpay.png';
JFile::copy($src, $destination,null, true );
}
/**
* This method return correct url to dotpay based on mode from config
*
* @return string
*/
public function getDotpayUrl()
{
if($this->payment_params->dotpay_mode == 'production'){
return self::DOTPAY_URL;
}
return self::DOTPAY_TEST_URL;
}
/**
*
* This method is internal hikashop method check out:
* http://www.hikashop.com/support/documentation/62-hikashop-developer-documentation.html#payment
*
* Is triggered before customer place an order and here it use to validate
*
* @param $order
* @param $do
* @return bool
*/
public function onBeforeOrderCreate(&$order,&$do){
if(parent::onBeforeOrderCreate($order, $do) === true)
return true;
if (!$this->isPaymentParametersValidate()){
$do = false;
}
}
/**
* This is internal hikashop method check out
* http://www.hikashop.com/support/documentation/62-hikashop-developer-documentation.html#payment
*
* It set default values when you create new payment method in admin
*
* @param $element
*/
public function getPaymentDefaultValues(&$element)
{
$element->payment_name='Dotpay';
$element->payment_description='Worldwide secure payment';
$element->payment_images='dotpay';
$element->payment_params->address_type="billing";
$element->payment_params->invalid_status='cancelled';
$element->payment_params->pending_status='created';
$element->payment_params->verified_status='confirmed';
}
/**
* This is internal hikashop method check out
* http://www.hikashop.com/support/documentation/62-hikashop-developer-documentation.html#payment
*
* It is triggered when payment provider send back notification to shop.
* This method is responsible for recive request, validete and if everything is correct it change order status
*
* @param $statuses
* @return bool|string
*/
public function onPaymentNotification(&$statuses)
{
$vars = $this->createVarsFromRequest();
$orderId = (int)@$vars['control'];
$orderDb = $this->loadOrderRelatedData($orderId);
if(!$this->isRequestValidated($vars, $orderDb)){
return $this->processRequest($orderId, $this->payment_params->invalid_status);
}
if(!$this->isSignatureMatch($vars)){
return $this->processRequest($orderId, $this->payment_params->pending_status);
}
if($this->isStatusCompleted($vars)){
return $this->processRequest($orderId, $this->payment_params->verified_status);
}else{
$this->statusMessage = $vars['operation_status'];
return $this->processRequest($orderId, $this->payment_params->pending_status);
}
}
/**
*
* Updateing order, collect error log and then return message do dotpay
*
* @param $orderId
* @param $status
* @return mixed
*/
private function processRequest($orderId, $status)
{
$this->modifyOrder($orderId, $status, true, true);
if(!($this->statusMessage == 'OK' || $this->statusMessage == 'new')){
$this->writeToLog('Dotpay error status message: '. $this->statusMessage);
}
return $this->statusMessage;
}
/**
* This method prepare array which is send to dotpay based on order, payment configuration
* and customer address
*
* @param $order
* @return array
*/
private function prepareVarsArray($order)
{
$returnUrl = HIKASHOP_LIVE.'index.php?option=com_hikashop&ctrl=checkout&task=after_end&order_id='.$order->order_id . $this->url_itemid;
$address = $this->getAddress($order);
$vars = array(
'id' => $this->payment_params->identifier,
'amount' => $this->getValidAmount($order),
'currency' => $this->currency->currency_code,
'control' => (string)$order->order_id,
'description' => $order->order_number,
'lang' => $this->getLanguage(),
'URL' => $returnUrl,
'URLC' => $this->pluginConfig['notify_url'][2],
'type' => self::PAYMENT_TYPE,
'api_version' => self::API_VERSION,
// below additional optional parameters
'firstname' => $address->address_firstname,
'lastname' => $address->address_lastname,
'email' => $order->customer->email,
'street' => $address->address_street,
'city' => $address->address_city,
'postcode' => $address->address_post_code,
'phone' => $address->address_telephone,
'country' => $address->address_country->zone_name,
);
return $vars;
}
/**
* Return address of customer who place an order
*
* @param $order
* @return mixed
*/
private function getAddress($order)
{
$addressType = self::ADDRESS_TYPE;
// below is equivalent to $order->cart->billing_address
return $order->cart->$addressType;
}
/**
* Return language of joomla which is set by admin
*
* @return string
*/
private function getLanguage()
{
$lang = JFactory::getLanguage();
return strtolower(substr($lang->get('tag'), 0, 2));
}
/**
* Check if dotpay parameters from config is not empty
*
* @return bool
*/
private function isPaymentParametersValidate()
{
if (!$this->payment_params->identifier || !$this->payment_params->pin || strlen($this->payment_params->pin) < 16){
$this->app->enqueueMessage(JText::_("INCOMPLETE_CONFIG"),'error');
return false;
}
if (!is_numeric($this->payment_params->identifier) || strlen($this->payment_params->identifier) < 6){
$this->app->enqueueMessage(JText::_("BAD_ID"),'error');
return false;
}
return true;
}
/**
* Return price in correct format (two decimal character like: 10,00)
*
* @param $order
* @return float
*/
private function getValidAmount($order)
{
return round($order->cart->full_total->prices[0]->price_value_with_tax,2);
}
/**
* Check if notification is validated,
* After validate request : trusted ip and if request is post
* Check folloed parameters:
* price from order and returned from dotpay
* operation status
*
* @param $vars
* @param $orderDb
* @return bool
*/
private function isRequestValidated($vars, $orderDb)
{
if($_SERVER['REQUEST_METHOD'] != 'POST'){
$this->statusMessage = 'invalid request method';
return false;
}
if(!$this->isTrustedIp()){
$this->statusMessage = 'untrusted Ip';
return false;
}
if(!$this->isPriceMatch($vars, $orderDb)){
$this->statusMessage = 'price does not match';
return false;
}
if($vars['operation_status'] == 'rejected'){
$this->statusMessage = 'OK';
return false;
}
return true;
}
/**
* Check whether status is completed
*
* @param $vars
* @return bool
*/
private function isStatusCompleted($vars)
{
if( $vars['operation_status'] == 'completed'){
$this->statusMessage = 'OK';
return true;
}
}
/**
*
* Check if incoming request goes from trusted ip
*
* @return bool
*/
private function isTrustedIp()
{
if($_SERVER['REMOTE_ADDR'] == self::TRUSTED_IP || $_SERVER['REMOTE_ADDR'] == '127.0.0.1' ){
return true;
}
return false;
}
/**
* It check if price from dotpay and from order are the same
*
* @param $vars
* @param $orderDb
* @return bool
*/
private function isPriceMatch($vars, $orderDb)
{
$price = round($orderDb->order_full_price, 2);
if(isset($vars['operation_original_amount']) && $vars['operation_original_amount'] == $price){
return true;
}
}
/**
* Check if signature from dotpay is the same like calculate signature
*
* @param $vars
* @return bool
*
*/
private function isSignatureMatch($vars)
{
if(!isset($vars['signature']) || $this->calculateSignature($vars) != $vars['signature']){
$this->statusMessage = 'signature do not match';
return false;
}
return true;
}
/**
* Calculate sygnature based on dotpay parameters and pin
*
* @param $vars
* @return string
*/
private function calculateSignature($vars)
{
$string = $this->payment_params->pin .
(isset($vars['id']) ? $vars['id'] : '').
(isset($vars['operation_number']) ? $vars['operation_number'] : '').
(isset($vars['operation_type']) ? $vars['operation_type'] : '').
(isset($vars['operation_status']) ? $vars['operation_status'] : '').
(isset($vars['operation_amount']) ? $vars['operation_amount'] : '').
(isset($vars['operation_currency']) ? $vars['operation_currency'] : '').
(isset($vars['operation_withdrawal_amount']) ? $vars['operation_withdrawal_amount'] : '').
(isset($vars['operation_commission_amount']) ? $vars['operation_commission_amount'] : '').
(isset($vars['operation_original_amount']) ? $vars['operation_original_amount'] : '').
(isset($vars['operation_original_currency']) ? $vars['operation_original_currency'] : '').
(isset($vars['operation_datetime']) ? $vars['operation_datetime'] : '').
(isset($vars['operation_related_number']) ? $vars['operation_related_number'] : '').
(isset($vars['control']) ? $vars['control'] : '').
(isset($vars['description']) ? $vars['description'] : '').
(isset($vars['email']) ? $vars['email'] : '').
(isset($vars['p_info']) ? $vars['p_info'] : '').
(isset($vars['p_email']) ? $vars['p_email'] : '').
(isset($vars['channel']) ? $vars['channel'] : '').
(isset($vars['channel_country']) ? $vars['channel_country'] : '').
(isset($vars['geoip_country']) ? $vars['geoip_country'] : '');
return hash('sha256', $string);
}
/**
* This method find order based on it id and then assign order parameter to class variables
* check out loadOrderParams and loadOrderData in parent class
*
* @param $orderId
* @return bool|null
*/
private function loadOrderRelatedData($orderId)
{
$dbOrder = $this->getOrder($orderId);
$this->loadPaymentParams($dbOrder);
if(empty($this->payment_params))
return false;
$this->loadOrderData($dbOrder);
return $dbOrder;
}
/**
* Modify request from dotpay to another format
*
* @return array
*/
private function createVarsFromRequest()
{
$vars = array();
$filter = JFilterInput::getInstance();
foreach($_REQUEST as $key => $value)
{
$key = $filter->clean($key);
$value = JRequest::getString($key);
$vars[$key]=$value;
}
return $vars;
}
}