|
|
@@ -1,6 +1,11 @@
|
|
|
package com.izouma.nineth.service;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.alipay.api.AlipayApiException;
|
|
|
+import com.alipay.api.AlipayClient;
|
|
|
+import com.alipay.api.request.AlipayTradePrecreateRequest;
|
|
|
+import com.alipay.api.response.AlipayTradePrecreateResponse;
|
|
|
+import com.izouma.nineth.config.AlipayProperties;
|
|
|
import com.izouma.nineth.config.Constants;
|
|
|
import com.izouma.nineth.config.GeneralProperties;
|
|
|
import com.izouma.nineth.domain.*;
|
|
|
@@ -18,8 +23,12 @@ import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
@@ -53,6 +62,8 @@ public class OrderPayService {
|
|
|
private final AuctionOrderRepo auctionOrderRepo;
|
|
|
private final AuctionOrderService auctionOrderService;
|
|
|
private final IdentityAuthRepo identityAuthRepo;
|
|
|
+ private final AlipayClient alipayClient;
|
|
|
+ private final AlipayProperties alipayProperties;
|
|
|
|
|
|
public static void setPayChannel(String payChannel) {
|
|
|
log.info("set pay channel {}", payChannel);
|
|
|
@@ -79,6 +90,52 @@ public class OrderPayService {
|
|
|
throw new BusinessException(Constants.PAY_ERR_MSG);
|
|
|
}
|
|
|
|
|
|
+ private String aliRequest(Long orderId, BigDecimal amount, String subject, String type) {
|
|
|
+ AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
|
|
|
+ request.setNotifyUrl(alipayProperties.getNotifyUrl());
|
|
|
+ JSONObject bizContent = new JSONObject();
|
|
|
+ bizContent.put("out_trade_no", orderId + "");
|
|
|
+ bizContent.put("total_amount", amount);
|
|
|
+ bizContent.put("subject", subject);
|
|
|
+ JSONObject body = new JSONObject();
|
|
|
+ body.put("type", type);
|
|
|
+ body.put("orderId", orderId);
|
|
|
+ bizContent.put("body", body.toString());
|
|
|
+
|
|
|
+ request.setBizContent(bizContent.toString());
|
|
|
+ AlipayTradePrecreateResponse response = null;
|
|
|
+ try {
|
|
|
+ response = alipayClient.execute(request);
|
|
|
+ } catch (AlipayApiException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException(Constants.PAY_ERR_MSG, e.getErrMsg());
|
|
|
+ }
|
|
|
+ if (response.isSuccess()) {
|
|
|
+ return response.getQrCode();
|
|
|
+ } else {
|
|
|
+ throw new BusinessException(response.getSubMsg());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Cacheable(value = "payOrder", key = "'order#'+#orderId")
|
|
|
+ public String payOrderAli(Long orderId) {
|
|
|
+ Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
|
+ if (order.getStatus() != OrderStatus.NOT_PAID) {
|
|
|
+ throw new BusinessException("订单状态错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ String qrCode = aliRequest(orderId, order.getTotalPrice(), order.getName(), Constants.OrderNotifyType.ORDER);
|
|
|
+
|
|
|
+ String ua = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader("User-Agent");
|
|
|
+ if (ua.toLowerCase().contains("micromessenger")) {
|
|
|
+ return "/static/wx_alipay_bridge.html?payUrl=" + URLEncoder.encode(Constants.ALIPAY_URL_SCHEME + qrCode, StandardCharsets.UTF_8)
|
|
|
+ + "&orderId=" + orderId + "&type=order&returnUrl="
|
|
|
+ + URLEncoder.encode(generalProperties.getHost() + "/9th/store", StandardCharsets.UTF_8);
|
|
|
+ } else {
|
|
|
+ return Constants.ALIPAY_URL_SCHEME + qrCode;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Cacheable(value = "payOrder", key = "'order#'+#orderId")
|
|
|
public String payOrderQuick(Long orderId) {
|
|
|
Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
|
@@ -166,6 +223,25 @@ public class OrderPayService {
|
|
|
throw new BusinessException(Constants.PAY_ERR_MSG);
|
|
|
}
|
|
|
|
|
|
+ @Cacheable(value = "payOrder", key = "'gift#'+#orderId")
|
|
|
+ public String payGiftAli(Long orderId) {
|
|
|
+ GiftOrder order = giftOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
|
+ if (order.getStatus() != OrderStatus.NOT_PAID) {
|
|
|
+ throw new BusinessException("订单状态错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ String qrCode = aliRequest(orderId, order.getGasPrice(), "转赠", Constants.OrderNotifyType.GIFT);
|
|
|
+
|
|
|
+ String ua = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader("User-Agent");
|
|
|
+ if (ua.toLowerCase().contains("micromessenger")) {
|
|
|
+ return "/static/wx_alipay_bridge.html?payUrl=" + URLEncoder.encode(Constants.ALIPAY_URL_SCHEME + qrCode, StandardCharsets.UTF_8)
|
|
|
+ + "&orderId=" + orderId + "&type=gift&returnUrl="
|
|
|
+ + URLEncoder.encode(generalProperties.getHost() + "/9th/store", StandardCharsets.UTF_8);
|
|
|
+ } else {
|
|
|
+ return Constants.ALIPAY_URL_SCHEME + qrCode;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Cacheable(value = "payOrder", key = "'gift#'+#orderId")
|
|
|
public String payGiftQuick(Long orderId) {
|
|
|
GiftOrder order = giftOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
|
@@ -237,6 +313,25 @@ public class OrderPayService {
|
|
|
throw new BusinessException("绿洲宇宙冷却系统已启动,请稍后支付");
|
|
|
}
|
|
|
|
|
|
+ @Cacheable(value = "payOrder", key = "'mintOrder#'+#orderId")
|
|
|
+ public String payMintAli(Long orderId) {
|
|
|
+ MintOrder order = mintOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
|
+ if (order.getStatus() != MintOrderStatus.NOT_PAID) {
|
|
|
+ throw new BusinessException("订单状态错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ String qrCode = aliRequest(orderId, order.getGasPrice(), "铸造活动:" + order.getMintActivityId(), Constants.OrderNotifyType.MINT);
|
|
|
+
|
|
|
+ String ua = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader("User-Agent");
|
|
|
+ if (ua.toLowerCase().contains("micromessenger")) {
|
|
|
+ return "/static/wx_alipay_bridge.html?payUrl=" + URLEncoder.encode(Constants.ALIPAY_URL_SCHEME + qrCode, StandardCharsets.UTF_8)
|
|
|
+ + "&orderId=" + orderId + "&type=mintOrder&returnUrl="
|
|
|
+ + URLEncoder.encode(generalProperties.getHost() + "/9th/store", StandardCharsets.UTF_8);
|
|
|
+ } else {
|
|
|
+ return Constants.ALIPAY_URL_SCHEME + qrCode;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Cacheable(value = "payOrder", key = "'mintOrder#'+#orderId")
|
|
|
public String payMintQuick(Long orderId) {
|
|
|
MintOrder order = mintOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
|
@@ -330,6 +425,34 @@ public class OrderPayService {
|
|
|
throw new BusinessException("绿洲宇宙冷却系统已启动,请稍后支付");
|
|
|
}
|
|
|
|
|
|
+ public String payRechargeAli(Long userId, BigDecimal amount) {
|
|
|
+ BigDecimal minAmount = sysConfigService.getBigDecimal("min_recharge_amount");
|
|
|
+ if (amount.compareTo(minAmount) < 0) {
|
|
|
+ throw new BusinessException("充值金额不能小于" + minAmount);
|
|
|
+ }
|
|
|
+ if (amount.compareTo(new BigDecimal("50000")) > 0) {
|
|
|
+ throw new BusinessException("充值金额不能大于50000");
|
|
|
+ }
|
|
|
+ RechargeOrder order = RechargeOrder.builder()
|
|
|
+ .id(snowflakeIdWorker.nextId())
|
|
|
+ .userId(userId)
|
|
|
+ .amount(amount)
|
|
|
+ .status(OrderStatus.NOT_PAID)
|
|
|
+ .build();
|
|
|
+ rechargeOrderRepo.save(order);
|
|
|
+
|
|
|
+ String qrCode = aliRequest(order.getId(), order.getAmount(), "余额充值", Constants.OrderNotifyType.RECHARGE);
|
|
|
+
|
|
|
+ String ua = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader("User-Agent");
|
|
|
+ if (ua.toLowerCase().contains("micromessenger")) {
|
|
|
+ return "/static/wx_alipay_bridge.html?payUrl=" + URLEncoder.encode(Constants.ALIPAY_URL_SCHEME + qrCode, StandardCharsets.UTF_8)
|
|
|
+ + "&orderId=" + order.getId() + "&type=recharge&returnUrl="
|
|
|
+ + URLEncoder.encode(generalProperties.getHost() + "/9th/store", StandardCharsets.UTF_8);
|
|
|
+ } else {
|
|
|
+ return Constants.ALIPAY_URL_SCHEME + qrCode;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public Map<String, Object> rechargeAgreement(Long userId, BigDecimal amount, String bindCardId) {
|
|
|
BigDecimal minAmount = sysConfigService.getBigDecimal("min_recharge_amount");
|
|
|
if (amount.compareTo(minAmount) < 0) {
|
|
|
@@ -474,6 +597,25 @@ public class OrderPayService {
|
|
|
throw new BusinessException("绿洲宇宙冷却系统已启动,请稍后支付");
|
|
|
}
|
|
|
|
|
|
+ @Cacheable(value = "payOrder", key = "'auctionOrder#'+#orderId")
|
|
|
+ public String payAuctionAli(Long orderId) {
|
|
|
+ AuctionOrder order = auctionOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
|
+ if (order.getStatus() != AuctionOrderStatus.NOT_PAID) {
|
|
|
+ throw new BusinessException("订单状态错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ String qrCode = aliRequest(orderId, order.getTotalPrice(), "拍卖:" + order.getName(), Constants.OrderNotifyType.AUCTION);
|
|
|
+
|
|
|
+ String ua = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader("User-Agent");
|
|
|
+ if (ua.toLowerCase().contains("micromessenger")) {
|
|
|
+ return "/static/wx_alipay_bridge.html?payUrl=" + URLEncoder.encode(Constants.ALIPAY_URL_SCHEME + qrCode, StandardCharsets.UTF_8)
|
|
|
+ + "&orderId=" + orderId + "&type=auctionOrder&returnUrl="
|
|
|
+ + URLEncoder.encode(generalProperties.getHost() + "/9th/store", StandardCharsets.UTF_8);
|
|
|
+ } else {
|
|
|
+ return Constants.ALIPAY_URL_SCHEME + qrCode;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Cacheable(value = "payOrder", key = "'auctionOrder#'+#orderId")
|
|
|
public String payAuctionQuick(Long orderId) {
|
|
|
AuctionOrder order = auctionOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|