微信支付的notify.php中如何獲取訂單號(php版)


不要直接使用demo中的notify.php,重寫notify.php,繼承WxPayNotify(可參考微信api),具體如下:

require_once "WxPay.Api.php";

require_once 'WxPay.Notify.php';

class PayNotifyCallBack extends WxPayNotify

{

//查詢訂單

public function Queryorder($transaction_id)

{

$input = new WxPayOrderQuery();

$input->SetTransaction_id($transaction_id);

$result = WxPayApi::orderQuery($input);

if (array_key_exists("return_code", $result)

&& array_key_exists("result_code", $result)

&& $result["return_code"] == "SUCCESS"

&& $result["result_code"] == "SUCCESS"

) {

return true;

}

return false;

}



//重寫回調處理函數

public function NotifyProcess($data, &$msg)

{ 

$notfiyOutput = array();



if (!array_key_exists("transaction_id", $data)) {

$msg = "輸入參數不正確";

return false;

}

//查詢訂單,判斷訂單真實性

if (!$this->Queryorder($data["transaction_id"])) {

$msg = "訂單查詢失敗";

return false;

}



//$data中各個字段在return_code為SUCCESS的時候有返回 SUCCESS/FAIL

//此字段是通信標識,非交易標識,交易是否成功需要查看result_code來判斷

//成功后寫入自己的數據庫

if ($data['return_code'] == 'SUCCESS' && $data['result_code'] == 'SUCCESS') {

//自己的業務邏輯 

$out_trade_no = $data['out_trade_no']; 

$transaction_id = $data['transaction_id'];

$bank_type = $data['bank_type'];

$fee_type = $data['fee_type'];

$time_end = $data['time_end'];

$amount = $data['total_fee']; 

} 

return true;

}

}

$notify = new PayNotifyCallBack();

$notify->Handle(false);

  自己的代碼改寫:將微信支付成功后,獲取他的微信生成的訂單號 trade_no,然后保存到數據庫中,為以后退款做准備,微信退款就是根劇這個訂單號來退款

不要直接使用demo中的notify.php,重寫回調頁面notify.php,繼承WxPayNotify(可參考微信api),具體如下:然后用它自帶的方法查詢出訂單跟訂單號保存到數據庫中去

require_once dirname(dirname(__FILE__)) . "/lib/WxPay.Api.php";
require_once dirname(dirname(__FILE__)) . '/lib/WxPay.Notify.php';

/**
 * 微信支付回調類
 * Class notify
 */
class notify extends WxPayNotify
{
    /**
     * 重寫父類異步驗證
     * @param array $data
     * @param string $msg
     * @return bool
     */
    public function NotifyProcess($data, &$msg)
    {
        $bool = false;
        //返回狀態碼、業務結果
        if (array_key_exists("return_code", $data) && array_key_exists("result_code", $data) && $data['return_code'] == 'SUCCESS' && $data['result_code'] == 'SUCCESS')
        {
            //查詢訂單
            if (isset($data["out_trade_no"]) && $data["out_trade_no"] != "")
            {
                $input = new WxPayOrderQuery();
                $input->SetOut_trade_no($data["out_trade_no"]);//商戶訂單號
                $result = WxPayApi::orderQuery($input);//訂單查詢
                $tip = '信息:微信公眾號交易,訂單金額與實際支付不一致';
                //這里針對微信訂單號作特殊處理,去掉后面的16位字符
                $ordersn = substr($data['out_trade_no'],0,strlen($data['out_trade_no'])-16);
                if (isset($result['total_fee']) && Common::total_fee_confirm($ordersn, $result['total_fee'] / 100, $tip))
                {
                    $bool = true;
                    $method = Common::C('mobile');

                    Common::pay_success($ordersn, $method['method']['8']['name']);
                    $online_transaction_no = array('source'=>'wxpay','transaction_no'=>$data['transaction_id']);
                    //寫入微信訂單號
                    DB::update('member_order')->set(array('online_transaction_no'=>json_encode($online_transaction_no)))
                        ->where('ordersn','=',$ordersn)
                        ->execute();

                }
            }
            else
            {
                new Pay_Exception("信息:微信公眾號下單,未會返回商品訂單號");
            }
        }
        else
        {
            new Pay_Exception("信息:微信公眾號交易錯誤(msg_{$data['return_msg']})");
        }
        return $bool;
    }
}

  

 

支付地址
payment+application-classes-pay-mobile-wxpay.php           submit
回調地址:獲取微信生成的訂單號
payment-application-vendor-mobile-wzpay-jsapi-notify.php
引入的類文件在vendor-mobile-wxpay-lib里面

微信支付寶退款操作方法:
tools-classes-pay-online-refund.php

鏈接: https://pan.baidu.com/s/18uSJf8fkDPo41zn-m74ObQ 密碼: 18cg


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM