Completed
Push — master ( a9c6ec...e2169c )
by Songda
01:29
created

src/Gateways/Alipay/MiniGateway.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
     *
22
     * @throws GatewayException
23
     * @throws InvalidArgumentException
24
     * @throws InvalidConfigException
25
     * @throws InvalidSignException
26
     *
27
     * @see https://docs.alipay.com/mini/introduce/pay
28
     */
29
    public function pay($endpoint, array $payload): Collection
30
    {
31
        $biz_array = json_decode($payload['biz_content'], true);
32
        if (empty($biz_array['buyer_id'])) {
33
            throw new InvalidArgumentException('buyer_id required');
34
        }
35
        if ((Alipay::MODE_SERVICE === $this->mode) && (!empty(Support::getInstance()->pid))) {
36
            $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];
37
        }
38
        $payload['biz_content'] = json_encode($biz_array);
39
        $payload['method'] = 'alipay.trade.create';
40
        $payload['sign'] = Support::generateSign($payload);
41
42
        Events::dispatch(new Events\PayStarted('Alipay', 'Mini', $endpoint, $payload));
43
44
        return Support::requestApi($payload);
45
    }
46
}
47