创建支付订单
<h3>封装创建支付订单的WEB方法</h3>
<p>编写ZfbController.java中的方法</p>
<pre><code class="language-java">@Login
@PostMapping("/appPayOrder")
@ApiOperation("App程序付款")
public R appPayOrder(@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("用户不存在");
}
String openId = userService.getOne(wrapper).getOpenId();
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"
);
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
model.setBody("我是测试数据");
model.setSubject("App支付测试Java");
model.setOutTradeNo(order.getCode());
model.setTimeoutExpress("30m");
model.setTotalAmount(amount);
model.setProductCode("QUICK_MSECURITY_PAY");
request.setNotifyUrl("http://www.demo.com");
request.setBizModel(model);
AlipayTradeAppPayResponse response = client.sdkExecute(request);
if (response.isSuccess()) {
// String prepayId=response.getTradeNo();
// order.setPrepayId(prepayId);
order.setPaymentType(2);
UpdateWrapper updateWrapper = new UpdateWrapper();
updateWrapper.eq("id", order.getId());
orderService.update(order, updateWrapper);
return R.ok().put("orderString", response.getBody());
} else {
return R.error("支付订单创建失败");
}
} catch (Exception e) {
e.printStackTrace();
return R.error("支付宝支付模块故障");
}
}</code></pre>
<h3>前端发起支付请求</h3>
<p>编写order.js文件</p>
<pre><code class="language-javascript">$(".pay-btn").click(function(){
//支付宝
let orderId=$(this).parents(".order").data("order-id")
$.ajax({
url:url.zfb.appPayOrder,
type:"post",
dataType:"json",
contentType:"application/json",
beforeSend:function(request){
request.setRequestHeader("token",localStorage.getItem("token"))
},
data:JSON.stringify({
"orderId":orderId
}),
success:function(resp){
let orderString=resp.orderString
window.order.pay(orderString,localStorage.getItem("token"),orderId)
}
})</code></pre>