OrderPayService.java 50 KB

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