Completed
Push — master ( 047c92...9627e4 )
by Dmitry
06:50
created

PayController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 7.55%

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 8
dl 0
loc 116
ccs 4
cts 53
cp 0.0755
rs 10
c 0
b 0
f 0

6 Methods

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