These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Yansongda\Pay\Gateways\Alipay; |
||
4 | |||
5 | use Symfony\Component\HttpFoundation\RedirectResponse; |
||
6 | use Symfony\Component\HttpFoundation\Response; |
||
7 | use Yansongda\Pay\Contracts\GatewayInterface; |
||
8 | use Yansongda\Pay\Events; |
||
9 | use Yansongda\Pay\Exceptions\InvalidConfigException; |
||
10 | |||
11 | class WebGateway implements GatewayInterface |
||
12 | { |
||
13 | /** |
||
14 | * Pay an order. |
||
15 | * |
||
16 | * @author yansongda <[email protected]> |
||
17 | * |
||
18 | * @param string $endpoint |
||
19 | * @param array $payload |
||
20 | * |
||
21 | * @throws InvalidConfigException |
||
22 | * |
||
23 | * @return Response |
||
24 | */ |
||
25 | public function pay($endpoint, array $payload): Response |
||
26 | { |
||
27 | $biz_array = json_decode($payload['biz_content'], true); |
||
28 | $biz_array['product_code'] = $this->getProductCode(); |
||
29 | |||
30 | $method = $biz_array['http_method'] ?? 'POST'; |
||
31 | |||
32 | unset($biz_array['http_method']); |
||
33 | |||
34 | $payload['method'] = $this->getMethod(); |
||
35 | $payload['biz_content'] = json_encode($biz_array); |
||
36 | $payload['sign'] = Support::generateSign($payload); |
||
37 | |||
38 | Events::dispatch(Events::PAY_STARTED, new Events\PayStarted('Alipay', 'Web/Wap', $endpoint, $payload)); |
||
39 | |||
40 | return $this->buildPayHtml($endpoint, $payload, $method); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Find. |
||
45 | * |
||
46 | * @author yansongda <[email protected]> |
||
47 | * |
||
48 | * @param $order |
||
49 | * |
||
50 | * @return array |
||
51 | */ |
||
52 | View Code Duplication | public function find($order): array |
|
0 ignored issues
–
show
|
|||
53 | { |
||
54 | return [ |
||
55 | 'method' => 'alipay.trade.query', |
||
56 | 'biz_content' => json_encode(is_array($order) ? $order : ['out_trade_no' => $order]), |
||
57 | ]; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Build Html response. |
||
62 | * |
||
63 | * @author yansongda <[email protected]> |
||
64 | * |
||
65 | * @param string $endpoint |
||
66 | * @param array $payload |
||
67 | * @param string $method |
||
68 | * |
||
69 | * @return Response |
||
70 | */ |
||
71 | protected function buildPayHtml($endpoint, $payload, $method = 'POST'): Response |
||
72 | { |
||
73 | if (strtoupper($method) === 'GET') { |
||
74 | return RedirectResponse::create($endpoint.'?'.http_build_query($payload)); |
||
75 | } |
||
76 | |||
77 | $sHtml = "<form id='alipay_submit' name='alipay_submit' action='".$endpoint."' method='.$method.'>"; |
||
78 | foreach ($payload as $key => $val) { |
||
79 | $val = str_replace("'", ''', $val); |
||
80 | $sHtml .= "<input type='hidden' name='".$key."' value='".$val."'/>"; |
||
81 | } |
||
82 | $sHtml .= "<input type='submit' value='ok' style='display:none;'></form>"; |
||
83 | $sHtml .= "<script>document.forms['alipay_submit'].submit();</script>"; |
||
84 | |||
85 | return Response::create($sHtml); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Get method config. |
||
90 | * |
||
91 | * @author yansongda <[email protected]> |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | protected function getMethod(): string |
||
96 | { |
||
97 | return 'alipay.trade.page.pay'; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Get productCode config. |
||
102 | * |
||
103 | * @author yansongda <[email protected]> |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | protected function getProductCode(): string |
||
108 | { |
||
109 | return 'FAST_INSTANT_TRADE_PAY'; |
||
110 | } |
||
111 | } |
||
112 |
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.