隨著行動支付的流行,微信支付成為了一個不可忽視的支付方式。作為開發人員,在使用微信支付時,對帳單查詢是必不可少的環節。本文將介紹如何利用PHP語言實現微信支付對帳單的查詢。
在使用微信付款前,首先需要設定微信支付帳號資訊。在微信支付開發者中心(https://pay.weixin.qq.com/)註冊帳號,完成相關認證後,即可取得到微信支付的APPID、商家ID、商家金鑰等資訊。
在微信支付完成後,系統會產生對應的交易記錄,並將交易記錄上傳至微信支付伺服器。我們可以透過呼叫微信支付提供的API,取得對帳單的下載連結。
具體的API為:https://api.mch.weixin.qq.com/pay/downloadbill
此API需要傳遞以下參數:
必填 | 類型 | 範例值 | 描述 | |
---|---|---|---|---|
##appid | 是 | string(32) | wxd678efh567hg6787 | 微信指派的公用帳號ID |
mch_id | 是 | string(32) | 1230000109 | 微信支付分配的商家號碼 |
#nonce_str | 是 | string(32) | 5K8264ILTKCH16CQ2502SI8ZNMTM67VS | 隨機字符串 |
sign | 是 | string(32) | C380BEC2BFD727A4B6845133519F3AD6 | 簽名 |
$url = 'https://api.mch.weixin.qq.com/pay/downloadbill'; $params = array( 'appid' => 'xxx', 'mch_id' => 'xxx', 'nonce_str' => 'xxx', 'sign' => 'xxx', 'bill_date' => 'xxx', // 对账单日期 'bill_type' => 'ALL', // ALL-返回当日所有订单信息,SUCCESS-返回当日成功支付的订单,REFUND-返回当日退款订单,REVOKED-已撤销或已退款的订单 ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params)); $response = curl_exec($ch); curl_close($ch);
$lines = explode(" ", $response); $header = explode(",", $lines[0]); // 第一行为表头信息 $data = array(); for($i = 1; $i < count($lines)-2; $i++) { // 最后两行为统计信息,不需要处理 $line = explode(",", $lines[$i]); $row = array(); foreach ($header as $key => $value) { $row[$value] = $line[$key]; } $data[] = $row; }
$grouped_data = array(); foreach ($data as $row) { $grouped_data[$row['商户订单号']][] = $row; } $result = array(); foreach ($grouped_data as $order_no => $order_lines) { $success_lines = array_filter($order_lines, function($line) { return $line['交易状态'] == 'SUCCESS'; }); if (count($success_lines) == 0) { $result[] = array( 'order_no' => $order_no, 'reconciliation_status' => 'fail', 'transaction_type' => $order_lines[0]['交易类型'], 'transaction_status' => $order_lines[0]['交易状态'], 'total_amount' => $order_lines[0]['应结订单金额'], 'refund_amount' => $order_lines[0]['已退款金额'], 'fee' => $order_lines[0]['手续费'], ); } else { $success_line = $success_lines[0]; $total_amount = array_sum(array_column($order_lines, '应结订单金额')); $refund_amount = array_sum(array_column($order_lines, '已退款金额')); $fee = array_sum(array_column($order_lines, '手续费')); $result[] = array( 'order_no' => $order_no, 'reconciliation_status' => 'success', 'transaction_type' => $success_line['交易类型'], 'transaction_status' => $success_line['交易状态'], 'total_amount' => $total_amount, 'refund_amount' => $refund_amount, 'fee' => $fee, ); } } // 输出结果到HTML页面上 echo "<table>"; echo "<tr><td>订单号</td><td>对账状态</td><td>交易类型</td><td>交易状态</td><td>总金额</td><td>退款金额</td><td>手续费</td></tr>" foreach ($result as $row) { echo "<tr>"; echo "<td>{$row['order_no']}</td>"; echo "<td>{$row['reconciliation_status']}</td>"; echo "<td>{$row['transaction_type']}</td>"; echo "<td>{$row['transaction_status']}</td>"; echo "<td>{$row['total_amount']}</td>"; echo "<td>{$row['refund_amount']}</td>"; echo "<td>{$row['fee']}</td>"; echo "</tr>"; } echo "</table>";
以上是如何用PHP實現微信支付對帳單查詢的詳細內容。更多資訊請關注PHP中文網其他相關文章!