福州網(wǎng)站建設(shè)>網(wǎng)站新聞>php技術(shù)

        微信支付查詢訂單的php代碼

        發(fā)布日期:2023-04-08瀏覽次數(shù):521 來源:福州網(wǎng)站建設(shè)

        下面代碼中,我們首先構(gòu)造了微信支付查詢訂單API接口的請(qǐng)求參數(shù),包括商戶號(hào)、API密鑰、訂單號(hào)等信息。然后,我們根據(jù)微信支付的簽名算法生成了簽名,并將簽名添加到請(qǐng)求參數(shù)中。最后,我們使用PHP的file_get_contents函數(shù)向微信支付查詢訂單API接口發(fā)送了POST請(qǐng)求,并解析了響應(yīng)結(jié)果,輸出了查詢結(jié)果。

        需要注意的是,上述代碼中的your_mch_id、your_api_key、your_out_trade_no和your_appid需要替換為你自己的微信支付分配的商戶號(hào)、API密鑰

        <?php
        
        // 微信支付查詢訂單API接口
        $url = 'https://api.mch.weixin.qq.com/pay/orderquery';
        
        // 微信支付分配的商戶號(hào)
        $mch_id = 'your_mch_id';
        
        // 微信支付分配的API密鑰
        $api_key = 'your_api_key';
        
        // 訂單號(hào)
        $out_trade_no = 'your_out_trade_no';
        
        // 構(gòu)造請(qǐng)求參數(shù)
        $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));
        
        // 添加簽名到請(qǐng)求參數(shù)中
        $params['sign'] = $sign;
        
        // 發(fā)送請(qǐng)求
        $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);
        
        // 解析響應(yīng)結(jié)果
        $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 '訂單查詢成功,訂單號(hào):' . $out_trade_no . ',交易金額:' . $total_fee . ',微信支付訂單號(hào):' . $transaction_id;
            } else {
                echo '訂單查詢失敗,錯(cuò)誤代碼:' . $xml->err_code . ',錯(cuò)誤描述:' . $xml->err_code_des;
            }
        } else {
            echo '訂單查詢失敗,錯(cuò)誤代碼:' . $xml->return_code . ',錯(cuò)誤描述:' . $xml->return_msg;
        }
        
        ?>


        以上是由福州網(wǎng)站建設(shè)的小編為你分享了"微信支付查詢訂單的php代碼"文章,如果你在這方面有什么問題,隨時(shí)聯(lián)系我們

        php技術(shù)有關(guān)的文章
        如果您有什么問題,歡迎咨詢我們客服! 點(diǎn)擊QQ咨詢