|
@@ -3,6 +3,7 @@ package com.yonge.toolset.payment.original.ali;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alipay.api.AlipayApiException;
|
|
import com.alipay.api.AlipayApiException;
|
|
import com.alipay.api.AlipayClient;
|
|
import com.alipay.api.AlipayClient;
|
|
|
|
+import com.alipay.api.internal.util.AlipaySignature;
|
|
import com.alipay.api.request.AlipayTradeAppPayRequest;
|
|
import com.alipay.api.request.AlipayTradeAppPayRequest;
|
|
import com.alipay.api.request.AlipayTradeCloseRequest;
|
|
import com.alipay.api.request.AlipayTradeCloseRequest;
|
|
import com.alipay.api.request.AlipayTradeQueryRequest;
|
|
import com.alipay.api.request.AlipayTradeQueryRequest;
|
|
@@ -13,25 +14,35 @@ import com.alipay.api.response.AlipayTradeQueryResponse;
|
|
import com.alipay.api.response.AlipayTradeRefundResponse;
|
|
import com.alipay.api.response.AlipayTradeRefundResponse;
|
|
import com.yonge.toolset.base.result.BaseResult;
|
|
import com.yonge.toolset.base.result.BaseResult;
|
|
import com.yonge.toolset.payment.base.PaymentTemplate;
|
|
import com.yonge.toolset.payment.base.PaymentTemplate;
|
|
|
|
+import com.yonge.toolset.payment.base.constant.PaymentConstant;
|
|
import com.yonge.toolset.payment.base.enums.MethodNameEnum;
|
|
import com.yonge.toolset.payment.base.enums.MethodNameEnum;
|
|
import com.yonge.toolset.payment.base.enums.OpenEnum;
|
|
import com.yonge.toolset.payment.base.enums.OpenEnum;
|
|
import com.yonge.toolset.payment.base.enums.PayChannelEnum;
|
|
import com.yonge.toolset.payment.base.enums.PayChannelEnum;
|
|
import com.yonge.toolset.payment.base.enums.TradeStatusEnum;
|
|
import com.yonge.toolset.payment.base.enums.TradeStatusEnum;
|
|
import com.yonge.toolset.payment.base.model.*;
|
|
import com.yonge.toolset.payment.base.model.*;
|
|
|
|
+import com.yonge.toolset.payment.base.model.callback.ClosePaymentCallBack;
|
|
|
|
+import com.yonge.toolset.payment.base.model.callback.ExecutePaymentCallBack;
|
|
import com.yonge.toolset.payment.base.model.callback.PaymentCallBack;
|
|
import com.yonge.toolset.payment.base.model.callback.PaymentCallBack;
|
|
|
|
+import com.yonge.toolset.payment.base.model.callback.RefundPaymentCallBack;
|
|
import com.yonge.toolset.payment.core.props.PaymentProperties;
|
|
import com.yonge.toolset.payment.core.props.PaymentProperties;
|
|
import com.yonge.toolset.payment.core.service.SysConfigPaymentService;
|
|
import com.yonge.toolset.payment.core.service.SysConfigPaymentService;
|
|
|
|
+import com.yonge.toolset.payment.core.util.RequestKitBean;
|
|
|
|
+import com.yonge.toolset.payment.original.ali.constant.AlipayConstant;
|
|
|
|
+import com.yonge.toolset.utils.date.DateUtil;
|
|
|
|
+import com.yonge.toolset.utils.string.StringUtil;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.http.HttpStatus;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.web.context.request.RequestAttributes;
|
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.HashMap;
|
|
|
|
-import java.util.List;
|
|
|
|
-import java.util.Map;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
@Component
|
|
@Component
|
|
public class OriginalAliAppTemplate implements PaymentTemplate {
|
|
public class OriginalAliAppTemplate implements PaymentTemplate {
|
|
@@ -44,6 +55,12 @@ public class OriginalAliAppTemplate implements PaymentTemplate {
|
|
@Autowired
|
|
@Autowired
|
|
private AlipayClient alipayClient;
|
|
private AlipayClient alipayClient;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private SysConfigPaymentService configPaymentService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private RequestKitBean requestKitBean;
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public BaseResult<Map<String, Object>> getOpenAuthMsg(OpenAuth openAuth) {
|
|
public BaseResult<Map<String, Object>> getOpenAuthMsg(OpenAuth openAuth) {
|
|
return null;
|
|
return null;
|
|
@@ -108,13 +125,13 @@ public class OriginalAliAppTemplate implements PaymentTemplate {
|
|
payment.setPayAmt(response.getPayAmount());
|
|
payment.setPayAmt(response.getPayAmount());
|
|
//获取交易状态
|
|
//获取交易状态
|
|
String tradeStatus = response.getTradeStatus();
|
|
String tradeStatus = response.getTradeStatus();
|
|
- if("WAIT_BUYER_PAY".equals(tradeStatus)){
|
|
|
|
|
|
+ if ("WAIT_BUYER_PAY".equals(tradeStatus)) {
|
|
payment.setStatus(TradeStatusEnum.pending);
|
|
payment.setStatus(TradeStatusEnum.pending);
|
|
- }else if("TRADE_CLOSED".equals(tradeStatus)){
|
|
|
|
|
|
+ } else if ("TRADE_CLOSED".equals(tradeStatus)) {
|
|
payment.setStatus(TradeStatusEnum.succeeded);
|
|
payment.setStatus(TradeStatusEnum.succeeded);
|
|
- }else if("TRADE_SUCCESS".equals(tradeStatus)){
|
|
|
|
|
|
+ } else if ("TRADE_SUCCESS".equals(tradeStatus)) {
|
|
payment.setStatus(TradeStatusEnum.succeeded);
|
|
payment.setStatus(TradeStatusEnum.succeeded);
|
|
- }else if("TRADE_FINISHED".equals(tradeStatus)){
|
|
|
|
|
|
+ } else if ("TRADE_FINISHED".equals(tradeStatus)) {
|
|
payment.setStatus(TradeStatusEnum.succeeded);
|
|
payment.setStatus(TradeStatusEnum.succeeded);
|
|
}
|
|
}
|
|
return BaseResult.succeed(payment);
|
|
return BaseResult.succeed(payment);
|
|
@@ -132,7 +149,7 @@ public class OriginalAliAppTemplate implements PaymentTemplate {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public BaseResult<ClosePayment> closePayment(ClosePayment closePayment) {
|
|
public BaseResult<ClosePayment> closePayment(ClosePayment closePayment) {
|
|
- AlipayTradeCloseRequest request = new AlipayTradeCloseRequest ();
|
|
|
|
|
|
+ AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
|
|
|
|
|
|
JSONObject bizContent = new JSONObject();
|
|
JSONObject bizContent = new JSONObject();
|
|
bizContent.put("trade_no", closePayment.getId());
|
|
bizContent.put("trade_no", closePayment.getId());
|
|
@@ -186,8 +203,98 @@ public class OriginalAliAppTemplate implements PaymentTemplate {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public BaseResult<PaymentCallBack> analysisNotice(OpenEnum openType, PayChannelEnum payChannel, MethodNameEnum methodName, HttpServletRequest request) {
|
|
public BaseResult<PaymentCallBack> analysisNotice(OpenEnum openType, PayChannelEnum payChannel, MethodNameEnum methodName, HttpServletRequest request) {
|
|
- return null;
|
|
|
|
|
|
+ PaymentCallBack paymentCallBack = new PaymentCallBack();
|
|
|
|
+ try {
|
|
|
|
+ JSONObject params = requestKitBean.getReqParamJSON();
|
|
|
|
+
|
|
|
|
+ JSONObject jsonParams = (JSONObject) params;
|
|
|
|
+
|
|
|
|
+ //获取验签方式 是否使用证书 YES NO
|
|
|
|
+ String USE_CERT = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, AlipayConstant.ALI_USE_CERT).getParamValue();
|
|
|
|
+ String ALI_SIGN_TYPE = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, AlipayConstant.ALI_SIGN_TYPE).getParamValue();
|
|
|
|
+ String ALI_ALIPAY_PUBLIC_KEY = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, AlipayConstant.ALI_ALIPAY_PUBLIC_KEY).getParamValue();
|
|
|
|
+ String CERT_PATH = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, AlipayConstant.ALI_CERT_PATH).getParamValue();
|
|
|
|
+
|
|
|
|
+ boolean verifyResult;
|
|
|
|
+ if ("YES".equals(USE_CERT)) {
|
|
|
|
+ verifyResult = AlipaySignature.rsaCertCheckV1(jsonParams.toJavaObject(Map.class), CERT_PATH,
|
|
|
|
+ PaymentConstant.CHARSET, ALI_SIGN_TYPE);
|
|
|
|
+ } else {
|
|
|
|
+ verifyResult = AlipaySignature.rsaCheckV1(jsonParams.toJavaObject(Map.class), ALI_ALIPAY_PUBLIC_KEY, PaymentConstant.CHARSET, ALI_SIGN_TYPE);
|
|
|
|
+ }
|
|
|
|
+ //验签失败
|
|
|
|
+ if (!verifyResult) {
|
|
|
|
+ return BaseResult.failed(HttpStatus.INTERNAL_SERVER_ERROR, paymentCallBack, "验签失败");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ paymentCallBack.setId(jsonParams.getString("trade_no"));
|
|
|
|
+ paymentCallBack.setPaymentNo(params.getString("out_trade_no"));
|
|
|
|
+
|
|
|
|
+ //获取订单所处交易状态
|
|
|
|
+ String trade_status = jsonParams.getString("trade_status");
|
|
|
|
+ //退款单号
|
|
|
|
+ String refundNo = jsonParams.getString("out_biz_no");
|
|
|
|
+
|
|
|
|
+ paymentCallBack.setStatus(TradeStatusEnum.succeeded);
|
|
|
|
+ //退款
|
|
|
|
+ if (!StringUtil.isEmpty(refundNo)
|
|
|
|
+ || null != jsonParams.getBigDecimal("refund_fee")) {
|
|
|
|
+ paymentCallBack.setMethodName(MethodNameEnum.refundPayment);
|
|
|
|
+ //退款成功
|
|
|
|
+ RefundPaymentCallBack refundPayment = new RefundPaymentCallBack();
|
|
|
|
+ refundPayment.setBillNo(refundNo);
|
|
|
|
+ refundPayment.setTransNo(refundNo);
|
|
|
|
+
|
|
|
|
+ refundPayment.setRefundAmt(jsonParams.getString("refund_fee"));
|
|
|
|
+ refundPayment.setTradeTime(jsonParams.getDate("gmt_refund"));
|
|
|
|
+ } else {
|
|
|
|
+
|
|
|
|
+ if ("TRADE_CLOSED".equals(trade_status)) {
|
|
|
|
+ //关单成功
|
|
|
|
+ paymentCallBack.setMethodName(MethodNameEnum.closePayment);
|
|
|
|
+
|
|
|
|
+ ClosePaymentCallBack closePayment = new ClosePaymentCallBack();
|
|
|
|
+ closePayment.setTradeTime(jsonParams.getDate("gmt_close"));
|
|
|
|
+ paymentCallBack.setClosePaymentCallBack(closePayment);
|
|
|
|
+ } else {
|
|
|
|
+ //支付成功
|
|
|
|
+ paymentCallBack.setMethodName(MethodNameEnum.executePayment);
|
|
|
|
+
|
|
|
|
+ ExecutePaymentCallBack executePayment = new ExecutePaymentCallBack();
|
|
|
|
+ String total_amount = jsonParams.getString("total_amount");
|
|
|
|
+ String receipt_amount = jsonParams.getString("receipt_amount");
|
|
|
|
+
|
|
|
|
+ executePayment.setPayAmt(total_amount);
|
|
|
|
+ executePayment.setFeeAmt(sub(total_amount, receipt_amount));
|
|
|
|
+ executePayment.setTradeTime(jsonParams.getDate("gmt_payment"));
|
|
|
|
+ paymentCallBack.setExecutePaymentCallBack(executePayment);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return BaseResult.succeed(paymentCallBack);
|
|
|
|
+ } catch (AlipayApiException e) {
|
|
|
|
+ return BaseResult.failed(HttpStatus.INTERNAL_SERVER_ERROR, paymentCallBack, "微信回调对象转换失败");
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ return BaseResult.failed(HttpStatus.INTERNAL_SERVER_ERROR, paymentCallBack, "解析异常");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * v1-v2
|
|
|
|
+ *
|
|
|
|
+ * @param v1
|
|
|
|
+ * @param v2
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private String sub(String v1, String v2) {
|
|
|
|
+ try {
|
|
|
|
+ String s = new BigDecimal(v1).subtract(new BigDecimal(v2)).toString();
|
|
|
|
+ return s;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|