PHP 接入微信掃碼支付總結(jié)(總結(jié)篇)





微信掃碼支付分為兩種模式,
模式一比較復(fù)雜,需要公眾號(hào)配置回調(diào)地址。
模式二比較簡(jiǎn)單,只需要在代碼中配置回調(diào)地址就可以了。
我這次使用的是模式二。
需要配置參數(shù),
const APPID = 'xxx'; const MCHID = 'xxx'; const KEY = 'xxx'; const APPSECRET = 'xxx';
配置公眾號(hào)的appid,appsecret。以及微信支付的mchid與key。
生成二維碼,這個(gè)頁(yè)面需要自己去美化,不像支付寶那樣自帶效果。
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('預(yù)訂'.$product_info['name'].'訂單');
$input->SetAttach('預(yù)訂'.$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"); // 地址是外網(wǎng)能訪問的,且不能包含參數(shù)
$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;"/>
這里的回調(diào)地址很有講究,掃碼支付成功后,微信會(huì)自動(dòng)調(diào)用這個(gè)地址。這個(gè)地址不能包含任何參數(shù),否則調(diào)用失敗。啥都看不到!
微信調(diào)用的時(shí)候,會(huì)傳遞xml類型的參數(shù)。
include_once "../phpcms/base.php";
// 處理回調(diào)數(shù)據(jù)
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;
}
//重寫回調(diào)處理函數(shù)
public function NotifyProcess($data, &$msg)
{
Log::DEBUG("call back:" . json_encode($data));
$notfiyOutput = array();
if(!array_key_exists("transaction_id", $data)){
$msg = "輸入?yún)?shù)不正確";
return false;
}
//查詢訂單,判斷訂單真實(shí)性
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']));
}
?>
通過(guò)
$GLOBALS["HTTP_RAW_POST_DATA"];
可以獲取微信端傳入的參數(shù)
{
"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"
}
查詢是否已支付,支付完成的話,進(jìn)行訂單數(shù)據(jù)處理。
這里的一切都是異步的,二維碼頁(yè)面啥都看不到。
只能通過(guò)異步獲取訂單狀態(tài)來(lái)判斷是否操作成功。
// 檢測(cè)是否支付成功
$(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) { //訂單狀態(tài)為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"; //頁(yè)面跳轉(zhuǎn)
}
}
});
}
}
三秒執(zhí)行一次,如果成功,進(jìn)行跳轉(zhuǎn)處理。
贈(zèng)送函數(shù)
* 作用:array轉(zhuǎn)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轉(zhuǎn)為array
*/
function xmlToArray($xml)
{
//將XML轉(zhuǎn)為array
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $array_data;
}
贈(zèng)送小竅門
對(duì)于異步的調(diào)用,如果看不到效果??梢越ㄒ粋€(gè)日志表,把操作的數(shù)據(jù)記錄在表中。便于測(cè)試。支付回調(diào)都是異步的,可以通過(guò)日志表中的數(shù)據(jù)來(lái)判斷是否支付成功,是否調(diào)用了回調(diào),調(diào)用了幾次。
小結(jié):
微信掃碼支付不如支付寶掃碼支付便捷。需要自己做很多處理。
以上所述是小編給大家介紹的PHP 微信掃碼支付接入總結(jié)(總結(jié)篇),數(shù)據(jù)庫(kù)顯示空白的完美解決方案(圖文教程),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
thinkPHP框架單元測(cè)試庫(kù)tpunit用法示例
這篇文章主要介紹了thinkPHP框架單元測(cè)試庫(kù)tpunit用法,結(jié)合實(shí)例形式分析了tpunit簡(jiǎn)單使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2018-08-08
PHP加密技術(shù)的簡(jiǎn)單實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇PHP加密技術(shù)的簡(jiǎn)單實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09
laravel 框架執(zhí)行流程與原理簡(jiǎn)單分析
這篇文章主要介紹了laravel 框架執(zhí)行流程與原理,結(jié)合實(shí)例形式分析了laravel框架基本執(zhí)行流程、原理及相關(guān)操作技巧,需要的朋友可以參考下2020-02-02
Yii Framework框架使用PHPExcel組件的方法示例
這篇文章主要介紹了Yii Framework框架使用PHPExcel組件的方法,結(jié)合實(shí)例形式分析了Yii Framework框架中PHPExcel組件的下載、導(dǎo)入、調(diào)用等相關(guān)操作技巧,需要的朋友可以參考下2019-07-07

