MpGateway   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 6
dl 0
loc 52
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A pay() 0 17 2
A getTradeType() 0 4 1
1
<?php
2
3
namespace Yansongda\Pay\Gateways\Wechat;
4
5
use Exception;
6
use Yansongda\Pay\Events;
7
use Yansongda\Pay\Exceptions\GatewayException;
8
use Yansongda\Pay\Exceptions\InvalidArgumentException;
9
use Yansongda\Pay\Exceptions\InvalidSignException;
10
use Yansongda\Supports\Collection;
11
use Yansongda\Supports\Str;
12
13
class MpGateway extends Gateway
14
{
15
    /**
16
     * @var bool
17
     */
18
    protected $payRequestUseSubAppId = false;
19
20
    /**
21
     * Pay an order.
22
     *
23
     * @author yansongda <[email protected]>
24
     *
25
     * @param string $endpoint
26
     * @param array  $payload
27
     *
28
     * @throws GatewayException
29
     * @throws InvalidArgumentException
30
     * @throws InvalidSignException
31
     * @throws Exception
32
     *
33
     * @return Collection
34
     */
35
    public function pay($endpoint, array $payload): Collection
36
    {
37
        $payload['trade_type'] = $this->getTradeType();
38
39
        $pay_request = [
40
            'appId' => !$this->payRequestUseSubAppId ? $payload['appid'] : $payload['sub_appid'],
41
            'timeStamp' => strval(time()),
42
            'nonceStr' => Str::random(),
43
            'package' => 'prepay_id='.$this->preOrder($payload)->get('prepay_id'),
44
            'signType' => 'MD5',
45
        ];
46
        $pay_request['paySign'] = Support::generateSign($pay_request);
47
48
        Events::dispatch(new Events\PayStarted('Wechat', 'JSAPI', $endpoint, $pay_request));
49
50
        return new Collection($pay_request);
51
    }
52
53
    /**
54
     * Get trade type config.
55
     *
56
     * @author yansongda <[email protected]>
57
     *
58
     * @return string
59
     */
60
    protected function getTradeType(): string
61
    {
62
        return 'JSAPI';
63
    }
64
}
65