其他分享
首页 > 其他分享> > 微信支付

微信支付

作者:互联网

小程序调用统一的下单接口生成预付款订单   支付完成 调用回调地址

wx.requestPayment({
            'appId':res.appId,
            'timeStamp': res.timeStamp,
            'nonceStr': res.nonceStr,
            'package': res.package,
            'signType': 'MD5',//也可以其他的加密方式
            'paySign': res.paySign,
            'success':function(res){
             //如果支付成功或异步推送到后台地址  此处没有返回值
              wx.showToast({
                title: "支付成功",
                icon: 'none',
                duration: 2000
              })
            },
            'fail':function(res){
              if(res.errMsg=="requestPayment:fail cancel"){
                wx.showModal({
                  content: '确定取消支付吗?',
                })
              }else{
                wx.showModal({
                  content: '支付失败',
                })
              }
            },
          })
/**
     * 微信支付
     * 统一下单接口
     * @throws ParseException 
     */
    @ResponseBody
    @RequestMapping(value = "/payRechargeGold", method = RequestMethod.GET)
    public void weixinPayWap(HttpServletRequest request, HttpServletResponse response){
        String openid = request.getParameter("wxSession");
        String merchantId = request.getParameter("merchantId");//商户
        Map<String,String> orderMap = new HashMap<String, String>();
        Double amount = 1.2;
        String bodyName = "置顶充值";
        orderMap.put("merchantId", merchantId);
        orderMap.put("amount", amount.toString());//金额
        String orderNum = GetOrderNumUtil.generateOrderNum(merchantId);
        //此处我会存入缓存  在支付成功后回调是在将数据插入数据库
        redisUtil.hset("rechargeOrder", orderNum , orderMap,10*60*60);
        try {
            //拼接统一下单地址参数
            Map<String, String> paraMap = new HashMap<String, String>();
            //获取请求ip地址
            String ip = GetIpUtil.getIp(request);
            Double money = amount*100;//获取金额,单位分
            paraMap.put("appid", applets.getAppID());
            paraMap.put("body", bodyName); //商品名称
            paraMap.put("mch_id", pay.getMchId());//商户ID
            paraMap.put("nonce_str", WxPayUtil.generateNonceStr()); //String 随机字符串
            paraMap.put("attach", "test");
            paraMap.put("out_trade_no", orderNum);//订单号
            paraMap.put("notify_url", pay.getNotifyUrl());
            paraMap.put("openid", openid);
            paraMap.put("spbill_create_ip", ip);//请求IP
            paraMap.put("total_fee", money.intValue()+""); //金额
            paraMap.put("trade_type", pay.getTradeType()); //类型
            String sign = WxPayUtil.generateSignature(paraMap, pay.getKey());
            paraMap.put("sign", sign);
            String xml = WxPayUtil.mapToXml(paraMap);//将所有参数(map)转xml格式
            String unifiedorder_url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
            String xmlStr = HttpUtil.doPostJson(unifiedorder_url, xml);
            //xmlStr统一订单下发接口获取的参数
            //以下内容是返回前端页面的json数据
            if (xmlStr.indexOf("SUCCESS") != -1) {
          //微信官方会提供工具类 也可以自己封装 Map<String, String> map = WxPayUtil.xmlToMap(xmlStr); String prepayId = map.get("prepay_id"); String nonceStr = map.get("nonce_str"); long time = System.currentTimeMillis() / 1000; Map<String, String> payMap = new HashMap<String, String>(); payMap.put("appId", applets.getAppID()); payMap.put("nonceStr", nonceStr); payMap.put("package", "prepay_id="+ prepayId); payMap.put("timeStamp", time+""); payMap.put("signType", "MD5"); String newSign = WxPayUtil.generateSignature(payMap, pay.getKey()); JSONObject jo = new JSONObject(); jo.put("appId", applets.getAppID()); jo.put("timeStamp", time+""); jo.put("nonceStr", nonceStr); jo.put("package", "prepay_id="+ prepayId); jo.put("paySign", newSign); jo.put("status", 200); Writer out = response.getWriter(); out.write(jo.toString()); out.flush(); } } catch (Exception e) { e.printStackTrace(); } }
  
  //微信辅助成功后触发回调地址
  //原本是返回一个组装好的字符串 发现没用 现在return一个字符串 简单粗暴
  @RequestMapping(value = "/success") @ResponseBody public String success(HttpServletRequest request, HttpServletResponse response) { // System.out.println("触发支付回调------------------"); try { StringBuffer sb = new StringBuffer(); InputStream inputStream = request.getInputStream(); String s; BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); while ((s = in.readLine()) != null) { sb.append(s); } in.close(); inputStream.close(); Map<String,String> map = WxPayUtil.xmlToMap(sb.toString()); if(map.get("result_code").equals("SUCCESS")) { String orderNum = map.get("out_trade_no"); String openid = map.get("openid"); int res =0; if(res > 0) { System.out.println("订单从缓存添加到数据库成功。。。。。。"); return "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>" + "<return_msg><![CDATA[OK]]></return_msg>" + "</xml>"; }else { return "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[添加订单失败]]></return_msg>" + "</xml>"; } } } catch (Exception e) { e.printStackTrace(); } return "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[添加订单失败]]></return_msg>" + "</xml>"; }

 

标签:map,res,String,微信,支付,jo,paraMap,put
来源: https://www.cnblogs.com/ch94/p/14718616.html