Completed
Push — master ( a9c6ec...e2169c )
by Songda
01:29
created

src/Gateways/Alipay/WebGateway.php (1 issue)

Upgrade to new PHP Analysis Engine

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\Events;
8
use Yansongda\Pay\Exceptions\InvalidArgumentException;
9
use Yansongda\Pay\Exceptions\InvalidConfigException;
10
use Yansongda\Pay\Gateways\Alipay;
11
12
class WebGateway extends Gateway
13
{
14
    /**
15
     * Pay an order.
16
     *
17
     * @author yansongda <[email protected]>
18
     *
19
     * @param string $endpoint
20
     *
21
     * @throws InvalidConfigException
22
     * @throws InvalidArgumentException
23
     */
24
    public function pay($endpoint, array $payload): Response
25
    {
26
        $biz_array = json_decode($payload['biz_content'], true);
27
        $biz_array['product_code'] = $this->getProductCode();
28
29
        $method = $biz_array['http_method'] ?? 'POST';
30
31
        unset($biz_array['http_method']);
32
        if ((Alipay::MODE_SERVICE === $this->mode) && (!empty(Support::getInstance()->pid))) {
33
            $biz_array['extend_params'] = is_array($biz_array['extend_params']) ? array_merge(['sys_service_provider_id' => Support::getInstance()->pid], $biz_array['extend_params']) : ['sys_service_provider_id' => Support::getInstance()->pid];
34
        }
35
        $payload['method'] = $this->getMethod();
36
        $payload['biz_content'] = json_encode($biz_array);
37
        $payload['sign'] = Support::generateSign($payload);
38
39
        Events::dispatch(new Events\PayStarted('Alipay', 'Web/Wap', $endpoint, $payload));
40
41
        return $this->buildPayHtml($endpoint, $payload, $method);
42
    }
43
44
    /**
45
     * Find.
46
     *
47
     * @author yansongda <[email protected]>
48
     *
49
     * @param $order
50
     */
51 View Code Duplication
    public function find($order): array
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
52
    {
53
        return [
54
            'method' => 'alipay.trade.query',
55
            'biz_content' => json_encode(is_array($order) ? $order : ['out_trade_no' => $order]),
56
        ];
57
    }
58
59
    /**
60
     * Build Html response.
61
     *
62
     * @author yansongda <[email protected]>
63
     *
64
     * @param string $endpoint
65
     * @param array  $payload
66
     * @param string $method
67
     */
68
    protected function buildPayHtml($endpoint, $payload, $method = 'POST'): Response
69
    {
70
        if ('GET' === strtoupper($method)) {
71
            return RedirectResponse::create($endpoint.'&'.http_build_query($payload));
72
        }
73
74
        $sHtml = "<form id='alipay_submit' name='alipay_submit' action='".$endpoint."' method='".$method."'>";
75
        foreach ($payload as $key => $val) {
76
            $val = str_replace("'", '&apos;', $val);
77
            $sHtml .= "<input type='hidden' name='".$key."' value='".$val."'/>";
78
        }
79
        $sHtml .= "<input type='submit' value='ok' style='display:none;'></form>";
80
        $sHtml .= "<script>document.forms['alipay_submit'].submit();</script>";
81
82
        return Response::create($sHtml);
83
    }
84
85
    /**
86
     * Get method config.
87
     *
88
     * @author yansongda <[email protected]>
89
     */
90
    protected function getMethod(): string
91
    {
92
        return 'alipay.trade.page.pay';
93
    }
94
95
    /**
96
     * Get productCode config.
97
     *
98
     * @author yansongda <[email protected]>
99
     */
100
    protected function getProductCode(): string
101
    {
102
        return 'FAST_INSTANT_TRADE_PAY';
103
    }
104
}
105