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 | 12 | public function __construct(Merchant $merchant) |
|
74 | |||
75 | /** |
||
76 | * Pay the order. |
||
77 | * |
||
78 | * @param Order $order |
||
79 | * |
||
80 | * @return \EasyWeChat\Support\Collection |
||
81 | */ |
||
82 | 1 | public function pay(Order $order) |
|
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) |
|
100 | |||
101 | /** |
||
102 | * Query order. |
||
103 | * |
||
104 | * @param string $orderNo |
||
105 | * @param string $type |
||
106 | * |
||
107 | * @return \EasyWeChat\Support\Collection |
||
108 | */ |
||
109 | 1 | public function query($orderNo, $type = self::OUT_TRADE_NO) |
|
117 | |||
118 | /** |
||
119 | * Query order by transaction_id. |
||
120 | * |
||
121 | * @param string $transactionId |
||
122 | * |
||
123 | * @return \EasyWeChat\Support\Collection |
||
124 | */ |
||
125 | 1 | public function queryByTransactionId($transactionId) |
|
129 | |||
130 | /** |
||
131 | * Close order by out_trade_no. |
||
132 | * |
||
133 | * @param $tradeNo |
||
134 | * |
||
135 | * @return \EasyWeChat\Support\Collection |
||
136 | */ |
||
137 | 1 | public function close($tradeNo) |
|
145 | |||
146 | /** |
||
147 | * Reverse order. |
||
148 | * |
||
149 | * @param string $orderNo |
||
150 | * @param string $type |
||
151 | * |
||
152 | * @return \EasyWeChat\Support\Collection |
||
153 | */ |
||
154 | 1 | public function reverse($orderNo, $type = self::OUT_TRADE_NO) |
|
162 | |||
163 | /** |
||
164 | * Reverse order by transaction_id. |
||
165 | * |
||
166 | * @param int $transactionId |
||
167 | * |
||
168 | * @return \EasyWeChat\Support\Collection |
||
169 | */ |
||
170 | public function reverseByTransactionId($transactionId) |
||
174 | |||
175 | /** |
||
176 | * Make a refund request. |
||
177 | * |
||
178 | * @param string $orderNo |
||
179 | * @param float $totalFee |
||
180 | * @param float $refundFee |
||
181 | * @param string $opUserId |
||
182 | * @param string $type |
||
183 | * |
||
184 | * @return \EasyWeChat\Support\Collection |
||
185 | */ |
||
186 | 1 | public function refund( |
|
205 | |||
206 | /** |
||
207 | * Refund by transaction id. |
||
208 | * |
||
209 | * @param string $orderNo |
||
210 | * @param float $totalFee |
||
211 | * @param float $refundFee |
||
212 | * @param string $opUserId |
||
213 | * |
||
214 | * @return \EasyWeChat\Support\Collection |
||
215 | */ |
||
216 | public function refundByTransactionId( |
||
225 | |||
226 | /** |
||
227 | * Query refund status. |
||
228 | * |
||
229 | * @param string $orderNo |
||
230 | * @param string $type |
||
231 | * |
||
232 | * @return \EasyWeChat\Support\Collection |
||
233 | */ |
||
234 | 1 | public function queryRefund($orderNo, $type = self::OUT_TRADE_NO) |
|
242 | |||
243 | /** |
||
244 | * Query refund status by out_refund_no. |
||
245 | * |
||
246 | * @param string $refundNo |
||
247 | * |
||
248 | * @return \EasyWeChat\Support\Collection |
||
249 | */ |
||
250 | public function queryRefundByRefundNo($refundNo) |
||
254 | |||
255 | /** |
||
256 | * Query refund status by transaction_id. |
||
257 | * |
||
258 | * @param string $transactionId |
||
259 | * |
||
260 | * @return \EasyWeChat\Support\Collection |
||
261 | */ |
||
262 | public function queryRefundByTransactionId($transactionId) |
||
266 | |||
267 | /** |
||
268 | * Query refund status by refund_id. |
||
269 | * |
||
270 | * @param string $refundId |
||
271 | * |
||
272 | * @return \EasyWeChat\Support\Collection |
||
273 | */ |
||
274 | public function queryRefundByRefundId($refundId) |
||
278 | |||
279 | /** |
||
280 | * Download bill history as a table file. |
||
281 | * |
||
282 | * @param string $date |
||
283 | * @param string $type |
||
284 | * |
||
285 | * @return \Psr\Http\Message\ResponseInterface |
||
286 | */ |
||
287 | 1 | public function downloadBill($date, $type = self::BILL_TYPE_ALL) |
|
296 | |||
297 | /** |
||
298 | * Convert long url to short url. |
||
299 | * |
||
300 | * @param string $url |
||
301 | * |
||
302 | * @return \EasyWeChat\Support\Collection |
||
303 | */ |
||
304 | 1 | public function urlShorten($url) |
|
308 | |||
309 | /** |
||
310 | * Report API status to WeChat. |
||
311 | * |
||
312 | * @param string $api |
||
313 | * @param int $timeConsuming |
||
314 | * @param string $resultCode |
||
315 | * @param string $returnCode |
||
316 | * @param array $other ex: err_code,err_code_des,out_trade_no,user_ip... |
||
317 | * |
||
318 | * @return \EasyWeChat\Support\Collection |
||
319 | */ |
||
320 | public function report($api, $timeConsuming, $resultCode, $returnCode, array $other = []) |
||
334 | |||
335 | /** |
||
336 | * Get openid by auth code. |
||
337 | * |
||
338 | * @param string $authCode |
||
339 | * |
||
340 | * @return \EasyWeChat\Support\Collection |
||
341 | */ |
||
342 | 1 | public function authCodeToOpenId($authCode) |
|
346 | |||
347 | /** |
||
348 | * Merchant setter. |
||
349 | * |
||
350 | * @param Merchant $merchant |
||
351 | * |
||
352 | * @return $this |
||
353 | */ |
||
354 | 1 | public function setMerchant(Merchant $merchant) |
|
358 | |||
359 | /** |
||
360 | * Merchant getter. |
||
361 | * |
||
362 | * @return Merchant |
||
363 | */ |
||
364 | 1 | public function getMerchant() |
|
368 | |||
369 | /** |
||
370 | * Make a API request. |
||
371 | * |
||
372 | * @param string $api |
||
373 | * @param array $params |
||
374 | * @param string $method |
||
375 | * @param array $options |
||
376 | * @param bool $options |
||
377 | * |
||
378 | * @return \EasyWeChat\Support\Collection|\Psr\Http\Message\ResponseInterface |
||
379 | */ |
||
380 | 10 | protected function request($api, array $params, $method = 'post', array $options = [], $returnResponse = false) |
|
381 | { |
||
382 | 10 | $params['appid'] = $this->merchant->app_id; |
|
383 | 10 | $params['mch_id'] = $this->merchant->merchant_id; |
|
384 | 10 | $params['device_info'] = $this->merchant->device_info; |
|
385 | 10 | $params['nonce_str'] = uniqid(); |
|
386 | 10 | $params = array_filter($params); |
|
387 | 10 | $params['sign'] = generate_sign($params, $this->merchant->key, 'md5'); |
|
388 | |||
389 | 10 | $options = array_merge([ |
|
390 | 10 | 'body' => XML::build($params), |
|
391 | 10 | ], $options); |
|
392 | |||
393 | 10 | $response = $this->getHttp()->request($api, $method, $options); |
|
394 | |||
395 | 10 | return $returnResponse ? $response : $this->parseResponse($response); |
|
396 | } |
||
397 | |||
398 | /** |
||
399 | * Request with SSL. |
||
400 | * |
||
401 | * @param string $api |
||
402 | * @param array $params |
||
403 | * @param string $method |
||
404 | * |
||
405 | * @return \EasyWeChat\Support\Collection |
||
406 | */ |
||
407 | 2 | protected function safeRequest($api, array $params, $method = 'post') |
|
416 | |||
417 | /** |
||
418 | * Parse Response XML to array. |
||
419 | * |
||
420 | * @param string|\Psr\Http\Message\ResponseInterface $response |
||
421 | * |
||
422 | * @return \EasyWeChat\Support\Collection |
||
423 | */ |
||
424 | 9 | protected function parseResponse($response) |
|
432 | } |
||
433 |