Completed
Push — master ( b6322f...f418ae )
by Xu
07:09
created

Alipay::init()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 12
nop 0
dl 0
loc 24
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link http://www.tintsoft.com/
4
 * @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
5
 * @license http://www.tintsoft.com/license/
6
 */
7
8
namespace yuncms\payment\gateways;
9
10
11
use Yii;
12
use yii\base\InvalidConfigException;
13
use yuncms\payment\Gateway;
14
use yuncms\payment\traits\HasHttpRequest;
15
16
/**
17
 * Class Alipay
18
 *
19
 * @author Tongle Xu <[email protected]>
20
 * @since 3.0
21
 */
22
class Alipay extends Gateway
23
{
24
    use HasHttpRequest;
25
26
    const SIGNATURE_METHOD_RSA = 'RSA';
27
    const SIGNATURE_METHOD_RSA2 = 'RSA2';
28
29
    /**
30
     * @var integer
31
     */
32
    public $appId;
33
34
    /**
35
     * @var string 私钥
36
     */
37
    public $privateKey;
38
39
    /**
40
     * @var string 公钥
41
     */
42
    public $publicKey;
43
44
    /**
45
     * @var string 签名方法
46
     */
47
    public $signType = self::SIGNATURE_METHOD_RSA2;
48
49
    /**
50
     * @var string 网关地址
51
     */
52
    public $baseUrl = 'https://openapi.alipay.com';
53
54
    /**
55
     * 初始化
56
     * @throws InvalidConfigException
57
     */
58
    public function init()
59
    {
60
        parent::init();
61
        if (!in_array('sha256', openssl_get_md_methods(), true)) {
62
            trigger_error('need openssl support sha256', E_USER_ERROR);
63
        }
64
        if (empty ($this->appId)) {
65
            throw new InvalidConfigException ('The "appId" property must be set.');
66
        }
67
        if (empty ($this->privateKey)) {
68
            throw new InvalidConfigException ('The "privateKey" property must be set.');
69
        }
70
        if (empty ($this->publicKey)) {
71
            throw new InvalidConfigException ('The "publicKey" property must be set.');
72
        }
73
        $privateKey = "file://" . Yii::getAlias($this->privateKey);
74
        $this->privateKey = openssl_pkey_get_private($privateKey);
0 ignored issues
show
Documentation Bug introduced by
It seems like openssl_pkey_get_private($privateKey) of type resource or false is incompatible with the declared type string of property $privateKey.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
75
        if ($this->privateKey === false) {
76
            throw new InvalidConfigException(openssl_error_string());
77
        }
78
        $publicKey = "file://" . Yii::getAlias($this->publicKey);
79
        $this->publicKey = openssl_pkey_get_public($publicKey);
0 ignored issues
show
Documentation Bug introduced by
It seems like openssl_pkey_get_public($publicKey) of type resource is incompatible with the declared type string of property $publicKey.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
80
        if ($this->publicKey === false) {
0 ignored issues
show
introduced by
The condition $this->publicKey === false can never be true.
Loading history...
81
            throw new InvalidConfigException(openssl_error_string());
82
        }
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getTitle()
89
    {
90
        return Yii::t('yuncms', 'Alipay');
91
    }
92
93
}