Completed
Push — master ( 8d56c2...eec4fb )
by Dmitry
17:13
created

PayController::actions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 1
nop 0
crap 6
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
    public function actions()
31
    {
32
        return array_merge(parent::actions(), [
33
            '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
                }
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()
55
    {
56
        $id = Yii::$app->request->get('transactionId')
57
            ?? Yii::$app->request->post('transactionId')
58
            ?? Yii::$app->session->get(self::SESSION_MERCHANT_LATEST_TRANSACTION_ID);
59
60
        $transaction = $this->getMerchantModule()->findTransaction($id);
61
        if ($transaction === null) {
62
            return null;
63
        }
64
65
        $data = array_merge([
66
            'transactionId' => $transaction->getId(),
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
        $transaction->complete();
116
        $transaction->addParameter('bill_id', $response['id']);
117
118
        $this->getMerchantModule()->saveTransaction($transaction);
119
120
        return $transaction;
121
    }
122
}
123