Completed
Push — master ( ec2601...0a64ef )
by Dmitry
05:39 queued 01:17
created

PayController::checkNotify()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 26
ccs 0
cts 15
cp 0
rs 8.5806
cc 4
eloc 17
nc 6
nop 0
crap 20
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 Guzzle\Plugin\ErrorResponse\Exception\ErrorResponseException;
14
use hipanel\modules\finance\models\Merchant;
15
use hiqdev\hiart\ResponseErrorException;
16
use hiqdev\yii2\merchant\transactions\Transaction;
17
use Yii;
18
19
/**
20
 * Class PayController.
21
 * @property \hipanel\modules\finance\Module $module
22
 */
23
class PayController extends \hiqdev\yii2\merchant\controllers\PayController
24
{
25
    public function getMerchantModule()
26
    {
27
        return $this->module->getMerchant();
28
    }
29
30
    public function render($view, $params = [])
31
    {
32
        return $this->getMerchantModule()->getPayController()->render($view, $params);
33
    }
34
35
    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...
36
    {
37
        $id = Yii::$app->request->get('transactionId') ?: Yii::$app->request->post('transactionId');
38
        $transaction = $this->getMerchantModule()->findTransaction($id);
39
        if ($transaction === null) {
40
            return null;
41
        }
42
43
        $data = array_merge([
44
            'transactionId' => $transaction->getId(),
45
            'username' => $transaction->getParameter('username'),
46
            'merchant' => $transaction->getParameter('merchant'),
47
        ], $_REQUEST);
48
        Yii::info(http_build_query($data), 'merchant');
49
50
        try {
51
            return Yii::$app->get('hiart')->callWithDisabledAuth(function () use ($transaction, $data) {
52
                $result = Merchant::perform('pay', $data);
53
54
                return $this->completeTransaction($transaction, $result);
55
            });
56
        } catch (ResponseErrorException $e) {
57
            // Does not matter
58
            return null;
59
        }
60
    }
61
62
    /**
63
     * @param Transaction $transaction
64
     * @param $response
65
     * @return mixed
66
     */
67
    protected function completeTransaction($transaction, $response)
68
    {
69
        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...
70
            return $transaction;
71
        }
72
73
        $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...
74
        $transaction->addParameter('bill_id', $response['id']);
75
76
        $this->getMerchantModule()->saveTransaction($transaction);
77
78
        return $transaction;
79
    }
80
}
81