|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Finance module for HiPanel |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/hipanel-module-finance |
|
6
|
|
|
* @package hipanel-module-finance |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hipanel\modules\finance\controllers; |
|
12
|
|
|
|
|
13
|
|
|
use hipanel\modules\finance\models\Merchant; |
|
14
|
|
|
use hiqdev\hiart\ResponseErrorException; |
|
15
|
|
|
use hiqdev\yii2\merchant\actions\RequestAction; |
|
16
|
|
|
use hiqdev\yii2\merchant\events\TransactionInsertEvent; |
|
17
|
|
|
use hiqdev\yii2\merchant\transactions\Transaction; |
|
18
|
|
|
use Yii; |
|
19
|
|
|
use yii\base\InvalidParamException; |
|
20
|
|
|
use yii\helpers\Json; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class PayController. |
|
24
|
|
|
* @property \hipanel\modules\finance\Module $module |
|
25
|
|
|
*/ |
|
26
|
|
|
class PayController extends \hiqdev\yii2\merchant\controllers\PayController |
|
27
|
|
|
{ |
|
28
|
|
|
const SESSION_MERCHANT_LATEST_TRANSACTION_ID = 'MERCHANT_LATEST_TRANSACTION_ID'; |
|
29
|
|
|
|
|
30
|
|
|
public function actions() |
|
31
|
|
|
{ |
|
32
|
|
|
return array_merge(parent::actions(), [ |
|
33
|
|
|
'request' => [ |
|
34
|
|
|
'class' => RequestAction::class, |
|
35
|
|
|
'on ' . RequestAction::EVENT_AFTER_TRANSACTION_INSERT => function (TransactionInsertEvent $event) { |
|
36
|
|
|
if ($event->transaction instanceof Transaction) { |
|
37
|
|
|
Yii::$app->session->set(self::SESSION_MERCHANT_LATEST_TRANSACTION_ID, $event->transaction->getId()); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
] |
|
41
|
|
|
]); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getMerchantModule() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->module->getMerchant(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function render($view, $params = []) |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->getMerchantModule()->getPayController()->render($view, $params); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function checkNotify() |
|
55
|
|
|
{ |
|
56
|
|
|
$id = Yii::$app->request->get('transactionId') |
|
57
|
|
|
?? Yii::$app->request->post('transactionId') |
|
58
|
|
|
?? Yii::$app->session->get(self::SESSION_MERCHANT_LATEST_TRANSACTION_ID); |
|
59
|
|
|
|
|
60
|
|
|
$transaction = $this->getMerchantModule()->findTransaction($id); |
|
61
|
|
|
if ($transaction === null) { |
|
62
|
|
|
return null; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$data = array_merge([ |
|
66
|
|
|
'transactionId' => $transaction->getId(), |
|
67
|
|
|
'merchant' => $transaction->getMerchant(), |
|
68
|
|
|
'username' => $transaction->getParameter('username'), |
|
69
|
|
|
], $_REQUEST); |
|
70
|
|
|
Yii::info(http_build_query($data), 'merchant'); |
|
71
|
|
|
|
|
72
|
|
|
if (($input = file_get_contents('php://input')) !== null) { |
|
73
|
|
|
try { |
|
74
|
|
|
$data['rawBody'] = Json::decode($input); |
|
75
|
|
|
} catch (InvalidParamException $e) {} |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
try { |
|
79
|
|
|
return Yii::$app->get('hiart')->callWithDisabledAuth(function () use ($transaction, $data) { |
|
80
|
|
|
$result = Merchant::perform('pay', $data); |
|
81
|
|
|
|
|
82
|
|
|
$this->completeTransaction($transaction, $result); |
|
83
|
|
|
return $transaction; |
|
84
|
|
|
}); |
|
85
|
|
|
} catch (ResponseErrorException $e) { |
|
86
|
|
|
// Does not matter |
|
87
|
|
|
return $transaction; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function actionProxyNotification() |
|
92
|
|
|
{ |
|
93
|
|
|
// Currently used at least for FreeKassa integration |
|
94
|
|
|
$data = array_merge(Yii::$app->request->get(), Yii::$app->request->post()); |
|
95
|
|
|
|
|
96
|
|
|
$result = Yii::$app->get('hiart')->callWithDisabledAuth(function () use ($data) { |
|
97
|
|
|
return Merchant::perform('pay-transaction', $data); |
|
98
|
|
|
}); |
|
99
|
|
|
|
|
100
|
|
|
$this->layout = false; |
|
101
|
|
|
return $result; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param Transaction $transaction |
|
106
|
|
|
* @param $response |
|
107
|
|
|
* @return mixed |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function completeTransaction($transaction, $response) |
|
110
|
|
|
{ |
|
111
|
|
|
if ($transaction->isCompleted() || isset($data['_error'])) { |
|
|
|
|
|
|
112
|
|
|
return $transaction; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$transaction->complete(); |
|
116
|
|
|
$transaction->addParameter('bill_id', $response['id']); |
|
117
|
|
|
|
|
118
|
|
|
$this->getMerchantModule()->saveTransaction($transaction); |
|
119
|
|
|
|
|
120
|
|
|
return $transaction; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|