1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yansongda\Pay\Gateways\Wechat; |
4
|
|
|
|
5
|
|
|
use Yansongda\Pay\Contracts\GatewayInterface; |
6
|
|
|
use Yansongda\Pay\Events; |
7
|
|
|
use Yansongda\Pay\Exceptions\GatewayException; |
8
|
|
|
use Yansongda\Pay\Exceptions\InvalidArgumentException; |
9
|
|
|
use Yansongda\Pay\Exceptions\InvalidSignException; |
10
|
|
|
use Yansongda\Supports\Collection; |
11
|
|
|
|
12
|
|
|
abstract class Gateway implements GatewayInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Mode. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $mode; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Bootstrap. |
23
|
|
|
* |
24
|
|
|
* @author yansongda <[email protected]> |
25
|
|
|
* |
26
|
|
|
* @throws InvalidArgumentException |
27
|
|
|
*/ |
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
$this->mode = Support::getInstance()->mode; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Pay an order. |
35
|
|
|
* |
36
|
|
|
* @author yansongda <[email protected]> |
37
|
|
|
* |
38
|
|
|
* @param string $endpoint |
39
|
|
|
* @param array $payload |
40
|
|
|
* |
41
|
|
|
* @return Collection |
42
|
|
|
*/ |
43
|
|
|
abstract public function pay($endpoint, array $payload); |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get trade type config. |
47
|
|
|
* |
48
|
|
|
* @author yansongda <[email protected]> |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
abstract protected function getTradeType(); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Schedule an order. |
56
|
|
|
* |
57
|
|
|
* @author yansongda <[email protected]> |
58
|
|
|
* |
59
|
|
|
* @param array $payload |
60
|
|
|
* |
61
|
|
|
* @throws GatewayException |
62
|
|
|
* @throws InvalidArgumentException |
63
|
|
|
* @throws InvalidSignException |
64
|
|
|
* |
65
|
|
|
* @return Collection |
66
|
|
|
*/ |
67
|
|
|
protected function preOrder($payload): Collection |
68
|
|
|
{ |
69
|
|
|
$payload['sign'] = Support::generateSign($payload); |
70
|
|
|
|
71
|
|
|
Events::dispatch(Events::METHOD_CALLED, new Events\MethodCalled('Wechat', 'PreOrder', '', $payload)); |
72
|
|
|
|
73
|
|
|
return Support::requestApi('pay/unifiedorder', $payload); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Find. |
78
|
|
|
* |
79
|
|
|
* @author yansongda <[email protected]> |
80
|
|
|
* |
81
|
|
|
* @param string|array $order |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
public function find($order): array |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
return [ |
88
|
|
|
'endpoint' => 'pay/orderquery', |
89
|
|
|
'order' => is_array($order) ? $order : ['out_trade_no' => $order], |
90
|
|
|
'cert' => false, |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.