Completed
Push — master ( 22f05f...6ee38e )
by Vuong
02:09
created

PaymentClient::init()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 7
cp 0.7143
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3.2098
1
<?php
2
/**
3
 * @link https://github.com/yiiviet/yii2-payment
4
 * @copyright Copyright (c) 2017 Yii Viet
5
 * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
6
 */
7
8
namespace yiiviet\payment\vnpayment;
9
10
use Yii;
11
12
use yii\base\InvalidConfigException;
13
14
use yiiviet\payment\BasePaymentClient;
15
use yiiviet\payment\HashDataSignature;
16
17
/**
18
 * Lớp PaymentClient hổ trợ tạo và kiểm tra chữ ký dữ liệu và có các thuộc tính kết nối đến cổng thanh toán VnPayment
19
 *
20
 * @method PaymentGateway getGateway()
21
 *
22
 * @property PaymentGateway $gateway
23
 *
24
 * @author Vuong Minh <[email protected]>
25
 * @since 1.0
26
 */
27
class PaymentClient extends BasePaymentClient
28
{
29
30
    /**
31
     * @var string Mã TMN được dùng để gửi lên VnPayment xác định là yêu cầu từ bạn.
32
     * Nó thường được dùng khi gọi [[request()]] ở [[PaymentGateway]].
33
     * Nó do VnPayment cấp khi thực hiện tích hợp website của bạn.
34
     */
35
    public $tmnCode;
36
37
    /**
38
     * @var string Thuộc tính dùng để tạo và kiểm tra chữ ký dữ liệu.
39
     * Nó do VnPayment cấp khi thực hiện tích hợp website của bạn.
40
     */
41
    public $hashSecret;
42
43
    /**
44
     * @var int Mã loại hàng mặc định của website bạn bán.
45
     */
46
    public $defaultOrderType;
47
48
    /**
49
     * @inheritdoc
50
     * @throws InvalidConfigException
51
     * @since 1.0.3
52
     */
53 7
    public function init()
54
    {
55 7
        if ($this->tmnCode === null) {
56
            throw new InvalidConfigException('Property `tmnCode` must be set!');
57
        }
58
59 7
        if ($this->hashSecret === null) {
60
            throw new InvalidConfigException('Property `hashSecret` must be set!');
61
        }
62
63 7
        parent::init();
64 7
    }
65
66
    /**
67
     * @inheritdoc
68
     * @throws \yii\base\InvalidConfigException
69
     */
70 3
    protected function initDataSignature(string $data, string $type = null): ?\yiiviet\payment\DataSignature
71
    {
72 3
        $data = $this->hashSecret . $data;
73
74 3
        return Yii::createObject([
75 3
            'class' => HashDataSignature::class,
76 3
            'hashAlgo' => $type,
77 3
        ], [$data]);
78
    }
79
}
80