微信公众号支付

微信公众号 JSAPI方式支付

*前端验证地址*
 @RequestMapping(value = "/pay", method = RequestMethod.GET)
    public @ResponseBody String pay(HttpSession httpSession, HttpServletRequest request, String totalFee, String openId, String outTradeNo,Integer id) throws Exception {
     
        String paramContent = "";
        String sign = "";
        String nonceStr = create_nonce_str();
        TreeMap<String, String> param = new TreeMap<>();
        param.put("appid",WXPayConfig.APPID); //公众号id
        param.put("mch_id",WXPayConfig.MCH_ID);//商户平台id
        param.put("device_info","WEB");
        param.put("nonce_str",nonceStr);
        param.put("body","RFYX");//订单标题
        param.put("out_trade_no",outTradeNo);//订单ID
        param.put("total_fee",totalFee); //订单需要支付的金额
        param.put("trade_type","JSAPI");//交易类型 JSAPI 公众号支付 NATIVE 扫码支付 APP APP支付
        param.put("notify_url",WXPayConfig.NOTIFY_URL);//notify_url 支付成功之后 微信会进行异步回调的地址
        param.put("openid",openId);//微信授权获取openId
        param.put("sign_type", "MD5");
        String spbill_create_ip = CheckUtil.getIpAddr(request);
        if (StringUtils.isEmpty(spbill_create_ip)) {
     
            param.put("spbill_create_ip","192.168.0.1");// 消费IP地址
        } else {
     
            param.put("spbill_create_ip",spbill_create_ip);// 消费IP地址
        }
        String text = CheckUtil.generateSignature(param);
        sign = MD5.rijisign(text,WXPayConfig.API_KEY,"utf-8").toUpperCase();
        param.put("sign",sign);
        paramContent = CheckUtil.getXMLFromMap(param);
        String orderInfo = HttpsUtil.httpsRequest(WXPayConfig.UNIFIED_ORDER_URL,"POST", paramContent);
        String prepay_id;
        try {
     
            Map<String, String> orderInfoMap = CheckUtil.xmlToMap(orderInfo);
            if (orderInfoMap.get("return_code").equals("SUCCESS") && orderInfoMap.get("result_code").equals("SUCCESS")) {
     
                //预支付id
                prepay_id = orderInfoMap.get("prepay_id");
                String timestamp = CheckUtil.create_timestamp();
                TreeMap<String, String> pageParam = new TreeMap<>();
                pageParam.put("appId",WXPayConfig.APPID);
                pageParam.put("nonceStr",nonceStr);
                pageParam.put("package","prepay_id="+prepay_id);
                pageParam.put("timeStamp",timestamp);
                pageParam.put("signType","MD5");
                String text1 = CheckUtil.generateSignature(pageParam);
                String pageSign = MD5.rijisign(text1,WXPayConfig.NOTIFY_URL,"utf-8").toUpperCase();
                pageParam.put("paySign",pageSign);
                pageParam.put("prepay_id",prepay_id);

            
                int resule = memberrechargeService.insert(memberrecharge);


                return JSON.toJSONString(pageParam);
            }
        } catch (Exception e) {
     
            return null;
        }
        return null;
    }

前端

$.ajax({
     
                    url: "*验证地址*?totalFee={param.totalFee}&openId=${param.openid}&id=${param.id}&outTradeNo=" + guid2(),
                    type: "get",
                    dataType: "json",
                    success: function (result) {
     
                        console.log(result)
                        wx.chooseWXPay({
     
                            timestamp: result.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
                            nonceStr: result.nonceStr, // 支付签名随机串,不长于 32 位
                            package: "prepay_id=" + result.prepay_id,
                            signType: 'MD5', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
                            paySign: result.paySign, // 支付签名
                            success: function (res) {
     
                                if (res.err_msg == "get_brand_wcpay_request:ok") {
     
                                    window.location.href = "支付成功跳转地址"

                                }
                            }
                        });
                    },
                    error: function (data) {
     
                        alert("错误!");
                        alert(data);
                    }
                });

你可能感兴趣的:(java,javascript)