Completed
Push — master ( fa0274...9dd959 )
by Dmitry
04:54
created

PayController::actions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 3
cts 5
cp 0.6
rs 9.8333
c 0
b 0
f 0
cc 2
nc 1
nop 0
crap 2.2559
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-2019, 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 function is_array;
19
use Yii;
20
use yii\base\InvalidParamException;
21
use yii\helpers\Json;
22
23
/**
24
 * Class PayController.
25
 *
26
 * @property \hipanel\modules\finance\Module $module
27
 */
28
class PayController extends \hiqdev\yii2\merchant\controllers\PayController
29
{
30
    const SESSION_MERCHANT_LATEST_TRANSACTION_ID = 'MERCHANT_LATEST_TRANSACTION_ID';
31 1
32
    public function actions()
33 1
    {
34 1
        return array_merge(parent::actions(), [
35
            'request' => [
36
                'class' => RequestAction::class,
37
                'on ' . RequestAction::EVENT_AFTER_TRANSACTION_INSERT => function (TransactionInsertEvent $event) {
38
                    if ($event->transaction instanceof Transaction) {
39
                        Yii::$app->session->set(self::SESSION_MERCHANT_LATEST_TRANSACTION_ID, $event->transaction->getId());
40 1
                    }
41
                },
42
            ],
43
        ]);
44
    }
45
46
    public function getMerchantModule()
47
    {
48
        return $this->module->getMerchant();
49
    }
50
51
    public function render($view, $params = [])
52
    {
53
        return $this->getMerchantModule()->getPayController()->render($view, $params);
54
    }
55
56
    public function checkNotify(string $transactionId = null): ?Transaction
57
    {
58
        $id = $transactionId
59
            ?? Yii::$app->request->get('transactionId')
60
            ?? Yii::$app->request->post('transactionId')
61
            ?? Yii::$app->session->get(self::SESSION_MERCHANT_LATEST_TRANSACTION_ID);
62
63
        $transaction = $this->getMerchantModule()->findTransaction($id);
64
        if ($transaction === null) {
65
            return null;
66
        }
67
68
        $data = array_merge([
69
            'merchant' => $transaction->getMerchant(),
70
            'username' => $transaction->getParameter('username'),
71
        ], $_REQUEST);
72
        Yii::info(http_build_query($data), 'merchant');
73
74
        if (($input = file_get_contents('php://input')) !== null) {
75
            try {
76
                $data['rawBody'] = Json::decode($input);
77
            } catch (InvalidParamException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
78
            }
79
        }
80
81
        try {
82
            return Yii::$app->get('hiart')->callWithDisabledAuth(function () use ($transaction, $data) {
83
                $result = Merchant::perform('pay-transaction', $data);
84
                $this->completeTransaction($transaction, $result);
85
86
                return $transaction;
87
            });
88
        } catch (ResponseErrorException $e) {
89
        } // Does not matter, let's try the old way then.
90
91
        try {
92
            return Yii::$app->get('hiart')->callWithDisabledAuth(function () use ($transaction, $data) {
93
                $result = Merchant::perform('pay', $data);
94
                $this->completeTransaction($transaction, $result);
95
96
                return $transaction;
97
            });
98
        } catch (ResponseErrorException $e) {
99
        } // Still no luck? Then it's not the right request.
100
101
102
        return $transaction;
103
    }
104
105
    public function actionProxyNotification()
106
    {
107
        // Currently used at least for FreeKassa integration
108
        $data = array_merge(Yii::$app->request->get(), Yii::$app->request->post());
109
110
        $result = Yii::$app->get('hiart')->callWithDisabledAuth(function () use ($data) {
111
            return Merchant::perform('pay-transaction', $data);
112
        });
113
114
        $this->layout = false;
115
116
        return $result;
117
    }
118
119
    /**
120
     * @param Transaction $transaction
121
     * @param string|array $response
122
     * @return Transaction
123
     * @throws \yii\base\ExitException
124
     */
125
    protected function completeTransaction($transaction, $response)
126
    {
127
        if ($transaction->isCompleted() || isset($response['_error'])) {
128
            return $transaction;
129
        }
130
131
        if ($response === '"OK"') {
132
            echo $response;
133
            Yii::$app->end();
134
        }
135
136
        if (!is_array($response)) {
137
            return $transaction;
138
        }
139
140
        $transaction->complete();
141
        $transaction->addParameter('bill_id', $response['id']);
142
143
        $this->getMerchantModule()->saveTransaction($transaction);
144
145
        return $transaction;
146
    }
147
}
148