您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

如何用PHP实现微信支付对账单查询

2024/2/18 1:31:04发布28次查看
随着移动支付的流行,微信支付成为了一个不可忽视的支付方式。作为开发人员,在使用微信支付时,对账单查询是必不可少的环节。本文将介绍如何利用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 签名
其中,签名的生成需要参照微信支付的签名规则。具体的实现方法可以参考微信支付提供的官方文档。
下载对账单通过上一步获得的对账单下载链接,我们可以使用php的curl库下载对账单。具体的代码实现如下:
$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);
解析对账单下载下来的对账单是一个csv文件,需要通过php解析进行进一步处理。我们可以使用fgetcsv函数读取每一行的数据,然后对数据进行解析。
具体的代码实现如下:
$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;}
处理数据对账单返回的是原始数据,需要进行一定的处理才能得到对账结果。我们可以将数据按照商户订单号进行分组,然后按照订单号、交易类型、交易状态等条件筛选数据。最后,可以将对账结果输出到html页面上。
具体的代码实现如下:
$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实现微信支付对账单查询的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product