Completed
Pull Request — master (#590)
by
unknown
03:20
created

API::getMerchant()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
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
/**
13
 * API.php.
14
 *
15
 * @author    tianyong90 <[email protected]>
16
 * @copyright 2015 overtrue <[email protected]>
17
 *
18
 * @see      https://github.com/overtrue
19
 * @see      http://overtrue.me
20
 */
21
22
namespace EasyWeChat\Payment\LuckyMoney;
23
24
use EasyWeChat\Core\AbstractAPI;
25
use EasyWeChat\Payment\Merchant;
26
use EasyWeChat\Support\Collection;
27
use EasyWeChat\Support\XML;
28
use Psr\Http\Message\ResponseInterface;
29
30
/**
31
 * Class API.
32
 */
33
class API extends AbstractAPI
34
{
35
    /**
36
     * Merchant instance.
37
     *
38
     * @var Merchant
39
     */
40
    protected $merchant;
41
42
    // api
43
    const API_SEND = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
44
    const API_SEND_GROUP = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack';
45
    const API_QUERY = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo';
46
    const API_PREPARE = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/hbpreorder';
47
48
    // LuckyMoney type
49
    const TYPE_NORMAL = 'NORMAL';
50
    const TYPE_GROUP = 'GROUP';
51
52
    // Risk control type.
53
    const RISK_NORMAL = 'NORMAL';
54
    const RISK_IGN_FREQ_LMT = 'IGN_FREQ_LMT';
55
    const RISK_IGN_DAY_LMT = 'IGN_DAY_LMT';
56
    const RISK_IGN_FREQ_DAY_LMT = 'IGN_FREQ_DAY_LMT';
57
58
    /**
59
     * API constructor.
60
     *
61
     * @param \EasyWeChat\Payment\Merchant $merchant
62
     */
63 7
    public function __construct(Merchant $merchant)
64
    {
65 7
        $this->merchant = $merchant;
66 7
    }
67
68
    /**
69
     * Prepare luckymoney.
70
     *
71
     * @param array $params
72
     *
73
     * @return \EasyWeChat\Support\Collection
74
     */
75 1
    public function prepare(array $params)
76
    {
77 1
        $params['wxappid'] = $this->merchant->app_id;
78
79
        // XXX: PLEASE DON'T CHANGE THE FOLLOWING LINES.
80 1
        $params['auth_mchid'] = '1000052601';
81 1
        $params['auth_appid'] = 'wxbf42bd79c4391863';
82
83 1
        $params['amt_type'] = 'ALL_RAND';
84
85 1
        return $this->request(self::API_PREPARE, $params);
86
    }
87
88
    /**
89
     * Query luckymoney.
90
     *
91
     * @param string $mchBillNo
92
     *
93
     * @return \EasyWeChat\Support\Collection
94
     */
95 1
    public function query($mchBillNo)
96
    {
97
        $params = [
98 1
            'appid' => $this->merchant->app_id,
99 1
            'mch_billno' => $mchBillNo,
100 1
            'bill_type' => 'MCHT',
101
        ];
102
103 1
        return $this->request(self::API_QUERY, $params);
104
    }
105
106
    /**
107
     * Send LuckyMoney.
108
     *
109
     * @param array  $params
110
     * @param string $type
111
     *
112
     * @return \EasyWeChat\Support\Collection
113
     */
114 3
    public function send(array $params, $type = self::TYPE_NORMAL)
115
    {
116 3
        $api = ($type === self::TYPE_NORMAL) ? self::API_SEND : self::API_SEND_GROUP;
117
118 3
        $params['wxappid'] = $this->merchant->app_id;
119
        //如果类型为分裂红则去掉client_ip参数,否则签名会出错
120 3
        if ($type === self::TYPE_GROUP) {
121 2
            unset($params['client_ip']);
122
        }
123
124 3
        return $this->request($api, $params);
125
    }
126
127
    /**
128
     * Send normal LuckyMoney.
129
     *
130
     * @param array $params
131
     *
132
     * @return \EasyWeChat\Support\Collection
133
     */
134 1
    public function sendNormal($params)
135
    {
136 1
        $params['total_num'] = 1;
137 1
        $params['client_ip'] = !empty($params['client_ip']) ? $params['client_ip'] : getenv('SERVER_ADDR');
138
139 1
        return $this->send($params, self::TYPE_NORMAL);
140
    }
141
142
    /**
143
     * Send group luckymoney.
144
     *
145
     * @param array $params
146
     *
147
     * @return \EasyWeChat\Support\Collection
148
     */
149 1
    public function sendGroup($params)
150
    {
151 1
        $params['amt_type'] = 'ALL_RAND';
152
153 1
        return $this->send($params, self::TYPE_GROUP);
154
    }
155
156
    /**
157
     * Merchant setter.
158
     *
159
     * @param Merchant $merchant
160
     *
161
     * @return $this
162
     */
163 1
    public function setMerchant(Merchant $merchant)
164
    {
165 1
        $this->merchant = $merchant;
166 1
    }
167
168
    /**
169
     * Merchant getter.
170
     *
171
     * @return Merchant
172
     */
173 1
    public function getMerchant()
174
    {
175 1
        return $this->merchant;
176
    }
177
178
    /**
179
     * Make a API request.
180
     *
181
     * @param string $api
182
     * @param array  $params
183
     * @param string $method
184
     *
185
     * @return \EasyWeChat\Support\Collection
186
     */
187 5 View Code Duplication
    protected function request($api, array $params, $method = 'post')
188
    {
189 5
        $params = array_filter($params);
190 5
        $params['mch_id'] = $this->merchant->merchant_id;
191 5
        $params['nonce_str'] = uniqid();
192 5
        $params['sign'] = \EasyWeChat\Payment\generate_sign($params, $this->merchant->key, 'md5');
193
194
        $options = [
195 5
            'body' => XML::build($params),
196 5
            'cert' => $this->merchant->get('cert_path'),
197 5
            'ssl_key' => $this->merchant->get('key_path'),
198
        ];
199
200 5
        return $this->parseResponse($this->getHttp()->request($api, $method, $options));
201
    }
202
203
    /**
204
     * Parse Response XML to array.
205
     *
206
     * @param \Psr\Http\Message\ResponseInterface|string $response
207
     *
208
     * @return \EasyWeChat\Support\Collection
209
     */
210 5
    protected function parseResponse($response)
211
    {
212 5
        if ($response instanceof ResponseInterface) {
213
            $response = $response->getBody();
214
        }
215
216 5
        return new Collection((array) XML::parse($response));
217
    }
218
}
219