|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\components\payment; |
|
4
|
|
|
|
|
5
|
|
|
use app\modules\shop\models\OrderTransaction; |
|
6
|
|
|
use yii\helpers\Json; |
|
7
|
|
|
use yii\web\BadRequestHttpException; |
|
8
|
|
|
|
|
9
|
|
|
/*** |
|
10
|
|
|
* |
|
11
|
|
|
* В настройках прописать следующие данные, отправлять с помощью POST |
|
12
|
|
|
* |
|
13
|
|
|
* Result URL: /shop/payment/custom-check?type=2&action=result |
|
14
|
|
|
* Success Url /shop/payment/custom-check?type=2&action=success |
|
15
|
|
|
* Fail Url /shop/payment/custom-check?type=2&action=fail |
|
16
|
|
|
* |
|
17
|
|
|
* Где type - id Robokassa на странице /shop/backend-payment-type/index |
|
18
|
|
|
* |
|
19
|
|
|
* Class RobokassaPayment |
|
20
|
|
|
* @package app\components\payment |
|
21
|
|
|
*/ |
|
22
|
|
|
class RobokassaPayment extends AbstractPayment |
|
23
|
|
|
{ |
|
24
|
|
|
public $merchantLogin; |
|
25
|
|
|
public $merchantPass1; |
|
26
|
|
|
public $merchantPass2; |
|
27
|
|
|
public $merchantUrl; |
|
28
|
|
|
|
|
29
|
|
|
public function content() |
|
30
|
|
|
{ |
|
31
|
|
|
$invoiceDescription = urlencode(\Yii::t('app', 'Payment of order #{orderId}', ['orderId' => $this->order->id])); |
|
32
|
|
|
$inCurrency = ''; |
|
33
|
|
|
$culture = 'ru'; |
|
34
|
|
|
$data = [ |
|
35
|
|
|
'MrchLogin' => $this->merchantLogin, |
|
36
|
|
|
'OutSum' => $this->transaction->total_sum, |
|
37
|
|
|
'InvId' => $this->transaction->id, |
|
38
|
|
|
'IncCurrLabel' => $inCurrency, |
|
39
|
|
|
'Desc' => $invoiceDescription, |
|
40
|
|
|
'Culture' => $culture, |
|
41
|
|
|
'Encoding' => 'utf-8' |
|
42
|
|
|
]; |
|
43
|
|
|
if ($this->merchantUrl == 'test.robokassa.ru') { |
|
44
|
|
|
$data['IsTest'] = 1; |
|
45
|
|
|
} |
|
46
|
|
|
$SignatureValue = $this->getSignature(); |
|
47
|
|
|
$data['SignatureValue'] = $SignatureValue; |
|
48
|
|
|
$url = 'http://' . $this->merchantUrl . '/Index.aspx?' . http_build_query($data); |
|
49
|
|
|
|
|
50
|
|
|
$this->redirect($url); |
|
51
|
|
|
|
|
52
|
|
|
\Yii::$app->end(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return string|null |
|
|
|
|
|
|
57
|
|
|
*/ |
|
58
|
|
|
public function customCheck() |
|
59
|
|
|
{ |
|
60
|
|
|
|
|
61
|
|
View Code Duplication |
if (null === $this->transaction = $this->loadTransaction(\Yii::$app->request->post('InvId'))) { |
|
62
|
|
|
throw new BadRequestHttpException(); |
|
63
|
|
|
} |
|
64
|
|
|
if (\Yii::$app->request->get('action') == 'result') { |
|
65
|
|
|
return $this->checkResult($this->transaction->generateHash()); |
|
66
|
|
|
} elseif (\Yii::$app->request->get('action') == 'success') { |
|
67
|
|
|
return $this->redirect( |
|
|
|
|
|
|
68
|
|
|
$this->createSuccessUrl() |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
return $this->redirect( |
|
|
|
|
|
|
72
|
|
|
$this->createErrorUrl() |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
public function checkResult($hash = '') |
|
79
|
|
|
{ |
|
80
|
|
|
\Yii::$app->response->format = \yii\web\Response::FORMAT_RAW; |
|
81
|
|
|
$result = "bad sign\n"; |
|
82
|
|
|
if ($this->getSignatureResult() == \Yii::$app->request->post('SignatureValue')) { |
|
83
|
|
|
$this->transaction->result_data = Json::encode([ |
|
84
|
|
|
\Yii::$app->request->post('OutSum'), |
|
85
|
|
|
\Yii::$app->request->post('InvId'), |
|
86
|
|
|
\Yii::$app->request->post('SignatureValue') |
|
87
|
|
|
]); |
|
88
|
|
|
$this->transaction->status = OrderTransaction::TRANSACTION_SUCCESS; |
|
89
|
|
|
if ($this->transaction->save(true, ['status', 'result_data'])) { |
|
90
|
|
|
$result = "OK" . $this->transaction->id . "\n"; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
return $result; |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function getSignature() |
|
98
|
|
|
{ |
|
99
|
|
|
$sData = [ |
|
100
|
|
|
$this->merchantLogin, |
|
101
|
|
|
$this->transaction->total_sum, |
|
102
|
|
|
$this->transaction->id, |
|
103
|
|
|
|
|
104
|
|
|
]; |
|
105
|
|
|
$sData[] = $this->merchantPass1; |
|
106
|
|
|
return md5(implode(':', $sData)); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
protected function getSignatureResult() |
|
110
|
|
|
{ |
|
111
|
|
|
$sData = [ |
|
112
|
|
|
\Yii::$app->request->post('OutSum'), |
|
113
|
|
|
\Yii::$app->request->post('InvId'), |
|
114
|
|
|
$this->merchantPass2 |
|
115
|
|
|
]; |
|
116
|
|
|
return strtoupper(md5(implode(':', $sData))); |
|
117
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
?> |
|
123
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.