前言:由於沒有商戶相關的id信息,只能測試這個掃碼的功能,但是這個也是我們日常常用到的功能
(一) 前期准備
從微信官方網站下載最新的支付的接口
1.首先在:https://pay.weixin.qq.com/wiki/doc/api/native_sl.php?chapter=11_1 平台下載第三方接口文件(我下載的是PHP)
2.把項目放到入口文件中去查看效果(如下)
3.點擊掃碼支付(注意路徑問題)
這是模式二無法顯示圖片,但是其實也是不影響我們項目的運行的
(二) 集成到我的項目中去
1.運行會報兩個錯誤
WxPay.NativePay.php文件報第9行和11行的錯誤 解決方法是注釋第九行文件內容
WxPay.Config.php文件報第9行錯誤 解決方法是注釋第九行文件內容
//到了這里此時訂單的相關信息已經完成了 <?php
$notify = new \NativePay();
$input = new \WxPayUnifiedOrder();
$input->SetBody($order->order_name);//商品描述
$input->SetAttach($order->order_name);//附加數據
$input->SetOut_trade_no($order->order_number);
$input->SetTotal_fee($order->price * 100);//微信的單位是分主要為浮點數精度不丟失到了微信那里會轉換
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag("test");
$input->SetNotify_url("http://paysdk.weixin.qq.com/notify.php");
$input->SetTrade_type("NATIVE");
$input->SetProduct_id("123456789");
$result = $notify->GetPayUrl($input);
$url2 = $result["code_url"];
//下面主要是顯示二維碼
require_once '../example/phpqrcode/phpqrcode.php';//引入二維碼所在的文件位置
if(substr($url,0,6) == 'weixin'){
ob_end_clean();
header('Content-Type:image/png');
\QRcode::png($url);
}else{
header('HTTP/1.1 404 Not Found');
}
<?php //通過彈出層顯示二維碼,這個操作主要是用戶點擊了微信支付按鈕彈出支付的二維碼 public function getqrcode($url2){ $url = base64_decode($url2); require_once '../wxpay/example/phpqrcode/phpqrcode.php'; if(substr($url,0,6) == 'weixin'){ ob_end_clean(); header('Content-Type:image/png'); \QRcode::png($url); }else{ header('HTTP/1.1 404 Not Found'); } }
<?php //怎樣知道用戶是否支付了,主要是在前端彈出用一個定時器不斷請求后台的查詢得到的結果 public function wxquery(Order $order){ require_once "../wxpay/lib/WxPay.Api.php"; require_once "../wxpay/example/WxPay.Config.php"; //查詢訂單是否支付了 $out_trade_no = $order->order_number;//拿到用戶支付的訂單號 $input = new \WxPayOrderQuery(); $input->SetOut_trade_no($out_trade_no);//把用戶訂單放到訂單中查詢 $config = new \WxPayConfig(); $res = \WxPayApi::orderQuery($config,$input);//查詢訂單支付情況 if($res['result_code'] =='SUCCESS' && $res['trade_state'] == 'SUCCESS' && $res['trade_state_desc'] == '支付成功')//判斷是否成功return ['status'=> true,'message'=>'支付成功']; } return ['status'=> false,'message'=>'服務器繁忙請稍后-----']; }