微信支付

在微信公众号中调用微信支付所选的是jspai支付方式
首先是公众号的配置
其次微信支付平台配置

在api安全中心下载安装操作证书,设置API密钥微信支付_第1张图片
到产品中心-开发配置 配置JSAPI的支付目录
比如
http://www.xxx.com/weixin/Order/pay/id/23
则配置时写
http://www.xxx.com/weixin/Order/pay/id/
比如
http://www.xxx.com/wxpay/order?id=56
则配置时写
http://www.xxx.com/wxpay/order/
总之要具体到请求方法,如有参数要加上参数名参数名后以"/"结束

最后配置公众号的appid,在微信支付平台中关联
微信支付_第2张图片

大概流程就是点击支付按钮调用后台方法,获取参数后返回给前端,然后发起支付

//获取参数
 private function payRoutine($openid,$payprice,$orderno){
        $userip = self::get_client_ip();
        $appid  = WXOPEN_APPID;                  //应用 APPID
        $mch_id = MCHID;                  //微信支付商户号
        $key    = MCHKEY;                 //微信商户 API 密钥
        $nonce_str = self::nonce_str();//随机字符串
        $body = "商品购买";//内容
        $total_fee = $payprice*100;//金额微信是以分为单位的所以乘100
        $notify_url = "http://fatest.bangjia.me/weixin/Order/extension"; //回调地址
        $trade_type = 'JSAPI';//交易类型 默认
        $post=array();
        $post['appid'] = $appid;
        $post['attach'] = '123131';
        $post['body'] = $body;
        $post['mch_id'] = $mch_id;
        $post['nonce_str'] = $nonce_str;//随机字符串
        $post['notify_url'] = $notify_url;
        $post['openid'] = $openid;
        $post['out_trade_no'] = $orderno;
        $post['spbill_create_ip'] = $userip;//终端的ip
        $post['total_fee'] = $total_fee;//总金额
        $post['trade_type'] = $trade_type;
        $sign = self::sign($post,$key);//签名
        $post_xml = '
            '.$appid.'
            '.$post['attach'].'
            '.$body.'
            '.$mch_id.'
            '.$nonce_str.'
            '.$notify_url.'
            '.$post['openid'].'
            '.$post['out_trade_no'].'
            '.$userip.'
            '.$total_fee.'
            '.$trade_type.'
            '.$sign.'
        ';
        //统一接口
        $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
        $xml = self::http_request($url,$post_xml);
        $array = self::xml_CDATA($xml);

        if ($array['return_code'] == 'SUCCESS' && $array['result_code'] == 'SUCCESS'){
            $time = time();
            $tmp = array();//临时数组用于签名
            $tmp['appId'] = $appid;//公众号id
            $tmp['nonceStr'] = $nonce_str;//随机字符串
            $tmp['package'] = 'prepay_id='.$array['prepay_id'];//订单详情扩展字段
            $tmp['signType'] = 'MD5';//签名方式,
            $tmp['timeStamp'] = "$time"; //时间戳

            $data['state'] = 1;
            $data['timeStamp'] = "$time";//时间戳
            $data['nonceStr'] = $nonce_str;//随机字符串
            $data['signType'] = 'MD5';//签名算法,暂支持 MD5
            $data['package'] = 'prepay_id='.$array['prepay_id'];//统一下单接口返回的 prepay_id 参数值,提交格式如:prepay_id=*
            $data['paySign'] = self::sign($tmp,$key);//签名,具体签名方案参见微信公众号支付帮助文档;
            $data['out_trade_no'] = time().rand(1,999);
            $data['appId'] = $appid;
            $data['wxorder'] = $array['prepay_id'];
        }else{
            $data['state'] = 0;
            $data['text'] = $array['err_code_des'];
            $data["err_code_des"] = $array['err_code_des'];
            $data["err_code"] = $array['err_code'];
            $data['return_code'] = $array['return_code'];
            $data['return_msg'] = $array['return_msg'];
        }
        $this->ajaxReturn($data);
    }

前端调用方法

$('.ok').click(function () {
        var payid = $('#payid').val();
        $.ajax({
            type:'POST',
            url:'/weixin/order/dopay',
            data:{
                'payid':payid,
            },
            dataType:'json',
            success:function (info) {
                WeixinJSBridge.invoke(
                    'getBrandWCPayRequest',{
                        "appId": info['appId'],     //公众号名称,由商户传入
                        "timeStamp": info['timeStamp'],         //时间戳,自1970年以来的秒数
                        "nonceStr": info['nonceStr'], //随机串
                        "package": info['package'],
                        "signType": info['signType'],         //微信签名方式:
                        "paySign": info['paySign'] ,//微信签名
                        "productId":123
                    },
                    function(res){
                        if(res.err_msg == "get_brand_wcpay_request:ok"){
                            alert("支付成功!");
                            addwxorderno(info['wxorder'],payid);
                            window.location.href="/weixin/Order/index";
                        }else if(res.err_msg == "get_brand_wcpay_request:cancel"){
                            alert("取消支付!");
                            window.location.href="/weixin/Order/index";
                        }else{
                            alert("支付失败!");
                            window.location.href="/weixin/Order/index";
                        }
                    }
                );
            }
        })
    });

你可能感兴趣的:(微信)