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

PurchaseRequestCollection::convertMerchant()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 1
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\merchant;
12
13
use hipanel\modules\finance\models\Merchant;
14
use hiqdev\hiart\ResponseErrorException;
15
use hiqdev\php\merchant\response\RedirectPurchaseResponse;
16
use hiqdev\yii2\merchant\models\DepositRequest;
17
use hiqdev\yii2\merchant\models\PurchaseRequest;
18
use Yii;
19
20
class PurchaseRequestCollection extends \hiqdev\yii2\merchant\Collection
21
{
22
    public function init()
23
    {
24
        parent::init();
25
26
        if ($this->depositRequest === null) {
0 ignored issues
show
Documentation introduced by
The property depositRequest does not exist on object<hipanel\modules\f...chaseRequestCollection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
27
            $this->depositRequest = $this->createDefaultDepositRequest();
0 ignored issues
show
Documentation introduced by
The property depositRequest does not exist on object<hipanel\modules\f...chaseRequestCollection>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
28
        }
29
30
        $this->loadMerchants($this->depositRequest);
0 ignored issues
show
Documentation introduced by
The property depositRequest does not exist on object<hipanel\modules\f...chaseRequestCollection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
31
    }
32
33
    public function loadMerchants($depositRequest)
34
    {
35
        $this->addItems($this->fetchMerchants($depositRequest));
36
    }
37
38
    public static $supportedSystems = [
39
        'webmoney' => 1,
40
        'paypal' => 1,
41
        'yandex' => 1,
42
        'interkassa' => 1,
43
        'paxum' => 1,
44
        'ecoin' => 1,
45
        'okpay' => 1,
46
        'robokassa' => 1,
47
        'freekassa' => 1,
48
        'bitpay' => 1,
49
        'epayservice' => 1,
50
    ];
51
52
    public function fetchMerchants(DepositRequest $depositRequest)
53
    {
54
        $params = [
55
            'transactionId' => $depositRequest->id,
56
            'sum' => floatval($depositRequest->amount),
57
            'site' => Yii::$app->request->getHostInfo(),
58
        ];
59
60
        if (Yii::$app->user->getIsGuest()) {
61
            $params['seller'] = Yii::$app->params['user.seller'];
62
        } elseif (isset($depositRequest->username)) {
63
            $params['username'] = $depositRequest->username;
64
        }
65
66
        if (isset($depositRequest->merchant)) {
67
            // When the Request contains concrete merchant name,
68
            // parameters `finishUrl`, `cancelUrl`, `notifyUrl` contain
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
69
            // correct URLs, adjusted by [[hiqdev\yii2-merchant\Module::prepareRequestData()]]
0 ignored issues
show
Unused Code Comprehensibility introduced by
39% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
70
            // and they must be used as success, failure and confirm URLs
71
            $params['success_url'] = $depositRequest->returnUrl;
72
            $params['failure_url'] = $depositRequest->cancelUrl;
73
            $params['confirm_url'] = $depositRequest->notifyUrl;
74
        }
75
76
        try {
77
            $merchants = $this->requestMerchants($params);
78
        } catch (ResponseErrorException $e) {
79
            if ($e->getResponse()->getData() === null) {
80
                Yii::info('No available payment methods found', 'hipanel:finance');
81
                $merchants = [];
82
            } else {
83
                throw $e;
84
            }
85
        }
86
87
        $result = [];
88
        foreach (array_keys(static::$supportedSystems) as $system) {
89
            foreach ($merchants as $name => $merchant) {
90
                if ($merchant['system'] === $system) {
91
                    $result[$name] = $this->convertMerchant($merchant);
92
                }
93
            }
94
        }
95
96
        return $result;
97
    }
98
99
    public function requestMerchants($params)
100
    {
101
        $userId = Yii::$app->user->getIsGuest() ? null : Yii::$app->user->identity->getId();
102
103
        return Yii::$app->getCache()->getOrSet([__METHOD__, $params, $userId], function () use ($params) {
104
            return Merchant::perform('prepare-info', $params, ['batch' => true]);
105
        }, 3600*1);
106
    }
107
108
    public function convertMerchant($data)
109
    {
110
        $request = new PurchaseRequest();
111
112
        $request->merchant_name = $data['name'];
113
        $request->system = $data['system'];
114
        $request->currency = strtoupper($data['currency']);
115
        $request->label = $data['label'];
116
        $request->fee = $data['fee'];
117
        $request->commission_fee = $data['commission_fee'];
118
        $request->id = $data['invoice_id'];
119
        $request->amount = $data['sum'];
120
        $request->form = (new RedirectPurchaseResponse($data['action'], $data['inputs']))->setMethod($data['method']);
121
        $request->disableReason = $data['disable_reason'] ?? null;
122
123
        return $request;
124
    }
125
126
    protected function createDefaultDepositRequest()
127
    {
128
        $request = new DepositRequest();
129
        $request->amount = 10;
130
131
        return $request;
132
    }
133
}
134