Issues (120)

src/Provider/Jsb.php (4 issues)

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\Exception\ContainerException;
14
use Yansongda\Artful\Exception\InvalidParamsException;
15
use Yansongda\Artful\Exception\ServiceNotFoundException;
16
use Yansongda\Artful\Plugin\ParserPlugin;
17
use Yansongda\Artful\Rocket;
18
use Yansongda\Pay\Contract\ProviderInterface;
19
use Yansongda\Pay\Event;
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
     * @throws ContainerException
45
     * @throws InvalidParamsException
46
     * @throws ServiceNotFoundException
47
     */
48
    public function __call(string $name, array $params): null|Collection|MessageInterface|Rocket
49
    {
50
        $plugin = '\Yansongda\Pay\Shortcut\Jsb\\'.Str::studly($name).'Shortcut';
51
52
        return Artful::shortcut($plugin, ...$params);
0 ignored issues
show
$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

52
        return Artful::shortcut($plugin, /** @scrutinizer ignore-type */ ...$params);
Loading history...
53
    }
54
55
    /**
56
     * @throws ContainerException
57
     * @throws InvalidParamsException
58
     */
59
    public function pay(array $plugins, array $params): null|Collection|MessageInterface|Rocket
60
    {
61
        return Artful::artful($plugins, $params);
62
    }
63
64
    public function mergeCommonPlugins(array $plugins): array
65
    {
66
        return array_merge(
67
            [StartPlugin::class],
68
            $plugins,
69
            [AddPayloadSignPlugin::class, AddRadarPlugin::class, VerifySignaturePlugin::class, ResponsePlugin::class, ParserPlugin::class],
70
        );
71
    }
72
73
    /**
74
     * @throws InvalidParamsException
75
     */
76
    public function cancel(array $order): Collection|Rocket
77
    {
78
        throw new InvalidParamsException(Exception::PARAMS_METHOD_NOT_SUPPORTED, 'Jsb does not support cancel api');
79
    }
80
81
    /**
82
     * @throws InvalidParamsException
83
     */
84
    public function close(array $order): Collection|Rocket
85
    {
86
        throw new InvalidParamsException(Exception::PARAMS_METHOD_NOT_SUPPORTED, 'Jsb does not support close api');
87
    }
88
89
    /**
90
     * @throws ContainerException
91
     * @throws InvalidParamsException
92
     * @throws ServiceNotFoundException
93
     */
94
    public function refund(array $order): Collection|Rocket
95
    {
96
        Event::dispatch(new MethodCalled('jsb', __METHOD__, $order, null));
97
98
        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...
99
    }
100
101
    /**
102
     * @throws ContainerException
103
     * @throws InvalidParamsException
104
     */
105
    public function callback(null|array|ServerRequestInterface $contents = null, ?array $params = null): Collection|Rocket
106
    {
107
        $request = $this->getCallbackParams($contents);
108
109
        Event::dispatch(new CallbackReceived('jsb', $request->all(), $params, null));
110
111
        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...
112
            [CallbackPlugin::class],
113
            ['request' => $request, 'params' => $params]
114
        );
115
    }
116
117
    public function success(): ResponseInterface
118
    {
119
        return new Response(
120
            200,
121
            ['Content-Type' => 'text/html'],
122
            'success',
123
        );
124
    }
125
126
    /**
127
     * @throws ContainerException
128
     * @throws InvalidParamsException
129
     * @throws ServiceNotFoundException
130
     */
131
    public function query(array $order): Collection|Rocket
132
    {
133
        Event::dispatch(new MethodCalled('jsb', __METHOD__, $order, null));
134
135
        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...
136
    }
137
138
    protected function getCallbackParams($contents = null): Collection
139
    {
140
        if (is_array($contents)) {
141
            return Collection::wrap($contents);
142
        }
143
144
        if ($contents instanceof ServerRequestInterface) {
145
            return Collection::wrap($contents->getParsedBody());
146
        }
147
148
        $request = ServerRequest::fromGlobals();
149
150
        return Collection::wrap($request->getParsedBody());
151
    }
152
}
153