1 | <?php |
||
13 | class API extends AbstractAPI |
||
14 | { |
||
15 | /** |
||
16 | * Merchant instance. |
||
17 | * |
||
18 | * @var Merchant |
||
19 | */ |
||
20 | protected $merchant; |
||
21 | |||
22 | // api |
||
23 | const API_PAY_ORDER = 'https://api.mch.weixin.qq.com/pay/micropay'; |
||
24 | const API_PREPARE_ORDER = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; |
||
25 | const API_QUERY = 'https://api.mch.weixin.qq.com/pay/orderquery'; |
||
26 | const API_CLOSE = 'https://api.mch.weixin.qq.com/pay/closeorder'; |
||
27 | const API_REVERSE = 'https://api.mch.weixin.qq.com/secapi/pay/reverse'; |
||
28 | const API_REFUND = 'https://api.mch.weixin.qq.com/secapi/pay/refund'; |
||
29 | const API_QUERY_REFUND = 'https://api.mch.weixin.qq.com/pay/refundquery'; |
||
30 | const API_DOWNLOAD_BILL = 'https://api.mch.weixin.qq.com/pay/downloadbill'; |
||
31 | const API_REPORT = 'https://api.mch.weixin.qq.com/payitil/report'; |
||
32 | const API_URL_SHORTEN = 'https://api.mch.weixin.qq.com/tools/shorturl'; |
||
33 | const API_AUTH_CODE_TO_OPENID = 'https://api.mch.weixin.qq.com/tools/authcodetoopenid'; |
||
34 | |||
35 | // order id types. |
||
36 | const TRANSACTION_ID = 'transaction_id'; |
||
37 | const OUT_TRADE_NO = 'out_trade_no'; |
||
38 | const OUT_REFUND_NO = 'out_refund_no'; |
||
39 | const REFUND_ID = 'refund_id'; |
||
40 | |||
41 | // bill types. |
||
42 | const BILL_TYPE_ALL = 'ALL'; |
||
43 | const BILL_TYPE_SUCCESS = 'SUCCESS'; |
||
44 | const BILL_TYPE_REFUND = 'REFUND'; |
||
45 | const BILL_TYPE_REVOKED = 'REVOKED'; |
||
46 | |||
47 | /** |
||
48 | * API constructor. |
||
49 | * |
||
50 | * @param \EntWeChat\Payment\Merchant $merchant |
||
51 | */ |
||
52 | public function __construct(Merchant $merchant) |
||
56 | |||
57 | /** |
||
58 | * Pay the order. |
||
59 | * |
||
60 | * @param Order $order |
||
61 | * |
||
62 | * @return \EntWeChat\Support\Collection |
||
63 | */ |
||
64 | public function pay(Order $order) |
||
68 | |||
69 | /** |
||
70 | * Prepare order to pay. |
||
71 | * |
||
72 | * @param Order $order |
||
73 | * |
||
74 | * @return \EntWeChat\Support\Collection |
||
75 | */ |
||
76 | public function prepare(Order $order) |
||
85 | |||
86 | /** |
||
87 | * Query order. |
||
88 | * |
||
89 | * @param string $orderNo |
||
90 | * @param string $type |
||
91 | * |
||
92 | * @return \EntWeChat\Support\Collection |
||
93 | */ |
||
94 | public function query($orderNo, $type = self::OUT_TRADE_NO) |
||
102 | |||
103 | /** |
||
104 | * Query order by transaction_id. |
||
105 | * |
||
106 | * @param string $transactionId |
||
107 | * |
||
108 | * @return \EntWeChat\Support\Collection |
||
109 | */ |
||
110 | public function queryByTransactionId($transactionId) |
||
114 | |||
115 | /** |
||
116 | * Close order by out_trade_no. |
||
117 | * |
||
118 | * @param $tradeNo |
||
119 | * |
||
120 | * @return \EntWeChat\Support\Collection |
||
121 | */ |
||
122 | public function close($tradeNo) |
||
130 | |||
131 | /** |
||
132 | * Reverse order. |
||
133 | * |
||
134 | * @param string $orderNo |
||
135 | * @param string $type |
||
136 | * |
||
137 | * @return \EntWeChat\Support\Collection |
||
138 | */ |
||
139 | public function reverse($orderNo, $type = self::OUT_TRADE_NO) |
||
147 | |||
148 | /** |
||
149 | * Reverse order by transaction_id. |
||
150 | * |
||
151 | * @param int $transactionId |
||
152 | * |
||
153 | * @return \EntWeChat\Support\Collection |
||
154 | */ |
||
155 | public function reverseByTransactionId($transactionId) |
||
159 | |||
160 | /** |
||
161 | * Make a refund request. |
||
162 | * |
||
163 | * @param string $orderNo |
||
164 | * @param float $totalFee |
||
165 | * @param float $refundFee |
||
166 | * @param string $opUserId |
||
167 | * @param string $type |
||
168 | * @param string $refundAccount |
||
169 | * |
||
170 | * @return \EntWeChat\Support\Collection |
||
171 | */ |
||
172 | public function refund( |
||
193 | |||
194 | /** |
||
195 | * Refund by transaction id. |
||
196 | * |
||
197 | * @param string $orderNo |
||
198 | * @param float $totalFee |
||
199 | * @param float $refundFee |
||
200 | * @param string $opUserId |
||
201 | * @param string $refundAccount |
||
202 | * |
||
203 | * @return \EntWeChat\Support\Collection |
||
204 | */ |
||
205 | public function refundByTransactionId( |
||
215 | |||
216 | /** |
||
217 | * Query refund status. |
||
218 | * |
||
219 | * @param string $orderNo |
||
220 | * @param string $type |
||
221 | * |
||
222 | * @return \EntWeChat\Support\Collection |
||
223 | */ |
||
224 | public function queryRefund($orderNo, $type = self::OUT_TRADE_NO) |
||
232 | |||
233 | /** |
||
234 | * Query refund status by out_refund_no. |
||
235 | * |
||
236 | * @param string $refundNo |
||
237 | * |
||
238 | * @return \EntWeChat\Support\Collection |
||
239 | */ |
||
240 | public function queryRefundByRefundNo($refundNo) |
||
244 | |||
245 | /** |
||
246 | * Query refund status by transaction_id. |
||
247 | * |
||
248 | * @param string $transactionId |
||
249 | * |
||
250 | * @return \EntWeChat\Support\Collection |
||
251 | */ |
||
252 | public function queryRefundByTransactionId($transactionId) |
||
256 | |||
257 | /** |
||
258 | * Query refund status by refund_id. |
||
259 | * |
||
260 | * @param string $refundId |
||
261 | * |
||
262 | * @return \EntWeChat\Support\Collection |
||
263 | */ |
||
264 | public function queryRefundByRefundId($refundId) |
||
268 | |||
269 | /** |
||
270 | * Download bill history as a table file. |
||
271 | * |
||
272 | * @param string $date |
||
273 | * @param string $type |
||
274 | * |
||
275 | * @return \Psr\Http\Message\ResponseInterface |
||
276 | */ |
||
277 | public function downloadBill($date, $type = self::BILL_TYPE_ALL) |
||
286 | |||
287 | /** |
||
288 | * Convert long url to short url. |
||
289 | * |
||
290 | * @param string $url |
||
291 | * |
||
292 | * @return \EntWeChat\Support\Collection |
||
293 | */ |
||
294 | public function urlShorten($url) |
||
298 | |||
299 | /** |
||
300 | * Report API status to WeChat. |
||
301 | * |
||
302 | * @param string $api |
||
303 | * @param int $timeConsuming |
||
304 | * @param string $resultCode |
||
305 | * @param string $returnCode |
||
306 | * @param array $other ex: err_code,err_code_des,out_trade_no,user_ip... |
||
307 | * |
||
308 | * @return \EntWeChat\Support\Collection |
||
309 | */ |
||
310 | public function report($api, $timeConsuming, $resultCode, $returnCode, array $other = []) |
||
324 | |||
325 | /** |
||
326 | * Get openid by auth code. |
||
327 | * |
||
328 | * @param string $authCode |
||
329 | * |
||
330 | * @return \EntWeChat\Support\Collection |
||
331 | */ |
||
332 | public function authCodeToOpenId($authCode) |
||
336 | |||
337 | /** |
||
338 | * Merchant setter. |
||
339 | * |
||
340 | * @param Merchant $merchant |
||
341 | * |
||
342 | * @return $this |
||
343 | */ |
||
344 | public function setMerchant(Merchant $merchant) |
||
348 | |||
349 | /** |
||
350 | * Merchant getter. |
||
351 | * |
||
352 | * @return Merchant |
||
353 | */ |
||
354 | public function getMerchant() |
||
358 | |||
359 | /** |
||
360 | * Make a API request. |
||
361 | * |
||
362 | * @param string $api |
||
363 | * @param array $params |
||
364 | * @param string $method |
||
365 | * @param array $options |
||
366 | * @param bool $returnResponse |
||
367 | * |
||
368 | * @return \EntWeChat\Support\Collection|\Psr\Http\Message\ResponseInterface |
||
369 | */ |
||
370 | protected function request($api, array $params, $method = 'post', array $options = [], $returnResponse = false) |
||
389 | |||
390 | /** |
||
391 | * Request with SSL. |
||
392 | * |
||
393 | * @param string $api |
||
394 | * @param array $params |
||
395 | * @param string $method |
||
396 | * |
||
397 | * @return \EntWeChat\Support\Collection |
||
398 | */ |
||
399 | protected function safeRequest($api, array $params, $method = 'post') |
||
408 | |||
409 | /** |
||
410 | * Parse Response XML to array. |
||
411 | * |
||
412 | * @param ResponseInterface $response |
||
413 | * |
||
414 | * @return \EntWeChat\Support\Collection |
||
415 | */ |
||
416 | protected function parseResponse($response) |
||
424 | } |
||
425 |