Completed
Pull Request — master (#203)
by
unknown
01:37
created

MiniGateway::getMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Yansongda\Pay\Gateways\Alipay;
4
5
use Yansongda\Supports\Collection;
6
use Yansongda\Pay\Contracts\GatewayInterface;
7
use Yansongda\Pay\Events;
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