微信 JSAPI 支付 微信内部起调H5支付 支付完成跳转公众号并发送公众号消息模板

微信 JSAPI 支付 微信内部起调H5支付 支付完成跳转公众号并发送公众号消息模板

JSAPI支付 openID是必传的,所以这里先获取用户openID,想要获取openID,首先我们要拿到code,再获取openID

JS:

<script>
    // 强制关注公众号,获取openid
    getCode = function () {
   
        var code = getUrlParam('code'); // 截取路径中的code,如果没有就去微信授权,如果已经获取到了就直接传code给后台获取openId
        var local = window.location.href;
        var APPID = 'xxxxxxxxxx';
        if (code == null || code === '') {
   
            window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + APPID + '&redirect_uri=' + encodeURIComponent(local) + '&response_type=code&scope=snsapi_base&state=#wechat_redirect'
        } else {
   
            getOpenId(code) //把code传给后台获取用户信息
        }
    };

    //获取地址栏的参数
    getUrlParam= function (name) {
   
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    };
    //页面执行调用
    getCode();
</script>

页面加载 执行 getCode方法,获取URL中的code,如果没有code,起调公众号获取code接口,然后会跳转到 redirect_uri 这个参数对应的URL路径,这个URL后面会自动拼接出请求的code,这里的 redirect_uri 还是本网页链接地址,这样再次执行 getCode方法的时候就能获取到URL链接后面的 code 。

然后根据 code ajax请求后端 起调公众号获取网页授权access_token接口 ,这个接口会返回 openID 和 access_token

JS:

	//把code传给后台,得到openid
    getOpenId = function (code) {
   
        $.ajax({
   
            type: 'POST',
            dataType: 'json',
            url: "{:U('getOpenid')}",
            data: {
    code: code },
            success: function (res) {
   
                if (res.status == -1) {
   
                    alert(result.msg);
                    return false;
                } else {
   
                	// 起调统一下单接口
                    unifiedorder(res.data.openid);
                }
            }
        });
    };

后端接口PHP :

function getOpenid(){
   
        $code = I('post.code');
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=xxxxxxxxx&secret=xxxxxxxxxxxxxxxx&code=$code&grant_type=authorization_code";
        $data = file_get_contents($url);
        if($data){
   
            $this->ajaxReturn(array('data'=>json_decode($data,true),'msg'=>'获取成功','status'=>1));
        }else{
   
            $this->ajaxReturn(array('data'=>'','msg'=>'获取失败','status'=>0));
        }
    }

拿到 openID 以后传给后端,起调JSAPI支付统一下单接口,拿到参数 prepay_id

JS:

 	// 起调统一下单接口
    unifiedorder = function(openid){
   
        $.ajax({
   
            type: 'POST',
            dataType: 'json',
            url: "{:U('index')}",
            data: {
    openid: openid },
            success: function (result) {
   
                if (result.status == 0) {
   
                    alert(result.msg);
                    return false;
                } else {
   
                    h5();
                }
            }
        });
    };

PHP:

public function index()
    {
   
        if(IS_POST){
   
            $time = time();
            $key = "支付密钥";
            $post['appid'] = "xxxxxxxxxxxx";
            $post['body'] = '商品购买';
            $post['mch_id'] = "商户ID";
            $post['nonce_str'] = $this->randomkeys(32);      //随机字符串
            $post['notify_url'] = "https://".$_SERVER['HTTP_HOST'] . '/index.php/Api/Native/notify_url';  //支付完成回调地址url,不能带参数;
            $post['openid'] = I('post.openid');  // openID
            $post['out_trade_no'] = $time.rand(10000,99999);  // 订单号
            $post['spbill_create_ip'] = $_SERVER['SERVER_ADDR'];    //服务器终端的ip
            $post['total_fee'] = 1;                        //总金额 最低为一分钱 必须是整数
            $post['trade_type'] = 'JSAPI';                          //交易类型 默认JSAPI
            $sign = $this->MakeSign($post, $key);                   //签名
            $res['key'] = $key;
            $post['sign'] = $sign;
            $post_xml = $this->arrToXml($post);
            //统一下单接口prepay_id
            $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
            $xml = $this->http_request($url, $post_xml);            //POST方式请求http
            $array = $this->xml_to_array($xml);                     //将【统一下单】api返回xml数据转换成数组,全要大写
            if ($array['return_code'] == "SUCCESS" && $array['result_code'] == "SUCCESS") {
   
                $sign_arr['appId'] = $array['appid'];
                $sign_arr['nonceStr'] = $this->randomkeys(32);      //随机字符串
                $sign_arr['package'] = "prepay_id=".$array['prepay_id'];
                $sign_arr['signType'] = "MD5";
                $sign_arr['timeStamp'] = $time;
                $newSign = $this->MakeSign($sign_arr, $key);                   //签名
                $result['data'] = $sign_arr;
                $result['newSign'] = $newSign;
                $result['status'] = 1;
                $result['msg'] = '统一下单成功';
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值