创建扫码订单,完成支付
<ol>
<li>编写UnionPayController.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().multiply(new BigDecimal("100")).intValue() + "";
Map<String, String> contentData = new HashMap<String, String>();
contentData.put("version", "5.0.0"); //版本号 全渠道默认值
contentData.put("encoding", "UTF-8"); //字符集编码 可以使用UTF-8,GBK两种方式
contentData.put("signMethod", "01"); //签名方法
contentData.put("txnType", "01"); //交易类型 01:消费
contentData.put("txnSubType", "06"); //交易子类 06:二维码消费
contentData.put("bizType", "000000"); //填写000000
contentData.put("channelType", "08"); //渠道类型 08手机
contentData.put("merId", mchId); //商户号码
contentData.put("accessType", "0"); //接入类型,商户接入填0 ,不需修改
contentData.put("orderId", order.getCode()); //商户订单号
//订单发送时间,取系统时间,格式为YYYYMMDDhhmmss,必须取当前时间,否则会报txnTime无效
contentData.put("txnTime", new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
contentData.put("txnAmt", amount); //交易金额
contentData.put("currencyCode", "156"); //境内商户固定 156 人民币
contentData.put("qrNo", form.getAuthCode()); //C2B码,1-20位数字
contentData.put("termId", "49000002"); //终端号
contentData.put("backUrl", backUrl);
Map<String, String> reqData = acpService.sign(contentData,"UTF-8");
String requestAppUrl = config.getBackRequestUrl();
//发送请求报文并接受同步应答(默认连接超时时间30秒,读取返回结果超时时间30秒)
Map<String, String> rspData = AcpService.post(reqData,requestAppUrl,"UTF-8");
if(!rspData.isEmpty()){
if(acpService.validate(rspData, "UTF-8")){
String respCode = rspData.get("respCode") ;
if(("00").equals(respCode)){
String queryId=rspData.get("queryId");
order.setPrepayId(queryId);
order.setStatus(2);
order.setPaymentType(3);
UpdateWrapper updateWrapper = new UpdateWrapper();
updateWrapper.eq("id", order.getId());
orderService.update(order, updateWrapper);
return R.ok("付款成功");
}else{
return R.error("付款失败");
}
}else{
return R.error("付款失败");
}
}else{
return R.error("付款失败");
}
}</code></pre></li>
</ol>
<p>2.编写payment.js文件,加入else语句</p>
<pre><code class="language-javascript">else if(/^622[0-9]{16}$/.test(authCode)){
url=unionUrl.scanCodePayOrder
}
//下面发送Ajax的代码不需要修改</code></pre>