微信公众号支付调用统一下单接口时,微信返回的数据一定要二次组装再给前台,否则会有问题的,正确示范如下:
/** * 获取weixin支付的返回信息 * @param payOrderId * @return */ @Override public String getPayInfo(String payOrderId,String openid,String orderType) { //返回结果 String res = ""; double total_fee_money = 0; //0代表普通车支付,1代表活动支付 if ("0".equals(orderType)) { //payOrderId即订单的id亦是订单表的主键 Order_main omm = orderMainService.fetch(payOrderId); if (omm == null) { res = "订单不存在"; return res; } //如果已支付状态为1的话 if (OrderPayStatusEnum.PAYALL.getKey() == omm.getPayStatus()) { res = "订单已支付"; return res; } total_fee_money = omm.getFrontMoney(); } else { //clueId即Clue_main的id //update by zhangyi 2017-12-1 ActivityPayRecord activityPayRecord = activitypayrecordService.fetch(payOrderId); if (activityPayRecord == null) { res = "活动定金支付记录不存在"; return res; } //如果已支付状态为1的话 if (1 == activityPayRecord.getPayStatus()) { res = "活动定金已支付"; return res; } total_fee_money = activityPayRecord.getFrontMoney(); } // double total_fee_money= 1; // 总金额以分为单位,不带小数点 // 格式转换 DecimalFormat dformat = new DecimalFormat("0"); String totalFee = dformat.format(total_fee_money); if ("true".equals(config.get("isTest", ""))) { totalFee = "1"; } // 把请求参数打包成数组 HashMap<String, String> sParaTemp = new HashMap<String, String>(); String body = "长城二手车"; //订单生成的机器 IP String exter_invoke_ip = this.localIp(); sParaTemp.put("appid", WeixinConfig.appid); sParaTemp.put("mch_id", WeixinConfig.mch_id); sParaTemp.put("notify_url", WeixinConfig.notify_url); sParaTemp.put("trade_type", WeixinConfig.trade_type); sParaTemp.put("sign_type", WeixinConfig.sign_type); sParaTemp.put("openid", openid); String nonce_str = String.valueOf(new Date().getTime()); sParaTemp.put("nonce_str", WXPayUtil.generateNonceStr()); sParaTemp.put("spbill_create_ip", exter_invoke_ip); sParaTemp.put("out_trade_no", payOrderId); sParaTemp.put("total_fee", totalFee); sParaTemp.put("body", body); sParaTemp.put("timeStamp", nonce_str); //add by zhangyi 付款类型 0代表普通车支付,1代表活动支付 sParaTemp.put("attach", orderType); try { sParaTemp.put("sign", WXPayUtil.generateSignature(sParaTemp, WeixinConfig.key, WeixinConfig.sign_type)); String reqBody = WXPayUtil.mapToXml(sParaTemp); String resp = wXPayRequest.requestWithoutCert(WeixinConfig.req_url, nonce_str, reqBody, WeixinConfig.connectTimeoutMs, WeixinConfig.readTimeoutMs, true); //验证签名是否正确 if (WXPayUtil.isSignatureValid(WXPayUtil.xmlToMap(resp), WeixinConfig.key, WeixinConfig.sign_type)) { //将返参转为map Map<String, String> resultmap = WXPayUtil.xmlToMap(resp); //resultmap.put("timeStamp",nonce_str); //System.out.println(JSON.toJSONString(resultmap)); //return JSON.toJSONString(resultmap); //return JSON.toJSONString(WXPayUtil.xmlToMap(resp).put("timeStamp",nonce_str)); //二次签名 String paySign = WXPayUtil.MD5("appId=" + WeixinConfig.appid + "&nonceStr=" + resultmap.get("nonce_str") + "&package=prepay_id=" + resultmap.get("prepay_id") + "&signType=MD5&timeStamp=" + nonce_str + "&key=" + WeixinConfig.key); //重新组装返参 Map resultmap1 = new HashMap(); resultmap1.put("timeStamp", nonce_str); resultmap1.put("nonceStr", resultmap.get("nonce_str")); resultmap1.put("package", "prepay_id=" + resultmap.get("prepay_id")); resultmap1.put("paySign", paySign); resultmap1.put("signType", WeixinConfig.sign_type); resultmap1.put("orderType", orderType); System.out.println(JSON.toJSONString(resultmap1)); //将支付参数返给前端 return JSON.toJSONString(resultmap1); } else { return "fail"; } } catch (Exception e) { e.printStackTrace(); } return res; }
时间: 2024-11-05 19:40:26