Completed
Push — master ( 616bd3...21ad1f )
by Dmitry
06:08
created

PayButton   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 16
lcom 2
cbo 8
dl 0
loc 130
ccs 0
cts 78
cp 0
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 1
A registerMerchantCss() 0 9 2
A run() 0 4 1
A renderButton() 0 13 1
A getMerchantName() 0 4 1
A getTotalCommission() 0 4 1
A includesVat() 0 4 1
A getVatRate() 0 4 1
A getVatSum() 0 4 1
A getButtonData() 0 4 1
A renderButtonComment() 0 4 1
A formatMoney() 0 16 3
A isDisabled() 0 4 1
1
<?php
2
/**
3
 * Yii2 extension for payment processing with Omnipay, Payum and more later.
4
 *
5
 * @link      https://github.com/hiqdev/yii2-merchant
6
 * @package   yii2-merchant
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\yii2\merchant\widgets;
12
13
use hiqdev\paymenticons\yii2\PaymentIconsAsset;
14
use hiqdev\php\merchant\AbstractRequest;
15
use hiqdev\yii2\merchant\models\DepositForm;
16
use hiqdev\yii2\merchant\models\DepositRequest;
17
use hiqdev\yii2\merchant\models\PurchaseRequest;
18
use NumberFormatter;
19
use Yii;
20
use yii\base\Event;
21
use yii\helpers\Json;
22
23
/**
24
 * Class PayButton.
25
 */
26
class PayButton extends \yii\base\Widget
27
{
28
    const EVENT_RENDER_COMMENT = 'renderComment';
29
30
    /**
31
     * @var PurchaseRequest
32
     */
33
    public $request;
34
35
    /**
36
     * @var DepositForm
37
     */
38
    public $depositForm;
39
40
    /**
41
     * @var array|string the URL for action
42
     */
43
    public $action = ['/merchant/pay/request'];
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function init()
49
    {
50
        parent::init();
51
        PaymentIconsAsset::register(Yii::$app->getView());
52
        $this->registerMerchantCss();
53
    }
54
55
    protected function registerMerchantCss()
56
    {
57
        $pathToCss = Yii::getAlias('@hiqdev/yii2/merchant/assets/css/selectPayment.css');
58
        if (is_file($pathToCss)) {
59
            Yii::$app->assetManager->publish($pathToCss);
60
            $file = Yii::$app->assetManager->getPublishedUrl($pathToCss);
61
            $this->view->registerCssFile($file);
62
        }
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function run()
69
    {
70
        return $this->renderButton();
71
    }
72
73
    /**
74
     * Renders the payment button.
75
     *
76
     * @return string
77
     */
78
    public function renderButton()
79
    {
80
        return $this->render('pay-button', [
81
            'widget' => $this,
82
            'request' => $this->request,
83
            'depositRequest' => new DepositRequest([
84
                'id' => $this->request->id,
85
                'amount' => $this->depositForm->amount,
86
                'merchant' => $this->getMerchantName(),
87
                'finishUrl' => $this->depositForm->finishUrl,
88
            ])
89
        ]);
90
    }
91
92
    /**
93
     * Extracts merchant name from the [[request]].
94
     * @return string
95
     */
96
    public function getMerchantName()
97
    {
98
        return $this->request->merchant_name;
99
    }
100
101
    public function getTotalCommission()
102
    {
103
        return $this->request->fee + $this->request->commission_fee;
104
    }
105
106
    public function includesVat(): bool
107
    {
108
        return $this->request->vat_rate !== null;
109
    }
110
111
    public function getVatRate(): float
112
    {
113
        return $this->request->vat_rate;
114
    }
115
116
    public function getVatSum(): float
117
    {
118
        return $this->request->vat_sum;
119
    }
120
121
    public function getButtonData()
122
    {
123
        return Json::encode($this->request->getFormInputs());
124
    }
125
126
    /**
127
     * Renders the button comment. Normally triggers [[EVENT_RENDER_COMMENT]] event.
128
     */
129
    public function renderButtonComment()
130
    {
131
        $this->trigger(self::EVENT_RENDER_COMMENT);
132
    }
133
134
    public function formatMoney($sum)
135
    {
136
        $currency = $this->request->currency;
137
        $formatter = new NumberFormatter(Yii::$app->language, NumberFormatter::CURRENCY);
138
139
        switch ($currency) {
140
            case 'BTC':
141
                $formatter->setAttribute(NumberFormatter::MAX_SIGNIFICANT_DIGITS, 6);
142
                break;
143
            case 'RUB':
144
                $formatter->setSymbol(NumberFormatter::CURRENCY_SYMBOL, '₽');
145
                break;
146
        }
147
148
        return $formatter->formatCurrency($sum, $currency);
149
    }
150
151
    public function isDisabled()
152
    {
153
        return $this->request->disableReason !== null;
154
    }
155
}
156