Passed
Push — master ( df334d...db8528 )
by Songda
03:29 queued 01:21
created

Jsb::callback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 9
rs 10
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\Artful\Artful;
13
use Yansongda\Artful\Event;
14
use Yansongda\Artful\Exception\ContainerException;
15
use Yansongda\Artful\Exception\InvalidParamsException;
16
use Yansongda\Artful\Exception\ServiceNotFoundException;
17
use Yansongda\Artful\Plugin\ParserPlugin;
18
use Yansongda\Artful\Rocket;
19
use Yansongda\Pay\Contract\ProviderInterface;
20
use Yansongda\Pay\Event\CallbackReceived;
21
use Yansongda\Pay\Event\MethodCalled;
22
use Yansongda\Pay\Exception\Exception;
23
use Yansongda\Pay\Pay;
24
use Yansongda\Pay\Plugin\Jsb\AddPayloadSignPlugin;
25
use Yansongda\Pay\Plugin\Jsb\AddRadarPlugin;
26
use Yansongda\Pay\Plugin\Jsb\CallbackPlugin;
27
use Yansongda\Pay\Plugin\Jsb\ResponsePlugin;
28
use Yansongda\Pay\Plugin\Jsb\StartPlugin;
29
use Yansongda\Pay\Plugin\Jsb\VerifySignaturePlugin;
30
use Yansongda\Supports\Collection;
31
use Yansongda\Supports\Str;
32
33
/**
34
 * @method Collection|Rocket scan(array $order) 扫码支付[微信支付宝都可扫描]
35
 */
36
class Jsb implements ProviderInterface
37
{
38
    public const URL = [
39
        Pay::MODE_NORMAL => 'https://mybank.jsbchina.cn:577/eis/merchant/merchantServices.htm',
40
        Pay::MODE_SANDBOX => 'https://epaytest.jsbchina.cn:9999/eis/merchant/merchantServices.htm',
41
    ];
42
43
    /**
44
     * @param mixed $name
45
     * @param mixed $params
46
     *
47
     * @throws ContainerException
48
     * @throws InvalidParamsException
49
     * @throws ServiceNotFoundException
50
     */
51
    public function __call($name, $params): null|Collection|MessageInterface|Rocket
52
    {
53
        $plugin = '\Yansongda\Pay\Shortcut\Jsb\\'.Str::studly($name).'Shortcut';
54
55
        return Artful::shortcut($plugin, ...$params);
0 ignored issues
show
Bug introduced by
$params is expanded, but the parameter $params of Yansongda\Artful\Artful::shortcut() 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

55
        return Artful::shortcut($plugin, /** @scrutinizer ignore-type */ ...$params);
Loading history...
56
    }
57
58
    /**
59
     * @throws ContainerException
60
     * @throws InvalidParamsException
61
     */
62
    public function pay(array $plugins, array $params): null|Collection|MessageInterface|Rocket
63
    {
64
        return Artful::artful($plugins, $params);
65
    }
66
67
    public function mergeCommonPlugins(array $plugins): array
68
    {
69
        return array_merge(
70
            [StartPlugin::class],
71
            $plugins,
72
            [AddPayloadSignPlugin::class, AddRadarPlugin::class, VerifySignaturePlugin::class, ResponsePlugin::class, ParserPlugin::class],
73
        );
74
    }
75
76
    /**
77
     * @throws InvalidParamsException
78
     */
79
    public function cancel(array $order): Collection|Rocket
80
    {
81
        throw new InvalidParamsException(Exception::PARAMS_METHOD_NOT_SUPPORTED, 'Jsb does not support cancel api');
82
    }
83
84
    /**
85
     * @throws InvalidParamsException
86
     */
87
    public function close(array $order): Collection|Rocket
88
    {
89
        throw new InvalidParamsException(Exception::PARAMS_METHOD_NOT_SUPPORTED, 'Jsb does not support close api');
90
    }
91
92
    /**
93
     * @throws ContainerException
94
     * @throws InvalidParamsException
95
     * @throws ServiceNotFoundException
96
     */
97
    public function refund(array $order): Collection|Rocket
98
    {
99
        Event::dispatch(new MethodCalled('jsb', __METHOD__, $order, null));
100
101
        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\Artful\Rocket|...gda\Supports\Collection. Consider adding an additional type-check to rule them out.
Loading history...
102
    }
103
104
    /**
105
     * @throws ContainerException
106
     * @throws InvalidParamsException
107
     */
108
    public function callback(null|array|ServerRequestInterface $contents = null, ?array $params = null): Collection|Rocket
109
    {
110
        $request = $this->getCallbackParams($contents);
111
112
        Event::dispatch(new CallbackReceived('jsb', $request->all(), $params, null));
113
114
        return $this->pay(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->pay(array(..., 'params' => $params)) could return the type Psr\Http\Message\MessageInterface|null which is incompatible with the type-hinted return Yansongda\Artful\Rocket|...gda\Supports\Collection. Consider adding an additional type-check to rule them out.
Loading history...
115
            [CallbackPlugin::class],
116
            ['request' => $request, 'params' => $params]
117
        );
118
    }
119
120
    public function success(): ResponseInterface
121
    {
122
        return new Response(
123
            200,
124
            ['Content-Type' => 'text/html'],
125
            'success',
126
        );
127
    }
128
129
    /**
130
     * @throws ContainerException
131
     * @throws InvalidParamsException
132
     * @throws ServiceNotFoundException
133
     */
134
    public function query(array $order): Collection|Rocket
135
    {
136
        Event::dispatch(new MethodCalled('jsb', __METHOD__, $order, null));
137
138
        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\Artful\Rocket|...gda\Supports\Collection. Consider adding an additional type-check to rule them out.
Loading history...
139
    }
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($contents->getParsedBody());
149
        }
150
151
        $request = ServerRequest::fromGlobals();
152
153
        return Collection::wrap($request->getParsedBody());
154
    }
155
}
156