Passed
Push — master ( 96d02d...d08d15 )
by Songda
02:13 queued 10s
created

Alipay::getCallbackParams()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 15
rs 10
cc 4
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Provider;
6
7
use GuzzleHttp\Psr7\ServerRequest;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Yansongda\Pay\Contract\ShortcutInterface;
10
use Yansongda\Pay\Exception\InvalidParamsException;
11
use Yansongda\Pay\Pay;
12
use Yansongda\Pay\Plugin\Alipay\CallbackPlugin;
13
use Yansongda\Pay\Plugin\Alipay\LaunchPlugin;
14
use Yansongda\Pay\Plugin\Alipay\PreparePlugin;
15
use Yansongda\Pay\Plugin\Alipay\RadarPlugin;
16
use Yansongda\Pay\Plugin\Alipay\SignPlugin;
17
use Yansongda\Pay\Plugin\ParserPlugin;
18
use Yansongda\Supports\Collection;
19
use Yansongda\Supports\Str;
20
21
class Alipay extends AbstractProvider
22
{
23
    public const URL = [
24
        Pay::MODE_NORMAL => 'https://openapi.alipay.com/gateway.do?charset=utf-8',
25
        Pay::MODE_SANDBOX => 'https://openapi.alipaydev.com/gateway.do?charset=utf-8',
26
        Pay::MODE_SERVICE => 'https://openapi.alipay.com/gateway.do?charset=utf-8',
27
    ];
28
29
    /**
30
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
31
     * @throws \Yansongda\Pay\Exception\ContainerException
32
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
33
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
34
     *
35
     * @return \Yansongda\Supports\Collection|\Psr\Http\Message\ResponseInterface
36
     */
37
    public function __call(string $shortcut, array $params)
38
    {
39
        $plugin = '\\Yansongda\\Pay\\Plugin\\Alipay\\Shortcut\\'.
40
            Str::studly($shortcut).'Shortcut';
41
42
        if (!class_exists($plugin) || !in_array(ShortcutInterface::class, class_implements($plugin))) {
43
            throw new InvalidParamsException(InvalidParamsException::SHORTCUT_NOT_FOUND, "[$plugin] is not incompatible");
44
        }
45
46
        /* @var ShortcutInterface $money */
47
        $money = Pay::get($plugin);
48
49
        return $this->pay(
50
            $this->mergeCommonPlugins($money->getPlugins(...$params)),
0 ignored issues
show
Bug introduced by
$params is expanded, but the parameter $params of Yansongda\Pay\Contract\S...Interface::getPlugins() 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

50
            $this->mergeCommonPlugins($money->getPlugins(/** @scrutinizer ignore-type */ ...$params)),
Loading history...
51
            ...$params
0 ignored issues
show
Bug introduced by
$params is expanded, but the parameter $params of Yansongda\Pay\Provider\AbstractProvider::pay() 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

51
            /** @scrutinizer ignore-type */ ...$params
Loading history...
52
        );
53
    }
54
55
    /**
56
     * @param string|array $order
57
     *
58
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
59
     * @throws \Yansongda\Pay\Exception\ContainerException
60
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
61
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
62
     */
63
    public function find($order): Collection
64
    {
65
        $order = is_array($order) ? $order : ['out_trade_no' => $order];
66
67
        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\ResponseInterface which is incompatible with the type-hinted return Yansongda\Supports\Collection. Consider adding an additional type-check to rule them out.
Loading history...
68
    }
69
70
    /**
71
     * @param string|array $order
72
     *
73
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
74
     * @throws \Yansongda\Pay\Exception\ContainerException
75
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
76
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
77
     */
78
    public function cancel($order): Collection
79
    {
80
        $order = is_array($order) ? $order : ['out_trade_no' => $order];
81
82
        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\ResponseInterface which is incompatible with the type-hinted return Yansongda\Supports\Collection. Consider adding an additional type-check to rule them out.
Loading history...
83
    }
84
85
    /**
86
     * @param string|array $order
87
     *
88
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
89
     * @throws \Yansongda\Pay\Exception\ContainerException
90
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
91
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
92
     */
93
    public function close($order): Collection
94
    {
95
        $order = is_array($order) ? $order : ['out_trade_no' => $order];
96
97
        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\ResponseInterface which is incompatible with the type-hinted return Yansongda\Supports\Collection. Consider adding an additional type-check to rule them out.
Loading history...
98
    }
99
100
    /**
101
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
102
     * @throws \Yansongda\Pay\Exception\ContainerException
103
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
104
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
105
     */
106
    public function refund(array $order): Collection
107
    {
108
        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\ResponseInterface which is incompatible with the type-hinted return Yansongda\Supports\Collection. Consider adding an additional type-check to rule them out.
Loading history...
109
    }
110
111
    /**
112
     * @param array|ServerRequestInterface|null $contents
113
     *
114
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
115
     * @throws \Yansongda\Pay\Exception\ContainerException
116
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
117
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
118
     */
119
    public function verify($contents = null, ?array $config = null): Collection
120
    {
121
        $response = $this->getCallbackParams($contents);
122
123
        return $this->pay(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->pay(array(...>merge($config)->all()) could return the type Psr\Http\Message\ResponseInterface which is incompatible with the type-hinted return Yansongda\Supports\Collection. Consider adding an additional type-check to rule them out.
Loading history...
124
            [CallbackPlugin::class], $response->merge($config)->all()
125
        );
126
    }
127
128
    public function mergeCommonPlugins(array $plugins): array
129
    {
130
        return array_merge(
131
            [PreparePlugin::class],
132
            $plugins,
133
            [SignPlugin::class, RadarPlugin::class],
134
            [LaunchPlugin::class, ParserPlugin::class],
135
        );
136
    }
137
138
    /**
139
     * @param array|ServerRequestInterface|null $contents
140
     */
141
    protected function getCallbackParams($contents = null): Collection
142
    {
143
        if (is_array($contents)) {
144
            return Collection::wrap($contents);
145
        }
146
147
        if ($contents instanceof ServerRequestInterface) {
148
            return Collection::wrap('GET' === $contents->getMethod() ? $contents->getQueryParams() :
149
                $contents->getParsedBody());
150
        }
151
152
        $request = ServerRequest::fromGlobals();
153
154
        return Collection::wrap(
155
            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

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