From acb14e4abc643f05dd641b2b08e2b449132dfce2 Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期三, 23 十二月 2020 08:54:31 +0800
Subject: [PATCH] 优化 相关代码
---
service-front/src/main/java/com/java110/front/smo/payment/impl/ToNotifySMOImpl.java | 141 +++++-----------------------------------------
1 files changed, 16 insertions(+), 125 deletions(-)
diff --git a/service-front/src/main/java/com/java110/front/smo/payment/impl/ToNotifySMOImpl.java b/service-front/src/main/java/com/java110/front/smo/payment/impl/ToNotifySMOImpl.java
index c1b121b..396310c 100644
--- a/service-front/src/main/java/com/java110/front/smo/payment/impl/ToNotifySMOImpl.java
+++ b/service-front/src/main/java/com/java110/front/smo/payment/impl/ToNotifySMOImpl.java
@@ -1,13 +1,23 @@
package com.java110.front.smo.payment.impl;
+import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
+import com.java110.core.factory.WechatFactory;
+import com.java110.dto.smallWeChat.SmallWeChatDto;
import com.java110.front.properties.WechatAuthProperties;
import com.java110.front.smo.AppAbstractComponentSMO;
import com.java110.front.smo.payment.IToNotifySMO;
+import com.java110.front.smo.payment.adapt.IPayNotifyAdapt;
+import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.CommonConstant;
+import com.java110.utils.constant.ServiceCodeConstant;
import com.java110.utils.constant.ServiceConstant;
+import com.java110.utils.constant.WechatConstant;
+import com.java110.utils.factory.ApplicationContextFactory;
+import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.PayUtil;
+import com.java110.utils.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -23,139 +33,20 @@
public class ToNotifySMOImpl implements IToNotifySMO {
private static final Logger logger = LoggerFactory.getLogger(AppAbstractComponentSMO.class);
- private static final String APP_ID = "992020011134400001";
- @Autowired
- private RestTemplate restTemplate;
+ private static final String DEFAULT_PAY_NOTIFY_ADAPT = "wechatPayNotifyAdapt";// 榛樿寰俊閫氱敤鏀粯
- @Autowired
- private WechatAuthProperties wechatAuthProperties;
@Override
public ResponseEntity<String> toNotify(String param, HttpServletRequest request) {
- String resXml = "";
- ResponseEntity responseEntity = null;
- try {
- Map<String, Object> map = PayUtil.getMapFromXML(param);
- logger.info("銆愬皬绋嬪簭鏀粯鍥炶皟銆� 鍥炶皟鏁版嵁锛� \n" + map);
- String returnCode = (String) map.get("return_code");
- if ("SUCCESS".equalsIgnoreCase(returnCode)) {
- String returnmsg = (String) map.get("result_code");
- if ("SUCCESS".equals(returnmsg)) {
-//鏇存柊鏁版嵁
-
- int result = confirmPayFee(map);
-
- if (result > 0) {
-//鏀粯鎴愬姛
- resXml = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>"
- + "<return_msg><![CDATA[OK]]></return_msg>" + "</xml>";
- }
- } else {
- resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>"
- + "<return_msg><![CDATA[鎶ユ枃涓虹┖]></return_msg>" + "</xml>";
- logger.info("鏀粯澶辫触:" + resXml);
- }
- } else {
- resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>"
- + "<return_msg><![CDATA[鎶ユ枃涓虹┖]></return_msg>" + "</xml>";
- logger.info("銆愯鍗曟敮浠樺け璐ャ��");
- }
- } catch (Exception e) {
- logger.error("閫氱煡澶辫触", e);
- resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>"
- + "<return_msg><![CDATA[閴存潈澶辫触]></return_msg>" + "</xml>";
- }
-
+ String payNotifyAdapt = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, WechatConstant.PAY_NOTIFY_ADAPT);
+ payNotifyAdapt = StringUtil.isEmpty(payNotifyAdapt) ? DEFAULT_PAY_NOTIFY_ADAPT : payNotifyAdapt;
+ //鏀粯閫傞厤鍣�
+ IPayNotifyAdapt tPayNotifyAdapt = ApplicationContextFactory.getBean(payNotifyAdapt, IPayNotifyAdapt.class);
+ String resXml = tPayNotifyAdapt.confirmPayFee(param);
logger.info("銆愬皬绋嬪簭鏀粯鍥炶皟鍝嶅簲銆� 鍝嶅簲鍐呭锛歕n" + resXml);
return new ResponseEntity<String>(resXml, HttpStatus.OK);
}
-
- public int confirmPayFee(Map<String, Object> map) {
- SortedMap<String, String> paramMap = new TreeMap<String, String>();
- ResponseEntity<String> responseEntity = null;
- for (String key : map.keySet()) {
- paramMap.put(key, map.get(key).toString());
- }
- String sign = PayUtil.createSign(paramMap, wechatAuthProperties.getKey());
-
- if (!sign.equals(map.get("sign"))) {
- throw new IllegalArgumentException("閴存潈澶辫触");
- }
-
- String outTradeNo = map.get("out_trade_no").toString();
- String openId = map.get("openid").toString();
-
- responseEntity = getUserInfoByOpenId(restTemplate, openId);
-
- logger.debug("鏌ヨ鐢ㄦ埛淇℃伅杩斿洖鎶ユ枃锛�" + responseEntity);
- if (responseEntity.getStatusCode() != HttpStatus.OK) {
- throw new IllegalArgumentException("鏍圭粷openId 鏌ヨ鐢ㄦ埛淇℃伅寮傚父" + openId);
- }
-
- JSONObject userResult = JSONObject.parseObject(responseEntity.getBody());
- JSONObject realUserInfo = userResult.getJSONArray("users").getJSONObject(0);
- String useId = realUserInfo.getString("userId");
-
- //鏌ヨ鐢ㄦ埛ID
- JSONObject paramIn = new JSONObject();
- paramIn.put("oId", outTradeNo);
- String url = ServiceConstant.SERVICE_API_URL + "/api/fee.payFeeConfirm";
- responseEntity = this.callCenterService(restTemplate, useId, paramIn.toJSONString(), url, HttpMethod.POST);
-
- if (responseEntity.getStatusCode() != HttpStatus.OK) {
- return 0;
- }
- return 1;
- }
-
- /**
- * 鑾峰彇鐢ㄦ埛淇℃伅
- *
- * @param restTemplate
- * @return
- */
- protected ResponseEntity<String> getUserInfoByOpenId(RestTemplate restTemplate, String openId) {
- //Assert.hasLength(pd.getUserId(), "鐢ㄦ埛鏈櫥褰曡鍏堢櫥褰�");
- ResponseEntity<String> responseEntity = null;
- responseEntity = this.callCenterService(restTemplate, "-1", "",
- ServiceConstant.SERVICE_API_URL + "/api/user.listUsers?openId=" + openId + "&page=1&row=1", HttpMethod.GET);
- // 杩囨护杩斿洖鎶ユ枃涓殑瀛楁锛屽彧杩斿洖name瀛楁
- //{"address":"","orderTypeCd":"Q","serviceCode":"","responseTime":"20190401194712","sex":"","localtionCd":"","userId":"302019033054910001","levelCd":"00","transactionId":"-1","dataFlowId":"-1","response":{"code":"0000","message":"鎴愬姛"},"name":"996icu","tel":"18909780341","bId":"-1","businessType":"","email":""}
-
- return responseEntity;
-
- }
-
- /**
- * 璋冪敤涓績鏈嶅姟
- *
- * @return
- */
- protected ResponseEntity<String> callCenterService(RestTemplate restTemplate, String userId, String param, String url, HttpMethod httpMethod) {
-
- ResponseEntity<String> responseEntity = null;
- HttpHeaders header = new HttpHeaders();
- header.add(CommonConstant.HTTP_APP_ID.toLowerCase(), APP_ID);
- header.add(CommonConstant.HTTP_USER_ID.toLowerCase(), userId);
- header.add(CommonConstant.HTTP_TRANSACTION_ID.toLowerCase(), UUID.randomUUID().toString());
- header.add(CommonConstant.HTTP_REQ_TIME.toLowerCase(), DateUtil.getDefaultFormateTimeString(new Date()));
- header.add(CommonConstant.HTTP_SIGN.toLowerCase(), "");
- HttpEntity<String> httpEntity = new HttpEntity<String>(param, header);
- //logger.debug("璇锋眰涓績鏈嶅姟淇℃伅锛寋}", httpEntity);
- try {
- responseEntity = restTemplate.exchange(url, httpMethod, httpEntity, String.class);
- } catch (HttpStatusCodeException e) { //杩欓噷spring 妗嗘灦 鍦�4XX 鎴� 5XX 鏃舵姏鍑� HttpServerErrorException 寮傚父锛岄渶瑕侀噸鏂板皝瑁呬竴涓�
- responseEntity = new ResponseEntity<String>( e.getResponseBodyAsString(), e.getStatusCode());
- } catch (Exception e) {
- responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
- } finally {
- logger.debug("璇锋眰鍦板潃涓�,{} 璇锋眰涓績鏈嶅姟淇℃伅锛寋},涓績鏈嶅姟杩斿洖淇℃伅锛寋}", url, httpEntity, responseEntity);
- return responseEntity;
- }
-
- }
-
}
--
Gitblit v1.8.0