Completed
Push — master ( 87b34c...047c92 )
by Dmitry
10:00
created

PayController::getMerchantModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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 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 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()
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(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) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
76
            }
77
        }
78
79
        try {
80
            return Yii::$app->get('hiart')->callWithDisabledAuth(function () use ($transaction, $data) {
81
                $result = Merchant::perform('pay', $data);
82
83
                $this->completeTransaction($transaction, $result);
84
85
                return $transaction;
86
            });
87
        } catch (ResponseErrorException $e) {
88
            // Does not matter
89
            return $transaction;
90
        }
91
    }
92
93
    public function actionProxyNotification()
94
    {
95
        // Currently used at least for FreeKassa integration
96
        $data = array_merge(Yii::$app->request->get(), Yii::$app->request->post());
97
98
        $result = Yii::$app->get('hiart')->callWithDisabledAuth(function () use ($data) {
99
            return Merchant::perform('pay-transaction', $data);
100
        });
101
102
        $this->layout = false;
103
104
        return $result;
105
    }
106
107
    /**
108
     * @param Transaction $transaction
109
     * @param string|array $response
110
     * @throws \yii\base\ExitException
111
     * @return mixed
112
     */
113
    protected function completeTransaction($transaction, $response)
114
    {
115
        if ($transaction->isCompleted() || isset($response['_error'])) {
116
            return $transaction;
117
        }
118
119
        if ($response === '"OK"') {
120
            echo $response;
121
            Yii::$app->end();
122
        }
123
124
        $transaction->complete();
125
        $transaction->addParameter('bill_id', $response['id']);
126
127
        $this->getMerchantModule()->saveTransaction($transaction);
128
129
        return $transaction;
130
    }
131
}
132