1. 程式人生 > >微信公眾號支付--錯誤記錄

微信公眾號支付--錯誤記錄

二次 數組 格式轉換 println equals sig col package reat

微信公眾號支付調用統一下單接口時,微信返回的數據一定要二次組裝再給前臺,否則會有問題的,正確示範如下:

/**
     * 獲取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; }

微信公眾號支付--錯誤記錄