Completed
Branch develop (966c07)
by Carlos
02:52
created

API::query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
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
/**
13
 * API.php.
14
 *
15
 * @author    tianyong90 <[email protected]>
16
 * @copyright 2015 overtrue <[email protected]>
17
 *
18
 * @link      https://github.com/overtrue
19
 * @link      http://overtrue.me
20
 */
21
namespace EasyWeChat\Payment\LuckyMoney;
22
23
use EasyWeChat\Core\AbstractAPI;
24
use EasyWeChat\Payment\Merchant;
25
use EasyWeChat\Support\Collection;
26
use EasyWeChat\Support\XML;
27
use Psr\Http\Message\ResponseInterface;
28
29
/**
30
 * Class API.
31
 */
32
class API extends AbstractAPI
33
{
34
    /**
35
     * Merchant instance.
36
     *
37
     * @var Merchant
38
     */
39
    protected $merchant;
40
41
    // api
42
    const API_SEND = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
43
    const API_SEND_GROUP = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack';
44
    const API_QUERY = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo';
45
    const API_PREPARE = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/hbpreorder';
46
47
    // LuckyMoney type
48
    const TYPE_NORMAL = 'NORMAL';
49
    const TYPE_GROUP = 'GROUP';
50
51
    // Risk control type.
52
    const RISK_NORMAL = 'NORMAL';
53
    const RISK_IGN_FREQ_LMT = 'IGN_FREQ_LMT';
54
    const RISK_IGN_DAY_LMT = 'IGN_DAY_LMT';
55
    const RISK_IGN_FREQ_DAY_LMT = 'IGN_FREQ_DAY_LMT';
56
57
    /**
58
     * API constructor.
59
     *
60
     * @param \EasyWeChat\Payment\Merchant $merchant
61
     */
62
    public function __construct(Merchant $merchant)
63
    {
64
        $this->merchant = $merchant;
65
    }
66
67
    /**
68
     * Prepare luckymoney.
69
     *
70
     *
71
     * @return \EasyWeChat\Support\Collection
72
     */
73
    public function prepare(array $params)
74
    {
75
        $params['wxappid'] = $this->merchant->app_id;
76
77
        //XXX: PLEASE DON'T CHANGE THE FOLLOWING LINES.
78
        $params['auth_mchid'] = '1000052601';
79
        $params['auth_appid'] = 'wxbf42bd79c4391863';
80
81
        $params['amt_type'] = 'ALL_RAND';
82
83
        return $this->request(self::API_PREPARE, $params);
84
    }
85
86
    /**
87
     * Query luckymoney.
88
     *
89
     * @param string $orderNo
90
     */
91
    public function query($orderNo)
92
    {
93
        $params = [
94
            'appid' => $this->merchant->app_id,
95
            'mch_billno' => $orderNo,
96
            'bill_type' => 'MCHT',
97
        ];
98
99
        return $this->request(self::API_QUERY, $params);
100
    }
101
102
    /**
103
     * Send Luckymoney.
104
     *
105
     * @param array  $params
106
     * @param string $type
107
     *
108
     * @return \EasyWeChat\Support\Collection
109
     */
110
    public function send(array $params, $type = self::TYPE_NORMAL)
111
    {
112
        $api = ($type === self::TYPE_NORMAL) ? self::API_SEND : self::API_SEND_GROUP;
113
114
        $params['wxappid'] = $this->merchant->app_id;
115
116
        return $this->request($api, $params);
117
    }
118
119
    /**
120
     * Send normal lucnymoney.
121
     *
122
     * @param array $params
123
     *
124
     * @return \EasyWeChat\Support\Collection
125
     */
126 View Code Duplication
    public function sendNormal($params)
0 ignored issues
show
Coding Style introduced by
sendNormal uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Duplication introduced by
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...
127
    {
128
        $params['total_num'] = 1;
129
        $params['client_ip'] = $params['client_ip'] ?: $_SERVER['HTTP_CLIENT_IP'];
130
131
        return $this->send($params, self::TYPE_NORMAL);
132
    }
133
134
    /**
135
     * Send group luckymoney.
136
     *
137
     * @param array $params
138
     *
139
     * @return \EasyWeChat\Support\Collection
140
     */
141 View Code Duplication
    public function sendGroup($params)
0 ignored issues
show
Coding Style introduced by
sendGroup uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Duplication introduced by
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...
142
    {
143
        $params['amt_type'] = 'ALL_RAND';
144
        $params['client_ip'] = $params['client_ip'] ?: $_SERVER['HTTP_CLIENT_IP'];
145
146
        return $this->send($params, self::TYPE_GROUP);
147
    }
148
149
    /**
150
     * Merchant setter.
151
     *
152
     * @param Merchant $merchant
153
     *
154
     * @return $this
155
     */
156
    public function setMerchant(Merchant $merchant)
157
    {
158
        $this->merchant = $merchant;
159
    }
160
161
    /**
162
     * Merchant getter.
163
     *
164
     * @return Merchant
165
     */
166
    public function getMerchant()
167
    {
168
        return $this->merchant;
169
    }
170
171
    /**
172
     * Make a API request.
173
     *
174
     * @param string $api
175
     * @param array  $params
176
     * @param string $method
177
     *
178
     * @return \EasyWeChat\Support\Collection
179
     */
180
    protected function request($api, array $params, $method = 'post')
181
    {
182
        $params['mch_id'] = $this->merchant->merchant_id;
183
        $params['nonce_str'] = uniqid();
184
        $params['sign'] = \EasyWeChat\Payment\generate_sign($params, $this->merchant->key, 'md5');
185
186
        $options = [
187
            'body' => XML::build($params),
188
            'cert' => $this->merchant->get('cert_path'),
189
            'ssl_key' => $this->merchant->get('key_path'),
190
        ];
191
192
        return $this->parseResponse($this->getHttp()->request($api, $method, $options));
193
    }
194
195
    /**
196
     * Parse Response XML to array.
197
     *
198
     * @param \Psr\Http\Message\ResponseInterface|string $response
199
     *
200
     * @return \EasyWeChat\Support\Collection
201
     */
202
    protected function parseResponse($response)
203
    {
204
        if ($response instanceof ResponseInterface) {
205
            $response = $response->getBody();
206
        }
207
208
        return new Collection((array) XML::parse($response));
209
    }
210
}
211