创建支付订单
<h3>封装创建支付订单的WEB方法</h3>
<p>编写ZfbController.java</p>
<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() + "";
try {
AlipayClient client = new DefaultAlipayClient(
gateway,
app_appId,
app_privateKey,
"json",
"UTF-8",
app_publicKey,
"RSA2"
);
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
request.setReturnUrl("http://192.168.99.100:8848/mall/order.html");
request.setNotifyUrl("http://www.demo.com");
String subject = "Native付款";
String body = "测试Native付款";
request.setBizContent("{\"out_trade_no\":\"" + order.getCode() + "\","
+ "\"total_amount\":\"" + amount + "\","
+ "\"subject\":\"" + subject + "\","
+ "\"body\":\"" + body + "\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
AlipayTradePagePayResponse response = client.pageExecute(request);
String htmlCode = response.getBody();
if (response.isSuccess()) {
order.setPaymentType(2);
UpdateWrapper updateWrapper = new UpdateWrapper();
updateWrapper.eq("id", order.getId());
orderService.update(order, updateWrapper);
return R.ok().put("htmlCode", htmlCode);
} else {
return R.error("支付订单创建失败");
}
} catch (Exception e) {
e.printStackTrace();
return R.error("支付宝支付模块故障");
}
}</code></pre>
<h2>编写订单页面</h2>
<p>编写order.js文件</p>
<pre><code class="language-javascript">$.ajax({
url:zfbUrl.nativePayOrder,
type:"post",
dataType:"json",
contentType:"application/json",
beforeSend: function (request) {
request.setRequestHeader("token", localStorage.getItem("token"));
request.setRequestHeader("ip", localStorage.getItem("ip"));
},
data:JSON.stringify({
"orderId":orderId
}),
success:function(resp){
if(resp.code==0){
let htmlCode=resp.htmlCode
// console.log(htmlCode)
$("#htmlCode").html(htmlCode)
}
}
})</code></pre>