Completed
Push — master ( 60539d...725f3a )
by Dmitry
07:23
created

PayButton::getTotalCommission()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 0
cts 0
cp 0
cc 1
eloc 2
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 Yii;
19
use yii\base\Event;
20
use yii\helpers\Json;
21
22
/**
23
 * Class PayButton.
24
 */
25
class PayButton extends \yii\base\Widget
26
{
27
    const EVENT_RENDER_COMMENT = 'renderComment';
28
29
    /**
30
     * @var PurchaseRequest
31
     */
32
    public $request;
33
34
    /**
35
     * @var DepositForm
36
     */
37
    public $depositForm;
38
39
    /**
40
     * @var array|string the URL for action
41
     */
42
    public $action = ['/merchant/pay/request'];
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function init()
48
    {
49
        parent::init();
50
        PaymentIconsAsset::register(Yii::$app->getView());
51
        $this->registerMerchantCss();
52
    }
53
54
    protected function registerMerchantCss()
55
    {
56
        $pathToCss = Yii::getAlias('@hiqdev/yii2/merchant/assets/css/selectPayment.css');
57
        if (is_file($pathToCss)) {
58
            Yii::$app->assetManager->publish($pathToCss);
59
            $file = Yii::$app->assetManager->getPublishedUrl($pathToCss);
60
            $this->view->registerCssFile($file);
61
        }
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function run()
68
    {
69
        return $this->renderButton();
70
    }
71
72
    /**
73
     * Renders the payment button.
74
     *
75
     * @return string
76
     */
77
    public function renderButton()
78
    {
79
        return $this->render('pay-button', [
80
            'widget' => $this,
81
            'request' => $this->request,
82
            'depositRequest' => new DepositRequest([
83
                'id' => $this->request->id,
84
                'amount' => $this->depositForm->amount,
85
                'merchant' => $this->getMerchantName()
86
            ])
87
        ]);
88
    }
89
90
    /**
91
     * Extracts merchant name from the [[request]].
92
     * @return string
93
     */
94
    public function getMerchantName()
95
    {
96
        return $this->request->merchant_name;
97
    }
98
99
    public function getTotalCommission()
100
    {
101
        return $this->request->fee + $this->request->commission_fee;
102
    }
103
104
    public function getButtonData()
105
    {
106
        return Json::encode($this->request->getFormInputs());
107
    }
108
109
    /**
110
     * Renders the button comment. Normally triggers [[EVENT_RENDER_COMMENT]] event.
111
     */
112
    public function renderButtonComment()
113
    {
114
        $this->trigger(self::EVENT_RENDER_COMMENT);
115
    }
116
117
    public function formatMoney($sum)
118
    {
119
        return Yii::$app->formatter->format($sum, ['currency', $this->request->currency]);
120
    }
121
122
    public function isDisabled()
123
    {
124
        return $this->request->disableReason !== null;
125
    }
126
}
127