Passed
Pull Request — master (#897)
by Songda
02:02
created

Unipay::query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

47
        return $this->call($plugin, /** @scrutinizer ignore-type */ ...$params);
Loading history...
48
    }
49
50
    /**
51
     * @throws ContainerException
52
     * @throws InvalidParamsException
53
     * @throws ServiceNotFoundException
54
     */
55
    public function query(array $order): array|Collection
56
    {
57
        Event::dispatch(new Event\MethodCalled('unipay', __METHOD__, $order, null));
58
59
        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...
60
    }
61
62
    /**
63
     * @throws ContainerException
64
     * @throws InvalidParamsException
65
     * @throws ServiceNotFoundException
66
     */
67
    public function cancel(array|string $order): null|array|Collection
68
    {
69
        if (!is_array($order)) {
0 ignored issues
show
introduced by
The condition is_array($order) is always true.
Loading history...
70
            throw new InvalidParamsException(Exception::UNIPAY_CANCEL_STRING_NOT_SUPPORTED);
71
        }
72
73
        Event::dispatch(new Event\MethodCalled('unipay', __METHOD__, $order, null));
74
75
        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...
76
    }
77
78
    /**
79
     * @throws InvalidParamsException
80
     */
81
    public function close(array|string $order): null|array|Collection
82
    {
83
        throw new InvalidParamsException(Exception::METHOD_NOT_SUPPORTED, 'Unipay does not support close api');
84
    }
85
86
    /**
87
     * @throws ContainerException
88
     * @throws InvalidParamsException
89
     * @throws ServiceNotFoundException
90
     */
91
    public function refund(array $order): array|Collection
92
    {
93
        Event::dispatch(new Event\MethodCalled('unipay', __METHOD__, $order, null));
94
95
        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...
96
    }
97
98
    /**
99
     * @throws ContainerException
100
     * @throws InvalidParamsException
101
     */
102
    public function callback(null|array|ServerRequestInterface $contents = null, ?array $params = null): Collection
103
    {
104
        $request = $this->getCallbackParams($contents);
105
106
        Event::dispatch(new Event\CallbackReceived('unipay', $request->all(), $params, null));
107
108
        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...
109
            [CallbackPlugin::class],
110
            $request->merge($params)->all()
111
        );
112
    }
113
114
    public function success(): ResponseInterface
115
    {
116
        return new Response(200, [], 'success');
117
    }
118
119
    public function mergeCommonPlugins(array $plugins): array
120
    {
121
        return array_merge(
122
            [PreparePlugin::class],
123
            $plugins,
124
            [RadarSignPlugin::class],
125
            [LaunchPlugin::class, ParserPlugin::class],
126
        );
127
    }
128
129
    protected function getCallbackParams(null|array|ServerRequestInterface $contents = null): Collection
130
    {
131
        if (is_array($contents)) {
132
            return Collection::wrap($contents);
133
        }
134
135
        if ($contents instanceof ServerRequestInterface) {
136
            return Collection::wrap($contents->getParsedBody());
137
        }
138
139
        $request = ServerRequest::fromGlobals();
140
141
        return Collection::wrap($request->getParsedBody());
142
    }
143
}
144