微信支付 - 构建商户订单

    科技2024-11-13  8

    // 微信支付成功 -> 支付中心 -> 吃货平台 // |-> 回调通知的url String payReturnUrl = "http://api.z.xuexi.com/dev-api/orders/notifyMerchantOrderPaid"; /** * 接受商户订单信息,保存到自己的数据库 */ @PostMapping("/createMerchantOrder") public JSONResult createMerchantOrder(@RequestBody MerchantOrdersBO merchantOrdersBO, HttpServletRequest request, HttpServletResponse response) throws Exception { String merchantOrderId = merchantOrdersBO.getMerchantOrderId(); // 订单id String merchantUserId = merchantOrdersBO.getMerchantUserId(); // 用户id Integer amount = merchantOrdersBO.getAmount(); // 实际支付订单金额 Integer payMethod = merchantOrdersBO.getPayMethod(); // 支付方式 String returnUrl = merchantOrdersBO.getReturnUrl(); // 支付成功后的回调地址(学生自定义) if (StringUtils.isBlank(merchantOrderId)) { return JSONResult.errorMsg("参数[orderId]不能为空"); } if (StringUtils.isBlank(merchantUserId)) { return JSONResult.errorMsg("参数[userId]不能为空"); } if (amount == null || amount < 1) { return JSONResult.errorMsg("参数[realPayAmount]不能为空并且不能小于1"); } if (payMethod == null) { return JSONResult.errorMsg("参数[payMethod]不能为空并且不能小于1"); } if (payMethod != PayMethod.WEIXIN.type && payMethod != PayMethod.ALIPAY.type) { return JSONResult.errorMsg("参数[payMethod]目前只支持微信支付或支付宝支付"); } if (StringUtils.isBlank(returnUrl)) { return JSONResult.errorMsg("参数[returnUrl]不能为空"); } // 保存传来的商户订单信息 boolean isSuccess = false; try { isSuccess = paymentOrderService.createPaymentOrder(merchantOrdersBO); } catch (Exception e) { e.printStackTrace(); JSONResult.errorException(e.getMessage()); } if (isSuccess) { return JSONResult.ok("商户订单创建成功!"); } else { return JSONResult.errorMsg("商户订单创建失败,请重试..."); } } public class MerchantOrdersBO { private String merchantOrderId; // 商户订单号 private String merchantUserId; // 商户方的发起用户的用户主键id private Integer amount; // 实际支付总金额(包含商户所支付的订单费邮费总额) private Integer payMethod; // 支付方式 1:微信 2:支付宝 private String returnUrl; // 支付成功后的回调地址(学生自定义) public String getMerchantOrderId() { return merchantOrderId; } public void setMerchantOrderId(String merchantOrderId) { this.merchantOrderId = merchantOrderId; } public String getMerchantUserId() { return merchantUserId; } public void setMerchantUserId(String merchantUserId) { this.merchantUserId = merchantUserId; } public Integer getAmount() { return amount; } public void setAmount(Integer amount) { this.amount = amount; } public Integer getPayMethod() { return payMethod; } public void setPayMethod(Integer payMethod) { this.payMethod = payMethod; } public String getReturnUrl() { return returnUrl; } public void setReturnUrl(String returnUrl) { this.returnUrl = returnUrl; } } @Transactional(propagation=Propagation.REQUIRED) @Override public boolean createPaymentOrder(MerchantOrdersBO merchantOrdersBO) { String id = sid.nextShort(); Orders paymentOrder = new Orders(); BeanUtils.copyProperties(merchantOrdersBO, paymentOrder); paymentOrder.setId(id); paymentOrder.setPayStatus(PaymentStatus.WAIT_PAY.type); paymentOrder.setComeFrom("吃货"); paymentOrder.setIsDelete(YesOrNo.NO.type); paymentOrder.setCreatedTime(new Date()); int result = ordersMapper.insert(paymentOrder); return result == 1 ? true : false; } // 1. 创建订单 OrderVO orderVO = orderService.createOrder(submitOrderBO); public class MerchantOrdersVO { private String merchantOrderId; // 商户订单号 private String merchantUserId; // 商户方的发起用户的用户主键id private Integer amount; // 实际支付总金额(包含商户所支付的订单费邮费总额) private Integer payMethod; // 支付方式 1:微信 2:支付宝 private String returnUrl; // 支付成功后的回调地址(学生自定义) public String getMerchantOrderId() { return merchantOrderId; } public void setMerchantOrderId(String merchantOrderId) { this.merchantOrderId = merchantOrderId; } public String getMerchantUserId() { return merchantUserId; } public void setMerchantUserId(String merchantUserId) { this.merchantUserId = merchantUserId; } public Integer getAmount() { return amount; } public void setAmount(Integer amount) { this.amount = amount; } public Integer getPayMethod() { return payMethod; } public void setPayMethod(Integer payMethod) { this.payMethod = payMethod; } public String getReturnUrl() { return returnUrl; } public void setReturnUrl(String returnUrl) { this.returnUrl = returnUrl; } } public class OrderVO { private String orderId; private MerchantOrdersVO merchantOrdersVO; public String getOrderId() { return orderId; } public void setOrderId(String orderId) { this.orderId = orderId; } public MerchantOrdersVO getMerchantOrdersVO() { return merchantOrdersVO; } public void setMerchantOrdersVO(MerchantOrdersVO merchantOrdersVO) { this.merchantOrdersVO = merchantOrdersVO; } } // 4. 构建商户订单,用于传给支付中心 MerchantOrdersVO merchantOrdersVO = new MerchantOrdersVO(); merchantOrdersVO.setMerchantOrderId(orderId); merchantOrdersVO.setMerchantUserId(userId); merchantOrdersVO.setAmount(realPayAmount + postAmount); merchantOrdersVO.setPayMethod(payMethod); // 5. 构建自定义订单vo OrderVO orderVO = new OrderVO(); orderVO.setOrderId(orderId); orderVO.setMerchantOrdersVO(merchantOrdersVO);

     

    Processed: 0.009, SQL: 8