|
|
@@ -0,0 +1,252 @@
|
|
|
+package shop.alien.lawyer.util.ali;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.alipay.api.AlipayApiException;
|
|
|
+import com.alipay.api.AlipayClient;
|
|
|
+import com.alipay.api.AlipayConfig;
|
|
|
+import com.alipay.api.DefaultAlipayClient;
|
|
|
+import com.alipay.api.domain.*;
|
|
|
+import com.alipay.api.request.*;
|
|
|
+import com.alipay.api.response.*;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import shop.alien.entity.store.*;
|
|
|
+import shop.alien.lawyer.service.StoreAliPayRefundLogService;
|
|
|
+import shop.alien.util.common.UniqueRandomNumGenerator;
|
|
|
+import shop.alien.util.common.UrlEncode;
|
|
|
+import shop.alien.util.system.OSUtil;
|
|
|
+
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 支付宝相关组件
|
|
|
+ *
|
|
|
+ * @author ssk
|
|
|
+ * @version 1.0
|
|
|
+ * @date 2024/12/10 8:49
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class AliApi {
|
|
|
+
|
|
|
+
|
|
|
+private final StoreAliPayRefundLogService storeAliPayRefundLogService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 商家端appId
|
|
|
+ */
|
|
|
+ @Value("${app.business.appId}")
|
|
|
+ private String businessAppId;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 商家端app私钥
|
|
|
+ */
|
|
|
+ @Value("${app.business.appPrivateKey}")
|
|
|
+ private String businessAppPrivateKey;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 商家端app公钥
|
|
|
+ */
|
|
|
+ @Value("${app.business.appPublicKey}")
|
|
|
+ private String businessAppPublicKey;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * windows应用公钥证书文件路径
|
|
|
+ */
|
|
|
+ @Value("${app.business.win.appCertPath}")
|
|
|
+ private String businessWinAppCertPath;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * windows支付宝公钥证书文件路径
|
|
|
+ */
|
|
|
+ @Value("${app.business.win.alipayPublicCertPath}")
|
|
|
+ private String businessWinAlipayPublicCertPath;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * windows支付宝根证书文件路径
|
|
|
+ */
|
|
|
+ @Value("${app.business.win.alipayRootCertPath}")
|
|
|
+ private String businessWinAlipayRootCertPath;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * linux应用公钥证书文件路径
|
|
|
+ */
|
|
|
+ @Value("${app.business.linux.appCertPath}")
|
|
|
+ private String businessLinuxAppCertPath;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * linux支付宝公钥证书文件路径
|
|
|
+ */
|
|
|
+ @Value("${app.business.linux.alipayPublicCertPath}")
|
|
|
+ private String businessLinuxAlipayPublicCertPath;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * linux支付宝根证书文件路径
|
|
|
+ */
|
|
|
+ @Value("${app.business.linux.alipayRootCertPath}")
|
|
|
+ private String businessLinuxAlipayRootCertPath;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 支对称加密算法密钥
|
|
|
+ */
|
|
|
+ @Value("${ali.aes.encryptKey}")
|
|
|
+ private String encryptKey;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 填写阿里配置
|
|
|
+ *
|
|
|
+ * @return AlipayConfig
|
|
|
+ */
|
|
|
+ private AlipayConfig setAlipayConfig(String type) {
|
|
|
+ AlipayConfig alipayConfig = new AlipayConfig();
|
|
|
+ alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
|
|
|
+ alipayConfig.setAppId(businessAppId);
|
|
|
+ alipayConfig.setPrivateKey(businessAppPrivateKey);
|
|
|
+ alipayConfig.setFormat("json");
|
|
|
+ alipayConfig.setCharset("UTF-8");
|
|
|
+ alipayConfig.setSignType("RSA2");
|
|
|
+ if ("windows".equals(OSUtil.getOsName())) {
|
|
|
+ alipayConfig.setAppCertPath(businessWinAppCertPath);
|
|
|
+ alipayConfig.setAlipayPublicCertPath(businessWinAlipayPublicCertPath);
|
|
|
+ alipayConfig.setRootCertPath(businessWinAlipayRootCertPath);
|
|
|
+ } else {
|
|
|
+ alipayConfig.setAppCertPath(businessLinuxAppCertPath);
|
|
|
+ alipayConfig.setAlipayPublicCertPath(businessLinuxAlipayPublicCertPath);
|
|
|
+ alipayConfig.setRootCertPath(businessLinuxAlipayRootCertPath);
|
|
|
+ }
|
|
|
+ if ("aes".equals(type)) {
|
|
|
+ alipayConfig.setEncryptType("AES");
|
|
|
+ alipayConfig.setEncryptKey(encryptKey);
|
|
|
+ }
|
|
|
+ return alipayConfig;
|
|
|
+ }
|
|
|
+
|
|
|
+ public JSONObject promotionPackagePay(String price, String subject) {
|
|
|
+ try {
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
|
+ String orderNo = UniqueRandomNumGenerator.generateUniqueCode(19);
|
|
|
+ AlipayClient alipayClient = new DefaultAlipayClient(setAlipayConfig(null));
|
|
|
+ AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
|
|
|
+ AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
|
|
|
+ model.setOutTradeNo(orderNo);
|
|
|
+ model.setTotalAmount(price);
|
|
|
+ model.setSubject(subject);
|
|
|
+ model.setPassbackParams(UrlEncode.getUrlEncode(dateFormat.toString()));
|
|
|
+ request.setBizModel(model);
|
|
|
+ AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
|
|
|
+ String orderStr = "";
|
|
|
+ if (response.isSuccess()) {
|
|
|
+ orderStr = response.getBody();
|
|
|
+ } else {
|
|
|
+ System.out.println("调用失败 response:" + response);
|
|
|
+ orderStr = "调用失败";
|
|
|
+ }
|
|
|
+ jsonObject.put("orderStr", orderStr);
|
|
|
+ jsonObject.put("orderNo", orderNo);
|
|
|
+ return jsonObject;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 退款
|
|
|
+ *
|
|
|
+ * @param outTradeNo
|
|
|
+ * @param refundAmount
|
|
|
+ * @param refundReason
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+
|
|
|
+ public String processRefund(String outTradeNo, String refundAmount, String refundReason, String partialRefundCode) {
|
|
|
+ try {
|
|
|
+ // TODO 部分退款支付宝接口修改
|
|
|
+ AlipayClient alipayClient = new DefaultAlipayClient(setAlipayConfig(null));
|
|
|
+ AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
|
|
|
+ AlipayTradeRefundModel model = new AlipayTradeRefundModel();
|
|
|
+ model.setOutTradeNo(outTradeNo);
|
|
|
+ model.setRefundAmount(refundAmount);
|
|
|
+ model.setRefundReason(refundReason);
|
|
|
+ if (StringUtils.isNotBlank(partialRefundCode)) {
|
|
|
+ model.setOutRequestNo(partialRefundCode);
|
|
|
+ }
|
|
|
+ request.setBizModel(model);
|
|
|
+ AlipayTradeRefundResponse response = alipayClient.certificateExecute(request);
|
|
|
+ String refundReslut = "";
|
|
|
+ if (response.isSuccess()) {
|
|
|
+ refundReslut = "调用成功";
|
|
|
+ // 保存退款信息进入到退款记录表
|
|
|
+ JSONObject responseBody = JSONObject.parseObject(response.getBody());
|
|
|
+ JSONObject refundResponse = responseBody.getJSONObject("alipay_trade_refund_response");
|
|
|
+
|
|
|
+ StoreAliPayRefundLog refundLog = new StoreAliPayRefundLog();
|
|
|
+ // 响应基本信息
|
|
|
+ refundLog.setResponseCode(refundResponse.getString("code"));
|
|
|
+ refundLog.setResponseMsg(refundResponse.getString("msg"));
|
|
|
+ // 买家信息
|
|
|
+ refundLog.setBuyerLogonId(refundResponse.getString("buyer_logon_id"));
|
|
|
+ refundLog.setBuyerOpenId(refundResponse.getString("buyer_open_id"));
|
|
|
+ // 资金变动信息
|
|
|
+ refundLog.setFundChange(refundResponse.getString("fund_change"));
|
|
|
+ // 订单信息
|
|
|
+ refundLog.setOutTradeNo(refundResponse.getString("out_trade_no"));
|
|
|
+ refundLog.setTradeNo(refundResponse.getString("trade_no"));
|
|
|
+ // 退款金额信息
|
|
|
+ refundLog.setRefundFee(refundResponse.getString("refund_fee"));
|
|
|
+ refundLog.setSendBackFee(refundResponse.getString("send_back_fee"));
|
|
|
+ // 退款渠道明细(转换为JSON字符串)
|
|
|
+ if (refundResponse.containsKey("refund_detail_item_list")) {
|
|
|
+ JSONArray refundDetailList = refundResponse.getJSONArray("refund_detail_item_list");
|
|
|
+ if (refundDetailList != null) {
|
|
|
+ refundLog.setRefundDetailItemList(JSON.toJSONString(refundDetailList));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 退款时间(字符串转Date)
|
|
|
+ String gmtRefundPayStr = refundResponse.getString("gmt_refund_pay");
|
|
|
+ if (StringUtils.isNotBlank(gmtRefundPayStr)) {
|
|
|
+ try {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ refundLog.setGmtRefundPay(sdf.parse(gmtRefundPayStr));
|
|
|
+ } catch (ParseException e) {
|
|
|
+ log.warn("解析退款时间失败: {}", gmtRefundPayStr, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 退款原因和部分退款编号(从请求参数中获取)
|
|
|
+ refundLog.setRefundReason(refundReason);
|
|
|
+ refundLog.setOutRequestNo(partialRefundCode);
|
|
|
+ // 证书和签名信息
|
|
|
+ refundLog.setAlipayCertSn(responseBody.getString("alipay_cert_sn"));
|
|
|
+ refundLog.setSign(responseBody.getString("sign"));
|
|
|
+ // 标准字段
|
|
|
+ refundLog.setDeleteFlag(0);
|
|
|
+ refundLog.setCreatedTime(new Date());
|
|
|
+
|
|
|
+ storeAliPayRefundLogService.save(refundLog);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ log.warn("AliPayConfig.processRefund ERROR Msg={}", response.getBody());
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(response.getBody()).getJSONObject("alipay_trade_refund_response");
|
|
|
+ refundReslut = jsonObject.getString("sub_msg");
|
|
|
+ }
|
|
|
+ return refundReslut;
|
|
|
+ } catch (AlipayApiException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|