Passed
Push — master ( 625985...dc6fc6 )
by Songda
02:12 queued 16s
created

Alipay::mergeCommonPlugins()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Provider;
6
7
use GuzzleHttp\Psr7\Response;
8
use GuzzleHttp\Psr7\ServerRequest;
9
use Psr\Http\Message\MessageInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Yansongda\Pay\Event;
13
use Yansongda\Pay\Exception\ContainerException;
14
use Yansongda\Pay\Exception\InvalidParamsException;
15
use Yansongda\Pay\Exception\ServiceNotFoundException;
16
use Yansongda\Pay\Pay;
17
use Yansongda\Pay\Plugin\Alipay\AddRadarPlugin;
18
use Yansongda\Pay\Plugin\Alipay\AddSignaturePlugin;
19
use Yansongda\Pay\Plugin\Alipay\CallbackPlugin;
20
use Yansongda\Pay\Plugin\Alipay\FormatBizContentPlugin;
21
use Yansongda\Pay\Plugin\Alipay\ResponsePlugin;
22
use Yansongda\Pay\Plugin\Alipay\StartPlugin;
23
use Yansongda\Pay\Plugin\Alipay\VerifySignaturePlugin;
24
use Yansongda\Pay\Plugin\ParserPlugin;
25
use Yansongda\Supports\Collection;
26
use Yansongda\Supports\Str;
27
28
/**
29
 * @method ResponseInterface app(array $order)      APP 支付
30
 * @method Collection        pos(array $order)      刷卡支付
31
 * @method Collection        scan(array $order)     扫码支付
32
 * @method Collection        transfer(array $order) 帐户转账
33
 * @method ResponseInterface wap(array $order)      手机网站支付
34
 * @method ResponseInterface web(array $order)      电脑支付
35
 * @method Collection        mini(array $order)     小程序支付
36
 */
37
class Alipay extends AbstractProvider
38
{
39
    public const URL = [
40
        Pay::MODE_NORMAL => 'https://openapi.alipay.com/gateway.do?charset=utf-8',
41
        Pay::MODE_SANDBOX => 'https://openapi-sandbox.dl.alipaydev.com/gateway.do?charset=utf-8',
42
        Pay::MODE_SERVICE => 'https://openapi.alipay.com/gateway.do?charset=utf-8',
43
    ];
44
45
    /**
46
     * @throws ContainerException
47
     * @throws InvalidParamsException
48
     * @throws ServiceNotFoundException
49
     */
50
    public function __call(string $shortcut, array $params): null|Collection|MessageInterface
51
    {
52
        $plugin = '\\Yansongda\\Pay\\Shortcut\\Alipay\\'.Str::studly($shortcut).'Shortcut';
53
54
        return $this->call($plugin, ...$params);
0 ignored issues
show
Bug introduced by
$params is expanded, but the parameter $params of Yansongda\Pay\Provider\AbstractProvider::call() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
        return $this->call($plugin, /** @scrutinizer ignore-type */ ...$params);
Loading history...
55
    }
56
57
    /**
58
     * @throws ContainerException
59
     * @throws InvalidParamsException
60
     * @throws ServiceNotFoundException
61
     */
62
    public function query(array $order): array|Collection
63
    {
64
        Event::dispatch(new Event\MethodCalled('alipay', __METHOD__, $order, null));
65
66
        return $this->__call('query', [$order]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->__call('query', array($order)) could return the type Psr\Http\Message\MessageInterface|null which is incompatible with the type-hinted return Yansongda\Supports\Collection|array. Consider adding an additional type-check to rule them out.
Loading history...
67
    }
68
69
    /**
70
     * @throws ContainerException
71
     * @throws InvalidParamsException
72
     * @throws ServiceNotFoundException
73
     */
74
    public function cancel(array|string $order): null|array|Collection
75
    {
76
        $order = is_array($order) ? $order : ['out_trade_no' => $order];
0 ignored issues
show
introduced by
The condition is_array($order) is always true.
Loading history...
77
78
        Event::dispatch(new Event\MethodCalled('alipay', __METHOD__, $order, null));
79
80
        return $this->__call('cancel', [$order]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->__call('cancel', array($order)) could return the type Psr\Http\Message\MessageInterface which is incompatible with the type-hinted return Yansongda\Supports\Collection|array|null. Consider adding an additional type-check to rule them out.
Loading history...
81
    }
82
83
    /**
84
     * @throws ContainerException
85
     * @throws InvalidParamsException
86
     * @throws ServiceNotFoundException
87
     */
88
    public function close(array|string $order): null|array|Collection
89
    {
90
        $order = is_array($order) ? $order : ['out_trade_no' => $order];
0 ignored issues
show
introduced by
The condition is_array($order) is always true.
Loading history...
91
92
        Event::dispatch(new Event\MethodCalled('alipay', __METHOD__, $order, null));
93
94
        return $this->__call('close', [$order]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->__call('close', array($order)) could return the type Psr\Http\Message\MessageInterface which is incompatible with the type-hinted return Yansongda\Supports\Collection|array|null. Consider adding an additional type-check to rule them out.
Loading history...
95
    }
96
97
    /**
98
     * @throws ContainerException
99
     * @throws InvalidParamsException
100
     * @throws ServiceNotFoundException
101
     */
102
    public function refund(array $order): array|Collection
103
    {
104
        Event::dispatch(new Event\MethodCalled('alipay', __METHOD__, $order, null));
105
106
        return $this->__call('refund', [$order]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->__call('refund', array($order)) could return the type Psr\Http\Message\MessageInterface|null which is incompatible with the type-hinted return Yansongda\Supports\Collection|array. Consider adding an additional type-check to rule them out.
Loading history...
107
    }
108
109
    /**
110
     * @throws ContainerException
111
     * @throws InvalidParamsException
112
     */
113
    public function callback(null|array|ServerRequestInterface $contents = null, ?array $params = null): Collection
114
    {
115
        $request = $this->getCallbackParams($contents);
116
117
        Event::dispatch(new Event\CallbackReceived('alipay', $request->all(), $params, null));
118
119
        return $this->pay(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->pay(array(...>merge($params)->all()) could return the type Psr\Http\Message\MessageInterface|null which is incompatible with the type-hinted return Yansongda\Supports\Collection. Consider adding an additional type-check to rule them out.
Loading history...
120
            [CallbackPlugin::class],
121
            $request->merge($params)->all()
122
        );
123
    }
124
125
    public function success(): ResponseInterface
126
    {
127
        return new Response(200, [], 'success');
128
    }
129
130
    public function mergeCommonPlugins(array $plugins): array
131
    {
132
        return array_merge(
133
            [StartPlugin::class],
134
            $plugins,
135
            [FormatBizContentPlugin::class, AddSignaturePlugin::class, AddRadarPlugin::class, VerifySignaturePlugin::class, ResponsePlugin::class, ParserPlugin::class],
136
        );
137
    }
138
139
    protected function getCallbackParams(array|ServerRequestInterface $contents = null): Collection
140
    {
141
        if (is_array($contents)) {
142
            return Collection::wrap($contents);
143
        }
144
145
        if ($contents instanceof ServerRequestInterface) {
146
            return Collection::wrap('GET' === $contents->getMethod() ? $contents->getQueryParams() :
147
                $contents->getParsedBody());
148
        }
149
150
        $request = ServerRequest::fromGlobals();
151
152
        return Collection::wrap(
153
            array_merge($request->getQueryParams(), $request->getParsedBody())
0 ignored issues
show
Bug introduced by
It seems like $request->getParsedBody() can also be of type null and object; however, parameter $arrays of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

153
            array_merge($request->getQueryParams(), /** @scrutinizer ignore-type */ $request->getParsedBody())
Loading history...
154
        );
155
    }
156
}
157