AliPayCommonServiceImpl.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. package com.cyksj.service.pay.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.date.DateTime;
  4. import cn.hutool.core.date.DateUtil;
  5. import cn.hutool.core.util.RandomUtil;
  6. import cn.hutool.core.util.StrUtil;
  7. import com.alipay.api.AlipayApiException;
  8. import com.alipay.api.AlipayClient;
  9. import com.alipay.api.AlipayRequest;
  10. import com.alipay.api.domain.AlipayTradePagePayModel;
  11. import com.alipay.api.domain.AlipayTradeWapPayModel;
  12. import com.alipay.api.request.AlipayTradePagePayRequest;
  13. import com.alipay.api.request.AlipayTradeWapPayRequest;
  14. import com.baomidou.mybatisplus.core.toolkit.Assert;
  15. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  16. import com.cyksj.common.EnvCommonService;
  17. import com.cyksj.common.constant.Constant;
  18. import com.cyksj.common.exception.BusinessRuntimeException;
  19. import com.cyksj.common.snowflake.Sequence;
  20. import com.cyksj.common.util.Codec;
  21. import com.cyksj.common.util.Jsons;
  22. import com.cyksj.common.util.StringUtil;
  23. import com.cyksj.config.zfb.AliPayClientFactory;
  24. import com.cyksj.config.zfb.BaseZfbConfig;
  25. import com.cyksj.config.zfb.ZfbProductCode;
  26. import com.cyksj.mapper.*;
  27. import com.cyksj.mapper.channel.ShopConfigMapper;
  28. import com.cyksj.mapper.mirrorshop.UserMirrorShopFeeOrderDonMapper;
  29. import com.cyksj.mapper.order.OrderDonPostMapper;
  30. import com.cyksj.mapper.vip.VipOrderDonMapper;
  31. import com.cyksj.model.dto.AliPayParams;
  32. import com.cyksj.model.dto.OrderPayBuilderDto;
  33. import com.cyksj.model.entity.*;
  34. import com.cyksj.model.request.AlipayOrderDonReq;
  35. import com.cyksj.model.request.OrderAttach;
  36. import com.cyksj.service.pay.AliPayCommonService;
  37. import com.cyksj.service.sys.SysConfigService;
  38. import com.google.common.collect.Lists;
  39. import lombok.RequiredArgsConstructor;
  40. import lombok.extern.slf4j.Slf4j;
  41. import org.springframework.stereotype.Service;
  42. import java.math.BigDecimal;
  43. import java.math.RoundingMode;
  44. import java.util.Date;
  45. import java.util.List;
  46. import java.util.Optional;
  47. /*
  48. *项目名: netflix
  49. *文件名: AliPayCommonServiceImpl
  50. *创建者: JavaZou
  51. *创建时间:2023/6/14 15:19
  52. */
  53. @Service
  54. @RequiredArgsConstructor
  55. @Slf4j
  56. public class AliPayCommonServiceImpl implements AliPayCommonService {
  57. private final RegisterOrderDonMapper registerOrderDonMapper;
  58. private final OrderDonMapper orderDonMapper;
  59. private final GoodsDonMapper goodsDonMapper;
  60. private final GoodsDonCategoryRelateMapper goodsDonCategoryRelateMapper;
  61. private final ShopConfigMapper shopConfigMapper;
  62. private final EnvCommonService envCommonService;
  63. private final SysConfigService sysConfigService;
  64. private final OrderDonPostMapper orderDonPostMapper;
  65. private final UpgradeOrderDonMapper upgradeOrderDonMapper;
  66. private final UpgradePackageMapper upgradePackageMapper;
  67. private final GoodsWholesaleOrderDonMapper goodsWholesaleOrderDonMapper;
  68. private final GoodsWholesaleUserDepositMapper depositMapper;
  69. private final UserBenefitsOrderRecordMapper userBenefitsOrderRecordMapper;
  70. private final UserMirrorShopFeeOrderDonMapper feeOrderDonMapper;
  71. private final ZfbShopOrderDayLimitMapper zfbShopOrderDayLimitMapper;
  72. private final SpotifyUserUpgradeOrderMapper spotifyUserUpgradeOrderMapper;
  73. private final VipOrderDonMapper vipOrderDonMapper;
  74. private static final Sequence SEQUENCE = new Sequence(0);
  75. @Override
  76. public AliPayParams unifiedPayRegister(Long orderId, String productCode, String payMode, String returnUrl) throws Exception {
  77. RegisterOrderDon orderDon = registerOrderDonMapper.selectById(orderId);
  78. Assert.notNull(orderDon, "订单不存在");
  79. if (StrUtil.isEmpty(productCode)) throw BusinessRuntimeException.getInstance("支付异常");
  80. //获取支付宝默认商户
  81. String shopId = sysConfigService.getDefaultShopConfigByType("zfb");
  82. orderDon.setShopId(shopId);
  83. orderDon.setType(RegisterOrderDon.Type.ALI_PAY);
  84. if (orderDon.getType() != RegisterOrderDon.Type.ALI_PAY) {
  85. orderDon.setType(RegisterOrderDon.Type.ALI_PAY);
  86. }
  87. registerOrderDonMapper.updateById(orderDon);
  88. //回调地址
  89. String host = envCommonService.getNotifyHost();
  90. String notifyUrl = String.format("%s%s", host, BaseZfbConfig.zfb_register_notify_url);
  91. if (ZfbProductCode.MOBILE_WEB.getProductCode().equals(productCode) && BaseZfbConfig.qrPayMode_redirect.equals(payMode)) {
  92. returnUrl = String.format("%s%s", envCommonService.getDomain(), BaseZfbConfig.zfb_register_login_return_url);
  93. }
  94. String subject = "注册服务";
  95. AliPayParams aliPayParams = builderPayParams(orderDon.getId(),
  96. orderDon.getOrderNo(),
  97. orderDon.getShopId(),
  98. orderDon.getMoney(),
  99. orderDon.getCreatedTime(),
  100. subject,
  101. productCode,
  102. payMode,
  103. notifyUrl,
  104. returnUrl);
  105. return aliPayParams;
  106. }
  107. @Override
  108. public AliPayParams unifiedPay(AlipayOrderDonReq re, String productCode, String payMode) throws Exception {
  109. Long orderId = re.getOrderId();
  110. String returnUrl = re.getReturnUrl();
  111. OrderDon orderDon = orderDonMapper.selectById(orderId);
  112. Boolean isWebPay = orderDon.getIsWebPay();
  113. if (ZfbProductCode.PC_WEB.getProductCode().equals(productCode)) {
  114. orderDon.setIsWebPay(true);
  115. }
  116. if (ZfbProductCode.MOBILE_WEB.getProductCode().equals(productCode)) {
  117. orderDon.setIsWebPay(false);
  118. }
  119. //设备 默认使用益路同行
  120. if (orderDon.getGoodsId() == 15) {
  121. orderDon.setShopId(ShopConfig.ZFB_DEFAULT_APP_ID);
  122. }
  123. String appId = orderDon.getShopId();
  124. if (StrUtil.isEmpty(appId)) {
  125. appId = getZfbDayLimitShopId(orderDon);
  126. }
  127. //若切换支付环境
  128. if (StrUtil.isNotBlank(orderDon.getShopId()) && isWebPay != orderDon.getIsWebPay()) {
  129. //当前支付宝商户 不支持所有环境 使用默认支付宝
  130. ZfbShopOrderDayLimit dayLimit = zfbShopOrderDayLimitMapper.selectOne(Wrappers.lambdaQuery(ZfbShopOrderDayLimit.class)
  131. .eq(ZfbShopOrderDayLimit::getAppId, orderDon.getShopId())
  132. .last("limit 1"));
  133. if (dayLimit != null && dayLimit.getScope() != 0) {
  134. appId = sysConfigService.getDefaultShopConfigByType("zfb");
  135. //恢复支付宝每日限单数
  136. zfbShopOrderDayLimitMapper.incrDayLimitRemainOrders(orderDon.getShopId());
  137. //恢复小时限单数
  138. zfbShopOrderDayLimitMapper.incrHourLimitRemainOrders(orderDon.getShopId());
  139. }
  140. }
  141. if (OrderDon.Type.ALI_PAY != orderDon.getType()) {
  142. orderDon.setType(OrderDon.Type.ALI_PAY);
  143. }
  144. orderDon.setShopId(appId);
  145. orderDonMapper.updateById(orderDon);
  146. String notifyPath = BaseZfbConfig.notifyUrl;
  147. Boolean isUserBenefits = re.getIsUserBenefits();
  148. String subject;
  149. if (isUserBenefits) {
  150. subject = orderDon.getGoodsId() == 15 ? "套餐权益" : "续费减免";
  151. } else {
  152. GoodsDon goodsDon = goodsDonMapper.selectById(orderDon.getGoodsId());
  153. subject = goodsDon.getTitle().contains("ChatGPT") ? "AI订阅" : goodsDon.getTitle();
  154. }
  155. //回调地址
  156. String host = envCommonService.getNotifyHost();
  157. String notifyUrl = String.format("%s%s", host, notifyPath);
  158. //手机h5 跳转
  159. OrderDonPost orderDonPost = orderDonPostMapper.selectOne(Wrappers.lambdaQuery(OrderDonPost.class)
  160. .eq(OrderDonPost::getOrderId, orderId).last("limit 1"));
  161. //百度回传 跳转
  162. Boolean is_bdPost = false;
  163. if (orderDonPost != null && OrderDonPost.Type.BaiDu == orderDonPost.getType()) {
  164. is_bdPost = true;
  165. }
  166. if (StrUtil.isNotEmpty(returnUrl)) {
  167. log.info("支付宝支付成功返回地址:{}", returnUrl);
  168. }
  169. //是否在微信内支付 微信内支付宝支付后 支付宝回调页面不跳转
  170. Boolean isWx = Optional.ofNullable(re.getIsWx()).orElse(false);
  171. if (ZfbProductCode.MOBILE_WEB.getProductCode().equals(productCode) && !isWx) {
  172. if (is_bdPost) {
  173. //页面 拼接订单号,前端从url获取订单id.
  174. returnUrl = String.format("%s%s", envCommonService.getDomain(), BaseZfbConfig.SUCCESS_URL) + orderDon.getId();
  175. } else {
  176. if (StrUtil.isEmpty(returnUrl)) {
  177. //默认跳转页
  178. returnUrl = String.format("%s%s", envCommonService.getDomain(), BaseZfbConfig.DEFAULT_URL);
  179. }
  180. }
  181. }
  182. if (StrUtil.isNotEmpty(returnUrl)) {
  183. log.info("支付宝支付成功最终返回地址:{}", returnUrl);
  184. }
  185. AliPayParams aliPayParams = builderPayParams(orderDon.getId(),
  186. orderDon.getOrderNo(),
  187. orderDon.getShopId(),
  188. orderDon.getMoney(),
  189. orderDon.getCreatedTime(),
  190. subject,
  191. productCode,
  192. payMode,
  193. notifyUrl,
  194. returnUrl);
  195. return aliPayParams;
  196. }
  197. private ZfbShopOrderDayLimit getZfbShopOrdersDayLimit(Long goodsId, List<Integer> scope) {
  198. int hour = DateUtil.hour(DateTime.now(), true);
  199. //如果是ai订单 不分配给善缘
  200. Integer aiCount = goodsDonCategoryRelateMapper.selectCount(Wrappers.lambdaQuery(GoodsDonCategoryRelate.class)
  201. .eq(GoodsDonCategoryRelate::getCategory, 1)
  202. .eq(GoodsDonCategoryRelate::getGoodsId, goodsId));
  203. List<ZfbShopOrderDayLimit> orderDayLimits = null;
  204. //Ai订单 优先分配给崇宇
  205. if (aiCount > 0) {
  206. orderDayLimits = zfbShopOrderDayLimitMapper.selectList(Wrappers.lambdaQuery(ZfbShopOrderDayLimit.class)
  207. .le(ZfbShopOrderDayLimit::getHourStart, hour)
  208. .ge(ZfbShopOrderDayLimit::getHourEnd, hour)
  209. .gt(ZfbShopOrderDayLimit::getDayRemainOrders, 0)
  210. .gt(ZfbShopOrderDayLimit::getHourRemainOrders, 0)
  211. .eq(ZfbShopOrderDayLimit::getStatus, 1)
  212. .in(ZfbShopOrderDayLimit::getScope, scope)
  213. .eq(ZfbShopOrderDayLimit::getAppId, Constant.CY_SHOP_ID));
  214. }
  215. if (CollUtil.isEmpty(orderDayLimits)) {
  216. orderDayLimits = zfbShopOrderDayLimitMapper.selectList(Wrappers.lambdaQuery(ZfbShopOrderDayLimit.class)
  217. .le(ZfbShopOrderDayLimit::getHourStart, hour)
  218. .ge(ZfbShopOrderDayLimit::getHourEnd, hour)
  219. .gt(ZfbShopOrderDayLimit::getDayRemainOrders, 0)
  220. .gt(ZfbShopOrderDayLimit::getHourRemainOrders, 0)
  221. .eq(ZfbShopOrderDayLimit::getStatus, 1)
  222. .in(ZfbShopOrderDayLimit::getScope, scope)
  223. .ne(aiCount > 0, ZfbShopOrderDayLimit::getAppId, Constant.SY_SHOP_ID));
  224. }
  225. if (orderDayLimits.isEmpty()) {
  226. return null;
  227. }
  228. int randomIndex = RandomUtil.randomInt(orderDayLimits.size());
  229. ZfbShopOrderDayLimit dayLimit = orderDayLimits.get(randomIndex);
  230. Integer update = zfbShopOrderDayLimitMapper.decrRemainOrdersById(dayLimit.getId());
  231. if (update == 1) {
  232. return dayLimit;
  233. }
  234. for (ZfbShopOrderDayLimit orderDayLimit : orderDayLimits) {
  235. update = zfbShopOrderDayLimitMapper.decrRemainOrdersById(orderDayLimit.getId());
  236. if (update == 1) {
  237. return orderDayLimit;
  238. }
  239. }
  240. return null;
  241. }
  242. public AliPayParams builderPayParams(Long orderId, String orderNo, String shopId, BigDecimal money, Date orderCreatedTime, String subject, String productCode, String payMode, String notifyUrl, String returnUrl) throws Exception {
  243. OrderPayBuilderDto orderPayBuilderDto = new OrderPayBuilderDto();
  244. orderPayBuilderDto.setOrderId(orderId);
  245. orderPayBuilderDto.setMoney(money);
  246. orderPayBuilderDto.setOrderNo(orderNo);
  247. orderPayBuilderDto.setSubject(subject);
  248. orderPayBuilderDto.setOrderCreatedTime(orderCreatedTime);
  249. orderPayBuilderDto.setProductCode(productCode);
  250. orderPayBuilderDto.setPayMode(payMode);
  251. orderPayBuilderDto.setReturnUrl(returnUrl);
  252. orderPayBuilderDto.setNotifyUrl(notifyUrl);
  253. orderPayBuilderDto.setShopId(shopId);
  254. AliPayParams aliPayParams = commonAliPay(orderPayBuilderDto);
  255. return aliPayParams;
  256. }
  257. public AliPayParams commonAliPay(OrderPayBuilderDto orderPayBuilderDto) throws Exception {
  258. AlipayClient alipayClient = AliPayClientFactory.getAlipayClient(Optional.ofNullable(orderPayBuilderDto.getShopId()).orElse(ShopConfig.ZFB_DEFAULT_APP_ID));
  259. String returnUrl = orderPayBuilderDto.getReturnUrl();
  260. String notifyUrl = orderPayBuilderDto.getNotifyUrl();
  261. // 创建API对应的request
  262. AlipayRequest alipayRequest = getAliAayRequest(orderPayBuilderDto);
  263. if (alipayRequest == null) {
  264. throw BusinessRuntimeException.getInstance("支付宝不支持该支付方式:{0}", orderPayBuilderDto.getProductCode());
  265. }
  266. if (StrUtil.isNotBlank(returnUrl)) {
  267. alipayRequest.setReturnUrl(returnUrl);
  268. }
  269. alipayRequest.setNotifyUrl(notifyUrl);
  270. String form = null;
  271. try {
  272. //调用SDK生成表单
  273. //pagePay 统一下单接口
  274. form = alipayClient.pageExecute(alipayRequest).getBody();
  275. } catch (AlipayApiException e) {
  276. log.error("支付宝生成表单错误:{}", StringUtil.getErrorText(e));
  277. }
  278. log.info("支付宝返回参数:{}", form);
  279. return new AliPayParams(form);
  280. }
  281. /**
  282. * 支付宝获取不同的request
  283. */
  284. private AlipayRequest getAliAayRequest(OrderPayBuilderDto payBuilderDto) throws Exception {
  285. Long orderId = payBuilderDto.getOrderId();
  286. BigDecimal money = payBuilderDto.getMoney();
  287. String orderNo = payBuilderDto.getOrderNo();
  288. //productCode 销售产品码,与支付宝签约的产品码名称
  289. String productCode = payBuilderDto.getProductCode();
  290. String subject = payBuilderDto.getSubject();
  291. Date orderCreatedTime = payBuilderDto.getOrderCreatedTime();
  292. //网页支付跳转模式
  293. String qrPayMode = payBuilderDto.getPayMode();
  294. OrderAttach attach = new OrderAttach();
  295. attach.setO(orderId);
  296. String body = Codec.DoBase64.custom()
  297. .setData(Jsons.toJson(attach)).encodeAsText();
  298. String moneyStr = money.divide(new BigDecimal(100), 2, RoundingMode.HALF_EVEN).toString();
  299. if (ZfbProductCode.MOBILE_WEB.getProductCode().equals(productCode)) {
  300. //手机网站支付
  301. AlipayTradeWapPayRequest alipayTradeWapPayRequest = new AlipayTradeWapPayRequest();
  302. //手机网站支付支付参数
  303. AlipayTradeWapPayModel alipayTradeWapPayModel = new AlipayTradeWapPayModel();
  304. alipayTradeWapPayModel.setBody(body);
  305. alipayTradeWapPayModel.setOutTradeNo(orderNo);
  306. alipayTradeWapPayModel.setTotalAmount(moneyStr);
  307. alipayTradeWapPayModel.setSubject(subject);
  308. alipayTradeWapPayModel.setProductCode(productCode);
  309. //5分钟订单失效时间
  310. alipayTradeWapPayModel.setTimeExpire(DateUtil.offsetMinute(orderCreatedTime, 5).toString());
  311. alipayTradeWapPayRequest.setBizModel(alipayTradeWapPayModel);
  312. return alipayTradeWapPayRequest;
  313. } else if (ZfbProductCode.PC_WEB.getProductCode().equals(productCode)) {
  314. //电脑网站支付
  315. AlipayTradePagePayRequest alipayTradePagePayRequest = new AlipayTradePagePayRequest();
  316. //电脑网站支付参数
  317. AlipayTradePagePayModel alipayTradePagePayModel = new AlipayTradePagePayModel();
  318. alipayTradePagePayModel.setBody(body);
  319. alipayTradePagePayModel.setOutTradeNo(orderNo);
  320. alipayTradePagePayModel.setTotalAmount(moneyStr);
  321. alipayTradePagePayModel.setSubject(subject);
  322. alipayTradePagePayModel.setProductCode(productCode);
  323. //5分钟订单失效时间
  324. alipayTradePagePayModel.setTimeExpire(DateUtil.offsetMinute(orderCreatedTime, 5).toString());
  325. if (StrUtil.isEmpty(qrPayMode)) {
  326. qrPayMode = BaseZfbConfig.qrPayMode_orderCode;
  327. }
  328. alipayTradePagePayModel.setQrPayMode(qrPayMode);
  329. alipayTradePagePayRequest.setBizModel(alipayTradePagePayModel);
  330. return alipayTradePagePayRequest;
  331. }
  332. return null;
  333. }
  334. @Override
  335. public AliPayParams unifiedPayUpgrade(AlipayOrderDonReq re, String productCode, String payMode) throws Exception {
  336. Long orderId = re.getOrderId();
  337. String returnUrl = re.getReturnUrl();
  338. UpgradeOrderDon orderDon = upgradeOrderDonMapper.selectById(orderId);
  339. //获取支付宝默认商户
  340. String appId = sysConfigService.getDefaultShopConfigByType("zfb");
  341. if (UpgradeOrderDon.Type.ALI_PAY != orderDon.getType()) {
  342. orderDon.setType(UpgradeOrderDon.Type.ALI_PAY);
  343. }
  344. orderDon.setShopId(appId);
  345. if (ZfbProductCode.PC_WEB.getProductCode().equals(productCode)) {
  346. orderDon.setIsWebPay(true);
  347. }
  348. if (ZfbProductCode.MOBILE_WEB.getProductCode().equals(productCode)) {
  349. orderDon.setIsWebPay(false);
  350. }
  351. upgradeOrderDonMapper.updateById(orderDon);
  352. UpgradePackage upgradePackage = upgradePackageMapper.selectById(orderDon.getPackageId());
  353. String subject = upgradePackage.getName();
  354. //回调地址
  355. String host = envCommonService.getNotifyHost();
  356. String notifyUrl = String.format("%s%s", host, BaseZfbConfig.upgradeNotifyUrl);
  357. //是否在微信内支付 微信内支付宝支付后 支付宝回调页面不跳转
  358. Boolean isWx = Optional.ofNullable(re.getIsWx()).orElse(false);
  359. if (ZfbProductCode.MOBILE_WEB.getProductCode().equals(productCode) && !isWx) {
  360. //默认跳转页
  361. returnUrl = String.format("%s%s", envCommonService.getDomain(), BaseZfbConfig.DEFAULT_URL);
  362. }
  363. return builderPayParams(orderDon.getId(),
  364. orderDon.getOrderNo(),
  365. orderDon.getShopId(),
  366. orderDon.getMoney(),
  367. orderDon.getCreatedTime(),
  368. subject,
  369. productCode,
  370. payMode,
  371. notifyUrl,
  372. returnUrl);
  373. }
  374. @Override
  375. public AliPayParams aliPayUnifiedWholesale(AlipayOrderDonReq re, String productCode, String payMode) throws Exception {
  376. Long orderId = re.getOrderId();
  377. String returnUrl = re.getReturnUrl();
  378. GoodsWholesaleOrderDon orderDon = goodsWholesaleOrderDonMapper.selectById(orderId);
  379. //获取支付宝默认商户
  380. String appId = sysConfigService.getDefaultShopConfigByType("zfb");
  381. if (GoodsWholesaleOrderDon.Type.ALI_PAY != orderDon.getType()) {
  382. orderDon.setType(GoodsWholesaleOrderDon.Type.ALI_PAY);
  383. }
  384. orderDon.setShopId(appId);
  385. if (ZfbProductCode.PC_WEB.getProductCode().equals(productCode)) {
  386. orderDon.setIsWebPay(true);
  387. }
  388. if (ZfbProductCode.MOBILE_WEB.getProductCode().equals(productCode)) {
  389. orderDon.setIsWebPay(false);
  390. }
  391. goodsWholesaleOrderDonMapper.updateById(orderDon);
  392. GoodsDon goodsDon = goodsDonMapper.selectById(orderDon.getGoodsId());
  393. String subject = goodsDon.getTitle();
  394. //回调地址
  395. String host = envCommonService.getNotifyHost();
  396. String notifyUrl = String.format("%s%s", host, BaseZfbConfig.wholesaleNotifyUrl);
  397. //是否在微信内支付 微信内支付宝支付后 支付宝回调页面不跳转
  398. Boolean isWx = Optional.ofNullable(re.getIsWx()).orElse(false);
  399. if (StrUtil.isEmpty(returnUrl) && ZfbProductCode.MOBILE_WEB.getProductCode().equals(productCode) && !isWx) {
  400. //默认跳转页
  401. returnUrl = String.format("%s%s", envCommonService.getDomain(), BaseZfbConfig.DEFAULT_URL);
  402. }
  403. return builderPayParams(orderDon.getId(),
  404. orderDon.getOrderNo(),
  405. orderDon.getShopId(),
  406. orderDon.getMoney(),
  407. orderDon.getUpdateTime(),
  408. subject,
  409. productCode,
  410. payMode,
  411. notifyUrl,
  412. returnUrl);
  413. }
  414. @Override
  415. public AliPayParams aliPayUnifiedWholesaleDeposit(AlipayOrderDonReq re, String productCode, String payMode) throws Exception {
  416. Long orderId = re.getOrderId();
  417. String returnUrl = re.getReturnUrl();
  418. GoodsWholesaleUserDeposit orderDon = depositMapper.selectById(orderId);
  419. //获取支付宝默认商户
  420. String appId = sysConfigService.getDefaultShopConfigByType("zfb");
  421. if (GoodsWholesaleUserDeposit.Type.ALI_PAY != orderDon.getType()) {
  422. orderDon.setType(GoodsWholesaleUserDeposit.Type.ALI_PAY);
  423. }
  424. orderDon.setShopId(appId);
  425. if (ZfbProductCode.PC_WEB.getProductCode().equals(productCode)) {
  426. orderDon.setIsWebPay(true);
  427. }
  428. if (ZfbProductCode.MOBILE_WEB.getProductCode().equals(productCode)) {
  429. orderDon.setIsWebPay(false);
  430. }
  431. depositMapper.updateById(orderDon);
  432. String subject = "押金";
  433. //回调地址
  434. String host = envCommonService.getNotifyHost();
  435. String notifyUrl = String.format("%s%s", host, BaseZfbConfig.wholesaleDepositNotifyUrl);
  436. //是否在微信内支付 微信内支付宝支付后 支付宝回调页面不跳转
  437. Boolean isWx = Optional.ofNullable(re.getIsWx()).orElse(false);
  438. if (StrUtil.isEmpty(returnUrl) && ZfbProductCode.MOBILE_WEB.getProductCode().equals(productCode) && !isWx) {
  439. //默认跳转页
  440. returnUrl = String.format("%s%s", envCommonService.getDomain(), BaseZfbConfig.DEFAULT_URL);
  441. }
  442. //退款后重新支付
  443. if (orderDon.getStatus() == GoodsWholesaleUserDeposit.Status.refund) {
  444. orderDon.setOrderNo(SEQUENCE.nextId().toString());
  445. }
  446. return builderPayParams(orderDon.getId(),
  447. orderDon.getOrderNo(),
  448. orderDon.getShopId(),
  449. orderDon.getMoney(),
  450. orderDon.getUpdateTime(),
  451. subject,
  452. productCode,
  453. payMode,
  454. notifyUrl,
  455. returnUrl);
  456. }
  457. @Override
  458. public AliPayParams openShopAliPay(AlipayOrderDonReq re, String productCode, String payMode) throws Exception {
  459. Long orderId = re.getOrderId();
  460. String returnUrl = re.getReturnUrl();
  461. UserMirrorShopFeeOrderDon orderDon = feeOrderDonMapper.selectById(orderId);
  462. //获取支付宝默认商户
  463. String appId = sysConfigService.getDefaultShopConfigByType("zfb");
  464. if (UserMirrorShopFeeOrderDon.Type.ALI_PAY != orderDon.getType()) {
  465. orderDon.setType(UserMirrorShopFeeOrderDon.Type.ALI_PAY);
  466. }
  467. orderDon.setShopId(appId);
  468. feeOrderDonMapper.updateById(orderDon);
  469. String subject = "押金";
  470. //回调地址
  471. String host = envCommonService.getNotifyHost();
  472. String notifyUrl = String.format("%s%s", host, BaseZfbConfig.userMirrorOpenShopNotifyUrl);
  473. //退款后重新支付
  474. if (orderDon.getStatus() == UserMirrorShopFeeOrderDon.Status.refund) {
  475. orderDon.setOrderNo(SEQUENCE.nextId().toString());
  476. }
  477. return builderPayParams(orderDon.getId(),
  478. orderDon.getOrderNo(),
  479. orderDon.getShopId(),
  480. orderDon.getMoney(),
  481. orderDon.getUpdateTime(),
  482. subject,
  483. productCode,
  484. payMode,
  485. notifyUrl,
  486. returnUrl);
  487. }
  488. @Override
  489. public AliPayParams aliPaySpotifyUpgrade(AlipayOrderDonReq re, String productCode, String payMode) throws Exception {
  490. Long orderId = re.getOrderId();
  491. String returnUrl = re.getReturnUrl();
  492. SpotifyUserUpgradeOrder orderDon = spotifyUserUpgradeOrderMapper.selectById(orderId);
  493. //获取支付宝默认商户
  494. String appId = sysConfigService.getDefaultShopConfigByType("zfb");
  495. if (SpotifyUserUpgradeOrder.Type.ALI_PAY != orderDon.getType()) {
  496. orderDon.setType(SpotifyUserUpgradeOrder.Type.ALI_PAY);
  497. }
  498. orderDon.setShopId(appId);
  499. spotifyUserUpgradeOrderMapper.updateById(orderDon);
  500. String subject = "Spotify家庭组升级";
  501. //回调地址
  502. String host = envCommonService.getNotifyHost();
  503. String notifyUrl = String.format("%s%s", host, BaseZfbConfig.spotifyUpgradeNotifyUrl);
  504. //是否在微信内支付 微信内支付宝支付后 支付宝回调页面不跳转
  505. if (StrUtil.isEmpty(returnUrl)) {
  506. //默认跳转页
  507. returnUrl = String.format("%s%s", envCommonService.getDomain(), BaseZfbConfig.DEFAULT_URL);
  508. }
  509. return builderPayParams(orderDon.getId(),
  510. orderDon.getOrderNo(),
  511. orderDon.getShopId(),
  512. orderDon.getMoney(),
  513. orderDon.getUpdateTime(),
  514. subject,
  515. productCode,
  516. payMode,
  517. notifyUrl,
  518. returnUrl);
  519. }
  520. @Override
  521. public AliPayParams unifiedPayVip(Long orderId, String productCode, String payMode, String returnUrl) throws Exception {
  522. VipOrderDon orderDon = vipOrderDonMapper.selectById(orderId);
  523. Assert.notNull(orderDon, "订单不存在");
  524. if (StrUtil.isEmpty(productCode)) throw BusinessRuntimeException.getInstance("支付异常");
  525. //获取支付宝默认商户
  526. String shopId = sysConfigService.getDefaultShopConfigByType("zfb");
  527. orderDon.setShopId(shopId);
  528. orderDon.setType(VipOrderDon.Type.ALI_PAY);
  529. if (orderDon.getType() != VipOrderDon.Type.ALI_PAY) {
  530. orderDon.setType(VipOrderDon.Type.ALI_PAY);
  531. }
  532. vipOrderDonMapper.updateById(orderDon);
  533. //回调地址
  534. String host = envCommonService.getNotifyHost();
  535. String notifyUrl = String.format("%s%s", host, BaseZfbConfig.zfb_vip_notify_url);
  536. if (ZfbProductCode.MOBILE_WEB.getProductCode().equals(productCode) && BaseZfbConfig.qrPayMode_redirect.equals(payMode)) {
  537. returnUrl = String.format("%s%s", envCommonService.getDomain(), BaseZfbConfig.zfb_vip_login_return_url);
  538. }
  539. String subject = orderDon.getPayTitle();
  540. AliPayParams aliPayParams = builderPayParams(orderDon.getId(),
  541. orderDon.getOrderNo(),
  542. orderDon.getShopId(),
  543. orderDon.getMoney(),
  544. orderDon.getCreatedTime(),
  545. subject,
  546. productCode,
  547. payMode,
  548. notifyUrl,
  549. returnUrl);
  550. return aliPayParams;
  551. }
  552. public String getZfbDayLimitShopId(OrderDon orderDon) {
  553. String appId = orderDon.getShopId();
  554. if (orderDon.getPopularizeId() != null) {
  555. ShopConfig shopConfig = shopConfigMapper.getByChannelPopularizeId(orderDon.getPopularizeId(), "zfb");
  556. if (shopConfig != null) {
  557. appId = shopConfig.getAppId();
  558. }
  559. }
  560. List<Integer> scope = Lists.newArrayList(0);
  561. //是否是PC的端支付
  562. if (orderDon.getIsWebPay()) {
  563. scope.add(1);
  564. } else scope.add(2);
  565. //排除推广链接
  566. if (StrUtil.isEmpty(appId)) {
  567. //获取每日订单限制 支付宝商户
  568. ZfbShopOrderDayLimit dayLimit = getZfbShopOrdersDayLimit(orderDon.getGoodsId(), scope);
  569. if (dayLimit == null) {
  570. //获取支付宝默认商户
  571. appId = sysConfigService.getDefaultShopConfigByType("zfb");
  572. } else {
  573. appId = dayLimit.getAppId();
  574. }
  575. }
  576. return appId;
  577. }
  578. }