Completed
Pull Request — master (#1001)
by mingyoung
01:33
created

BaseClient::wrap()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EasyWeChat\Payment\Kernel;
13
14
use EasyWeChat\Kernel\Support;
15
use EasyWeChat\Kernel\Traits\HasHttpRequests;
16
use EasyWeChat\Payment\Application;
17
use Psr\Http\Message\ResponseInterface;
18
19
/**
20
 * Class BaseClient.
21
 *
22
 * @author overtrue <[email protected]>
23
 */
24
class BaseClient
25
{
26
    use HasHttpRequests { request as performRequest; }
27
28
    /**
29
     * @var \EasyWeChat\Payment\Application
30
     */
31
    protected $app;
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param \EasyWeChat\Payment\Application $app
37
     */
38
    public function __construct(Application $app)
39
    {
40
        $this->app = $app;
41
42
        $this->setHttpClient($this->app['http_client']);
43
    }
44
45
    /**
46
     * Extra request params.
47
     *
48
     * @return array
49
     */
50
    protected function prepends()
51
    {
52
        return [];
53
    }
54
55
    /**
56
     * Make a API request.
57
     *
58
     * @param string $endpoint
59
     * @param array  $params
60
     * @param string $method
61
     * @param array  $options
62
     * @param bool   $returnResponse
63
     *
64
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
65
     */
66
    protected function request(string $endpoint, array $params = [], $method = 'post', array $options = [], $returnResponse = false)
67
    {
68
        $base = [
69
            'appid' => $this->app['config']['app_id'],
70
            'mch_id' => $this->app['config']['mch_id'],
71
            'nonce_str' => uniqid(),
72
            'sub_mch_id' => $this->app['config']['sub_mch_id'],
73
            'sub_appid' => $this->app['config']['sub_appid'],
74
        ];
75
76
        $params = array_filter(array_merge($base, $this->prepends(), $params));
77
78
        $params['sign'] = Support\generate_sign($params, $this->app->getKey());
79
80
        $options = array_merge([
81
            'body' => Support\XML::build($params),
82
        ], $options);
83
84
        $response = $this->performRequest($endpoint, $method, $options);
85
86
        return $returnResponse ? $response : $this->resolveResponse($response, $this->app->config->get('response_type'));
87
    }
88
89
    /**
90
     * Make a request and return raw response.
91
     *
92
     * @param string $endpoint
93
     * @param array  $params
94
     * @param string $method
95
     * @param array  $options
96
     *
97
     * @return array|Support\Collection|object|ResponseInterface|string
98
     */
99
    protected function requestRaw($endpoint, array $params = [], $method = 'post', array $options = [])
100
    {
101
        return $this->request($endpoint, $params, $method, $options, true);
102
    }
103
104
    /**
105
     * Request with SSL.
106
     *
107
     * @param string $endpoint
108
     * @param array  $params
109
     * @param string $method
110
     * @param array  $options
111
     *
112
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
113
     */
114
    protected function safeRequest($endpoint, array $params, $method = 'post', array $options = [])
115
    {
116
        $options = array_merge([
117
            'cert' => $this->app['config']->get('cert_path'),
118
            'ssl_key' => $this->app['config']->get('key_path'),
119
        ], $options);
120
121
        return $this->request($endpoint, $params, $method, $options);
122
    }
123
124
    /**
125
     * Wrapping an API endpoint.
126
     *
127
     * @param string $endpoint
128
     *
129
     * @return string
130
     */
131
    protected function wrap(string $endpoint): string
132
    {
133
        return $this->app->inSandbox() ? "sandboxnew/{$endpoint}" : $endpoint;
134
    }
135
}
136