Completed
Push — master ( bf6fd0...fe0998 )
by i
04:47
created

Wechat::pay()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Nilnice\Payment;
4
5
use Illuminate\Config\Repository;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Str;
8
use Nilnice\Payment\Exception\GatewayException;
9
use Symfony\Component\HttpFoundation\Request;
10
11
class Wechat implements GatewayInterface
12
{
13
    /**
14
     * @var \Illuminate\Contracts\Config\Repository
15
     */
16
    protected $config;
17
18
    /**
19
     * @var array
20
     */
21
    protected $payload;
22
23
    /**
24
     * @var string
25
     */
26
    protected $gateway;
27
28
    /**
29
     * Wechat constructor.
30
     *
31
     * @param \Illuminate\Config\Repository $config
32
     */
33 6
    public function __construct(Repository $config)
34
    {
35 6
        $this->config = $config;
36 6
        $env = $config->get('env', 'pro');
37 6
        $this->gateway = self::getGatewayUrl($env);
38 6
        $this->payload = [
39
            // 公众账号 ID - 微信分配的公众账号 ID
40 6
            'appid'            => $this->config->get('app_id', ''),
41
42
            // 商户号 - 微信支付分配的商户号
43 6
            'mch_id'           => $this->config->get('mch_id', ''),
44
45
            // 随机字符串	 - 随机字符串,不长于32位
46 6
            'nonce_str'        => Str::random(),
47
48
            // 签名
49 6
            'sign'             => '',
50
51
            // 终端 IP - 必须传正确的用户端 IP
52 6
            'spbill_create_ip' => Request::createFromGlobals()->getClientIp(),
53
54
            // 通知地址 - 接收微信支付异步通知回调地址,通知 url 必须为直接可访问的 url,不能携带参数
55 6
            'notify_url'       => $this->config->get('notify_url'),
56
57
            // 交易类型 - H5 支付的交易类型为 MWEB
58 6
            'trade_type'       => '',
59
        ];
60 6
    }
61
62
    /**
63
     * @param string $method
64
     * @param array  $arguments
65
     *
66
     * @return mixed
67
     * @throws \Nilnice\Payment\Exception\GatewayException
68
     */
69 3
    public function __call(string $method, array $arguments)
70
    {
71 3
        return $this->pay($method, ...$arguments);
0 ignored issues
show
Bug introduced by
$arguments is expanded, but the parameter $array of Nilnice\Payment\Wechat::pay() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
        return $this->pay($method, /** @scrutinizer ignore-type */ ...$arguments);
Loading history...
72
    }
73
74
    /**
75
     * Pay dispatcher.
76
     *
77
     * @param string $gateway
78
     * @param array  $array
79
     *
80
     * @return mixed
81
     * @throws \Nilnice\Payment\Exception\GatewayException
82
     */
83 3
    public function pay(string $gateway, array $array = [])
84
    {
85 3
        $this->payload = array_merge($this->payload, $array);
86 3
        $class = \get_class($this) . '\\' . Str::studly($gateway) . 'Payment';
87
88 3
        if (class_exists($class)) {
89 3
            return $this->toPay($class);
90
        }
91
92
        throw new GatewayException("Pay gateway [{$gateway}] not exists", 1);
93
    }
94
95
    public function query($order) : Collection
96
    {
97
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Illuminate\Support\Collection. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
98
99
    /**
100
     * To pay.
101
     *
102
     * @param string $gateway
103
     *
104
     * @return mixed
105
     * @throws \Nilnice\Payment\Exception\GatewayException
106
     */
107 3
    protected function toPay(string $gateway)
108
    {
109 3
        $class = new $gateway($this->config); // Instantiate different gateways.
110
111 3
        if ($class instanceof PaymentInterface) {
112 3
            return $class->toPay($this->gateway, $this->payload);
113
        }
114
115
        throw new GatewayException(
116
            "Pay gateway [{$gateway}] must be an instance of the GatewayInterface.",
117
            2
118
        );
119
    }
120
121
    /**
122
     * Get gateway url.
123
     *
124
     * @param string $env
125
     *
126
     * @return string
127
     */
128 6
    private static function getGatewayUrl($env = '') : string
129
    {
130 6
        $uri = '';
131 6
        if ($env === 'pro') {
132 3
            $uri = Constant::WX_PAY_PRO_URI;
133 3
        } elseif ($env === 'dev') {
134 3
            $uri = Constant::WX_PAY_DEV_URI;
135
        }
136
137 6
        return $uri;
138
    }
139
}
140