封装扫码支付WEB方法
<ol>
<li>
<p>编写ScanCodePayOrderForm.java文件</p>
<pre><code class="language-java">@Data
@ApiModel(value = "收款码付款的表单")
public class ScanCodePayOrderForm {
@ApiModelProperty(value = "付款码")
@NotBlank
@Pattern(regexp = "^1[0-5][0-9]{16}$|^(25|26|27|28|29|30)[0-9]{14,22}$|^622[0-9]{16}$",message = "不符合正则表达式")
private String authCode;
@ApiModelProperty(value = "订单ID")
@Min(1)
private Integer orderId;
}</code></pre>
</li>
<li>编写ZfbController.java文件
<pre><code class="language-java">@Login
@PostMapping("/scanCodePayOrder")
@ApiOperation("付款码收款")
public R scanCodePayOrder(@RequestBody ScanCodePayOrderForm 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"
);
AlipayTradePayRequest request = new AlipayTradePayRequest();
request.setBizContent("{" +
" \"out_trade_no\":\"" + order.getCode() + "\"," +
" \"scene\":\"bar_code\"," +
" \"auth_code\":\"" + form.getAuthCode() + "\"," +
" \"subject\":\"扫码付款测试\"," +
" \"store_id\":\"NJ_001\"," +
" \"timeout_express\":\"2m\"," +
" \"total_amount\":\"" + amount + "\"" +
" }");
AlipayTradePayResponse response = client.execute(request);
if (response.isSuccess() && response.getCode().equals("10000")) {
String prepayId = response.getTradeNo();
order.setPrepayId(prepayId);
order.setStatus(2);
order.setPaymentType(2);
UpdateWrapper updateWrapper = new UpdateWrapper();
updateWrapper.eq("id", order.getId());
orderService.update(order, updateWrapper);
return R.ok("付款成功");
} else {
return R.error("付款失败");
}
} catch (Exception e) {
e.printStackTrace();
return R.error("支付宝支付模块故障");
}
}</code></pre></li>
</ol>