MiniGateway::pay()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.3888
c 0
b 0
f 0
cc 5
nc 4
nop 2
1
<?php
2
3
namespace Yansongda\Pay\Gateways\Alipay;
4
5
use Yansongda\Pay\Events;
6
use Yansongda\Pay\Exceptions\GatewayException;
7
use Yansongda\Pay\Exceptions\InvalidArgumentException;
8
use Yansongda\Pay\Exceptions\InvalidConfigException;
9
use Yansongda\Pay\Exceptions\InvalidSignException;
10
use Yansongda\Pay\Gateways\Alipay;
11
use Yansongda\Supports\Collection;
12
13 View Code Duplication
class MiniGateway extends Gateway
0 ignored issues
show
Duplication introduced by iwzh
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
14
{
15
    /**
16
     * Pay an order.
17
     *
18
     * @author xiaozan <[email protected]>
19
     *
20
     * @param string $endpoint
21
     * @param array  $payload
22
     *
23
     * @throws GatewayException
24
     * @throws InvalidArgumentException
25
     * @throws InvalidConfigException
26
     * @throws InvalidSignException
27
     *
28
     * @see https://docs.alipay.com/mini/introduce/pay
29
     *
30
     * @return Collection
31
     */
32
    public function pay($endpoint, array $payload): Collection
33
    {
34
        $biz_array = json_decode($payload['biz_content'], true);
35
        if (empty($biz_array['buyer_id'])) {
36
            throw new InvalidArgumentException('buyer_id required');
37
        }
38
        if ((Alipay::MODE_SERVICE === $this->mode) && (!empty(Support::getInstance()->pid))) {
39
            $biz_array['extend_params'] = is_array($biz_array['extend_params']) ? array_merge(['sys_service_provider_id' => Support::getInstance()->pid], $biz_array['extend_params']) : ['sys_service_provider_id' => Support::getInstance()->pid];
40
        }
41
        $payload['biz_content'] = json_encode($biz_array);
42
        $payload['method'] = 'alipay.trade.create';
43
        $payload['sign'] = Support::generateSign($payload);
44
45
        Events::dispatch(new Events\PayStarted('Alipay', 'Mini', $endpoint, $payload));
46
47
        return Support::requestApi($payload);
48
    }
49
}
50