API::query()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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