Completed
Push — master ( 74a8ab...ec2601 )
by Dmitry
04:54
created

PayController::checkNotify()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 14
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 14
nc 4
nop 0
crap 12
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\transactions\Transaction;
16
use Yii;
17
18
/**
19
 * Class PayController.
20
 * @property \hipanel\modules\finance\Module $module
21
 */
22
class PayController extends \hiqdev\yii2\merchant\controllers\PayController
23
{
24
    public function getMerchantModule()
25
    {
26
        return $this->module->getMerchant();
27
    }
28
29
    public function render($view, $params = [])
30
    {
31
        return $this->getMerchantModule()->getPayController()->render($view, $params);
32
    }
33
34
    public function checkNotify()
0 ignored issues
show
Coding Style introduced by
checkNotify uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
35
    {
36
        $id = Yii::$app->request->get('transactionId') ?: Yii::$app->request->post('transactionId');
37
        $transaction = $this->getMerchantModule()->findTransaction($id);
38
        if ($transaction === null) {
39
            return null;
40
        }
41
42
        $data = array_merge([
43
            'transactionId' => $transaction->getId(),
44
            'username'      => $transaction->getParameter('username'),
45
            'merchant'      => $transaction->getParameter('merchant'),
46
        ], $_REQUEST);
47
        Yii::info(http_build_query($data), 'merchant');
48
49
        return Yii::$app->get('hiart')->callWithDisabledAuth(function () use ($transaction, $data) {
50
            $result = Merchant::perform('pay', $data);
51
            return $this->completeTransaction($transaction, $result);
52
        });
53
    }
54
55
    /**
56
     * @param Transaction $transaction
57
     * @param $response
58
     * @return mixed
59
     */
60
    protected function completeTransaction($transaction, $response)
61
    {
62
        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...
63
            return $transaction;
64
        }
65
66
        $transaction->complete();
0 ignored issues
show
Bug introduced by
The method complete() does not exist on hiqdev\yii2\merchant\transactions\Transaction. Did you maybe mean isCompleted()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
67
        $transaction->addParameter('bill_id', $response['id']);
68
69
        $this->getMerchantModule()->saveTransaction($transaction);
70
71
        return $transaction;
72
    }
73
}
74