BaseClient::buildRequestForm()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 3
rs 9.9332
c 1
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the wannanbigpig/alipay-sdk-php.
4
 *
5
 * (c) wannanbigpig <[email protected]>
6
 *
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace EasyAlipay\Payment\Kernel;
12
13
use EasyAlipay\Kernel\Support\Support;
14
15
/**
16
 * Class BaseClient
17
 *
18
 * @author   liuml  <[email protected]>
19
 * @DateTime 2019-07-24  14:25
20
 */
21
class BaseClient extends Support
22
{
23
    /**
24
     * sdkExecute.
25
     *
26
     * @param string $endpoint
27
     * @param array  $params
28
     *
29
     * @return string
30
     *
31
     * @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException
32
     */
33 4
    public function sdkExecute(string $endpoint, array $params = [])
34
    {
35
        // Get api system parameters
36 4
        $sysParams = $this->app->apiCommonConfig($endpoint);
37
        // Filter system parameters
38 4
        $sysParams = array_filter($sysParams, function ($value) {
39 4
            return !($this->checkEmpty($value));
40 4
        });
41 4
        $params = array_merge($sysParams, $this->json($params));
42
        // Set the signature
43 4
        $params['sign'] = $this->generateSign($params, $sysParams['sign_type']);
44
45 4
        ksort($params);
46
47 4
        return http_build_query($params);
48
    }
49
50
    /**
51
     * pageExecute.
52
     *
53
     * @param string $endpoint
54
     * @param array  $params
55
     * @param string $httpMethod
56
     *
57
     * @return string
58
     *
59
     * @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException
60
     */
61 8
    public function pageExecute(string $endpoint, array $params = [], string $httpMethod = "POST")
62
    {
63
64
        // Get api system parameters
65 8
        $sysParams = $this->app->apiCommonConfig($endpoint);
66
        // Filter system parameters
67 8
        $sysParams = array_filter($sysParams, function ($value) {
68 8
            return !($this->checkEmpty($value));
69 8
        });
70 8
        $params = array_merge($sysParams, $this->json($params));
71
        // Set the signature
72 8
        $params['sign'] = $this->generateSign($params, $sysParams['sign_type']);
73
74 8
        if ("GET" === strtoupper($httpMethod)) {
75
            // value urlencode
76 2
            $preString = $this->getSignContentUrlencode($params);
77
            // Stitching GET request string
78 2
            $requestUrl = $this->app->getGateway()."?".$preString;
79
80 2
            return $requestUrl;
81
        } else {
82
            // Stitching form string
83 8
            return $this->buildRequestForm($params);
84
        }
85
    }
86
87
    /**
88
     * buildRequestForm.
89
     *
90
     * @param array $paraTemp
91
     *
92
     * @return string
93
     */
94 8
    protected function buildRequestForm(array $paraTemp)
95
    {
96
        $sHtml = "<form id='alipaysubmit' name='alipaysubmit' action='".
97 8
            $this->app->getGateway()."?charset=".trim($paraTemp['charset']).
98 8
            "' method='POST'>";
99
100 8
        foreach ($paraTemp as $key => $val) {
101 8
            if (false === $this->checkEmpty($val)) {
102 8
                $val = str_replace("'", "&apos;", $val);
103 8
                $sHtml .= "<input type='hidden' name='".$key."' value='".$val."'/>";
104
            }
105
        }
106
107
        // Submit button control please do not include the name attribute
108 8
        $sHtml = $sHtml."<input type='submit' value='ok' style='display:none;''></form>";
109
110 8
        $sHtml = $sHtml."<script>document.forms['alipaysubmit'].submit();</script>";
111
112 8
        return $sHtml;
113
    }
114
}
115