Passed
Pull Request — master (#662)
by Songda
01:53
created

Unipay::__call()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 2
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\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Yansongda\Pay\Event;
12
use Yansongda\Pay\Exception\Exception;
13
use Yansongda\Pay\Exception\InvalidParamsException;
14
use Yansongda\Pay\Pay;
15
use Yansongda\Pay\Plugin\ParserPlugin;
16
use Yansongda\Pay\Plugin\Unipay\CallbackPlugin;
17
use Yansongda\Pay\Plugin\Unipay\LaunchPlugin;
18
use Yansongda\Pay\Plugin\Unipay\PreparePlugin;
19
use Yansongda\Pay\Plugin\Unipay\RadarSignPlugin;
20
use Yansongda\Supports\Collection;
21
use Yansongda\Supports\Str;
22
23
/**
24
 * @method ResponseInterface web(array $order) 电脑支付
25
 */
26
class Unipay extends AbstractProvider
27
{
28
    public const URL = [
29
        Pay::MODE_NORMAL => 'https://gateway.95516.com/',
30
        Pay::MODE_SANDBOX => 'https://gateway.test.95516.com/',
31
        Pay::MODE_SERVICE => 'https://gateway.95516.com',
32
    ];
33
34
    /**
35
     * @return \Psr\Http\Message\MessageInterface|\Yansongda\Supports\Collection|array|null
36
     *
37
     * @throws \Yansongda\Pay\Exception\ContainerException
38
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
39
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
40
     */
41
    public function __call(string $shortcut, array $params)
42
    {
43
        $plugin = '\\Yansongda\\Pay\\Plugin\\Unipay\\Shortcut\\'.
44
            Str::studly($shortcut).'Shortcut';
45
46
        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

46
        return $this->call($plugin, /** @scrutinizer ignore-type */ ...$params);
Loading history...
47
    }
48
49
    /**
50
     * @param string|array $order
51
     *
52
     * @return array|\Yansongda\Supports\Collection
53
     *
54
     * @throws \Yansongda\Pay\Exception\ContainerException
55
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
56
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
57
     */
58
    public function find($order)
59
    {
60
        if (!is_array($order)) {
61
            throw new InvalidParamsException(Exception::UNIPAY_FIND_STRING_NOT_SUPPORTED);
62
        }
63
64
        Event::dispatch(new Event\MethodCalled('unipay', __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)) also could return the type Psr\Http\Message\MessageInterface which is incompatible with the documented return type Yansongda\Supports\Collection|array.
Loading history...
67
    }
68
69
    /**
70
     * @param string|array $order
71
     *
72
     * @return array|\Yansongda\Supports\Collection
73
     *
74
     * @throws \Yansongda\Pay\Exception\ContainerException
75
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
76
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
77
     */
78
    public function cancel($order)
79
    {
80
        if (!is_array($order)) {
81
            throw new InvalidParamsException(Exception::UNIPAY_CANCEL_STRING_NOT_SUPPORTED);
82
        }
83
84
        Event::dispatch(new Event\MethodCalled('unipay', __METHOD__, $order, null));
85
86
        return $this->__call('cancel', [$order]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->__call('cancel', array($order)) also could return the type Psr\Http\Message\MessageInterface which is incompatible with the documented return type Yansongda\Supports\Collection|array.
Loading history...
87
    }
88
89
    /**
90
     * @param string|array $order
91
     *
92
     * @return array|\Yansongda\Supports\Collection
93
     *
94
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
95
     */
96
    public function close($order)
97
    {
98
        throw new InvalidParamsException(Exception::METHOD_NOT_SUPPORTED, 'Unipay does not support close api');
99
    }
100
101
    /**
102
     * @return array|\Yansongda\Supports\Collection
103
     *
104
     * @throws \Yansongda\Pay\Exception\ContainerException
105
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
106
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
107
     */
108
    public function refund(array $order)
109
    {
110
        Event::dispatch(new Event\MethodCalled('unipay', __METHOD__, $order, null));
111
112
        return $this->__call('refund', [$order]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->__call('refund', array($order)) also could return the type Psr\Http\Message\MessageInterface which is incompatible with the documented return type Yansongda\Supports\Collection|array.
Loading history...
113
    }
114
115
    /**
116
     * @param array|\Psr\Http\Message\ServerRequestInterface|null $contents
117
     *
118
     * @throws \Yansongda\Pay\Exception\ContainerException
119
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
120
     */
121
    public function callback($contents = null, ?array $params = null): Collection
122
    {
123
        Event::dispatch(new Event\CallbackReceived('unipay', $contents, $params, null));
124
125
        $request = $this->getCallbackParams($contents);
126
127
        return $this->pay(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->pay(array(...>merge($params)->all()) also could return the type Psr\Http\Message\MessageInterface|array|null which is incompatible with the return type mandated by Yansongda\Pay\Contract\P...erInterface::callback() of Yansongda\Supports\Collection.
Loading history...
128
            [CallbackPlugin::class], $request->merge($params)->all()
129
        );
130
    }
131
132
    public function success(): ResponseInterface
133
    {
134
        return new Response(200, [], 'success');
135
    }
136
137
    public function mergeCommonPlugins(array $plugins): array
138
    {
139
        return array_merge(
140
            [PreparePlugin::class],
141
            $plugins,
142
            [RadarSignPlugin::class],
143
            [LaunchPlugin::class, ParserPlugin::class],
144
        );
145
    }
146
147
    /**
148
     * @param array|ServerRequestInterface|null $contents
149
     */
150
    protected function getCallbackParams($contents = null): Collection
151
    {
152
        if (is_array($contents)) {
153
            return Collection::wrap($contents);
154
        }
155
156
        if ($contents instanceof ServerRequestInterface) {
157
            return Collection::wrap($contents->getParsedBody());
158
        }
159
160
        $request = ServerRequest::fromGlobals();
161
162
        return Collection::wrap(
163
            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

163
            array_merge($request->getQueryParams(), /** @scrutinizer ignore-type */ $request->getParsedBody())
Loading history...
164
        );
165
    }
166
}
167