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

Epay::pay()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
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\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
    public function cancel($order): Collection|Rocket
68
    {
69
        throw new InvalidParamsException(Exception::PARAMS_METHOD_NOT_SUPPORTED, 'Epay does not support cancel api');
70
    }
71
72
    public function close($order): Collection|Rocket
73
    {
74
        throw new InvalidParamsException(Exception::PARAMS_METHOD_NOT_SUPPORTED, 'Epay does not support close api');
75
    }
76
77
    public function refund(array $order): Collection|Rocket
78
    {
79
        Event::dispatch(new MethodCalled('epay', __METHOD__, $order, null));
80
81
        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...
82
    }
83
84
    public function callback(null|array|ServerRequestInterface $contents = null, ?array $params = null): Collection|Rocket
85
    {
86
        $request = $this->getCallbackParams($contents);
87
88
        Event::dispatch(new CallbackReceived('epay', $request->all(), $params, null));
89
90
        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...
91
            [CallbackPlugin::class],
92
            ['request' => $request, 'params' => $params]
93
        );
94
    }
95
96
    public function success(): ResponseInterface
97
    {
98
        return new Response(
99
            200,
100
            ['Content-Type' => 'text/html'],
101
            'success',
102
        );
103
    }
104
105
    public function query(array $order): Collection|Rocket
106
    {
107
        $order = is_array($order) ? $order : ['outTradeNo' => $order];
0 ignored issues
show
introduced by
The condition is_array($order) is always true.
Loading history...
108
109
        Event::dispatch(new MethodCalled('epay', __METHOD__, $order, null));
110
111
        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...
112
    }
113
114
    protected function getCallbackParams($contents = null): Collection
115
    {
116
        if (is_array($contents)) {
117
            return Collection::wrap($contents);
118
        }
119
120
        if ($contents instanceof ServerRequestInterface) {
121
            return Collection::wrap($contents->getParsedBody());
122
        }
123
124
        $request = ServerRequest::fromGlobals();
125
126
        return Collection::wrap($request->getParsedBody());
127
    }
128
}
129