Completed
Push — master ( fe0998...b200bd )
by i
09:50
created

Wechat::dispatcher()   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
/**
12
 * @method Wechat\WapPayment wap(array $array)
13
 */
14
class Wechat implements GatewayInterface
15
{
16
    /**
17
     * @var \Illuminate\Contracts\Config\Repository
18
     */
19
    protected $config;
20
21
    /**
22
     * @var array
23
     */
24
    protected $payload;
25
26
    /**
27
     * @var string
28
     */
29
    protected $gateway;
30
31
    /**
32
     * Wechat constructor.
33
     *
34
     * @param array $config
35
     */
36 6
    public function __construct(array $config)
37
    {
38 6
        $this->config = new Repository($config);
39 6
        $env = $this->config->get('env', 'pro');
40 6
        $this->gateway = self::getGatewayUrl($env);
41 6
        $this->payload = [
42
            // 公众账号 ID - 微信分配的公众账号 ID
43 6
            'appid'            => $this->config->get('app_id', ''),
44
45
            // 商户号 - 微信支付分配的商户号
46 6
            'mch_id'           => $this->config->get('mch_id', ''),
47
48
            // 随机字符串	 - 随机字符串,不长于32位
49 6
            'nonce_str'        => Str::random(),
50
51
            // 签名
52 6
            'sign'             => '',
53
54
            // 终端 IP - 必须传正确的用户端 IP
55 6
            'spbill_create_ip' => Request::createFromGlobals()->getClientIp(),
56
57
            // 通知地址 - 接收微信支付异步通知回调地址,通知 url 必须为直接可访问的 url,不能携带参数
58 6
            'notify_url'       => $this->config->get('notify_url'),
59
60
            // 交易类型 - H5 支付的交易类型为 MWEB
61 6
            'trade_type'       => '',
62
        ];
63 6
    }
64
65
    /**
66
     * @param string $method
67
     * @param array  $arguments
68
     *
69
     * @return mixed
70
     * @throws \Nilnice\Payment\Exception\GatewayException
71
     */
72 3
    public function __call(string $method, array $arguments)
73
    {
74 3
        return $this->dispatcher($method, ...$arguments);
0 ignored issues
show
Bug introduced by
$arguments is expanded, but the parameter $array of Nilnice\Payment\Wechat::dispatcher() 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

74
        return $this->dispatcher($method, /** @scrutinizer ignore-type */ ...$arguments);
Loading history...
75
    }
76
77
    /**
78
     * Pay dispatcher.
79
     *
80
     * @param string $gateway
81
     * @param array  $array
82
     *
83
     * @return mixed
84
     * @throws \Nilnice\Payment\Exception\GatewayException
85
     */
86 3
    private function dispatcher(string $gateway, array $array = [])
87
    {
88 3
        $this->payload = array_merge($this->payload, $array);
89 3
        $class = \get_class($this) . '\\' . Str::studly($gateway) . 'Payment';
90
91 3
        if (class_exists($class)) {
92 3
            return $this->toPay($class);
93
        }
94
95
        throw new GatewayException("Pay gateway [{$gateway}] not exists", 1);
96
    }
97
98
    public function query($order) : Collection
99
    {
100
    }
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...
101
102
    /**
103
     * To pay.
104
     *
105
     * @param string $gateway
106
     *
107
     * @return mixed
108
     * @throws \Nilnice\Payment\Exception\GatewayException
109
     */
110 3
    protected function toPay(string $gateway)
111
    {
112 3
        $class = new $gateway($this->config); // Instantiate different gateways.
113
114 3
        if ($class instanceof PaymentInterface) {
115 3
            return $class->toPay($this->gateway, $this->payload);
116
        }
117
118
        throw new GatewayException(
119
            "Pay gateway [{$gateway}] must be an instance of the GatewayInterface.",
120
            2
121
        );
122
    }
123
124
    /**
125
     * Get gateway url.
126
     *
127
     * @param string $env
128
     *
129
     * @return string
130
     */
131 6
    private static function getGatewayUrl($env = '') : string
132
    {
133 6
        $uri = '';
134 6
        if ($env === 'pro') {
135 3
            $uri = Constant::WX_PAY_PRO_URI;
136 3
        } elseif ($env === 'dev') {
137 3
            $uri = Constant::WX_PAY_DEV_URI;
138
        }
139
140 6
        return $uri;
141
    }
142
}
143