thikphp 3.2 實現微信支付(jsapi支付)流程
這裡說一下微信支付(jsapi支付)流程 ,支付肯定是有訂單號的、訂單這一塊就不多說了。
第一步:點選去支付,首先你要知道你的訂單號是多少。拿著訂單號,我們來處理 ,上一下程式碼,具體看一下
//支付選擇
//先下載好微信支付sdk 、引入檔案 ,配置就不
use Vendor\WxMPay; //匯入 用到了名稱空間
// orderNo 是 我們需要支付的訂單號、
public function index($orderNo=''){
if(!empty($orderNo)){
$data = M("order")->where(array('E_CartOrderNo'=>$orderNo))->order('ID Desc')->find();/
if($this->getBrowser()=="wx"){
//**********************************************
$PayOrder = trim($data['E_CartOrderNo']); //這裡也就是我們的關聯訂單號
$PayMoney = floatval($data['E_OrderMoney']); //訂單的錢
$WxPMoney = $PayMoney * 100; // 支付的錢
$order_no = time().rand(100,999).$this->Uid; //生成支付編號
if(empty($PayPoint)) $PayPoint = $WxPMoney;
$_SESSION["GoWxWebPay_pWxName"] = 'payByWXMp';
vendor("WxMPay.WxPay#Api"); //將下載好的sdk 放入到 Vendor/WxMPay/ 路徑
vendor("WxMPay.WxPay#JsApiPay"); //
//①、獲取使用者openid
$tools = new \Vendor\WxMPay\JsApiPay();
$openId = $tools->GetOpenid();
//②、統一下單
$input = new \Vendor\WxMPay\WxPayUnifiedOrder();
$input->SetBody("微信支付");
$input->SetAttach("微信支付");
$input->SetOut_trade_no($order_no);
$input->SetTotal_fee("1");
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag("微信支付");
$input->SetNotify_url($this->HTTP_HOST . "/rech_notify.php"); // http//:××.com/rech_notify.php
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$WxPayApi = new \Vendor\WxMPay\WxPayApi();
$order = $WxPayApi->unifiedOrder($input);
$order = $WxPayApi->unifiedOrder($input);
if($order["result_code"]=="SUCCESS" && $order["return_code"]=="SUCCESS" &&
!empty($order["prepay_id"])){
$jsApiParameters = $tools->GetJsApiParameters($order);
$this->assign('jsApiParameters',($jsApiParameters));
//********************* 我們將請求的加入到資料庫
$m_recharge['E_CreateDate'] = time();
$m_recharge['E_UpdateDate'] = time();
$m_recharge['E_MemberID'] = $this->Uid;
$m_recharge['E_Meney'] = $PayMoney;
$m_recharge['E_Points'] = $PayPoint;
$m_recharge['E_PayType'] = "JSAPI";
$m_recharge['E_PayFlag'] = "PAY";
$m_recharge['E_OrderNo'] = $order_no;
$m_recharge['E_RelationOrder'] = $PayOrder;
$m_recharge['E_Message'] = json_encode($order);
M('member_recharge')->add($m_recharge);
$_SESSION["GoWxWebPay_Order_No"] = $PayOrder; // session 儲存訂單、下面檢測要用到
}
}
}
$this->assign('data',$data);
$this->display();
}
//前端html
// <div id="submit_order">支付</div>
<div>{$data.E_OrderMoney}</div>
$("#submit_order").click(function(){
if (typeof WeixinJSBridge == "undefined"){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
}else if (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
}
}else{
onBridgeReady();
checkWxIMGPay(); //檢測是否支付成功、呼叫函式判斷支付成功。
}
})
function onBridgeReady(){
WeixinJSBridge.invoke(
'getBrandWCPayRequest', {$jsApiParameters},
function(res){
if(res.err_msg == "get_brand_wcpay_request:ok" ) {} // 使用以上方式判斷前端返回,微信團隊鄭重提示:res.err_msg將在使用者支付成功後返回 ok,但並不保證它絕對可靠。
}
);
}
//檢測是否支付成功、
function checkWxIMGPay(){
var PayOrder = '{$data.E_CartOrderNo}';
var nows_times = (new Date().getTime()) / 1000;
$.get("{:U('Pay/CHECK_WXPAY_FLAGS')}?PayOrder="+PayOrder+"&rtimes="+ nows_times ,function(dataObj){
if(dataObj.status==1){
//alert('跳到支付成功頁面');
window.location.href="{:U('Order/index')}"; //跳到 訂單頁面
}else{
setTimeout(function (){checkWxIMGPay();},3000); ///檢測一次
}
}, "json");
}
//最好就是簡單粗暴的[http://serverName/xxx.php],我在跟目錄下,類似於index.php,重新寫了一個專門的供支付回撥的入口檔案payment.php,
和它對應的Application/目錄下的模組(rech_notify)、控制器(Index)及方法(index):
define('BIND_MODULE','rech_notify');
define('DIR_ROOT', __DIR__);
define("Upload_PATH", "./public/File/");
define('APP_DEBUG',true);
require './ThinkPHP/ThinkPHP.php';
//define('BIND_MODULE','rech_notify'); 預設是 rech_notify 模組 Index控制器中的index方法
//回撥
public function index(){
$str_Post = $GLOBALS["HTTP_RAW_POST_DATA"];
vendor("WxMPay.WxPay#Api");
vendor("WxMPay.WxPay#Notify");
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($str_Post, 'SimpleXMLElement', LIBXML_NOCDATA);
$postObj = json_encode($postObj);
$postObj = json_decode($postObj,true);
if ($postObj["result_code"] == 'SUCCESS' && $postObj["return_code"]=='SUCCESS'){
$out_trade_no = $postObj["out_trade_no"];
$sWhere["E_OrderNo"] = $out_trade_no; //剛才生成的支付號
$recharge = M('member_recharge');
$info = $recharge->where($sWhere)->find();
$sUpd = $recharge->where($sWhere)->save(array("E_State"=>1,"E_UpdateDate"=>time()));//更新為支付完成狀態
/*********** 這裡就是修改訂單狀態了。。。。
}
}
//**PHP 檢測當前訂單是否支付成功
public function CHECK_WXPAY_FLAGS($PayOrder=""){
$WxPayOrderNo = $_SESSION["GoWxWebPay_Order_No"];
if(!empty($WxPayOrderNo)){
if($PayOrder == $WxPayOrderNo){
$info = M('order')->where(array("E_CartOrderNo"=>$PayOrder))->find();
if(intval($info["E_State"])==3){
$this->jsonReturn(1, "訂單支付成功", '');
}else{
$this->jsonReturn(0, "訂單已下單、支付中", '');
}
}
$this->jsonReturn(0, "支付異常、支付失敗", '');
}
$this->jsonReturn(0, "支付異常、支付失敗", '');
}