package com.zsElectric.boot.business.service; import com.zsElectric.boot.business.mapper.RechargeLevelMapper; import com.zsElectric.boot.business.mapper.UserInfoMapper; import com.zsElectric.boot.business.mapper.UserOrderInfoMapper; import com.zsElectric.boot.business.model.entity.RechargeLevel; import com.zsElectric.boot.business.model.entity.UserOrderInfo; import com.zsElectric.boot.business.model.form.applet.AppLevelOrderForm; import com.zsElectric.boot.business.model.form.applet.AppUserPayForm; import com.zsElectric.boot.core.pay.WFT.WFTConstants; import com.zsElectric.boot.core.pay.swiftpass.config.SwiftpassConfig; import com.zsElectric.boot.core.pay.swiftpass.util.PayUtill; import com.zsElectric.boot.security.util.SecurityUtils; import jakarta.annotation.Resource; import jakarta.validation.Valid; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.SimpleDateFormat; import java.util.*; /** * 威富通支付服务 * 用于处理威富通JSAPI支付接口 * * @author zsElectric * @since 2025-12-29 */ @Slf4j @Service public class WFTOrderService { @Resource private UserOrderInfoMapper userOrderInfoMapper; @Resource private UserInfoMapper userInfoMapper; @Resource private RechargeLevelMapper rechargeLevelMapper; @Resource protected SwiftpassConfig swiftpassConfig; /** * 创建商户订单号 * 要求 32个字符内,只能是数字、大小写字母_-|*且在同一个商户号下唯一 * 组成 两位前缀 + 17位时间戳 + 9位id补零 + 4位随机数 合计32位 * * @param head 例如 商品-SP 退款-TK 等等 * @param id 用户id * @return */ public String createOrderNo(String head, Long id) { StringBuilder uid = new StringBuilder(id.toString()); Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); int length = uid.length(); for (int i = 0; i < 9 - length; i++) { uid.insert(0, "0"); } return head + sdf.format(date) + uid + (int) ((Math.random() * 9 + 1) * 1000); } /** * 金额元转分字符串 * * @param cny 元 * @return */ public String amount_fee(BigDecimal cny) { BigDecimal b2 = new BigDecimal("100"); return cny.multiply(b2).setScale(0, RoundingMode.DOWN).toString(); } public AppUserPayForm createOrder(@Valid AppLevelOrderForm appLevelOrderForm) { Long userId = SecurityUtils.getUserId(); String userOpenId = userInfoMapper.getAppletUserInfo(userId).getOpenid(); String orderNo = createOrderNo("SP", userId); //创建订单 UserOrderInfo orderInfo = new UserOrderInfo(); orderInfo.setUserId(userId); orderInfo.setOrderNo(orderNo); orderInfo.setLevelId(appLevelOrderForm.getLevelId()); orderInfo.setOpenid(userOpenId); userOrderInfoMapper.insert(orderInfo); //构建支付表单返回给前端支撑JsApi支付调用 AppUserPayForm payForm = new AppUserPayForm(); payForm.setOrderId(orderInfo.getId()).setOrderNo(orderNo); //查询档位 RechargeLevel level = rechargeLevelMapper.selectById(appLevelOrderForm.getLevelId()); PayUtill wx = new PayUtill(); /** * 小程序支付 */ String payWay = "1"; /** * 请求支付 */ try { log.debug("通知第三开始支付:"); // 通知第三方支付---------------------------------------------------------- SortedMap map = new TreeMap<>(); // 订单编号 map.put("out_trade_no", orderNo); // 商品描述 map.put("body", "购买充电抵扣券"); // 附加信息 map.put("attach", "支付人" + userId); // pifList.get(0).setHydOrderPayMoney(new BigDecimal("0.01")); // 总金额(分) map.put("total_fee", amount_fee(level.getMoney())); // 终端ip map.put("mch_create_ip", appLevelOrderForm.getIp()); // 签名方式 map.put("sign_type", "RSA_1_256"); // 回调地址 map.put("notify_url", swiftpassConfig.getNotify_url()); // 公众账号或小程序ID map.put("sub_appid", "wx9894a01b9e92c368"); if (payWay.equals("1")) {// 支付渠道(1 微信 2支付宝支付 4建行支付 6微信小程序支付) // 是否小程序支付--值为1,表示小程序支付;不传或值不为1,表示公众账号内支付 map.put("is_minipg", "1"); map.put("sub_appid", "wx9894a01b9e92c368"); }else if(payWay.equals("2")){ map.put("is_minipg", "2"); } // --------微信支付请求 Map wxMap = wx.pay(map, userOpenId,swiftpassConfig); if (wxMap.get("status").toString().equals("200")) { Map accountMap = new HashMap(); accountMap.put("wx", wxMap); payForm.setParams(accountMap); return payForm; } else { throw new RuntimeException("请求支付失败。"); } } catch (Exception e) { e.printStackTrace(); } throw new RuntimeException("请求支付失败。"); } }