小程序
小游戏
企业微信
微信支付
扫描小程序码分享
微信支付V3
2 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
楼主解决了吗?什么原因
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
SortedMap<String, Object> sortedMap = new TreeMap<String, Object>(); sortedMap.put("appid", wxPayConfigEntity.getApppid()); //应用ID sortedMap.put("mchid", wxPayConfigEntity.getMch_id()); //商户号 sortedMap.put("description", "珠宝纯定制-" + weiXinPayPo.getBody()); //商品描述 sortedMap.put("out_trade_no", out_trade_no); //商户订单号 sortedMap.put("notify_url", wxPayConfigEntity.getNotify_url()); //通知回调地址 //总金额 int totalFee = weiXinPayPo.getTotal_fee().multiply(new BigDecimal(100)).intValue();//总金额 需要乘以100 ,微信支付默认单位分 Map<String, Object> map1 = new HashMap<>(); map1.put("total",totalFee); map1.put("currency","CNY"); sortedMap.put("amount",map1); JSONObject json = new JSONObject(sortedMap); String reqdata = json.toJSONString(); //校验支付路径 HttpUrl httpurl = HttpUrl.parse(wxPayConfigEntity.getPayurl()); // 请求body参数 log.info(reqdata); String Sign = null; PrivateKey privateKey = null;//商户私钥 AutoUpdateCertificatesVerifier verifier = null; try { Sign = wxPayUtil.getToken(nonceStr,timestamp,POST,httpurl,reqdata); //======设置请求头======== privateKey = wxPayUtil.getPrivateKey(wxPayConfigEntity.getKeyPath()); //不需要传入微信支付证书,将会自动更新 verifier = new AutoUpdateCertificatesVerifier( new WechatPay2Credentials(wxPayConfigEntity.getMch_id(), new PrivateKeySigner(wxPayConfigEntity.getSerialNo(), privateKey)), wxPayConfigEntity.getApiV3Key().getBytes("utf-8")); } catch (IOException e) { e.printStackTrace(); } catch (SignatureException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } //通过WechatPayHttpClientBuilder构造的HttpClient,会自动的处理签名和验签 CloseableHttpClient httpClient = WechatPayHttpClientBuilder.create() .withMerchant(wxPayConfigEntity.getMch_id(), wxPayConfigEntity.getSerialNo(), privateKey) .withValidator(new WechatPay2Validator(verifier)).build(); HttpPost httpPost = new HttpPost(wxPayConfigEntity.getPayurl()); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build(); httpPost.setConfig(requestConfig); httpPost.addHeader("Content-Type", "application/json"); httpPost.addHeader("Accept","application/json"); httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"); String authType ="WECHATPAY2-SHA256-RSA2048"; String authorization = wxPayUtil.getAuthorization(Sign, authType); httpPost.addHeader("Authorization",authorization); StringEntity entity = new StringEntity(reqdata,"UTF-8"); httpPost.setEntity(entity); //======发起请求======== CloseableHttpResponse response = null; String result = null; try { response = httpClient.execute(httpPost); result = EntityUtils.toString(response.getEntity()); } catch (IOException e) { e.printStackTrace(); } int statusCode = response.getStatusLine().getStatusCode();
参数列表里面六个必填的参数都传了,打印处理的json也没问题,请求报400
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
楼主解决了吗?什么原因
SortedMap<String, Object> sortedMap = new TreeMap<String, Object>(); sortedMap.put("appid", wxPayConfigEntity.getApppid()); //应用ID sortedMap.put("mchid", wxPayConfigEntity.getMch_id()); //商户号 sortedMap.put("description", "珠宝纯定制-" + weiXinPayPo.getBody()); //商品描述 sortedMap.put("out_trade_no", out_trade_no); //商户订单号 sortedMap.put("notify_url", wxPayConfigEntity.getNotify_url()); //通知回调地址 //总金额 int totalFee = weiXinPayPo.getTotal_fee().multiply(new BigDecimal(100)).intValue();//总金额 需要乘以100 ,微信支付默认单位分 Map<String, Object> map1 = new HashMap<>(); map1.put("total",totalFee); map1.put("currency","CNY"); sortedMap.put("amount",map1); JSONObject json = new JSONObject(sortedMap); String reqdata = json.toJSONString(); //校验支付路径 HttpUrl httpurl = HttpUrl.parse(wxPayConfigEntity.getPayurl()); // 请求body参数 log.info(reqdata); String Sign = null; PrivateKey privateKey = null;//商户私钥 AutoUpdateCertificatesVerifier verifier = null; try { Sign = wxPayUtil.getToken(nonceStr,timestamp,POST,httpurl,reqdata); //======设置请求头======== privateKey = wxPayUtil.getPrivateKey(wxPayConfigEntity.getKeyPath()); //不需要传入微信支付证书,将会自动更新 verifier = new AutoUpdateCertificatesVerifier( new WechatPay2Credentials(wxPayConfigEntity.getMch_id(), new PrivateKeySigner(wxPayConfigEntity.getSerialNo(), privateKey)), wxPayConfigEntity.getApiV3Key().getBytes("utf-8")); } catch (IOException e) { e.printStackTrace(); } catch (SignatureException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } //通过WechatPayHttpClientBuilder构造的HttpClient,会自动的处理签名和验签 CloseableHttpClient httpClient = WechatPayHttpClientBuilder.create() .withMerchant(wxPayConfigEntity.getMch_id(), wxPayConfigEntity.getSerialNo(), privateKey) .withValidator(new WechatPay2Validator(verifier)).build(); HttpPost httpPost = new HttpPost(wxPayConfigEntity.getPayurl()); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build(); httpPost.setConfig(requestConfig); httpPost.addHeader("Content-Type", "application/json"); httpPost.addHeader("Accept","application/json"); httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"); String authType ="WECHATPAY2-SHA256-RSA2048"; String authorization = wxPayUtil.getAuthorization(Sign, authType); httpPost.addHeader("Authorization",authorization); StringEntity entity = new StringEntity(reqdata,"UTF-8"); httpPost.setEntity(entity); //======发起请求======== CloseableHttpResponse response = null; String result = null; try { response = httpClient.execute(httpPost); result = EntityUtils.toString(response.getEntity()); } catch (IOException e) { e.printStackTrace(); } int statusCode = response.getStatusLine().getStatusCode();
参数列表里面六个必填的参数都传了,打印处理的json也没问题,请求报400