Completed
Push — master ( 4d1323...3ea89a )
by Dmitry
09:07
created

PayController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 8.51%

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 8
dl 0
loc 102
ccs 4
cts 47
cp 0.0851
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getMerchantModule() 0 4 1
A render() 0 4 1
A completeTransaction() 0 18 4
A actions() 0 13 2
A actionProxyNotification() 0 12 1
A checkNotify() 0 36 5
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 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
        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'])) {
0 ignored issues
show
Bug introduced by
The variable $data seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
112
            return $transaction;
113
        }
114
115
        if ($response === '"OK"') {
116
            echo $response;
117
            Yii::$app->end();
118
        }
119
120
        $transaction->complete();
121
        $transaction->addParameter('bill_id', $response['id']);
122
123
        $this->getMerchantModule()->saveTransaction($transaction);
124
125
        return $transaction;
126
    }
127
}
128