微信支付(公众号支付)

开发文档
https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_1

1.根据相关信息, 通过微信接口 https://api.mch.weixin.qq.com/pay/unifiedorder 获取“预支付id”

String xml = 
                "<xml>" 
                + "<appid>" + appid + "</appid>" 
                + "<mch_id>" + mch_id + "</mch_id>" 
                + "<nonce_str>" + nonce_str + "</nonce_str>" 
                + "<sign>" + sign + "</sign>"
                + "<body><![CDATA[" + body + "]]></body>" 
                + "<out_trade_no>" + orderNo + "</out_trade_no>" 
//              + "<attach>" + attach + "</attach>"
                + "<total_fee>" + 1 + "</total_fee>"
                + "<spbill_create_ip>" + "127.0.0.1" + "</spbill_create_ip>" 
                + "<notify_url>" + notifyurl + "</notify_url>"
                + "<trade_type>" + trade_type + "</trade_type>" 
                + "<openid>"+openid+"</openid>"
                + "</xml>";
  1. 获取后,对信息进行签名 md5,并返回数据
SortedMap<String, String> finalpackage = new TreeMap<String, String>();
            String packages = "prepay_id="+prepay_id;
            finalpackage.put("appId", appid);  
            finalpackage.put("timeStamp", timestamp);  
            finalpackage.put("nonceStr", nonce_str);  
            finalpackage.put("package", packages);  
            finalpackage.put("signType", "MD5");
            //md5 签名
            String finalsign = reqHandler.createSign(finalpackage);
            String finaPackage = 
                "\"appId\":\"" + appid
                + "\",\"timeStamp\":\"" + timestamp
                + "\",\"nonceStr\":\""  + nonce_str 
                + "\",\"package\":\""   + packages 
                + "\",\"signType\" : \"MD5" 
                + "\",\"paySign\":\""   + finalsign + "\"";
  1. 页面发起支付
    $(document).ready( function(){
        //点击测试,注意参数是demo中生成的package
        $('#surepay').click(function(){
              WeixinJSBridge.invoke('getBrandWCPayRequest',{
                      ${finaPackage}

             },function(res){
                   //alert(res.err_code + res.err_desc + res.err_msg);
                   //支付成功或失败前台判断
                   if(res.err_msg=='get_brand_wcpay_request:ok'){
                        //支付成功,直接跳转界面
                        alert("支付成功!");  
                      // window.location.href="/user?backurl="+window.location.href; 
                      // window.parent.location='/user';
                      // window.location.href='/user'; 
                        // window.history.back(-1);
                        window.history.go(-2); 
                       // window.navigate("/user"); 
                       // top.location="/user";
                       // self.location='/user';
                   }else if(res.err_msg == "get_brand_wcpay_request:cancel"){  
                        //用户取消支付!
                        alert("支付失败!");  
                   }else{  
                        alert("支付失败!");  
                   }  

             })
        });
  });

你可能感兴趣的:(微信支付(公众号支付))