创建支付订单并且完成支付
<h3>编写WxController.java</h3>
<pre><code class="language-java">@PostMapping("/nativePayOrder")
@ApiOperation("native付款")
public R nativePayOrder(@RequestBody PayOrderForm form, @RequestHeader HashMap header) {
ValidatorUtils.validateEntity(form);
String token = header.get("token").toString();
Long userId = Long.parseLong(jwtUtils.getClaimByToken(token).getSubject());
int orderId = form.getOrderId();
UserEntity user = new UserEntity();
user.setUserId(userId);
QueryWrapper wrapper = new QueryWrapper(user);
long count = userService.count(wrapper);
if (count == 0) {
return R.error("用户不存在");
}
OrderEntity order = new OrderEntity();
order.setUserId(userId.intValue());
order.setId(orderId);
order.setStatus(1);
wrapper = new QueryWrapper(order);
count = orderService.count(wrapper);
if (count == 0) {
return R.error("不是有效的订单");
}
//验证购物券是否有效
//验证团购活动是否有效
order = new OrderEntity();
order.setId(orderId);
wrapper = new QueryWrapper(order);
order = orderService.getOne(wrapper);
//向微信平台发出请求,创建支付订单
String amount = order.getAmount().multiply(new BigDecimal("100")).intValue() + "";
try {
WXPay wxPay = new WXPay(myWXPayConfig);
HashMap map = new HashMap();
map.put("nonce_str", WXPayUtil.generateNonceStr()); //随机字符串
map.put("body", "订单备注");
map.put("out_trade_no", order.getCode());
map.put("total_fee", amount);
map.put("spbill_create_ip", "127.0.0.1");
map.put("notify_url", "https://127.0.0.1/test");
map.put("trade_type", "NATIVE");
String sign=WXPayUtil.generateSignature(map,key);
map.put("sign",sign);
Map<String, String> result = wxPay.unifiedOrder(map);
String prepayId = result.get("prepay_id");
String codeUrl=result.get("code_url");
if (prepayId != null) {
order.setPrepayId(prepayId);
UpdateWrapper updateWrapper = new UpdateWrapper();
updateWrapper.eq("id", order.getId());
orderService.update(order, updateWrapper);
return R.ok().put("codeUrl", codeUrl);
} else {
return R.error("支付订单创建失败");
}
} catch (Exception e) {
e.printStackTrace();
return R.error("微信支付模块故障");
}
}</code></pre>