1 | <?php |
||
26 | class PayController extends \hiqdev\yii2\merchant\controllers\PayController |
||
27 | { |
||
28 | const SESSION_MERCHANT_LATEST_TRANSACTION_ID = 'MERCHANT_LATEST_TRANSACTION_ID'; |
||
29 | |||
30 | 1 | public function actions() |
|
31 | { |
||
32 | 1 | return array_merge(parent::actions(), [ |
|
33 | 1 | '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 | 1 | } |
|
40 | ] |
||
41 | ]); |
||
42 | } |
||
43 | |||
44 | public function getMerchantModule() |
||
48 | |||
49 | public function render($view, $params = []) |
||
53 | |||
54 | public function checkNotify(string $transactionId = null): ?Transaction |
||
55 | { |
||
56 | $id = $transactionId |
||
57 | ?? Yii::$app->request->get('transactionId') |
||
58 | ?? Yii::$app->request->post('transactionId') |
||
59 | ?? Yii::$app->session->get(self::SESSION_MERCHANT_LATEST_TRANSACTION_ID); |
||
60 | |||
61 | $transaction = $this->getMerchantModule()->findTransaction($id); |
||
62 | if ($transaction === null) { |
||
63 | return null; |
||
64 | } |
||
65 | |||
66 | $data = array_merge([ |
||
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) |
||
127 | } |
||
128 |