PHP 微信支付-藉助SDK
阿新 • • 發佈:2018-12-27
記一次自己親自走通微信支付流程的過程:
首先拿到需要的公眾號Appid、AppSecret、商戶id、支付祕鑰
然後到微信支付的開發文件中下載 需要的SDK版本,第一次是全部使用SDK只是放到伺服器上走了一遍。
然後自己動手寫了一遍
前端頁面:
<?php header("Content-type:text/html;charset=utf-8"); include './lib/WxPay.Api.php'; include './wxConfig.php'; include './wxJsapiPay.php'; $jsapi = new wxJsapiPay(); $preOrder = $jsapi->makeWxPrepareOrder(); var_dump($preOrder); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"/> <title>微信支付</title> <style type="text/css"> .pay-btn{ display: block; width: 200px; height: 32px; color:#FFF; text-align: center; background-color: #09BB07; border-radius: 0.2rem; line-height: 32px; margin:0 auto; margin-top: 10%; } </style> </head> <body> <span class="pay-btn" onclick="wxpay()">微信支付</span> <script type="text/javascript"> window.onload = function(){ if (typeof WeixinJSBridge == "undefined"){ if( document.addEventListener ){ document.addEventListener('WeixinJSBridgeReady', editAddress, false); }else if (document.attachEvent){ document.attachEvent('WeixinJSBridgeReady', editAddress); document.attachEvent('onWeixinJSBridgeReady', editAddress); } }else{ editAddress(); } }; function wxpay() { if (typeof WeixinJSBridge == "undefined"){ if( document.addEventListener ){ document.addEventListener('WeixinJSBridgeReady', jsApiCall, false); }else if (document.attachEvent){ document.attachEvent('WeixinJSBridgeReady', jsApiCall); document.attachEvent('onWeixinJSBridgeReady', jsApiCall); } }else{ jsApiCall(); } } //呼叫微信JS api 支付 function jsApiCall() { WeixinJSBridge.invoke( 'getBrandWCPayRequest', { "appId":"<?php echo $preOrder['appId']; ?>", //公眾號名稱,由商戶傳入 "timeStamp":"<?php echo $preOrder['timeStamp']; ?>", //時間戳,自1970年以來的秒數 "nonceStr":"<?php echo $preOrder['nonceStr']; ?>", //隨機串 "package":"<?php echo $preOrder['package']; ?>", "signType":"<?php echo $preOrder['signType']; ?>", //微信簽名方式: "paySign":"<?php echo $preOrder['paySign']; ?>" //微信簽名 }, function(res){ WeixinJSBridge.log(res.err_msg); alert(res.err_code+res.err_desc+res.err_msg); // alert(JSON.stringify(res)); //檢視錯誤訊息 } ); } </script> </body> </html>
php頁面:
include_once './lib/WxPay.Api.php'; include_once './wxConfig.php'; class wxJsapiPay { /** * 建立微信支付預訂單資料 */ public function makeWxPrepareOrder(){ // $openid = $this->GetOpenid(); $wxOrderData = new WxPayUnifiedOrder(); $wxOrderData->SetOut_trade_no('20180806125346'); //設定訂單號 $wxOrderData->SetTrade_type('JSAPI'); //微信支付方式 $wxOrderData->SetBody('我的微信支付'); //商品描述 $wxOrderData->SetFee_type('CNY'); //貨幣種類 CNY人民幣 $wxOrderData->SetTotal_fee(1); //設定訂單總價 $wxOrderData->SetOpenid('o75920o4fPcP74pTWI8MfDyYsB_A'); //設定使用者openid $wxOrderData->SetNotify_url('http://paysdk.weixin.qq.com/notify.php'); //設定微信支付回掉地址 $wxPayConfig = new wxConfig(); $wxOrder = WxPayApi::unifiedOrder($wxPayConfig,$wxOrderData); $jsApiParameters = $this->GetJsApiParameters($wxOrder); $jsApiParameters = json_decode($jsApiParameters,TRUE); return $jsApiParameters; } }