微信掃碼支付分為兩種模式,
模式一比較複雜,需要公眾號配置回調地址。
模式二比較簡單,只需要在代碼中配置回調地址就可以了。
我這次使用的是模式二。
需要配置參數,
const APPID = 'xxx';const MCHID = 'xxx';const KEY = 'xxx';const APPSECRET = 'xxx';
配置公眾號的appid,appsecret。以及微信支付的mchid與key。
產生二維碼,這個頁面需要自己去美化,不像支付寶那樣內建效果。
require_once "./phpcms/plugin/weixinpay/lib/WxPay.Api.php";require_once "./phpcms/plugin/weixinpay/example/WxPay.NativePay.php";require_once './phpcms/plugin/weixinpay/example/log.php';$input = new WxPayUnifiedOrder();$input->SetBody('預訂'.$product_info['name'].'訂單');$input->SetAttach('預訂'.$product_info['name'].'訂單');$input->SetOut_trade_no($order_info['orderno']);$input->SetTotal_fee($order_info['payprice'] * 100);$input->SetTime_start(date("YmdHis"));$input->SetTime_expire(date("YmdHis", time() + 600));$input->SetGoods_tag("");$input->SetNotify_url("http://www.ayuanduanzu.com/wxpay/notify.php"); // 地址是外網能訪問的,且不能包含參數$input->SetTrade_type("NATIVE");$input->SetProduct_id($product_info['id']);$notify = new NativePay();$result = $notify->GetPayUrl($input);$code_url = $result["code_url"];<img alt="掃碼支付" src="http://paysdk.weixin.qq.com/example/qrcode.php?data={urlencode($code_url)}" style="width:150px;height:150px;"/>
這裡的回調地址很有講究,掃碼支付成功後,微信會自動調用這個地址。這個地址不能包含任何參數,否則調用失敗。啥都看不到!
微信調用的時候,會傳遞xml類型的參數。
include_once "../phpcms/base.php";// 處理回調資料error_reporting(E_ERROR);require_once "../phpcms/plugin/weixinpay/lib/WxPay.Api.php";require_once '../phpcms/plugin/weixinpay/lib/WxPay.Notify.php';require_once '../phpcms/plugin/weixinpay/example/log.php';//初始化日誌$logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');$log = Log::Init($logHandler, 15);class PayNotifyCallBack extends WxPayNotify//查詢訂單public function Queryorder($transaction_id){$input = new WxPayOrderQuery();$input->SetTransaction_id($transaction_id);$result = WxPayApi::orderQuery($input);Log::DEBUG("query:" . json_encode($result));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){Log::DEBUG("call back:" . json_encode($data));$notfiyOutput = array();if(!array_key_exists("transaction_id", $data)){$msg = "輸入參數不正確";return false;}//查詢訂單,判斷訂單真實性if(!$this->Queryorder($data["transaction_id"])){$msg = "訂單查詢失敗";return false;}return true;}Log::DEBUG("begin notify");$notify = new PayNotifyCallBack();$ilog_db = pc_base::load_model('ilog_model');$order_db = pc_base::load_model('order_model');$postXml = $GLOBALS["HTTP_RAW_POST_DATA"];$postArr = xmlToArray($postXml);// 查詢是否支付成功$r = $notify->Queryorder($postArr['transaction_id']);if ($r) {// 擷取訂單資訊$order_info = $order_db->get_one(array('orderno'=>$postArr['out_trade_no']));if ($order_info['pay_status'] != '1') {$data['pay_status'] = '1';$data['pay_type'] = 'weixinpay';$data['pay_code'] = $postArr['transaction_id'];$data['paytime'] = time();$data['order_status']= 3; // 已支付$order_db->update($data,array('orderno'=>$postArr['out_trade_no']));}?>
通過
$GLOBALS["HTTP_RAW_POST_DATA"];
可以擷取微信端傳入的參數
{"appid": "wxed7996e9ad58345d","attach": "u9884u8ba2u5bbfu8fc1u00b7u592au53e4u91ccu7f8eu5f0fu5957u623fu8ba2u5355","bank_type": "CFT","cash_fee": "1","fee_type": "CNY","is_subscribe": "Y","mch_id": "1283301801","nonce_str": "20xn5e0lbk2u1u6pes2uonape2sdyfs4","openid": "oR8C7wsknwVELIRrzTlZX2eONWeY","out_trade_no": "2016091455521024608","result_code": "SUCCESS","return_code": "SUCCESS","sign": "95C2C532D095E7BF7588522C579758C4","time_end": "20160914135518","total_fee": "1","trade_type": "NATIVE","transaction_id": "4009602001201609143926590576"}
查詢是否已支付,支付完成的話,進行訂單資料處理。
這裡的一切都是非同步,二維碼頁面啥都看不到。
只能通過非同步擷取訂單狀態來判斷是否操作成功。
// 檢測是否支付成功$(document).ready(function () {setInterval("ajaxstatus()", 3000); function ajaxstatus() {var orderno = $("#out_trade_no").val();if (orderno != 0) {$.ajax({url: "?m=home&c=order&a=ajax",type: "GET",dataType:"json",data: {todo: 'ajaxCheckWxPay',orderno: orderno,},success: function (json) {if (json.status == 1) { //訂單狀態為1表示支付成功layer.msg('支付成功',{icon:1,time: 2000},function(){window.location.href = "?m=home&c=order&a=payDone&orderno="+json.info['orderno'];});// window.location.href = "wxScanSuccessUrl.action"; //頁面跳轉}}});}}
三秒執行一次,如果成功,進行跳轉處理。
贈送函數
* 作用:array轉xml*/function arrayToXml($arr)$xml = "<xml>";foreach ($arr as $key=>$val){if (is_numeric($val)){$xml.="<".$key.">".$val."</".$key.">"; }else$xml.="<".$key."><![CDATA[".$val."]]></".$key.">"; }$xml.="</xml>";return $xml; * 作用:將xml轉為array*/function xmlToArray($xml){ //將XML轉為array $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); return $array_data;}
贈送小竅門
對於非同步調用,如果看不到效果。可以建一個日誌表,把操作的資料記錄在表中。便於測試。支付回調都是非同步,可以通過日誌表中的資料來判斷是否支付成功,是否調用了回調,調用了幾次。
小結:
微信掃碼支付不如支付寶掃碼支付便捷。需要自己做很多處理。
以上所述是小編給大家介紹的PHP 微信掃碼支付接入總結(總結篇),資料庫顯示空白的完美解決方案(圖文教程),希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!