|
|
@@ -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);
|
|
|
@@ -67,16 +78,50 @@ public class OrderPayService {
|
|
|
if (order.getStatus() != OrderStatus.NOT_PAID) {
|
|
|
throw new BusinessException("订单状态错误");
|
|
|
}
|
|
|
- switch (PAY_CHANNEL) {
|
|
|
- case Constants.PayChannel.SAND:
|
|
|
- return sandPayService.pay(orderId + "", order.getName(), order.getTotalPrice(),
|
|
|
- order.getCreatedAt().plusMinutes(3), "order");
|
|
|
- case Constants.PayChannel.HM:
|
|
|
- return hmPayService.requestAlipay(orderId + "", order.getTotalPrice(), order.getName(),
|
|
|
- HMPayService.getTimeout(order.getCreatedAt(), 180), Constants.OrderNotifyType.ORDER,
|
|
|
- generalProperties.getHost() + "/9th/orderDetail?id=" + orderId);
|
|
|
+// switch (PAY_CHANNEL) {
|
|
|
+// case Constants.PayChannel.SAND:
|
|
|
+// return sandPayService.pay(orderId + "", order.getName(), order.getTotalPrice(),
|
|
|
+// order.getCreatedAt().plusMinutes(3), "order");
|
|
|
+// case Constants.PayChannel.HM:
|
|
|
+// return hmPayService.requestAlipay(orderId + "", order.getTotalPrice(), order.getName(),
|
|
|
+// HMPayService.getTimeout(order.getCreatedAt(), 180), Constants.OrderNotifyType.ORDER,
|
|
|
+// generalProperties.getHost() + "/9th/orderDetail?id=" + orderId);
|
|
|
+// }
|
|
|
+// throw new BusinessException(Constants.PAY_ERR_MSG);
|
|
|
+
|
|
|
+ AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
|
|
|
+ request.setNotifyUrl(alipayProperties.getNotifyUrl());
|
|
|
+ JSONObject bizContent = new JSONObject();
|
|
|
+ bizContent.put("out_trade_no", orderId + "");
|
|
|
+ bizContent.put("total_amount", order.getTotalPrice());
|
|
|
+ bizContent.put("subject", order.getName());
|
|
|
+ JSONObject body = new JSONObject();
|
|
|
+ body.put("type", Constants.OrderNotifyType.ORDER);
|
|
|
+ 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());
|
|
|
+ }
|
|
|
+ String qrCode;
|
|
|
+ if (response.isSuccess()) {
|
|
|
+ qrCode = response.getQrCode();
|
|
|
+ } else {
|
|
|
+ throw new BusinessException(response.getSubMsg());
|
|
|
+ }
|
|
|
+ String ua = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader("User-Agent");
|
|
|
+ if (ua.toLowerCase().contains("micromessenger")) {
|
|
|
+ return "/static/wx_alipay_bridge.html?payUrl=" + URLEncoder.encode(qrCode, StandardCharsets.UTF_8)
|
|
|
+ + "&orderId=" + orderId + "&type=order&returnUrl="
|
|
|
+ + URLEncoder.encode(generalProperties.getHost() + "/9th/store", StandardCharsets.UTF_8);
|
|
|
+ } else {
|
|
|
+ return "alipays://platformapi/startapp?saId=10000007&qrcode=" + qrCode;
|
|
|
}
|
|
|
- throw new BusinessException(Constants.PAY_ERR_MSG);
|
|
|
}
|
|
|
|
|
|
@Cacheable(value = "payOrder", key = "'order#'+#orderId")
|