Completed
Push — master ( f03559...c88c79 )
by
unknown
28:42 queued 13:14
created

PayButton::getMerchantSystem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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 = dirname(__DIR__) . '/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
                'currency' => $this->depositForm->currency,
87
                'merchant' => $this->getMerchantName(),
88
                'finishUrl' => $this->depositForm->finishUrl,
89
            ])
90
        ]);
91
    }
92
93
    /**
94
     * Extracts merchant name from the [[request]].
95
     * @return string
96
     */
97
    public function getMerchantName()
98
    {
99
        return $this->request->merchant_name;
100
    }
101
102
    /**
103
     * Extracts merchant system from the [[request]].
104
     * @return string
105
     */
106
    public function getMerchantSystem()
107
    {
108
        return $this->request->system;
109
    }
110
111
    public function getTotalCommission()
112
    {
113
        return $this->request->fee + $this->request->commission_fee;
114
    }
115
116
    public function includesVat(): bool
117
    {
118
        return $this->request->vat_rate !== null;
119
    }
120
121
    public function getVatRate(): float
122
    {
123
        return $this->request->vat_rate;
124
    }
125
126
    public function getVatSum(): float
127
    {
128
        return $this->request->vat_sum;
129
    }
130
131
    public function getButtonData()
132
    {
133
        return Json::encode($this->request->getFormInputs());
134
    }
135
136
    /**
137
     * Renders the button comment. Normally triggers [[EVENT_RENDER_COMMENT]] event.
138
     */
139
    public function renderButtonComment()
140
    {
141
        $this->trigger(self::EVENT_RENDER_COMMENT);
142
    }
143
144
    public function formatMoney($sum)
145
    {
146
        $currency = $this->request->currency;
147
        $formatter = new NumberFormatter(Yii::$app->language, NumberFormatter::CURRENCY);
148
149
        switch ($currency) {
150
            case 'BTC':
151
                $formatter->setAttribute(NumberFormatter::MAX_SIGNIFICANT_DIGITS, 6);
152
                break;
153
            case 'RUB':
154
                $formatter->setSymbol(NumberFormatter::CURRENCY_SYMBOL, '₽');
155
                break;
156
        }
157
158
        return $formatter->formatCurrency($sum, $currency);
159
    }
160
161
    public function isDisabled()
162
    {
163
        return $this->request->disableReason !== null;
164
    }
165
}
166