开发环境:java、linux、wechatpay-apache-httpclient依赖
后端代码
//整理需要的微信预付订单信息
log.info("------整理需要的微信预付订单信息 begin------");
JSONObject paramObject = new JSONObject();
JSONObject amountObject = new JSONObject();
amountObject.put("total", info.getMoney());
amountObject.put("currency", "CNY");
paramObject.put("amount", amountObject);
paramObject.put("mchid", weChatPayProperties.getMchId());
paramObject.put("description", order.getOrder_describe());
paramObject.put("notify_url", "https://tgg.ygdzsw.cn/wx/pay/notifyUrl");
JSONObject payerObject = new JSONObject();
payerObject.put("openid", user.getOpenid());
paramObject.put("payer", payerObject);
paramObject.put("out_trade_no", order.getOrder_num());
paramObject.put("appid", WxAppletInfo.APPID);
log.info("微信预付订单信息:" + paramObject.toString());
log.info("------整理需要的微信预付订单信息 end------");
log.info("------发起请求 begin------");
HttpPost httpPost = new HttpPost(applet_req_url);
httpPost.setHeader("Accept", "application/json");
StringEntity entity = new StringEntity(paramObject.toJSONString(), "utf-8");
entity.setContentType("application/json");
log.info("[微信下单请求参数列表]=" + paramObject);
httpPost.setEntity(entity);
//开启回滚点
Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint();
//完成签名并执行请求
CloseableHttpResponse response = null;
Map<String, Object> resMap = new HashMap<>();
try {
response = closeableHttpClient.execute(httpPost);
log.info("------发起请求 end------");
int statusCode = response.getStatusLine().getStatusCode();
log.info("请求结果:" + (statusCode == 200 ? "成功" : "失败"));
Assert.isTrue(statusCode == 200, "微信下单请求失败");
JSONObject jsonObject = JSON.parseObject(EntityUtils.toString(response.getEntity()));
String prepayId = jsonObject.getString("prepay_id");
log.info("请求下单后返回的数据:"+prepayId);
Assert.isTrue(StringUtils.isNotBlank(prepayId), "下单获取参数失败");
请求数据(两种都一样的数据):
{
"amount":{
"total":1,
"currency":"CNY"
},
"mchid":"商户id(手改了)",
"out_trade_no":"76065476193145380716139149640209",
"appid":"appiid(手改了)",
"description":"电商支付",
"notify_url":"https://域名/wx/pay/notifyUrl",
"payer":{
"openid":"openid(手改了)"
}
}
官方请求示例:
postman请求微信统一下单JSAPI
参数服务器请求后复制的。
服务器请求微信统一下单JSAPI