OrderPayService.java 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. package com.izouma.nineth.service;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.alipay.api.AlipayApiException;
  4. import com.alipay.api.AlipayClient;
  5. import com.alipay.api.request.AlipayTradePrecreateRequest;
  6. import com.alipay.api.response.AlipayTradePrecreateResponse;
  7. import com.google.zxing.WriterException;
  8. import com.izouma.nineth.config.AlipayProperties;
  9. import com.izouma.nineth.config.Constants;
  10. import com.izouma.nineth.config.GeneralProperties;
  11. import com.izouma.nineth.domain.*;
  12. import com.izouma.nineth.domain.nftdomain.DomainAsk;
  13. import com.izouma.nineth.dto.PayQuery;
  14. import com.izouma.nineth.dto.UserBankCard;
  15. import com.izouma.nineth.enums.*;
  16. import com.izouma.nineth.event.OrderNotifyEvent;
  17. import com.izouma.nineth.exception.BusinessException;
  18. import com.izouma.nineth.repo.*;
  19. import com.izouma.nineth.repo.nftdomain.DomainAskRepo;
  20. import com.izouma.nineth.service.nftdomain.DomainAskService;
  21. import com.izouma.nineth.utils.SnowflakeIdWorker;
  22. import lombok.AllArgsConstructor;
  23. import lombok.extern.slf4j.Slf4j;
  24. import org.apache.commons.lang3.StringUtils;
  25. import org.apache.rocketmq.spring.core.RocketMQTemplate;
  26. import org.springframework.cache.annotation.Cacheable;
  27. import org.springframework.security.crypto.password.PasswordEncoder;
  28. import org.springframework.stereotype.Service;
  29. import org.springframework.web.context.request.RequestContextHolder;
  30. import org.springframework.web.context.request.ServletRequestAttributes;
  31. import javax.swing.text.DateFormatter;
  32. import java.awt.*;
  33. import java.io.IOException;
  34. import java.math.BigDecimal;
  35. import java.net.URLEncoder;
  36. import java.nio.charset.StandardCharsets;
  37. import java.text.DateFormat;
  38. import java.time.LocalDateTime;
  39. import java.time.format.DateTimeFormatter;
  40. import java.util.Map;
  41. import java.util.Objects;
  42. import java.util.Optional;
  43. import java.util.stream.Stream;
  44. @Service
  45. @Slf4j
  46. @AllArgsConstructor
  47. public class OrderPayService {
  48. private static String PAY_CHANNEL = Constants.PayChannel.SAND;
  49. private final OrderService orderService;
  50. private final OrderRepo orderRepo;
  51. private final MintOrderRepo mintOrderRepo;
  52. private final GiftOrderRepo giftOrderRepo;
  53. private final SandPayService sandPayService;
  54. private final HMPayService hmPayService;
  55. private final GeneralProperties generalProperties;
  56. private final UserBalanceService userBalanceService;
  57. private final RocketMQTemplate rocketMQTemplate;
  58. private final GiftOrderService giftOrderService;
  59. private final MintOrderService mintOrderService;
  60. private final UserRepo userRepo;
  61. private final SnowflakeIdWorker snowflakeIdWorker;
  62. private final RechargeOrderRepo rechargeOrderRepo;
  63. private final SysConfigService sysConfigService;
  64. private final PasswordEncoder passwordEncoder;
  65. private final PayEaseService payEaseService;
  66. private final UserBankCardRepo userBankCardRepo;
  67. private final AuctionOrderRepo auctionOrderRepo;
  68. private final AuctionOrderService auctionOrderService;
  69. private final IdentityAuthRepo identityAuthRepo;
  70. private final AlipayClient alipayClient;
  71. private final AlipayProperties alipayProperties;
  72. private final AlipayService alipayService;
  73. private final PhotoAssetRepo photoAssetRepo;
  74. private final PhotoAssetService photoAssetService;
  75. private final DomainOrderService domainOrderService;
  76. private final DomainOrderRepo domainOrderRepo;
  77. private final TradeAuctionRepo tradeAuctionRepo;
  78. private final TradeAuctionOrderRepo tradeAuctionOrderRepo;
  79. private final TradeAuctionOrderService tradeAuctionOrderService;
  80. private final DomainAskRepo domainAskRepo;
  81. private final DomainAskService domainAskService;
  82. public static void setPayChannel(String payChannel) {
  83. log.info("set pay channel {}", payChannel);
  84. if (Constants.PayChannel.HM.equals(payChannel) || Constants.PayChannel.SAND.equals(payChannel)) {
  85. PAY_CHANNEL = payChannel;
  86. }
  87. }
  88. @Cacheable(value = "payOrder", key = "'order#'+#orderId")
  89. public String payOrder(Long orderId) {
  90. Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  91. if (order.getStatus() != OrderStatus.NOT_PAID) {
  92. throw new BusinessException("订单状态错误");
  93. }
  94. switch (PAY_CHANNEL) {
  95. case Constants.PayChannel.SAND:
  96. return sandPayService.pay(orderId + "", order.getName(), order.getTotalPrice(),
  97. order.getCreatedAt().plusMinutes(3), "order");
  98. case Constants.PayChannel.HM:
  99. return hmPayService.requestAlipay(orderId + "", order.getTotalPrice(), order.getName(),
  100. HMPayService.getTimeout(order.getCreatedAt(), 180), Constants.OrderNotifyType.ORDER,
  101. generalProperties.resolveFrontUrl(order.getCompanyId(), "/orderDetail?id=" + orderId));
  102. }
  103. throw new BusinessException(Constants.PAY_ERR_MSG);
  104. }
  105. private String aliRequest(Long orderId, BigDecimal amount, String subject, String type) {
  106. AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
  107. request.setNotifyUrl(alipayProperties.getNotifyUrl());
  108. JSONObject bizContent = new JSONObject();
  109. bizContent.put("out_trade_no", orderId + "");
  110. bizContent.put("total_amount", amount);
  111. bizContent.put("subject", subject);
  112. DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  113. bizContent.put("time_expire", df.format(LocalDateTime.now().plusSeconds(175)));
  114. JSONObject body = new JSONObject();
  115. body.put("type", type);
  116. body.put("orderId", orderId);
  117. bizContent.put("body", body.toString());
  118. request.setBizContent(bizContent.toString());
  119. AlipayTradePrecreateResponse response = null;
  120. try {
  121. response = alipayClient.execute(request);
  122. } catch (AlipayApiException e) {
  123. e.printStackTrace();
  124. throw new BusinessException(Constants.PAY_ERR_MSG, e.getErrMsg());
  125. }
  126. if (response.isSuccess()) {
  127. return response.getQrCode();
  128. } else {
  129. throw new BusinessException(response.getSubMsg());
  130. }
  131. }
  132. @Cacheable(value = "payOrder", key = "'order#'+#orderId")
  133. public String payOrderAli(Long orderId) {
  134. Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  135. if (order.getStatus() != OrderStatus.NOT_PAID) {
  136. throw new BusinessException("订单状态错误");
  137. }
  138. String qrCode = aliRequest(orderId, order.getTotalPrice(), order.getName(), Constants.OrderNotifyType.ORDER);
  139. String ua = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()
  140. .getHeader("User-Agent");
  141. if (ua.toLowerCase().contains("micromessenger")) {
  142. return "/static/wx_alipay_bridge.html?payUrl=" + URLEncoder
  143. .encode(Constants.ALIPAY_URL_SCHEME + qrCode, StandardCharsets.UTF_8)
  144. + "&orderId=" + orderId + "&type=order&returnUrl="
  145. + URLEncoder
  146. .encode(generalProperties.resolveFrontUrl(order.getCompanyId(), "/store"), StandardCharsets.UTF_8);
  147. } else {
  148. return Constants.ALIPAY_URL_SCHEME + qrCode;
  149. }
  150. }
  151. @Cacheable(value = "payOrder", key = "'order#'+#orderId")
  152. public String payOrderQuick(Long orderId) {
  153. Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  154. if (order.getStatus() != OrderStatus.NOT_PAID) {
  155. throw new BusinessException("订单状态错误");
  156. }
  157. return sandPayService.payQuick(orderId + "", order.getName(), order.getTotalPrice(),
  158. order.getCreatedAt().plusMinutes(3), Constants.OrderNotifyType.ORDER,
  159. generalProperties.resolveFrontUrl(order.getCompanyId(), "/store"));
  160. }
  161. @Cacheable(value = "payOrder", key = "'order#'+#orderId")
  162. public String payOrderQuickBind(Long orderId) {
  163. Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  164. if (order.getStatus() != OrderStatus.NOT_PAID) {
  165. throw new BusinessException("订单状态错误");
  166. }
  167. IdentityAuth identityAuth = identityAuthRepo
  168. .findFirstByUserIdAndStatusAndDelFalseOrderByCreatedAtDesc(order.getUserId(), AuthStatus.SUCCESS)
  169. .orElseThrow(new BusinessException("请先完成实名认证"));
  170. return sandPayService.payQuickBind(orderId + "", order.getName(), order.getTotalPrice(),
  171. order.getCreatedAt().plusMinutes(3), Constants.OrderNotifyType.ORDER,
  172. generalProperties.resolveFrontUrl(order.getCompanyId(), "/store"),
  173. order.getUserId(), identityAuth.getRealName(), identityAuth.getIdNo());
  174. }
  175. public void payOrderBalance(Long orderId, Long userId, String tradeCode) {
  176. Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  177. if (order.getStatus() != OrderStatus.NOT_PAID) {
  178. throw new BusinessException("订单状态错误");
  179. }
  180. checkTradeCode(userId, tradeCode, order.getUserId());
  181. BalanceRecord record = userBalanceService
  182. .balancePay(order.getUserId(), order.getTotalPrice(), orderId, order.getName());
  183. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  184. new OrderNotifyEvent(orderId, PayMethod.BALANCE, record.getId().toString(),
  185. System.currentTimeMillis()));
  186. }
  187. @Cacheable(value = "payOrder", key = "'order#'+#orderId")
  188. public Map<String, Object> payOrderAgreement(Long orderId, String bindCardId) {
  189. Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  190. if (order.getStatus() != OrderStatus.NOT_PAID) {
  191. throw new BusinessException("订单状态错误");
  192. }
  193. if (StringUtils.isEmpty(bindCardId)) {
  194. bindCardId = userBankCardRepo.findByUserId(order.getUserId())
  195. .stream().map(UserBankCard::getBindCardId).findFirst().orElse(null);
  196. }
  197. if (StringUtils.isEmpty(bindCardId)) {
  198. throw new BusinessException("请先绑定银行卡");
  199. }
  200. return payEaseService.pay(order.getName(), orderId.toString(), order.getTotalPrice(),
  201. order.getUserId().toString(), bindCardId, Constants.OrderNotifyType.ORDER);
  202. }
  203. public void confirmOrderAgreement(String requestId, String paymentOrderId, String code) {
  204. try {
  205. payEaseService.payConfirm(requestId, paymentOrderId, code);
  206. } catch (BusinessException e) {
  207. try {
  208. new Thread(() -> {
  209. orderService.cancel(Long.parseLong(requestId));
  210. }).start();
  211. } catch (Exception ee) {
  212. }
  213. throw e;
  214. }
  215. }
  216. @Cacheable(value = "payOrder", key = "'gift#'+#orderId")
  217. public String payGiftOrder(Long orderId) {
  218. GiftOrder order = giftOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  219. if (order.getStatus() != OrderStatus.NOT_PAID) {
  220. throw new BusinessException("订单状态错误");
  221. }
  222. switch (PAY_CHANNEL) {
  223. case Constants.PayChannel.SAND:
  224. return sandPayService.pay(orderId + "", "转赠" + order.getAssetId(), order.getGasPrice(),
  225. order.getCreatedAt().plusMinutes(3), Constants.OrderNotifyType.GIFT);
  226. case Constants.PayChannel.HM:
  227. return hmPayService.requestAlipay(orderId + "", order.getGasPrice(),
  228. "转赠" + order.getAssetId(),
  229. HMPayService.getTimeout(order.getCreatedAt(), 180),
  230. Constants.OrderNotifyType.GIFT, generalProperties
  231. .resolveFrontUrl(order.getCompanyId(), "/store"));
  232. }
  233. throw new BusinessException(Constants.PAY_ERR_MSG);
  234. }
  235. @Cacheable(value = "payOrder", key = "'gift#'+#orderId")
  236. public String payGiftAli(Long orderId) {
  237. GiftOrder order = giftOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  238. if (order.getStatus() != OrderStatus.NOT_PAID) {
  239. throw new BusinessException("订单状态错误");
  240. }
  241. String qrCode = aliRequest(orderId, order.getGasPrice(), "转赠", Constants.OrderNotifyType.GIFT);
  242. String ua = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()
  243. .getHeader("User-Agent");
  244. if (ua.toLowerCase().contains("micromessenger")) {
  245. return "/static/wx_alipay_bridge.html?payUrl=" + URLEncoder
  246. .encode(Constants.ALIPAY_URL_SCHEME + qrCode, StandardCharsets.UTF_8)
  247. + "&orderId=" + orderId + "&type=gift&returnUrl="
  248. + URLEncoder
  249. .encode(generalProperties.resolveFrontUrl(order.getCompanyId(), "/store"), StandardCharsets.UTF_8);
  250. } else {
  251. return Constants.ALIPAY_URL_SCHEME + qrCode;
  252. }
  253. }
  254. @Cacheable(value = "payOrder", key = "'gift#'+#orderId")
  255. public String payGiftQuick(Long orderId) {
  256. GiftOrder order = giftOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  257. if (order.getStatus() != OrderStatus.NOT_PAID) {
  258. throw new BusinessException("订单状态错误");
  259. }
  260. return sandPayService.payQuick(orderId + "", "转赠" + order.getAssetId(), order.getGasPrice(),
  261. order.getCreatedAt().plusMinutes(3), Constants.OrderNotifyType.GIFT,
  262. generalProperties.resolveFrontUrl(order.getCompanyId(), "/store"));
  263. }
  264. @Cacheable(value = "payOrder", key = "'gift#'+#orderId")
  265. public String payGiftQuickBind(Long orderId) {
  266. GiftOrder order = giftOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  267. if (order.getStatus() != OrderStatus.NOT_PAID) {
  268. throw new BusinessException("订单状态错误");
  269. }
  270. IdentityAuth identityAuth = identityAuthRepo
  271. .findFirstByUserIdAndStatusAndDelFalseOrderByCreatedAtDesc(order.getUserId(), AuthStatus.SUCCESS)
  272. .orElseThrow(new BusinessException("请先完成实名认证"));
  273. return sandPayService.payQuickBind(orderId + "", "转赠" + order.getAssetId(), order.getGasPrice(),
  274. order.getCreatedAt().plusMinutes(3), Constants.OrderNotifyType.GIFT,
  275. generalProperties.resolveFrontUrl(order.getCompanyId(), "/store"),
  276. order.getUserId(), identityAuth.getRealName(), identityAuth.getIdNo());
  277. }
  278. public void payGiftBalance(Long orderId, Long userId, String tradeCode) {
  279. GiftOrder order = giftOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  280. if (order.getStatus() != OrderStatus.NOT_PAID) {
  281. throw new BusinessException("订单状态错误");
  282. }
  283. checkTradeCode(userId, tradeCode, order.getUserId());
  284. BalanceRecord record = userBalanceService.balancePay(order.getUserId(), order.getGasPrice(), orderId, "转赠");
  285. giftOrderService.giftNotify(orderId, PayMethod.BALANCE, record.getId().toString());
  286. }
  287. @Cacheable(value = "payOrder", key = "'gift#'+#orderId")
  288. public Map<String, Object> payGiftOrderAgreement(Long orderId, String bindCardId) {
  289. GiftOrder order = giftOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  290. if (order.getStatus() != OrderStatus.NOT_PAID) {
  291. throw new BusinessException("订单状态错误");
  292. }
  293. if (StringUtils.isEmpty(bindCardId)) {
  294. bindCardId = userBankCardRepo.findByUserId(order.getUserId())
  295. .stream().map(UserBankCard::getBindCardId).findFirst().orElse(null);
  296. }
  297. if (StringUtils.isEmpty(bindCardId)) {
  298. throw new BusinessException("请先绑定银行卡");
  299. }
  300. return payEaseService.pay("转赠" + order.getAssetId(), orderId.toString(), order.getGasPrice(),
  301. order.getUserId().toString(), bindCardId, Constants.OrderNotifyType.GIFT);
  302. }
  303. @Cacheable(value = "payOrder", key = "'mintOrder#'+#orderId")
  304. public String payMintOrder(Long orderId) {
  305. MintOrder order = mintOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  306. if (order.getStatus() != MintOrderStatus.NOT_PAID) {
  307. throw new BusinessException("订单状态错误");
  308. }
  309. switch (PAY_CHANNEL) {
  310. case Constants.PayChannel.SAND:
  311. return sandPayService.pay(orderId + "", "铸造活动:" + order.getMintActivityId(),
  312. order.getGasPrice(), order.getCreatedAt().plusMinutes(3), Constants.OrderNotifyType.MINT);
  313. case Constants.PayChannel.HM:
  314. return hmPayService.requestAlipay(orderId + "", order.getGasPrice(),
  315. "铸造活动:" + order.getMintActivityId(),
  316. HMPayService.getTimeout(order.getCreatedAt(), 180),
  317. Constants.OrderNotifyType.MINT, generalProperties
  318. .resolveFrontUrl(order.getCompanyId(), "/home"));
  319. }
  320. throw new BusinessException("绿洲宇宙冷却系统已启动,请稍后支付");
  321. }
  322. @Cacheable(value = "payOrder", key = "'mintOrder#'+#orderId")
  323. public String payMintAli(Long orderId) {
  324. MintOrder order = mintOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  325. if (order.getStatus() != MintOrderStatus.NOT_PAID) {
  326. throw new BusinessException("订单状态错误");
  327. }
  328. String qrCode = aliRequest(orderId, order.getGasPrice(), "铸造活动:" + order
  329. .getMintActivityId(), Constants.OrderNotifyType.MINT);
  330. String ua = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()
  331. .getHeader("User-Agent");
  332. if (ua.toLowerCase().contains("micromessenger")) {
  333. return "/static/wx_alipay_bridge.html?payUrl=" + URLEncoder
  334. .encode(Constants.ALIPAY_URL_SCHEME + qrCode, StandardCharsets.UTF_8)
  335. + "&orderId=" + orderId + "&type=mintOrder&returnUrl="
  336. + URLEncoder
  337. .encode(generalProperties.resolveFrontUrl(order.getCompanyId(), "/home"), StandardCharsets.UTF_8);
  338. } else {
  339. return Constants.ALIPAY_URL_SCHEME + qrCode;
  340. }
  341. }
  342. @Cacheable(value = "payOrder", key = "'mintOrder#'+#orderId")
  343. public String payMintQuick(Long orderId) {
  344. MintOrder order = mintOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  345. if (order.getStatus() != MintOrderStatus.NOT_PAID) {
  346. throw new BusinessException("订单状态错误");
  347. }
  348. return sandPayService.payQuick(orderId + "", "铸造活动:" + order.getMintActivityId(),
  349. order.getGasPrice(), order.getCreatedAt().plusMinutes(3), Constants.OrderNotifyType.MINT,
  350. generalProperties.resolveFrontUrl(order.getCompanyId(), "/home"));
  351. }
  352. @Cacheable(value = "payOrder", key = "'mintOrder#'+#orderId")
  353. public String payMintQuickBind(Long orderId) {
  354. MintOrder order = mintOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  355. if (order.getStatus() != MintOrderStatus.NOT_PAID) {
  356. throw new BusinessException("订单状态错误");
  357. }
  358. IdentityAuth identityAuth = identityAuthRepo
  359. .findFirstByUserIdAndStatusAndDelFalseOrderByCreatedAtDesc(order.getUserId(), AuthStatus.SUCCESS)
  360. .orElseThrow(new BusinessException("请先完成实名认证"));
  361. return sandPayService.payQuickBind(orderId + "", "铸造活动:" + order.getMintActivityId(),
  362. order.getGasPrice(), order.getCreatedAt().plusMinutes(3), Constants.OrderNotifyType.MINT,
  363. generalProperties.resolveFrontUrl(order.getCompanyId(), "/home"),
  364. order.getUserId(), identityAuth.getRealName(), identityAuth.getIdNo());
  365. }
  366. public void payMintOrderBalance(Long orderId, Long userId, String tradeCode) {
  367. MintOrder order = mintOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  368. if (order.getStatus() != MintOrderStatus.NOT_PAID) {
  369. throw new BusinessException("订单状态错误");
  370. }
  371. checkTradeCode(userId, tradeCode, order.getUserId());
  372. BalanceRecord record = userBalanceService.balancePay(order.getUserId(), order.getGasPrice(), orderId, "铸造活动");
  373. mintOrderService.mintNotify(orderId, PayMethod.BALANCE, record.getId().toString());
  374. }
  375. private void checkTradeCode(Long userId, String tradeCode, Long orderUserId) {
  376. if (!Objects.equals(orderUserId, userId)) {
  377. throw new BusinessException("订单不属于该用户");
  378. }
  379. String encodedPwd = userRepo.findTradeCode(userId);
  380. if (StringUtils.isEmpty(encodedPwd)) {
  381. throw new BusinessException("请先设置交易密码");
  382. }
  383. if (!passwordEncoder.matches(tradeCode, encodedPwd)) {
  384. throw new BusinessException("交易码错误");
  385. }
  386. }
  387. @Cacheable(value = "payOrder", key = "'mint#'+#orderId")
  388. public Map<String, Object> payMintOrderAgreement(Long orderId, String bindCardId) {
  389. MintOrder order = mintOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  390. if (order.getStatus() != MintOrderStatus.NOT_PAID) {
  391. throw new BusinessException("订单状态错误");
  392. }
  393. if (StringUtils.isEmpty(bindCardId)) {
  394. bindCardId = userBankCardRepo.findByUserId(order.getUserId())
  395. .stream().map(UserBankCard::getBindCardId).findFirst().orElse(null);
  396. }
  397. if (StringUtils.isEmpty(bindCardId)) {
  398. throw new BusinessException("请先绑定银行卡");
  399. }
  400. return payEaseService.pay("铸造活动:" + order.getMintActivityId(), orderId.toString(), order.getGasPrice(),
  401. order.getUserId().toString(), bindCardId, Constants.OrderNotifyType.MINT);
  402. }
  403. private Long getCompanyId() {
  404. Long companyId = 1L;
  405. try {
  406. String hCompanyId = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()
  407. .getHeader("companyId");
  408. if (StringUtils.isNotBlank(hCompanyId)) {
  409. companyId = Long.parseLong(hCompanyId);
  410. }
  411. } catch (Exception ignored) {
  412. }
  413. return companyId;
  414. }
  415. public String recharge(Long userId, BigDecimal amount) {
  416. BigDecimal minAmount = sysConfigService.getBigDecimal("min_recharge_amount");
  417. if (amount.compareTo(minAmount) < 0) {
  418. throw new BusinessException("充值金额不能小于" + minAmount);
  419. }
  420. // if (amount.compareTo(new BigDecimal("50000")) > 0) {
  421. // throw new BusinessException("充值金额不能大于50000");
  422. // }
  423. RechargeOrder order = RechargeOrder.builder()
  424. .id(snowflakeIdWorker.nextId())
  425. .userId(userId)
  426. .amount(amount)
  427. .status(OrderStatus.NOT_PAID)
  428. .build();
  429. rechargeOrderRepo.save(order);
  430. switch (PAY_CHANNEL) {
  431. case Constants.PayChannel.SAND:
  432. return sandPayService.pay(order.getId() + "", "余额充值", order.getAmount(),
  433. order.getCreatedAt().plusMinutes(3), Constants.OrderNotifyType.RECHARGE);
  434. case Constants.PayChannel.HM:
  435. return hmPayService.requestAlipay(order.getId() + "", order.getAmount(),
  436. "余额充值",
  437. HMPayService.getTimeout(order.getCreatedAt(), 180),
  438. Constants.OrderNotifyType.RECHARGE, generalProperties.resolveFrontUrl(getCompanyId(), "/home"));
  439. }
  440. throw new BusinessException("绿洲宇宙冷却系统已启动,请稍后支付");
  441. }
  442. public String payRechargeAli(Long userId, BigDecimal amount) {
  443. BigDecimal minAmount = sysConfigService.getBigDecimal("min_recharge_amount");
  444. if (amount.compareTo(minAmount) < 0) {
  445. throw new BusinessException("充值金额不能小于" + minAmount);
  446. }
  447. if (amount.compareTo(new BigDecimal("50000")) > 0) {
  448. throw new BusinessException("充值金额不能大于50000");
  449. }
  450. RechargeOrder order = RechargeOrder.builder()
  451. .id(snowflakeIdWorker.nextId())
  452. .userId(userId)
  453. .amount(amount)
  454. .status(OrderStatus.NOT_PAID)
  455. .build();
  456. rechargeOrderRepo.save(order);
  457. String qrCode = aliRequest(order.getId(), order.getAmount(), "余额充值", Constants.OrderNotifyType.RECHARGE);
  458. String ua = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()
  459. .getHeader("User-Agent");
  460. if (ua.toLowerCase().contains("micromessenger")) {
  461. return "/static/wx_alipay_bridge.html?payUrl=" + URLEncoder
  462. .encode(Constants.ALIPAY_URL_SCHEME + qrCode, StandardCharsets.UTF_8)
  463. + "&orderId=" + order.getId() + "&type=recharge&returnUrl="
  464. + URLEncoder
  465. .encode(generalProperties.resolveFrontUrl(getCompanyId(), "/home"), StandardCharsets.UTF_8);
  466. } else {
  467. return Constants.ALIPAY_URL_SCHEME + qrCode;
  468. }
  469. }
  470. public Map<String, Object> rechargeAgreement(Long userId, BigDecimal amount, String bindCardId) {
  471. BigDecimal minAmount = sysConfigService.getBigDecimal("min_recharge_amount");
  472. if (amount.compareTo(minAmount) < 0) {
  473. throw new BusinessException("充值金额不能小于" + minAmount);
  474. }
  475. if (amount.compareTo(new BigDecimal("50000")) > 0) {
  476. throw new BusinessException("充值金额不能大于50000");
  477. }
  478. RechargeOrder order = RechargeOrder.builder()
  479. .id(snowflakeIdWorker.nextId())
  480. .userId(userId)
  481. .amount(amount)
  482. .status(OrderStatus.NOT_PAID)
  483. .build();
  484. rechargeOrderRepo.save(order);
  485. if (StringUtils.isEmpty(bindCardId)) {
  486. bindCardId = userBankCardRepo.findByUserId(order.getUserId())
  487. .stream().map(UserBankCard::getBindCardId).findFirst().orElse(null);
  488. }
  489. if (StringUtils.isEmpty(bindCardId)) {
  490. throw new BusinessException("请先绑定银行卡");
  491. }
  492. return payEaseService.pay("余额充值", order.getId().toString(), order.getAmount(),
  493. order.getUserId().toString(), bindCardId, Constants.OrderNotifyType.RECHARGE);
  494. }
  495. public String rechargeQuick(Long userId, BigDecimal amount, Long companyId) {
  496. BigDecimal minAmount = sysConfigService.getBigDecimal("min_recharge_amount");
  497. if (amount.compareTo(minAmount) < 0) {
  498. throw new BusinessException("充值金额不能小于" + minAmount);
  499. }
  500. if (amount.compareTo(new BigDecimal("50000")) > 0) {
  501. throw new BusinessException("充值金额不能大于50000");
  502. }
  503. RechargeOrder order = RechargeOrder.builder()
  504. .id(snowflakeIdWorker.nextId())
  505. .userId(userId)
  506. .amount(amount)
  507. .status(OrderStatus.NOT_PAID)
  508. .build();
  509. rechargeOrderRepo.save(order);
  510. return sandPayService.payQuick(order.getId() + "", "余额充值",
  511. order.getAmount(), LocalDateTime.now().plusMinutes(3), Constants.OrderNotifyType.RECHARGE,
  512. generalProperties.resolveFrontUrl(companyId, "/home"));
  513. }
  514. public String rechargeQuickBind(Long userId, BigDecimal amount, Long companyId) {
  515. BigDecimal minAmount = sysConfigService.getBigDecimal("min_recharge_amount");
  516. if (amount.compareTo(minAmount) < 0) {
  517. throw new BusinessException("充值金额不能小于" + minAmount);
  518. }
  519. if (amount.compareTo(new BigDecimal("50000")) > 0) {
  520. throw new BusinessException("充值金额不能大于50000");
  521. }
  522. IdentityAuth identityAuth = identityAuthRepo
  523. .findFirstByUserIdAndStatusAndDelFalseOrderByCreatedAtDesc(userId, AuthStatus.SUCCESS)
  524. .orElseThrow(new BusinessException("请先完成实名认证"));
  525. RechargeOrder order = RechargeOrder.builder()
  526. .id(snowflakeIdWorker.nextId())
  527. .userId(userId)
  528. .amount(amount)
  529. .status(OrderStatus.NOT_PAID)
  530. .build();
  531. rechargeOrderRepo.save(order);
  532. return sandPayService.payQuickBind(order.getId() + "", "余额充值",
  533. order.getAmount(), LocalDateTime.now().plusMinutes(3), Constants.OrderNotifyType.RECHARGE,
  534. generalProperties.resolveFrontUrl(companyId, "/home"),
  535. userId, identityAuth.getRealName(), identityAuth.getIdNo());
  536. }
  537. public JSONObject refund(String orderId, String transactionId, BigDecimal amount, String channel) {
  538. switch (channel) {
  539. case Constants.PayChannel.SAND: {
  540. JSONObject res = sandPayService.refund(orderId, amount);
  541. if (!"000000".equals(res.getJSONObject("head").getString("respCode"))) {
  542. String msg = res.getJSONObject("head").getString("respMsg");
  543. throw new BusinessException("退款失败:" + msg);
  544. }
  545. return res;
  546. }
  547. case Constants.PayChannel.HM: {
  548. JSONObject res = hmPayService.refund(orderId, amount);
  549. if (!"REFUND_SUCCESS".equals(res.getString("sub_code"))) {
  550. String msg = res.getString("msg");
  551. throw new BusinessException("退款失败:" + msg);
  552. }
  553. return res;
  554. }
  555. case Constants.PayChannel.PE: {
  556. JSONObject res = payEaseService.refund(orderId, transactionId, amount);
  557. String status = res.getString("status");
  558. if (!"SUCCESS".equals(status)) {
  559. String error = res.getString("error");
  560. String cause = res.getString("cause");
  561. throw new BusinessException("退款失败:" + error + ";" + cause);
  562. }
  563. return res;
  564. }
  565. case Constants.PayChannel.ALI: {
  566. alipayService.refund(orderId, amount);
  567. return null;
  568. }
  569. }
  570. throw new BusinessException("退款失败");
  571. }
  572. public PayQuery query(String orderId) {
  573. return query(orderId, null);
  574. }
  575. public PayQuery query(String orderId, String channel) {
  576. if (StringUtils.isNotEmpty(channel)) {
  577. switch (channel) {
  578. case Constants.PayChannel.SAND:
  579. return sandPayService.payQuery(orderId);
  580. case Constants.PayChannel.HM:
  581. return hmPayService.payQuery(orderId);
  582. case Constants.PayChannel.PE:
  583. return payEaseService.payQuery(orderId);
  584. }
  585. }
  586. PayQuery query = sandPayService.payQuery(orderId);
  587. if (query == null || !query.isExist()) {
  588. query = hmPayService.payQuery(orderId);
  589. }
  590. if (query == null || !query.isExist()) {
  591. query = payEaseService.payQuery(orderId);
  592. }
  593. if (query == null || !query.isExist()) {
  594. query = alipayService.payQuery(orderId);
  595. }
  596. return Optional.ofNullable(query).orElse(PayQuery.builder().exist(false).build());
  597. }
  598. @Cacheable(value = "payOrder", key = "'auctionOrder#'+#orderId")
  599. public String payAuctionOrder(Long orderId) {
  600. AuctionOrder order = auctionOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  601. if (order.getStatus() != AuctionOrderStatus.NOT_PAID) {
  602. throw new BusinessException("订单状态错误");
  603. }
  604. switch (PAY_CHANNEL) {
  605. case Constants.PayChannel.SAND:
  606. return sandPayService.pay(orderId + "", "拍卖:" + order.getName(), order.getTotalPrice(),
  607. order.getCreatedAt().plusMinutes(3), Constants.OrderNotifyType.AUCTION);
  608. case Constants.PayChannel.HM:
  609. return hmPayService.requestAlipay(orderId + "", order.getTotalPrice(),
  610. "拍卖:" + order.getName(), HMPayService.getTimeout(order.getCreatedAt(), 180),
  611. Constants.OrderNotifyType.AUCTION, generalProperties.resolveFrontUrl(getCompanyId(), "/home"));
  612. }
  613. throw new BusinessException("绿洲宇宙冷却系统已启动,请稍后支付");
  614. }
  615. @Cacheable(value = "payOrder", key = "'auctionOrder#'+#orderId")
  616. public String payAuctionAli(Long orderId) {
  617. AuctionOrder order = auctionOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  618. if (order.getStatus() != AuctionOrderStatus.NOT_PAID) {
  619. throw new BusinessException("订单状态错误");
  620. }
  621. String qrCode = aliRequest(orderId, order.getTotalPrice(), "拍卖:" + order
  622. .getName(), Constants.OrderNotifyType.AUCTION);
  623. String ua = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()
  624. .getHeader("User-Agent");
  625. if (ua.toLowerCase().contains("micromessenger")) {
  626. return "/static/wx_alipay_bridge.html?payUrl=" + URLEncoder
  627. .encode(Constants.ALIPAY_URL_SCHEME + qrCode, StandardCharsets.UTF_8)
  628. + "&orderId=" + orderId + "&type=auctionOrder&returnUrl="
  629. + URLEncoder
  630. .encode(generalProperties.resolveFrontUrl(getCompanyId(), "/home"), StandardCharsets.UTF_8);
  631. } else {
  632. return Constants.ALIPAY_URL_SCHEME + qrCode;
  633. }
  634. }
  635. @Cacheable(value = "payOrder", key = "'auctionOrder#'+#orderId")
  636. public String payAuctionQuick(Long orderId) {
  637. AuctionOrder order = auctionOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  638. if (order.getStatus() != AuctionOrderStatus.NOT_PAID) {
  639. throw new BusinessException("订单状态错误");
  640. }
  641. IdentityAuth identityAuth = identityAuthRepo
  642. .findFirstByUserIdAndStatusAndDelFalseOrderByCreatedAtDesc(order.getUserId(), AuthStatus.SUCCESS)
  643. .orElseThrow(new BusinessException("请先完成实名认证"));
  644. return sandPayService.payQuick(orderId + "", "拍卖:" + order.getName(),
  645. order.getTotalPrice(), order.getCreatedAt().plusMinutes(3), Constants.OrderNotifyType.AUCTION,
  646. generalProperties.resolveFrontUrl(getCompanyId(), "/home"));
  647. }
  648. @Cacheable(value = "payOrder", key = "'auctionOrder#'+#orderId")
  649. public String payAuctionQuickBind(Long orderId) {
  650. AuctionOrder order = auctionOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  651. if (order.getStatus() != AuctionOrderStatus.NOT_PAID) {
  652. throw new BusinessException("订单状态错误");
  653. }
  654. IdentityAuth identityAuth = identityAuthRepo
  655. .findFirstByUserIdAndStatusAndDelFalseOrderByCreatedAtDesc(order.getUserId(), AuthStatus.SUCCESS)
  656. .orElseThrow(new BusinessException("请先完成实名认证"));
  657. return sandPayService.payQuickBind(orderId + "", "拍卖:" + order.getName(),
  658. order.getTotalPrice(), order.getCreatedAt().plusMinutes(3), Constants.OrderNotifyType.AUCTION,
  659. generalProperties.resolveFrontUrl(getCompanyId(), "/home"),
  660. order.getUserId(), identityAuth.getRealName(), identityAuth.getIdNo());
  661. }
  662. public void payAuctionOrderBalance(Long orderId, Long userId, String tradeCode) {
  663. AuctionOrder order = auctionOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  664. if (order.getStatus() != AuctionOrderStatus.NOT_PAID) {
  665. throw new BusinessException("订单状态错误");
  666. }
  667. checkTradeCode(userId, tradeCode, order.getUserId());
  668. BalanceRecord record = userBalanceService.balancePay(order.getUserId(), order.getTotalPrice(), orderId, "拍卖");
  669. auctionOrderService.notify(orderId, PayMethod.BALANCE, record.getId().toString());
  670. }
  671. public void payTradeAuctionOrderBalance(Long orderId, Long userId, String tradeCode) {
  672. TradeAuctionOrder order = tradeAuctionOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  673. TradeAuction tradeAuction = tradeAuctionRepo.findById(order.getTradeAuctionId())
  674. .orElseThrow(new BusinessException("暂无"));
  675. if (order.getStatus() != AuctionOrderStatus.NOT_PAID) {
  676. throw new BusinessException("订单状态错误");
  677. }
  678. if (tradeAuction.getStock() < 1) {
  679. throw new BusinessException("库存不足");
  680. }
  681. if (LocalDateTime.now().compareTo(tradeAuction.getCurrentStartTime()) < 0) {
  682. throw new BusinessException("未到竞价时间");
  683. }
  684. if (!tradeAuction.getStatus().equals(TradeAuctionStatus.ONGOING) & !tradeAuction.getStatus()
  685. .equals(TradeAuctionStatus.PURCHASED)) {
  686. throw new BusinessException("易拍产品未处在竞价状态");
  687. }
  688. if (order.getPaymentType().equals(AuctionPaymentType.DEPOSIT)) {
  689. if (order.getPrice().compareTo(tradeAuction.getNextPrice()) != 0) {
  690. throw new BusinessException("已经失去购买资格");
  691. }
  692. if (tradeAuction.getSale() >= 30) {
  693. throw new BusinessException("已无购买资格");
  694. }
  695. }
  696. checkTradeCode(userId, tradeCode, order.getUserId());
  697. BigDecimal amount;
  698. if (order.getPaymentType() != AuctionPaymentType.DEPOSIT) {
  699. amount = order.getPrice();
  700. } else {
  701. amount = order.getServiceCharge();
  702. }
  703. BalanceRecord record = userBalanceService
  704. .balancePay(order.getUserId(), amount, orderId, "拍卖");
  705. tradeAuctionOrderService.commission(order, order.getPrice());
  706. tradeAuctionOrderService.notify(orderId, record.getId().toString(), PayMethod.BALANCE);
  707. }
  708. @Cacheable(value = "payOrder", key = "'auctionOrder#'+#orderId")
  709. public Map<String, Object> payAuctionOrderAgreement(Long orderId, String bindCardId) {
  710. AuctionOrder order = auctionOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  711. if (order.getStatus() != AuctionOrderStatus.NOT_PAID) {
  712. throw new BusinessException("订单状态错误");
  713. }
  714. if (StringUtils.isEmpty(bindCardId)) {
  715. bindCardId = userBankCardRepo.findByUserId(order.getUserId())
  716. .stream().map(UserBankCard::getBindCardId).findFirst().orElse(null);
  717. }
  718. if (StringUtils.isEmpty(bindCardId)) {
  719. throw new BusinessException("请先绑定银行卡");
  720. }
  721. return payEaseService.pay("拍卖:" + order.getAuctionId(), orderId.toString(), order.getTotalPrice(),
  722. order.getUserId().toString(), bindCardId, Constants.OrderNotifyType.AUCTION);
  723. }
  724. @Cacheable(value = "payOrder", key = "'picOrder#'+#orderId")
  725. public String payPicOrder(Long orderId) {
  726. PhotoAsset order = photoAssetRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  727. if (order.getOrderStatus() != OrderStatus.NOT_PAID) {
  728. throw new BusinessException("订单状态错误");
  729. }
  730. switch (PAY_CHANNEL) {
  731. case Constants.PayChannel.SAND:
  732. return sandPayService.pay(orderId + "", "星图:" + order.getPicName(), order.getPrice(),
  733. order.getCreatedAt().plusMinutes(3), Constants.OrderNotifyType.PIC);
  734. case Constants.PayChannel.HM:
  735. return hmPayService.requestAlipay(orderId + "", order.getPrice(),
  736. "星图:" + order.getPicName(), HMPayService.getTimeout(order.getCreatedAt(), 180),
  737. Constants.OrderNotifyType.PIC, generalProperties
  738. .resolveFrontUrl(getCompanyId(), "/orderDetail?id=" + orderId));
  739. }
  740. throw new BusinessException("绿洲宇宙冷却系统已启动,请稍后支付");
  741. }
  742. @Cacheable(value = "payOrder", key = "'picOrder#'+#orderId")
  743. public String payPicAli(Long orderId) {
  744. PhotoAsset order = photoAssetRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  745. if (order.getOrderStatus() != OrderStatus.NOT_PAID) {
  746. throw new BusinessException("订单状态错误");
  747. }
  748. String qrCode = aliRequest(orderId, order.getPrice(), "星图:" + order
  749. .getPicName(), Constants.OrderNotifyType.PIC);
  750. String ua = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()
  751. .getHeader("User-Agent");
  752. if (ua.toLowerCase().contains("micromessenger")) {
  753. return "/static/wx_alipay_bridge.html?payUrl=" + URLEncoder
  754. .encode(Constants.ALIPAY_URL_SCHEME + qrCode, StandardCharsets.UTF_8)
  755. + "&orderId=" + orderId + "&type=pic&returnUrl="
  756. + URLEncoder
  757. .encode(generalProperties
  758. .resolveFrontUrl(getCompanyId(), "/orderDetail?id=" + orderId), StandardCharsets.UTF_8);
  759. } else {
  760. return Constants.ALIPAY_URL_SCHEME + qrCode;
  761. }
  762. }
  763. @Cacheable(value = "payOrder", key = "'picOrder#'+#orderId")
  764. public String payPicQuick(Long orderId) {
  765. PhotoAsset order = photoAssetRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  766. if (order.getOrderStatus() != OrderStatus.NOT_PAID) {
  767. throw new BusinessException("订单状态错误");
  768. }
  769. IdentityAuth identityAuth = identityAuthRepo
  770. .findFirstByUserIdAndStatusAndDelFalseOrderByCreatedAtDesc(order.getUserId(), AuthStatus.SUCCESS)
  771. .orElseThrow(new BusinessException("请先完成实名认证"));
  772. return sandPayService.payQuick(orderId + "", "星图:" + order.getPicName(),
  773. order.getPrice(), order.getCreatedAt().plusMinutes(3), Constants.OrderNotifyType.PIC,
  774. generalProperties.resolveFrontUrl(getCompanyId(), "/orderDetail?id=" + orderId));
  775. }
  776. @Cacheable(value = "payOrder", key = "'picOrder#'+#orderId")
  777. public String payPicQuickBind(Long orderId) {
  778. PhotoAsset order = photoAssetRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  779. if (order.getOrderStatus() != OrderStatus.NOT_PAID) {
  780. throw new BusinessException("订单状态错误");
  781. }
  782. IdentityAuth identityAuth = identityAuthRepo
  783. .findFirstByUserIdAndStatusAndDelFalseOrderByCreatedAtDesc(order.getUserId(), AuthStatus.SUCCESS)
  784. .orElseThrow(new BusinessException("请先完成实名认证"));
  785. return sandPayService.payQuickBind(orderId + "", "星图:" + order.getPicName(),
  786. order.getPrice(), order.getCreatedAt().plusMinutes(3), Constants.OrderNotifyType.PIC,
  787. generalProperties.resolveFrontUrl(getCompanyId(), "/orderDetail?id=" + orderId),
  788. order.getUserId(), identityAuth.getRealName(), identityAuth.getIdNo());
  789. }
  790. public void payPicOrderBalance(Long orderId, Long userId, String tradeCode) {
  791. PhotoAsset order = photoAssetRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  792. if (order.getOrderStatus() != OrderStatus.NOT_PAID) {
  793. throw new BusinessException("订单状态错误");
  794. }
  795. checkTradeCode(userId, tradeCode, order.getUserId());
  796. BalanceRecord record = userBalanceService.balancePay(order.getUserId(), order.getPrice(), orderId, "星图");
  797. photoAssetService.notify(orderId, PayMethod.BALANCE, record.getId().toString());
  798. }
  799. @Cacheable(value = "payOrder", key = "'picOrder#'+#orderId")
  800. public Map<String, Object> payPicOrderAgreement(Long orderId, String bindCardId) {
  801. PhotoAsset order = photoAssetRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  802. if (order.getOrderStatus() != OrderStatus.NOT_PAID) {
  803. throw new BusinessException("订单状态错误");
  804. }
  805. if (StringUtils.isEmpty(bindCardId)) {
  806. bindCardId = userBankCardRepo.findByUserId(order.getUserId())
  807. .stream().map(UserBankCard::getBindCardId).findFirst().orElse(null);
  808. }
  809. if (StringUtils.isEmpty(bindCardId)) {
  810. throw new BusinessException("请先绑定银行卡");
  811. }
  812. return payEaseService.pay("星图:" + order.getPicName(), orderId.toString(), order.getPrice(),
  813. order.getUserId().toString(), bindCardId, Constants.OrderNotifyType.PIC);
  814. }
  815. @Cacheable(value = "payOrder", key = "'domain#'+#orderId")
  816. public String payDomainOrder(Long orderId) {
  817. DomainOrder order = domainOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  818. if (order.getOrderStatus() != OrderStatus.NOT_PAID) {
  819. throw new BusinessException("订单状态错误");
  820. }
  821. switch (PAY_CHANNEL) {
  822. case Constants.PayChannel.SAND:
  823. return sandPayService.pay(orderId + "", "域名:" + order.getPicName(), order.getPrice(),
  824. order.getCreatedAt().plusMinutes(3), Constants.OrderNotifyType.DOMAIN);
  825. case Constants.PayChannel.HM:
  826. return hmPayService.requestAlipay(orderId + "", order.getPrice(),
  827. "域名:" + order.getPicName(), HMPayService.getTimeout(order.getCreatedAt(), 180),
  828. Constants.OrderNotifyType.DOMAIN, generalProperties
  829. .resolveFrontUrl(getCompanyId(), "/domainname"));
  830. }
  831. throw new BusinessException("绿洲宇宙冷却系统已启动,请稍后支付");
  832. }
  833. @Cacheable(value = "payOrder", key = "'domain#'+#orderId")
  834. public String payDomainAli(Long orderId) {
  835. DomainOrder order = domainOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  836. if (order.getOrderStatus() != OrderStatus.NOT_PAID) {
  837. throw new BusinessException("订单状态错误");
  838. }
  839. String qrCode = aliRequest(orderId, order.getPrice(), "域名:" + order
  840. .getPicName(), Constants.OrderNotifyType.DOMAIN);
  841. String ua = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()
  842. .getHeader("User-Agent");
  843. if (ua.toLowerCase().contains("micromessenger")) {
  844. return "/static/wx_alipay_bridge.html?payUrl=" + URLEncoder
  845. .encode(Constants.ALIPAY_URL_SCHEME + qrCode, StandardCharsets.UTF_8)
  846. + "&orderId=" + orderId + "&type=domain&returnUrl="
  847. + URLEncoder
  848. .encode(generalProperties
  849. .resolveFrontUrl(getCompanyId(), "/domainname"), StandardCharsets.UTF_8);
  850. } else {
  851. return Constants.ALIPAY_URL_SCHEME + qrCode;
  852. }
  853. }
  854. @Cacheable(value = "payOrder", key = "'domain#'+#orderId")
  855. public String payDomainQuick(Long orderId) {
  856. DomainOrder order = domainOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  857. if (order.getOrderStatus() != OrderStatus.NOT_PAID) {
  858. throw new BusinessException("订单状态错误");
  859. }
  860. IdentityAuth identityAuth = identityAuthRepo
  861. .findFirstByUserIdAndStatusAndDelFalseOrderByCreatedAtDesc(order.getUserId(), AuthStatus.SUCCESS)
  862. .orElseThrow(new BusinessException("请先完成实名认证"));
  863. return sandPayService.payQuick(orderId + "", "域名:" + order.getPicName(),
  864. order.getPrice(), order.getCreatedAt().plusMinutes(3), Constants.OrderNotifyType.DOMAIN,
  865. generalProperties.resolveFrontUrl(getCompanyId(), "/domainname"));
  866. }
  867. @Cacheable(value = "payOrder", key = "'domain#'+#orderId")
  868. public String payDomainQuickBind(Long orderId) {
  869. DomainOrder order = domainOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  870. if (order.getOrderStatus() != OrderStatus.NOT_PAID) {
  871. throw new BusinessException("订单状态错误");
  872. }
  873. IdentityAuth identityAuth = identityAuthRepo
  874. .findFirstByUserIdAndStatusAndDelFalseOrderByCreatedAtDesc(order.getUserId(), AuthStatus.SUCCESS)
  875. .orElseThrow(new BusinessException("请先完成实名认证"));
  876. return sandPayService.payQuickBind(orderId + "", "域名:" + order.getPicName(),
  877. order.getPrice(), order.getCreatedAt().plusMinutes(3), Constants.OrderNotifyType.DOMAIN,
  878. generalProperties.resolveFrontUrl(getCompanyId(), "/domainname"),
  879. order.getUserId(), identityAuth.getRealName(), identityAuth.getIdNo());
  880. }
  881. public void payDomainOrderBalance(Long orderId, Long userId, String tradeCode) throws FontFormatException, IOException, WriterException {
  882. DomainOrder order = domainOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  883. if (order.getOrderStatus() != OrderStatus.NOT_PAID) {
  884. throw new BusinessException("订单状态错误");
  885. }
  886. checkTradeCode(userId, tradeCode, order.getUserId());
  887. BalanceRecord record = userBalanceService.balancePay(order.getUserId(), order.getPrice(), orderId, "域名");
  888. domainOrderService.notify(orderId, PayMethod.BALANCE, record.getId().toString());
  889. }
  890. @Cacheable(value = "payOrder", key = "'domain#'+#orderId")
  891. public Map<String, Object> payDomainOrderAgreement(Long orderId, String bindCardId) {
  892. DomainOrder order = domainOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  893. if (order.getOrderStatus() != OrderStatus.NOT_PAID) {
  894. throw new BusinessException("订单状态错误");
  895. }
  896. if (StringUtils.isEmpty(bindCardId)) {
  897. bindCardId = userBankCardRepo.findByUserId(order.getUserId())
  898. .stream().map(UserBankCard::getBindCardId).findFirst().orElse(null);
  899. }
  900. if (StringUtils.isEmpty(bindCardId)) {
  901. throw new BusinessException("请先绑定银行卡");
  902. }
  903. return payEaseService.pay("域名:" + order.getPicName(), orderId.toString(), order.getPrice(),
  904. order.getUserId().toString(), bindCardId, Constants.OrderNotifyType.DOMAIN);
  905. }
  906. public void payDomainAskBalance(Long orderId, Long userId, String tradeCode) throws FontFormatException, IOException, WriterException {
  907. DomainAsk order = domainAskRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  908. if (order.getStatus() != DomainAskStatus.NOT_PAID) {
  909. throw new BusinessException("订单状态错误");
  910. }
  911. checkTradeCode(userId, tradeCode, order.getUserId());
  912. BalanceRecord record = userBalanceService.balancePay(order.getUserId(), order.getPrice(), orderId, "元域名叫价");
  913. domainAskService.notifyOrder(orderId, PayMethod.BALANCE, record.getId().toString());
  914. }
  915. @Cacheable(value = "payOrder", key = "'ask#'+#orderId")
  916. public String payAskOrder(Long orderId) {
  917. DomainAsk order = domainAskRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  918. if (order.getStatus() != DomainAskStatus.NOT_PAID) {
  919. throw new BusinessException("订单状态错误");
  920. }
  921. switch (PAY_CHANNEL) {
  922. case Constants.PayChannel.SAND:
  923. return sandPayService.pay(orderId + "", "域名:" + order.getName(), order.getPrice(),
  924. order.getCreatedAt().plusMinutes(3), Constants.OrderNotifyType.ASK);
  925. case Constants.PayChannel.HM:
  926. return hmPayService.requestAlipay(orderId + "", order.getPrice(),
  927. "域名:" + order.getName(), HMPayService.getTimeout(order.getCreatedAt(), 180),
  928. Constants.OrderNotifyType.ASK, generalProperties
  929. .resolveFrontUrl(getCompanyId(), "/domainname"));
  930. }
  931. throw new BusinessException("绿洲宇宙冷却系统已启动,请稍后支付");
  932. }
  933. @Cacheable(value = "payOrder", key = "'ask#'+#orderId")
  934. public String payAskAli(Long orderId) {
  935. DomainAsk order = domainAskRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  936. if (order.getStatus() != DomainAskStatus.NOT_PAID) {
  937. throw new BusinessException("订单状态错误");
  938. }
  939. String qrCode = aliRequest(orderId, order.getPrice(), "叫价:" + order
  940. .getName(), Constants.OrderNotifyType.ASK);
  941. String ua = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()
  942. .getHeader("User-Agent");
  943. if (ua.toLowerCase().contains("micromessenger")) {
  944. return "/static/wx_alipay_bridge.html?payUrl=" + URLEncoder
  945. .encode(Constants.ALIPAY_URL_SCHEME + qrCode, StandardCharsets.UTF_8)
  946. + "&orderId=" + orderId + "&type=domain&returnUrl="
  947. + URLEncoder
  948. .encode(generalProperties
  949. .resolveFrontUrl(getCompanyId(), "/domainname"), StandardCharsets.UTF_8);
  950. } else {
  951. return Constants.ALIPAY_URL_SCHEME + qrCode;
  952. }
  953. }
  954. @Cacheable(value = "payOrder", key = "'ask#'+#orderId")
  955. public String payAskQuick(Long orderId) {
  956. DomainAsk order = domainAskRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  957. if (order.getStatus() != DomainAskStatus.NOT_PAID) {
  958. throw new BusinessException("订单状态错误");
  959. }
  960. IdentityAuth identityAuth = identityAuthRepo
  961. .findFirstByUserIdAndStatusAndDelFalseOrderByCreatedAtDesc(order.getUserId(), AuthStatus.SUCCESS)
  962. .orElseThrow(new BusinessException("请先完成实名认证"));
  963. return sandPayService.payQuick(orderId + "", "叫价:" + order.getName(),
  964. order.getPrice(), order.getCreatedAt().plusMinutes(3), Constants.OrderNotifyType.ASK,
  965. generalProperties.resolveFrontUrl(getCompanyId(), "/domainname"));
  966. }
  967. @Cacheable(value = "payOrder", key = "'ask#'+#orderId")
  968. public String payAskQuickBind(Long orderId) {
  969. DomainAsk order = domainAskRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  970. if (order.getStatus() != DomainAskStatus.NOT_PAID) {
  971. throw new BusinessException("订单状态错误");
  972. }
  973. IdentityAuth identityAuth = identityAuthRepo
  974. .findFirstByUserIdAndStatusAndDelFalseOrderByCreatedAtDesc(order.getUserId(), AuthStatus.SUCCESS)
  975. .orElseThrow(new BusinessException("请先完成实名认证"));
  976. return sandPayService.payQuickBind(orderId + "", "叫价:" + order.getName(),
  977. order.getPrice(), order.getCreatedAt().plusMinutes(3), Constants.OrderNotifyType.ASK,
  978. generalProperties.resolveFrontUrl(getCompanyId(), "/domainname"),
  979. order.getUserId(), identityAuth.getRealName(), identityAuth.getIdNo());
  980. }
  981. @Cacheable(value = "payOrder", key = "'ask#'+#orderId")
  982. public Map<String, Object> payAskOrderAgreement(Long orderId, String bindCardId) {
  983. DomainAsk order = domainAskRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  984. if (order.getStatus() != DomainAskStatus.NOT_PAID) {
  985. throw new BusinessException("订单状态错误");
  986. }
  987. if (StringUtils.isEmpty(bindCardId)) {
  988. bindCardId = userBankCardRepo.findByUserId(order.getUserId())
  989. .stream().map(UserBankCard::getBindCardId).findFirst().orElse(null);
  990. }
  991. if (StringUtils.isEmpty(bindCardId)) {
  992. throw new BusinessException("请先绑定银行卡");
  993. }
  994. return payEaseService.pay("叫价:" + order.getName(), orderId.toString(), order.getPrice(),
  995. order.getUserId().toString(), bindCardId, Constants.OrderNotifyType.ASK);
  996. }
  997. }