From 3721d9e4c1f79b7b38d89db3a73c62135640be64 Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期二, 24 五月 2022 15:52:04 +0800
Subject: [PATCH] 优化代码

---
 service-acct/src/main/java/com/java110/acct/smo/impl/QrCodeAliPaymentAdapt.java |   47 ++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 36 insertions(+), 11 deletions(-)

diff --git a/service-acct/src/main/java/com/java110/acct/smo/impl/QrCodeAliPaymentAdapt.java b/service-acct/src/main/java/com/java110/acct/smo/impl/QrCodeAliPaymentAdapt.java
index c208151..40da16d 100644
--- a/service-acct/src/main/java/com/java110/acct/smo/impl/QrCodeAliPaymentAdapt.java
+++ b/service-acct/src/main/java/com/java110/acct/smo/impl/QrCodeAliPaymentAdapt.java
@@ -9,14 +9,23 @@
 import com.alipay.api.response.AlipayTradePayResponse;
 import com.alipay.api.response.AlipayTradeQueryResponse;
 import com.java110.acct.smo.IQrCodePaymentSMO;
+import com.java110.core.factory.CommunitySettingFactory;
+import com.java110.core.log.LoggerFactory;
 import com.java110.utils.cache.MappingCache;
 import com.java110.utils.constant.WechatConstant;
+import com.java110.utils.util.StringUtil;
 import com.java110.vo.ResultVo;
+import org.slf4j.Logger;
+import org.springframework.stereotype.Service;
 
 /**
  * 闃块噷鏀粯
  */
+@Service
 public class QrCodeAliPaymentAdapt implements IQrCodePaymentSMO {
+
+    private static Logger logger = LoggerFactory.getLogger(QrCodeAliPaymentAdapt.class);
+
     /**
      * APP_ID 搴旂敤id
      */
@@ -38,12 +47,16 @@
     /**
      * (娌欑)缃戝叧
      */
-    public final static String GETEWAY_URL = "https://openapi.alipaydev.com/gateway.do";
+    public final static String GETEWAY_URL = "https://openapi.alipay.com/gateway.do";
 
     /**
      * 鏍煎紡鍖�
      */
     public final static String FORMAT = "json";
+    /**
+     * 鏍煎紡鍖�
+     */
+    public final static String APP_AUTH_TOKEN = "APP_AUTH_TOKEN";
 
     /**
      * 绛惧悕绫诲瀷
@@ -54,10 +67,14 @@
     public ResultVo pay(String communityId, String orderNum, double money, String authCode, String feeName) throws Exception {
         String systemName = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, WechatConstant.PAY_GOOD_NAME);
 
-        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
-                "app_id",
-                "your private_key", "json", "GBK", "alipay_public_key", "RSA2");
+        AlipayClient alipayClient = new DefaultAlipayClient(GETEWAY_URL,
+                CommunitySettingFactory.getValue(communityId, "APP_ID"),
+                CommunitySettingFactory.getRemark(communityId, "APP_PRIVATE_KEY"),
+                "json", "UTF-8", CommunitySettingFactory.getRemark(communityId, "ALIPAY_PUBLIC_KEY"), "RSA2");
         AlipayTradePayRequest request = new AlipayTradePayRequest();
+        if (!StringUtil.isEmpty(CommunitySettingFactory.getValue(communityId, APP_AUTH_TOKEN))) {
+            request.putOtherTextParam("app_auth_token", CommunitySettingFactory.getValue(communityId, APP_AUTH_TOKEN));
+        }
         JSONObject bizContent = new JSONObject();
         bizContent.put("out_trade_no", orderNum);
         bizContent.put("total_amount", money);
@@ -66,26 +83,34 @@
         bizContent.put("auth_code", authCode);
         request.setBizContent(bizContent.toString());
         AlipayTradePayResponse response = alipayClient.execute(request);
-        if (response.isSuccess()) {
+        logger.debug("鏀粯瀹濊繑鍥�:" + JSONObject.toJSONString(response));
+        if ("10000".equals(response.getCode()) && response.isSuccess()) {
             System.out.println("璋冪敤鎴愬姛");
         } else {
             System.out.println("璋冪敤澶辫触");
         }
 
-        if (response.isSuccess()) {
+        if ("10000".equals(response.getCode()) && response.isSuccess()) {
             return new ResultVo(ResultVo.CODE_OK, "鎴愬姛");
         } else {
-            return new ResultVo(ResultVo.CODE_ERROR, response.getSubMsg());
+            return new ResultVo(ResultVo.CODE_ERROR, StringUtil.isEmpty(response.getSubMsg())?"绛夊緟鐢ㄦ埛鏀粯":response.getSubMsg());
         }
     }
 
     @Override
     public ResultVo checkPayFinish(String communityId, String orderNum) {
-        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", "app_id", "your private_key", "json", "GBK", "alipay_public_key", "RSA2");
+        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
+                CommunitySettingFactory.getValue(communityId, "APP_ID"),
+                CommunitySettingFactory.getRemark(communityId, "APP_PRIVATE_KEY"),
+                "json", "UTF-8",
+                CommunitySettingFactory.getRemark(communityId, "ALIPAY_PUBLIC_KEY"), "RSA2");
         AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
+        if (!StringUtil.isEmpty(CommunitySettingFactory.getValue(communityId, APP_AUTH_TOKEN))) {
+            request.putOtherTextParam("app_auth_token", CommunitySettingFactory.getValue(communityId, APP_AUTH_TOKEN));
+        }
         request.setBizContent("{" +
-                "  \"out_trade_no\":\"20150320010101001\"," +
-                "  \"trade_no\":\"2014112611001004680 073956707\"," +
+                "  \"out_trade_no\":\"" + orderNum + "\"," +
+                "  \"trade_no\":\"\"," +
                 "  \"query_options\":[" +
                 "    \"trade_settle_info\"" +
                 "  ]" +
@@ -96,7 +121,7 @@
         } catch (AlipayApiException e) {
             return new ResultVo(ResultVo.CODE_ERROR, "鏌ヨ澶辫触" + e.getErrMsg());
         }
-
+        logger.debug("鏀粯瀹濊繑鍥�:" + JSONObject.toJSONString(response));
         if (!"10000".equals(response.getCode())) {
             return new ResultVo(ResultVo.CODE_ERROR, response.getMsg());
         }

--
Gitblit v1.8.0