Passed
Pull Request — master (#1002)
by
unknown
02:19
created

Epay   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 100
rs 10
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeCommonPlugins() 0 6 1
A __call() 0 5 1
A pay() 0 3 1
A query() 0 5 1
A success() 0 6 1
A getCallbackParams() 0 13 3
A cancel() 0 3 1
A refund() 0 5 1
A callback() 0 9 1
A close() 0 3 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\Artful\Artful;
13
use Yansongda\Artful\Event;
14
use Yansongda\Artful\Exception\ContainerException;
15
use Yansongda\Artful\Exception\InvalidParamsException;
16
use Yansongda\Artful\Plugin\ParserPlugin;
17
use Yansongda\Artful\Rocket;
18
use Yansongda\Pay\Contract\ProviderInterface;
19
use Yansongda\Pay\Event\CallbackReceived;
20
use Yansongda\Pay\Event\MethodCalled;
21
use Yansongda\Pay\Exception\Exception;
22
use Yansongda\Pay\Pay;
23
use Yansongda\Pay\Plugin\Epay\AddPayloadSignPlugin;
24
use Yansongda\Pay\Plugin\Epay\AddRadarPlugin;
25
use Yansongda\Pay\Plugin\Epay\CallbackPlugin;
26
use Yansongda\Pay\Plugin\Epay\ResponsePlugin;
27
use Yansongda\Pay\Plugin\Epay\StartPlugin;
28
use Yansongda\Pay\Plugin\Epay\VerifySignaturePlugin;
29
use Yansongda\Supports\Collection;
30
use Yansongda\Supports\Str;
31
32
/**
33
 * @method Collection|Rocket scan(array $order) 扫码支付[微信支付宝都可扫描]
34
 */
35
class Epay implements ProviderInterface
36
{
37
    public const URL = [
38
        Pay::MODE_NORMAL => 'https://mybank.jsbchina.cn:577/eis/merchant/merchantServices.htm',
39
        Pay::MODE_SANDBOX => 'https://epaytest.jsbchina.cn:9999/eis/merchant/merchantServices.htm',
40
    ];
41
42
    public function __call($name, $params)
43
    {
44
        $plugin = '\Yansongda\Pay\Shortcut\Epay\\'.Str::studly($name).'Shortcut';
45
46
        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

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