微信公众号支付一个大坑

微信公众号支付一个大坑_第1张图片

具体的实现逻辑:
前端:

<script type="text/javascript">
wx.config({
    debug:     true,
    appId:     "{$res['appid']}",
    timestamp: "{$res['timestamp']}",//生成签名的时间戳
    nonceStr:  "{$res['nonceStr']}",//生成签名的随机串
    signature: "{$res['signature']}",//签名


    jsApiList: [
        // 所有要调用的 API 都要加到这个列表中
        'checkJsApi',
        'chooseWXPay',  
    ]
});


$('button').on('click', function() {

    $.ajaxSetup({
        contentType: "application/x-www-form-urlencoded; charset=utf-8"
    });

    $.ajax({
        type:"post",
        url:"https://www.coucouchina.com/Home/WeixinPay/unionWeixinPay",
        dataType:'json',
        async:true,
        // data: {
        //  openid: "{$openid}",
        // },
        success:function(e){
            if (e.code == 1) {
                data = e.data;

                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{
                   WeixinJSBridge.invoke(
                       'getBrandWCPayRequest', data,
                       function(res){
                           if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                                alert('支付成功')
                           }     // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回    ok,但并不保证它绝对可靠。 
                       }
                   ); 
                }

            }
        },
        error:function(e) {
            alert('错误')
        }
    })
})
script>

后端签名的生成:

 /**
     * 生成公众号支付支付参数
     * @Author    刘博
     * @DateTime  2018-04-11
     * @copyright 凑凑
     * @params
     * @param     [type]     $prepayid [description]
     * @return    [type]               [description]
     */
    public function getWapAppPayParams( $prepayid ) {


        $data['appId'] = $this->appid;
        $data['package'] = "prepay_id=$prepayid";
        $data['nonceStr'] = $this->getRandomString();
        $data['timeStamp'] = strval( time() );
        $data['signType'] = "MD5";
        $data['paySign'] = $this->MakeSign( $data );

        return $data;
    }

你可能感兴趣的:(PHP,ThinkPHP,Web)