创建支付订单
<p>到课程git项目中下载银联SDK和相关数字证书文件,按照说明文档,设置后端Java项目。</p>
<p><img src="https://www.showdoc.cc/server/api/attachment/visitfile/sign/aa7bc301a8f395e44e4c582903059f3c?showdoc=.jpg" alt="" /></p>
<p>创建UnionPayController.java</p>
<pre><code class="language-java">@RestController
@RequestMapping("/app/unionpay")
@Api("银联业务接口")
public class UnionPayController {
@Autowired
private SDKConfig config;
@Autowired
private AcpService acpService;
@Value("${application.unionpay.mch-id}")
private String mchId;
@Value("${application.unionpay.frontUrl}")
private String frontUrl;
@Value("${application.unionpay.backUrl}")
private String backUrl;
@Value("${application.unionpay.orderUrl}")
private String orderUrl;
@Value("${application.unionpay.wapFrontUrl}")
private String wapFrontUrl;
@Autowired
private RedisUtils redisUtils;
@Autowired
private UserService userService;
@Autowired
private OrderService orderService;
@Autowired
private JwtUtils jwtUtils;
@Login
@PostMapping("/wapPayOrder")
@ApiOperation("Wap付款")
public R wapPayOrder(@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); //商户号码
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", wapFrontUrl);
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();
String htmlCode = AcpService.createAutoFormHtml(requestFrontUrl, submitFromData, "UTF-8");
//未完待续
} catch (Exception e) {
e.printStackTrace();
return R.error("银联支付模块故障");
}
}
}</code></pre>