WFTOrderService.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package com.zsElectric.boot.business.service;
  2. import com.zsElectric.boot.business.mapper.RechargeLevelMapper;
  3. import com.zsElectric.boot.business.mapper.UserInfoMapper;
  4. import com.zsElectric.boot.business.mapper.UserOrderInfoMapper;
  5. import com.zsElectric.boot.business.model.entity.RechargeLevel;
  6. import com.zsElectric.boot.business.model.entity.UserOrderInfo;
  7. import com.zsElectric.boot.business.model.form.applet.AppLevelOrderForm;
  8. import com.zsElectric.boot.business.model.form.applet.AppUserPayForm;
  9. import com.zsElectric.boot.core.pay.WFT.WFTConstants;
  10. import com.zsElectric.boot.core.pay.swiftpass.config.SwiftpassConfig;
  11. import com.zsElectric.boot.core.pay.swiftpass.util.PayUtill;
  12. import com.zsElectric.boot.security.util.SecurityUtils;
  13. import jakarta.annotation.Resource;
  14. import jakarta.validation.Valid;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.springframework.stereotype.Service;
  17. import java.math.BigDecimal;
  18. import java.math.RoundingMode;
  19. import java.text.SimpleDateFormat;
  20. import java.util.*;
  21. /**
  22. * 威富通支付服务
  23. * 用于处理威富通JSAPI支付接口
  24. *
  25. * @author zsElectric
  26. * @since 2025-12-29
  27. */
  28. @Slf4j
  29. @Service
  30. public class WFTOrderService {
  31. @Resource
  32. private UserOrderInfoMapper userOrderInfoMapper;
  33. @Resource
  34. private UserInfoMapper userInfoMapper;
  35. @Resource
  36. private RechargeLevelMapper rechargeLevelMapper;
  37. @Resource
  38. protected SwiftpassConfig swiftpassConfig;
  39. /**
  40. * 创建商户订单号
  41. * 要求 32个字符内,只能是数字、大小写字母_-|*且在同一个商户号下唯一
  42. * 组成 两位前缀 + 17位时间戳 + 9位id补零 + 4位随机数 合计32位
  43. *
  44. * @param head 例如 商品-SP 退款-TK 等等
  45. * @param id 用户id
  46. * @return
  47. */
  48. public String createOrderNo(String head, Long id) {
  49. StringBuilder uid = new StringBuilder(id.toString());
  50. Date date = new Date();
  51. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
  52. int length = uid.length();
  53. for (int i = 0; i < 9 - length; i++) {
  54. uid.insert(0, "0");
  55. }
  56. return head + sdf.format(date) + uid + (int) ((Math.random() * 9 + 1) * 1000);
  57. }
  58. /**
  59. * 金额元转分字符串
  60. *
  61. * @param cny 元
  62. * @return
  63. */
  64. public String amount_fee(BigDecimal cny) {
  65. BigDecimal b2 = new BigDecimal("100");
  66. return cny.multiply(b2).setScale(0, RoundingMode.DOWN).toString();
  67. }
  68. public AppUserPayForm createOrder(@Valid AppLevelOrderForm appLevelOrderForm) {
  69. Long userId = SecurityUtils.getUserId();
  70. String userOpenId = userInfoMapper.getAppletUserInfo(userId).getOpenid();
  71. String orderNo = createOrderNo("SP", userId);
  72. //创建订单
  73. UserOrderInfo orderInfo = new UserOrderInfo();
  74. orderInfo.setUserId(userId);
  75. orderInfo.setOrderNo(orderNo);
  76. orderInfo.setLevelId(appLevelOrderForm.getLevelId());
  77. orderInfo.setOpenid(userOpenId);
  78. userOrderInfoMapper.insert(orderInfo);
  79. //构建支付表单返回给前端支撑JsApi支付调用
  80. AppUserPayForm payForm = new AppUserPayForm();
  81. payForm.setOrderId(orderInfo.getId()).setOrderNo(orderNo);
  82. //查询档位
  83. RechargeLevel level = rechargeLevelMapper.selectById(appLevelOrderForm.getLevelId());
  84. PayUtill wx = new PayUtill();
  85. /**
  86. * 小程序支付
  87. */
  88. String payWay = "1";
  89. /**
  90. * 请求支付
  91. */
  92. try {
  93. log.debug("通知第三开始支付:");
  94. // 通知第三方支付----------------------------------------------------------
  95. SortedMap map = new TreeMap<>();
  96. // 订单编号
  97. map.put("out_trade_no", orderNo);
  98. // 商品描述
  99. map.put("body", "购买充电抵扣券");
  100. // 附加信息
  101. map.put("attach", "支付人" + userId);
  102. // pifList.get(0).setHydOrderPayMoney(new BigDecimal("0.01"));
  103. // 总金额(分)
  104. map.put("total_fee", amount_fee(level.getMoney()));
  105. // 终端ip
  106. map.put("mch_create_ip", appLevelOrderForm.getIp());
  107. // 签名方式
  108. map.put("sign_type", "RSA_1_256");
  109. // 回调地址
  110. map.put("notify_url", swiftpassConfig.getNotify_url());
  111. // 公众账号或小程序ID
  112. map.put("sub_appid", "wx9894a01b9e92c368");
  113. if (payWay.equals("1")) {// 支付渠道(1 微信 2支付宝支付 4建行支付 6微信小程序支付)
  114. // 是否小程序支付--值为1,表示小程序支付;不传或值不为1,表示公众账号内支付
  115. map.put("is_minipg", "1");
  116. map.put("sub_appid", "wx9894a01b9e92c368");
  117. }else if(payWay.equals("2")){
  118. map.put("is_minipg", "2");
  119. }
  120. // --------微信支付请求
  121. Map<String, Object> wxMap = wx.pay(map, userOpenId,swiftpassConfig);
  122. if (wxMap.get("status").toString().equals("200")) {
  123. Map accountMap = new HashMap();
  124. accountMap.put("wx", wxMap);
  125. payForm.setParams(accountMap);
  126. return payForm;
  127. } else {
  128. throw new RuntimeException("请求支付失败。");
  129. }
  130. } catch (Exception e) {
  131. e.printStackTrace();
  132. }
  133. throw new RuntimeException("请求支付失败。");
  134. }
  135. }