1 | <?php |
||
31 | class API extends AbstractAPI |
||
32 | { |
||
33 | /** |
||
34 | * Merchant instance. |
||
35 | * |
||
36 | * @var Merchant |
||
37 | */ |
||
38 | protected $merchant; |
||
39 | |||
40 | // api |
||
41 | const API_PAY_ORDER = 'https://api.mch.weixin.qq.com/pay/micropay'; |
||
42 | const API_PREPARE_ORDER = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; |
||
43 | const API_QUERY = 'https://api.mch.weixin.qq.com/pay/orderquery'; |
||
44 | const API_CLOSE = 'https://api.mch.weixin.qq.com/pay/closeorder'; |
||
45 | const API_REVERSE = 'https://api.mch.weixin.qq.com/secapi/pay/reverse'; |
||
46 | const API_REFUND = 'https://api.mch.weixin.qq.com/secapi/pay/refund'; |
||
47 | const API_QUERY_REFUND = 'https://api.mch.weixin.qq.com/pay/refundquery'; |
||
48 | const API_DOWNLOAD_BILL = 'https://api.mch.weixin.qq.com/pay/downloadbill'; |
||
49 | const API_REPORT = 'https://api.mch.weixin.qq.com/payitil/report'; |
||
50 | const API_URL_SHORTEN = 'https://api.mch.weixin.qq.com/tools/shorturl'; |
||
51 | const API_AUTH_CODE_TO_OPENID = 'https://api.mch.weixin.qq.com/tools/authcodetoopenid'; |
||
52 | |||
53 | // order id types. |
||
54 | const TRANSACTION_ID = 'transaction_id'; |
||
55 | const OUT_TRADE_NO = 'out_trade_no'; |
||
56 | const OUT_REFUND_NO = 'out_refund_no'; |
||
57 | const REFUND_ID = 'refund_id'; |
||
58 | |||
59 | // bill types. |
||
60 | const BILL_TYPE_ALL = 'ALL'; |
||
61 | const BILL_TYPE_SUCCESS = 'SUCCESS'; |
||
62 | const BILL_TYPE_REFUND = 'REFUND'; |
||
63 | const BILL_TYPE_REVOKED = 'REVOKED'; |
||
64 | |||
65 | /** |
||
66 | * API constructor. |
||
67 | * |
||
68 | * @param \EasyWeChat\Payment\Merchant $merchant |
||
69 | */ |
||
70 | 11 | public function __construct(Merchant $merchant) |
|
71 | { |
||
72 | 11 | $this->merchant = $merchant; |
|
73 | 11 | } |
|
74 | |||
75 | /** |
||
76 | * Pay the order. |
||
77 | * |
||
78 | * @param Order $order |
||
79 | * |
||
80 | * @return \EasyWeChat\Support\Collection |
||
81 | */ |
||
82 | public function pay(Order $order) |
||
83 | { |
||
84 | return $this->request(self::API_PAY_ORDER, $order->all()); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Prepare order to pay. |
||
89 | * |
||
90 | * @param Order $order |
||
91 | * |
||
92 | * @return \EasyWeChat\Support\Collection |
||
93 | */ |
||
94 | 1 | public function prepare(Order $order) |
|
95 | { |
||
96 | 1 | return $this->request(self::API_PREPARE_ORDER, $order->all()); |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * Query order. |
||
101 | * |
||
102 | * @param string $orderNo |
||
103 | * @param string $type |
||
104 | * |
||
105 | * @return \EasyWeChat\Support\Collection |
||
106 | */ |
||
107 | 1 | public function query($orderNo, $type = self::OUT_TRADE_NO) |
|
108 | { |
||
109 | $params = [ |
||
110 | 1 | $type => $orderNo, |
|
111 | 1 | ]; |
|
112 | |||
113 | 1 | return $this->request(self::API_QUERY, $params); |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * Query order by transaction_id. |
||
118 | * |
||
119 | * @param string $transactionId |
||
120 | * |
||
121 | * @return \EasyWeChat\Support\Collection |
||
122 | */ |
||
123 | 1 | public function queryByTransactionId($transactionId) |
|
124 | { |
||
125 | 1 | return $this->query($transactionId, self::TRANSACTION_ID); |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * Close order by out_trade_no. |
||
130 | * |
||
131 | * @param $tradeNo |
||
132 | * |
||
133 | * @return \EasyWeChat\Support\Collection |
||
134 | */ |
||
135 | 1 | public function close($tradeNo) |
|
136 | { |
||
137 | $params = [ |
||
138 | 1 | 'out_trade_no' => $tradeNo, |
|
139 | 1 | ]; |
|
140 | |||
141 | 1 | return $this->request(self::API_CLOSE, $params); |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * Reverse order. |
||
146 | * |
||
147 | * @param string $orderNo |
||
148 | * @param string $type |
||
149 | * |
||
150 | * @return \EasyWeChat\Support\Collection |
||
151 | */ |
||
152 | 1 | public function reverse($orderNo, $type = self::OUT_TRADE_NO) |
|
153 | { |
||
154 | $params = [ |
||
155 | 1 | $type => $orderNo, |
|
156 | 1 | ]; |
|
157 | |||
158 | 1 | return $this->request(self::API_REVERSE, $params); |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * Reverse order by transaction_id. |
||
163 | * |
||
164 | * @param int $transactionId |
||
165 | * |
||
166 | * @return \EasyWeChat\Support\Collection |
||
167 | */ |
||
168 | public function reverseByTransactionId($transactionId) |
||
169 | { |
||
170 | return $this->reverse($transactionId, self::TRANSACTION_ID); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Make a refund request. |
||
175 | * |
||
176 | * @param string $orderNo |
||
177 | * @param float $totalFee |
||
178 | * @param float $refundFee |
||
179 | * @param string $opUserId |
||
180 | * @param string $type |
||
181 | * |
||
182 | * @return \EasyWeChat\Support\Collection |
||
183 | */ |
||
184 | 1 | public function refund( |
|
185 | $orderNo, |
||
186 | $totalFee, |
||
187 | $refundFee = null, |
||
188 | $opUserId = null, |
||
189 | $type = self::OUT_TRADE_NO |
||
190 | ) { |
||
191 | $params = [ |
||
192 | 1 | $type => $orderNo, |
|
193 | 1 | 'total_fee' => $totalFee, |
|
194 | 1 | 'refund_fee' => $refundFee ?: $totalFee, |
|
195 | 1 | 'refund_fee_type' => $this->merchant->fee_type, |
|
196 | 1 | 'op_user_id' => $opUserId ?: $this->merchant->merchant_id, |
|
197 | 1 | ]; |
|
198 | |||
199 | 1 | return $this->request(self::API_REFUND, $params); |
|
200 | } |
||
201 | |||
202 | /** |
||
203 | * Refund by transaction id. |
||
204 | * |
||
205 | * @param string $orderNo |
||
206 | * @param float $totalFee |
||
207 | * @param float $refundFee |
||
208 | * @param string $opUserId |
||
209 | * |
||
210 | * @return \EasyWeChat\Support\Collection |
||
211 | */ |
||
212 | public function refundByTransactionId( |
||
213 | $orderNo, |
||
214 | $totalFee, |
||
215 | $refundFee = null, |
||
216 | $opUserId = null |
||
217 | ) { |
||
218 | return $this->refund($orderNo, $totalFee, $refundFee, $opUserId, self::TRANSACTION_ID); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Query refund status. |
||
223 | * |
||
224 | * @param string $orderNo |
||
225 | * @param string $type |
||
226 | * |
||
227 | * @return \EasyWeChat\Support\Collection |
||
228 | */ |
||
229 | 1 | public function queryRefund($orderNo, $type = self::OUT_TRADE_NO) |
|
230 | { |
||
231 | $params = [ |
||
232 | 1 | $type => $orderNo, |
|
233 | 1 | ]; |
|
234 | |||
235 | 1 | return $this->request(self::API_QUERY_REFUND, $params); |
|
236 | } |
||
237 | |||
238 | /** |
||
239 | * Query refund status by out_refund_no. |
||
240 | * |
||
241 | * @param string $refundNo |
||
242 | * |
||
243 | * @return \EasyWeChat\Support\Collection |
||
244 | */ |
||
245 | public function queryRefundByRefundNo($refundNo) |
||
246 | { |
||
247 | return $this->queryRefund($refundNo, self::OUT_REFUND_NO); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Query refund status by transaction_id. |
||
252 | * |
||
253 | * @param string $transactionId |
||
254 | * |
||
255 | * @return \EasyWeChat\Support\Collection |
||
256 | */ |
||
257 | public function queryRefundByTransactionId($transactionId) |
||
258 | { |
||
259 | return $this->queryRefund($transactionId, self::TRANSACTION_ID); |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Query refund status by refund_id. |
||
264 | * |
||
265 | * @param string $refundId |
||
266 | * |
||
267 | * @return \EasyWeChat\Support\Collection |
||
268 | */ |
||
269 | public function queryRefundByRefundId($refundId) |
||
270 | { |
||
271 | return $this->queryRefund($refundId, self::REFUND_ID); |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Download bill history as a table file. |
||
276 | * |
||
277 | * @param string $date |
||
278 | * @param string $type |
||
279 | * |
||
280 | * @return \EasyWeChat\Support\Collection |
||
281 | */ |
||
282 | 1 | public function downloadBill($date, $type = self::BILL_TYPE_ALL) |
|
283 | { |
||
284 | $params = [ |
||
285 | 1 | 'bill_date' => $date, |
|
286 | 1 | 'bill_type' => $type, |
|
287 | 1 | ]; |
|
288 | |||
289 | 1 | return $this->request(self::API_DOWNLOAD_BILL, $params); |
|
290 | } |
||
291 | |||
292 | /** |
||
293 | * Convert long url to short url. |
||
294 | * |
||
295 | * @param string $url |
||
296 | * |
||
297 | * @return \EasyWeChat\Support\Collection |
||
298 | */ |
||
299 | 1 | public function urlShorten($url) |
|
300 | { |
||
301 | 1 | return $this->request(self::API_URL_SHORTEN, ['long_url' => $url]); |
|
302 | } |
||
303 | |||
304 | /** |
||
305 | * Report API status to WeChat. |
||
306 | * |
||
307 | * @param string $api |
||
308 | * @param int $timeConsuming |
||
309 | * @param string $resultCode |
||
310 | * @param string $returnCode |
||
311 | * @param array $other ex: err_code,err_code_des,out_trade_no,user_ip... |
||
312 | * |
||
313 | * @return \EasyWeChat\Support\Collection |
||
314 | */ |
||
315 | public function report($api, $timeConsuming, $resultCode, $returnCode, array $other = []) |
||
|
|||
316 | { |
||
317 | $params = array_merge([ |
||
318 | 'interface_url' => $api, |
||
319 | 'execute_time_' => $timeConsuming, |
||
320 | 'return_code' => $returnCode, |
||
321 | 'return_msg' => null, |
||
322 | 'result_code' => $resultCode, |
||
323 | 'user_ip' => $_SERVER['SERVER_ADDR'], |
||
324 | 'time' => time(), |
||
325 | ], $other); |
||
326 | |||
327 | return $this->request(self::API_REPORT, $params); |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * Get openid by auth code. |
||
332 | * |
||
333 | * @param string $authCode |
||
334 | * |
||
335 | * @return \EasyWeChat\Support\Collection |
||
336 | */ |
||
337 | 1 | public function authCodeToOpenId($authCode) |
|
341 | |||
342 | /** |
||
343 | * Merchant setter. |
||
344 | * |
||
345 | * @param Merchant $merchant |
||
346 | * |
||
347 | * @return $this |
||
348 | */ |
||
349 | 1 | public function setMerchant(Merchant $merchant) |
|
350 | { |
||
351 | 1 | $this->merchant = $merchant; |
|
352 | 1 | } |
|
353 | |||
354 | /** |
||
355 | * Merchant getter. |
||
356 | * |
||
357 | * @return Merchant |
||
358 | */ |
||
359 | 1 | public function getMerchant() |
|
363 | |||
364 | /** |
||
365 | * Make a API request. |
||
366 | * |
||
367 | * @param string $api |
||
368 | * @param array $params |
||
369 | * @param string $method |
||
370 | * |
||
371 | * @return \EasyWeChat\Support\Collection |
||
372 | */ |
||
373 | 9 | protected function request($api, array $params, $method = 'post') |
|
374 | { |
||
375 | 9 | $params['appid'] = $this->merchant->app_id; |
|
376 | 9 | $params['mch_id'] = $this->merchant->merchant_id; |
|
377 | 9 | $params['device_info'] = $this->merchant->device_info; |
|
378 | // $params['time_stamp'] = time(); |
||
379 | 9 | $params['nonce_str'] = uniqid(); |
|
380 | 9 | $params['sign'] = generate_sign($params, $this->merchant->key, 'md5'); |
|
381 | |||
382 | 9 | return $this->parseResponse($this->getHttp()->{$method}($api, XML::build($params))); |
|
383 | } |
||
384 | |||
385 | /** |
||
386 | * Parse Response XML to array. |
||
387 | * |
||
388 | * @param string $response |
||
389 | * |
||
390 | * @return \EasyWeChat\Support\Collection |
||
391 | */ |
||
392 | 9 | protected function parseResponse($response) |
|
400 | } |
||
401 |
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: