1. 程式人生 > >PHP 微信支付-借助SDK

PHP 微信支付-借助SDK

貨幣 預訂 頁面 clu viewport utf api lib 服務

記一次自己親自走通微信支付流程的過程:


首先拿到需要的公眾號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;
    }
}

PHP 微信支付-借助SDK