|
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
|
|
|
use yii\web\ServerErrorHttpException; |
|
9
|
|
|
|
|
10
|
|
|
class FutubankPayment extends AbstractPayment |
|
11
|
|
|
{ |
|
12
|
|
|
protected $currency; |
|
13
|
|
|
protected $merchant; |
|
14
|
|
|
protected $secretKey; |
|
15
|
|
|
protected $testing; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param $data |
|
19
|
|
|
* @return string |
|
20
|
|
|
*/ |
|
21
|
|
|
private function getSignature($data) |
|
22
|
|
|
{ |
|
23
|
|
|
ksort($data); |
|
24
|
|
|
$chunks = array(); |
|
25
|
|
|
foreach ($data as $key => $value) { |
|
26
|
|
|
if ($value && ($key != 'signature')) { |
|
27
|
|
|
$chunks[] = $key . '=' . base64_encode($value); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
return sha1($this->secretKey . sha1($this->secretKey . implode('&', $chunks))); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param int $length |
|
35
|
|
|
* @return string |
|
36
|
|
|
*/ |
|
37
|
|
|
private function generateSalt($length = 10) |
|
38
|
|
|
{ |
|
39
|
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
40
|
|
|
$result = ''; |
|
41
|
|
|
for ($i = 0; $i < $length; $i++) { |
|
42
|
|
|
$result .= $characters[rand(0, strlen($characters) - 1)]; |
|
43
|
|
|
} |
|
44
|
|
|
return $result; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
public function content() |
|
51
|
|
|
{ |
|
52
|
|
|
$formData = [ |
|
53
|
|
|
'client_email' => '', |
|
54
|
|
|
'client_name' => '', |
|
55
|
|
|
'client_phone' => '', |
|
56
|
|
|
'merchant' => $this->merchant, |
|
57
|
|
|
'unix_timestamp' => time(), |
|
58
|
|
|
'salt' => $this->generateSalt(32), |
|
59
|
|
|
'amount' => $this->transaction->total_sum, |
|
60
|
|
|
'currency' => $this->currency, |
|
61
|
|
|
'description' => 'Order #' . $this->order->id, |
|
62
|
|
|
'order_id' => $this->transaction->id, |
|
63
|
|
|
'success_url' => $this->createSuccessUrl(['id' => $this->transaction->id, 'hash' => $this->transaction->generateHash()]), |
|
|
|
|
|
|
64
|
|
|
'fail_url' => $this->createFailUrl(['id' => $this->transaction->id, 'hash' => $this->transaction->generateHash()]), |
|
|
|
|
|
|
65
|
|
|
'cancel_url' => $this->createCancelUrl(['id' => $this->transaction->id, 'hash' => $this->transaction->generateHash()]), |
|
|
|
|
|
|
66
|
|
|
'meta' => Json::encode(['transactionId' => $this->transaction->id]), |
|
67
|
|
|
]; |
|
68
|
|
|
if ($this->testing == 1) { |
|
69
|
|
|
$formData['testing'] = 1; |
|
70
|
|
|
} |
|
71
|
|
|
$formData['signature'] = $this->getSignature($formData); |
|
72
|
|
|
return $this->render( |
|
73
|
|
|
'futubank', |
|
74
|
|
|
[ |
|
75
|
|
|
'formData' => $formData, |
|
76
|
|
|
'order' => $this->order, |
|
77
|
|
|
'transaction' => $this->transaction, |
|
78
|
|
|
] |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $hash |
|
84
|
|
|
* @return string |
|
85
|
|
|
* @throws BadRequestHttpException |
|
86
|
|
|
* @throws ServerErrorHttpException |
|
87
|
|
|
* @throws \yii\web\NotFoundHttpException |
|
88
|
|
|
*/ |
|
89
|
|
|
public function checkResult($hash = '') |
|
90
|
|
|
{ |
|
91
|
|
|
$transactionId = \Yii::$app->request->post('order_id'); |
|
92
|
|
|
if (null === $transactionId) { |
|
93
|
|
|
throw new BadRequestHttpException(); |
|
94
|
|
|
} |
|
95
|
|
|
if (null === $model = $this->loadTransaction($transactionId)) { |
|
96
|
|
|
throw new BadRequestHttpException(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$model->result_data = Json::encode(\Yii::$app->request->post()); |
|
100
|
|
|
if ($this->getSignature(\Yii::$app->request->post()) === \Yii::$app->request->post('signature')) { |
|
101
|
|
|
$model->status = OrderTransaction::TRANSACTION_SUCCESS; |
|
102
|
|
|
if ($model->save(true, ['status', 'result_data'])) { |
|
103
|
|
|
return 'OK ' . $model->id; |
|
104
|
|
|
} else { |
|
105
|
|
|
throw new ServerErrorHttpException(); |
|
106
|
|
|
} |
|
107
|
|
|
} else { |
|
108
|
|
|
$model->status = OrderTransaction::TRANSACTION_ERROR; |
|
109
|
|
|
$model->save(true, ['status', 'result_data']); |
|
110
|
|
|
throw new BadRequestHttpException(); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: