Completed
Pull Request — master (#15)
by
unknown
02:55
created

BankProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 102
ccs 16
cts 18
cp 0.8889
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A banks() 0 13 4
A getBankLogo() 0 12 3
A getMerchantData() 0 10 2
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
9
namespace nhuluc\payment\baokim;
10
11
use yii\base\InvalidCallException;
12
13
use nhuluc\payment\BankProvider as BaseBankProvider;
14
15
/**
16
 * Lớp BankProvider cung cấp thông tin ngân hàng mà Bảo Kim hổ trợ.
17
 *
18
 * @author Nhu Luc <[email protected]>
19
 * @since 1.0.3
20
 */
21
class BankProvider extends BaseBankProvider
22
{
23
    /**
24
     * Thẻ nội địa.
25
     */
26
    const TYPE_LOCAL_CARD = 1;
27
28
    /**
29
     * Thẻ tín dụng quốc tế.
30
     */
31
    const TYPE_CREDIT_CARD = 2;
32
33
    /**
34
     * Chuyển khoản online của các ngân hàng.
35
     */
36
    const TYPE_INTERNET_BANKING = 3;
37
38
    /**
39
     * Chuyển khoản ATM.
40
     */
41
    const TYPE_ATM_TRANSFER = 4;
42
43
    /**
44
     * Chuyển khoản truyền thống giữa các ngân hàng.
45
     */
46
    const TYPE_BANK_TRANSFER = 5;
47
48
    /**
49
     * @var int loại ngân hàng muốn lấy, ví dụ atm online, offline, giao dịch tại quầy...
50
     */
51
    public $type = self::TYPE_LOCAL_CARD;
52
53
    /**
54
     * @var mixed mã client dùng để truy xuất thông tin, nếu không chỉ định hệ thống sẽ tự động lấy theo client.
55
     */
56
    public $emailBusiness;
57
58
    /**
59
     * @var mixed mã client dùng để truy xuất thông tin, nếu không chỉ định hệ thống sẽ tự động lấy.
60
     */
61
    public $clientId;
62
63
    /**
64
     * @var PaymentGateway
65
     * @inheritdoc
66
     */
67
    protected $gateway;
68
69
    /**
70
     * @inheritdoc
71
     * @throws \ReflectionException
72
     * @throws \yii\base\InvalidConfigException
73
     */
74 4
    public function banks(): array
75
    {
76 4
        $merchantData = $this->getMerchantData();
77 4
        $banks = [];
78
79 4
        foreach ($merchantData['bank_payment_methods'] as $bank) {
80 4
            if ($bank['payment_method_type'] == $this->type || $this->type === null) {
81 4
                $banks[$bank['id']] = $bank['name'];
82
            }
83
        }
84
85 4
        return $banks;
86
    }
87
88
    /**
89
     * @inheritdoc
90
     * @throws \ReflectionException|\yii\base\InvalidConfigException
91
     */
92 1
    public function getBankLogo($bankId): string
93
    {
94 1
        $merchantData = $this->getMerchantData();
95
96 1
        foreach ($merchantData['bank_payment_methods'] as $bank) {
97 1
            if ($bank['id'] == $bankId) {
98 1
                return $bank['logo_url'];
99
            }
100
        }
101
102
        throw new InvalidCallException('Can\'t get bank logo of bank id: ' . $bankId);
103
    }
104
105
    /**
106
     * Phương thức hổ trợ lấy thông tin merchant theo cấu hình chỉ định từ đó lấy ra thông tin các ngân hàng phù hợp.
107
     *
108
     * @return \GatewayClients\DataInterface|ResponseData đối tượng phản hồi từ cổng thanh toán chứa thông tin merchant.
109
     * @throws \ReflectionException|\yii\base\InvalidConfigException
110
     */
111 5
    protected function getMerchantData()
112
    {
113 5
        $data = $this->gateway->getMerchantData($this->emailBusiness, $this->clientId);
114
115 5
        if ($data->getIsOk()) {
116 5
            return $data;
117
        } else {
118
            throw new InvalidCallException('Can not get merchant data!');
119
        }
120
    }
121
122
}
123