发布日期:2023-04-08浏览次数:676 来源:福州网站建设
下面代码中,我们首先构造了微信支付查询订单API接口的请求参数,包括商户号、API密钥、订单号等信息。然后,我们根据微信支付的签名算法生成了签名,并将签名添加到请求参数中。最后,我们使用PHP的file_get_contents函数向微信支付查询订单API接口发送了POST请求,并解析了响应结果,输出了查询结果。
需要注意的是,上述代码中的your_mch_id、your_api_key、your_out_trade_no和your_appid需要替换为你自己的微信支付分配的商户号、API密钥
<?php
// 微信支付查询订单API接口
$url = 'https://api.mch.weixin.qq.com/pay/orderquery';
// 微信支付分配的商户号
$mch_id = 'your_mch_id';
// 微信支付分配的API密钥
$api_key = 'your_api_key';
// 订单号
$out_trade_no = 'your_out_trade_no';
// 构造请求参数
$params = array(
'appid' => 'your_appid',
'mch_id' => $mch_id,
'out_trade_no' => $out_trade_no,
'nonce_str' => 'your_nonce_str',
);
// 生成签名
ksort($params);
$string = urldecode(http_build_query($params)) . '&key=' . $api_key;
$sign = strtoupper(md5($string));
// 添加签名到请求参数中
$params['sign'] = $sign;
// 发送请求
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query($params),
),
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
// 解析响应结果
$xml = simplexml_load_string($response);
$result_code = $xml->result_code;
if ($result_code == 'SUCCESS') {
$trade_state = $xml->trade_state;
if ($trade_state == 'SUCCESS') {
$total_fee = $xml->total_fee;
$transaction_id = $xml->transaction_id;
echo '订单查询成功,订单号:' . $out_trade_no . ',交易金额:' . $total_fee . ',微信支付订单号:' . $transaction_id;
} else {
echo '订单查询失败,错误代码:' . $xml->err_code . ',错误描述:' . $xml->err_code_des;
}
} else {
echo '订单查询失败,错误代码:' . $xml->return_code . ',错误描述:' . $xml->return_msg;
}
?>
以上是由福州网站建设的小编为你分享了"微信支付查询订单的php代码"文章,如果你在这方面有什么问题,随时联系我们