Completed
Push — master ( 47bcb8...758387 )
by Songda
12s
created

MiniGateway::pay()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Yansongda\Pay\Gateways\Alipay;
4
5
use Yansongda\Pay\Contracts\GatewayInterface;
6
use Yansongda\Pay\Events;
7
use Yansongda\Supports\Collection;
8
9
class MiniGateway implements GatewayInterface
10
{
11
    /**
12
     * Pay an order.
13
     *
14
     * @author xiaozan <[email protected]>
15
     *
16
     * @param string $endpoint
17
     * @param array  $payload
18
     *
19
     * @throws \Yansongda\Pay\Exceptions\GatewayException
20
     * @throws \Yansongda\Pay\Exceptions\InvalidArgumentException
21
     * @throws \Yansongda\Pay\Exceptions\InvalidConfigException
22
     * @throws \Yansongda\Pay\Exceptions\InvalidSignException
23
     *
24
     * @link https://docs.alipay.com/mini/introduce/pay
25
     *
26
     * @return Collection
27
     */
28
    public function pay($endpoint, array $payload): Collection
29
    {
30
        if (empty(json_decode($payload['biz_content'], true)['buyer_id'])) {
31
            throw new \Yansongda\Pay\Exceptions\InvalidArgumentException('buyer_id required');
32
        }
33
34
        $payload['method'] = $this->getMethod();
35
        $payload['sign'] = Support::generateSign($payload);
36
37
        Events::dispatch(Events::PAY_STARTED, new Events\PayStarted('Alipay', 'Mini', $endpoint, $payload));
38
39
        return Support::requestApi($payload);
40
    }
41
42
    /**
43
     * Get method config.
44
     *
45
     * @author yansongda <[email protected]>
46
     *
47
     * @return string
48
     */
49
    protected function getMethod(): string
50
    {
51
        return 'alipay.trade.create';
52
    }
53
}
54