|
|
@@ -0,0 +1,121 @@
|
|
|
+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.request.AlipayTradeQueryRequest;
|
|
|
+import com.alipay.api.request.AlipayTradeRefundRequest;
|
|
|
+import com.alipay.api.response.AlipayTradePrecreateResponse;
|
|
|
+import com.alipay.api.response.AlipayTradeQueryResponse;
|
|
|
+import com.alipay.api.response.AlipayTradeRefundResponse;
|
|
|
+import com.izouma.nineth.config.AlipayProperties;
|
|
|
+import com.izouma.nineth.config.Constants;
|
|
|
+import com.izouma.nineth.dto.PayQuery;
|
|
|
+import com.izouma.nineth.enums.PayStatus;
|
|
|
+import com.izouma.nineth.exception.BusinessException;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+@Slf4j
|
|
|
+public class AlipayService {
|
|
|
+
|
|
|
+ private final AlipayProperties alipayProperties;
|
|
|
+ private final AlipayClient alipayClient;
|
|
|
+
|
|
|
+ public 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());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void refund(String orderId, BigDecimal amount) {
|
|
|
+ AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
|
|
|
+ JSONObject bizContent = new JSONObject();
|
|
|
+ bizContent.put("out_trade_no", orderId);
|
|
|
+ bizContent.put("refund_amount", amount.stripTrailingZeros().toPlainString());
|
|
|
+ bizContent.put("out_request_no", orderId);
|
|
|
+
|
|
|
+ request.setBizContent(bizContent.toString());
|
|
|
+ AlipayTradeRefundResponse response = null;
|
|
|
+ try {
|
|
|
+ response = alipayClient.execute(request);
|
|
|
+ } catch (AlipayApiException e) {
|
|
|
+ log.error("支付宝退款失败", e);
|
|
|
+ throw new BusinessException(e.getErrMsg());
|
|
|
+ }
|
|
|
+ log.info("支付宝退款结果:{}", response.getBody());
|
|
|
+ if (response.isSuccess() && "10000".equals(response.getCode())) {
|
|
|
+ log.info("支付宝退款成功");
|
|
|
+ } else {
|
|
|
+ throw new BusinessException(response.getMsg() + ";" + response.getSubMsg());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public PayQuery payQuery(String orderId) throws AlipayApiException {
|
|
|
+ AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
|
|
|
+ JSONObject bizContent = new JSONObject();
|
|
|
+ bizContent.put("out_trade_no", orderId);
|
|
|
+ request.setBizContent(bizContent.toJSONString());
|
|
|
+ AlipayTradeQueryResponse response = alipayClient.execute(request);
|
|
|
+ PayQuery query = new PayQuery(Constants.PayChannel.ALI);
|
|
|
+ query.setOrderId(orderId);
|
|
|
+ if (response.isSuccess() && "10000".equals(response.getCode())) {
|
|
|
+ query.setExist(true);
|
|
|
+ query.setAmount(new BigDecimal(response.getTotalAmount()));
|
|
|
+ query.setTransactionId(response.getTradeNo());
|
|
|
+ switch (response.getTradeStatus()) {
|
|
|
+ case "TRADE_SUCCESS":
|
|
|
+ case "TRADE_FINISHED":
|
|
|
+ query.setStatus(PayStatus.SUCCESS);
|
|
|
+ break;
|
|
|
+ case "WAIT_BUYER_PAY":
|
|
|
+ query.setStatus(PayStatus.PENDING);
|
|
|
+ break;
|
|
|
+ case "TRADE_CLOSED":
|
|
|
+ query.setStatus(PayStatus.CANCEL);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (response.getSendPayDate() != null) {
|
|
|
+ query.setPayTime(response.getSendPayDate().toInstant()
|
|
|
+ .atZone(ZoneId.systemDefault())
|
|
|
+ .toLocalDateTime());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ query.setExist(false);
|
|
|
+ query.setMsg(response.getMsg() + ";" + response.getSubMsg());
|
|
|
+ }
|
|
|
+ return query;
|
|
|
+ }
|
|
|
+}
|