创建支付订单
<p>编写UnionPayController方法</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().multiply(new BigDecimal("100")).intValue() + "";
try {
Map<String, String> requestData = new HashMap<String, String>();
requestData.put("version", "5.0.0"); //版本号,全渠道默认值
requestData.put("encoding", "UTF-8"); //字符集编码,可以使用UTF-8,GBK两种方式
requestData.put("signMethod", "01"); //签名方法
requestData.put("txnType", "01"); //交易类型 ,01:消费
requestData.put("txnSubType", "01"); //交易子类型, 01:自助消费
requestData.put("bizType", "000201"); //业务类型,B2C网关支付,手机wap支付
requestData.put("channelType", "07"); //渠道类型;07:PC,平板 08:手机
requestData.put("merId", mchId); //商户号码,请改成自己申请的正式商户号或者open上注册得来的777测试商户号
requestData.put("accessType", "0"); //接入类型,0:直连商户
requestData.put("orderId", order.getCode()); //商户订单号
//订单发送时间,取系统时间,格式为YYYYMMDDhhmmss,必须取当前时间,否则会报txnTime无效
requestData.put("txnTime", new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
requestData.put("currencyCode", "156"); //交易币种(境内商户一般是156 人民币)
requestData.put("txnAmt", amount); //交易金额,单位分,不要带小数点
requestData.put("frontUrl", frontUrl);
requestData.put("backUrl", backUrl);
requestData.put("payTimeout", new SimpleDateFormat("yyyyMMddHHmmss").format(new Date().getTime() + 15 * 60 * 1000));
Map<String, String> submitFromData = acpService.sign(requestData, "UTF-8");
String requestFrontUrl = config.getFrontRequestUrl();
//生成自动跳转的Html表单
String htmlCode = AcpService.createAutoFormHtml(requestFrontUrl, submitFromData, "UTF-8");
if (htmlCode!=null && htmlCode.length()>0) {
order.setPaymentType(3);
UpdateWrapper updateWrapper = new UpdateWrapper();
updateWrapper.eq("id", order.getId());
orderService.update(order, updateWrapper);
String ip=header.get("ip").toString();
Document doc=Jsoup.connect("http://ip.ws.126.net/ipquery?ip="+ip).get();
String body=doc.body().text();
ScriptEngine engine=new ScriptEngineManager().getEngineByExtension("js");
String code="function getLocation(){";
code+=body;
code+=";return lo;}";
engine.eval(code);
String location=engine.eval("getLocation()").toString();
PaymentLogEntity paymentLogEntity=new PaymentLogEntity();
paymentLogEntity.setIp(ip);
paymentLogEntity.setLocation(location);
paymentLogEntity.setCode(order.getCode());
paymentLogEntity.setAmount(order.getAmount());
paymentLogEntity.setPaymentType(3);
paymentLogService.save(paymentLogEntity);
return R.ok().put("htmlCode", htmlCode);
} else {
return R.error("支付订单创建失败");
}
} catch (Exception e) {
e.printStackTrace();
return R.error("银联支付模块故障");
}
}</code></pre>