結合設計經驗與營銷實踐,提供有價值的互聯網資訊
發布日期:2023-04-08瀏覽次數:557 來源:福州網站建設
下面代碼中,我們首先構造了微信支付查詢訂單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代碼"文章,如果你在這方面有什么問題,隨時聯系我們