1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yansongda\Pay\Gateways\Wechat; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
6
|
|
|
use Yansongda\Pay\Events; |
7
|
|
|
use Yansongda\Pay\Exceptions\GatewayException; |
8
|
|
|
use Yansongda\Pay\Exceptions\InvalidArgumentException; |
9
|
|
|
use Yansongda\Pay\Exceptions\InvalidSignException; |
10
|
|
|
|
11
|
|
|
class WapGateway extends Gateway |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Pay an order. |
15
|
|
|
* |
16
|
|
|
* @author yansongda <[email protected]> |
17
|
|
|
* |
18
|
|
|
* @param string $endpoint |
19
|
|
|
* @param array $payload |
20
|
|
|
* |
21
|
|
|
* @throws GatewayException |
22
|
|
|
* @throws InvalidArgumentException |
23
|
|
|
* @throws InvalidSignException |
24
|
|
|
* |
25
|
|
|
* @return RedirectResponse |
26
|
|
|
*/ |
27
|
|
|
public function pay($endpoint, array $payload): RedirectResponse |
28
|
|
|
{ |
29
|
|
|
$payload['trade_type'] = $this->getTradeType(); |
30
|
|
|
|
31
|
|
|
Events::dispatch(Events::PAY_STARTED, new Events\PayStarted('Wechat', 'Wap', $endpoint, $payload)); |
32
|
|
|
|
33
|
|
|
$mweb_url = $this->preOrder($payload)->get('mweb_url'); |
34
|
|
|
|
35
|
|
|
$url = is_null(Support::getInstance()->return_url) ? $mweb_url : $mweb_url. |
36
|
|
|
'&redirect_url='.urlencode(Support::getInstance()->return_url); |
37
|
|
|
|
38
|
|
|
return RedirectResponse::create($url); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Find. |
43
|
|
|
* |
44
|
|
|
* @author yansongda <[email protected]> |
45
|
|
|
* |
46
|
|
|
* @param $order |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
public function find($order): array |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
return [ |
53
|
|
|
'endpoint' => 'pay/orderquery', |
54
|
|
|
'order' => is_array($order) ? $order : ['out_trade_no' => $order], |
55
|
|
|
'cert' => false, |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get trade type config. |
61
|
|
|
* |
62
|
|
|
* @author yansongda <[email protected]> |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
protected function getTradeType(): string |
67
|
|
|
{ |
68
|
|
|
return 'MWEB'; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
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.