微信JSSDK支付

 1 var appId,timeStamp,nonceStr,package,signType,paySign; 
 2 function goumai(){
 3     $.confirm({
 4       title: '确认购买',
 5       text: '<div class="comfirm-tip">确认购买?div>',
 6       onOK: function () {
 7         $('#rechargeHalf').removeClass('weui-popup__container--visible');
 8          $('#rechargeHalf').hide();
 9          var price = ($("#monid").text() * 100).toFixed(0);
10          mui.ajax(wxshop.serverUrl+"/wxUserCenter/gotozhifu",{
11            type:'post',
12            data:{
13               code:wxshop.getUrlParam("code"),
14               price:price,
15             },
16             dataType:'json',
17             success:function(data){
18               if(data.ret == 1){
19                  appId=data.payMap.appId;
20                   timeStamp=data.payMap.timeStamp;
21                   nonceStr=data.payMap.nonceStr;
22                   package=data.payMap.package;
23                   signType=data.payMap.signType;
24                   paySign=data.payMap.paySign;
25                   callpay();
26               }
27               if(data.ret == 2){
28                 wxshop.getOpenId(data.appid);
29                  return;
30               }
31               if(data.ret == 0){
32                 alert(data.msg);
33               }
34           }
35       });
36     },
37        onCancel: function () {
38          $('#rechargeHalf').removeClass('weui-popup__container--visible');
39           $('#rechargeHalf').hide();
40        }
41      });
42    function callpay(){
43        if (typeof WeixinJSBridge == "undefined"){
44           if( document.addEventListener ){
45              document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
46            }else if (document.attachEvent){
47              document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
48               document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
49            }
50         }else{
51            onBridgeReady();
52         }
53  }
54  function onBridgeReady(){
55     WeixinJSBridge.invoke( 'getBrandWCPayRequest', {
56        "appId":appId,     //公众号名称,由商户传入     
57         "timeStamp":timeStamp,         //时间戳,自1970年以来的秒数     
58         "nonceStr":nonceStr, //随机串     
59         "package":package,     
60         "signType":signType,         //微信签名方式:     
61         "paySign":paySign //微信签名 
62    }, 
63       function(res){     
64         if(res.err_msg == "get_brand_wcpay_request:ok" ) {
65            console.log('支付成功');                
66          }else if(res.err_msg == "get_brand_wcpay_request:cancel"){
67            console.log('支付取消');
68          }else if(res.err_msg == "get_brand_wcpay_request:fail"){
69            console.log('支付失败');
70             WeixinJSBridge.call('closeWindow');
71          }
72    });   
73  }
74}    
 1 public class PayUtil {
 2 
 3     /**
 4        * 微信jssdk支付
 5        * @param request
 6        * @param response
 7        * @param fee
 8        * @return
 9     */ 
10     public static Map pay(HttpServletRequest request, HttpServletResponse response, String fee) {
11       Map preData = new HashMap();
12       Map payMap = new HashMap();
13       try {
14          String openid = null;
15          WsMember member = WsUtils.getMember(request, response);
16           if (member != null) {
17             openid = member.getOpenId();
18           }
19           WxAccount account = WsUtils.getAccount();
20          preData.put("appid", account.getAccountAppid());
21           preData.put("mch_id", account.getMchId());
22           preData.put("nonce_str", WXPayUtil.generateNonceStr());
23           String body = utf8Utils.toUTF8("微信支付");
24           preData.put("body", body);
25           Random random = new Random();
26           String out_trade_no = "";
27           for (int i = 0; i < 30; i++) {
28               out_trade_no += random.nextInt(10);
29           }
30 //        preData.put("out_trade_no", WXPayUtil.generateNonceStr());
31          preData.put("total_fee", fee);
32           preData.put("spbill_create_ip", request.getLocalAddr());
33           preData.put("notify_url", "xxx.com/bpphone/index.html");
34           preData.put("openid", openid);
35           preData.put("trade_type", "JSAPI");
36           String sign = WXPayUtil.generateSignature(preData, account.getPayKey());
37           preData.put("sign", sign);
38           String xml = WXPayUtil.mapToXml(preData);
39           String unifiedorder_url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
40           String xmlStr = httpUtils.post(unifiedorder_url, xml);
41           System.out.println("xmlStr============="+xmlStr);
42           String prepay_id = "";
43           if (xmlStr.indexOf("SUCCESS") != -1) {
44             Map map = WXPayUtil.xmlToMap(xmlStr);
45              prepay_id = (String) map.get("prepay_id");
46           }
47           payMap.put("appId", account.getAccountAppid());
48           payMap.put("timeStamp", WXPayUtil.getCurrentTimestamp() + "");
49           payMap.put("nonceStr", WXPayUtil.generateNonceStr());
50           payMap.put("package", "prepay_id=" + prepay_id);
51           payMap.put("signType", "MD5");
52           String paySign = WXPayUtil.generateSignature(payMap, account.getPayKey());
53           payMap.put("paySign", paySign);
54       } catch (Exception e) {
55           payMap.put("ret", InterConstant.RET_WX);
56           payMap.put("appid", WsUtils.getAccount().getAccountAppid());
57           e.printStackTrace();
58       }
59           return payMap;
60       }       
61   }
62 }

 

转载于:https://www.cnblogs.com/3b2414/p/10820347.html

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