From 8b47dded9b195b5ddce8c9c0c15186bdf48e47e3 Mon Sep 17 00:00:00 2001
From: 吴学文 <wuxuewen@wuxuewendeMacBook-Pro.local>
Date: 星期六, 11 一月 2020 22:11:29 +0800
Subject: [PATCH] 打印微信回调信息

---
 AppFrontService/src/main/java/com/java110/app/smo/payment/impl/ToPaySMOImpl.java |  142 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 140 insertions(+), 2 deletions(-)

diff --git a/AppFrontService/src/main/java/com/java110/app/smo/payment/impl/ToPaySMOImpl.java b/AppFrontService/src/main/java/com/java110/app/smo/payment/impl/ToPaySMOImpl.java
index eaa55d1..4ac2347 100644
--- a/AppFrontService/src/main/java/com/java110/app/smo/payment/impl/ToPaySMOImpl.java
+++ b/AppFrontService/src/main/java/com/java110/app/smo/payment/impl/ToPaySMOImpl.java
@@ -1,14 +1,41 @@
 package com.java110.app.smo.payment.impl;
 
+import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.java110.app.properties.WechatAuthProperties;
 import com.java110.app.smo.AppAbstractComponentSMO;
 import com.java110.app.smo.payment.IToPaySMO;
 import com.java110.core.context.IPageData;
+import com.java110.dto.order.WxOrderDto;
+import com.java110.utils.constant.ServiceConstant;
+import com.java110.utils.util.Assert;
+import com.java110.utils.util.PayUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Service;
+import org.springframework.web.client.RestTemplate;
 
 import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.SortedMap;
+import java.util.TreeMap;
 
+@Service("toPaySMOImpl")
 public class ToPaySMOImpl extends AppAbstractComponentSMO implements IToPaySMO {
+    private static final Logger logger = LoggerFactory.getLogger(AppAbstractComponentSMO.class);
+
+
+    @Autowired
+    private RestTemplate restTemplate;
+
+    @Autowired
+    private WechatAuthProperties wechatAuthProperties;
+
     @Override
     public ResponseEntity<String> toPay(IPageData pd) {
         return super.businessProcess(pd);
@@ -17,10 +44,121 @@
     @Override
     protected void validate(IPageData pd, JSONObject paramIn) {
 
+        Assert.jsonObjectHaveKey(paramIn, "communityId", "璇锋眰鎶ユ枃涓湭鍖呭惈communityId鑺傜偣");
+        Assert.jsonObjectHaveKey(paramIn, "cycles", "璇锋眰鎶ユ枃涓湭鍖呭惈cycles鑺傜偣");
+        Assert.jsonObjectHaveKey(paramIn, "receivedAmount", "璇锋眰鎶ユ枃涓湭鍖呭惈receivedAmount鑺傜偣");
+        Assert.jsonObjectHaveKey(paramIn, "feeId", "璇锋眰鎶ユ枃涓湭鍖呭惈feeId鑺傜偣");
+
     }
 
     @Override
-    protected ResponseEntity<String> doBusinessProcess(IPageData pd, JSONObject paramIn) throws IOException {
-        return null;
+    protected ResponseEntity<String> doBusinessProcess(IPageData pd, JSONObject paramIn) throws Exception {
+
+        ResponseEntity responseEntity = null;
+        //鏌ヨ鐢ㄦ埛ID
+        paramIn.put("userId", pd.getUserId());
+        String url = ServiceConstant.SERVICE_API_URL + "/api/fee.payFeePre";
+        responseEntity = super.callCenterService(restTemplate, pd, paramIn.toJSONString(), url, HttpMethod.POST);
+
+        if (responseEntity.getStatusCode() != HttpStatus.OK) {
+            return responseEntity;
+        }
+        JSONObject orderInfo = JSONObject.parseObject(responseEntity.getBody().toString());
+        String orderId = orderInfo.getString("oId");
+        double money = Double.parseDouble(orderInfo.getString("receivableAmount"));
+        Map tmpParamIn = new HashMap();
+        tmpParamIn.put("userId", pd.getUserId());
+        responseEntity = super.getUserAndAttr(pd, restTemplate, tmpParamIn);
+        logger.debug("鏌ヨ鐢ㄦ埛淇℃伅杩斿洖鎶ユ枃锛�" + responseEntity);
+        if (responseEntity.getStatusCode() != HttpStatus.OK) {
+            throw new IllegalArgumentException("鏈煡璇㈢敤鎴蜂俊鎭紓甯�" + tmpParamIn);
+        }
+
+        JSONObject userResult = JSONObject.parseObject(responseEntity.getBody().toString());
+        int total = userResult.getIntValue("total");
+        if (total < 1) {
+            //鏈煡璇㈠埌鐢ㄦ埛淇℃伅
+            throw new IllegalArgumentException("鏈煡璇㈠井淇$敤鎴�");
+        }
+
+        JSONObject realUserInfo = userResult.getJSONArray("users").getJSONObject(0);
+
+        String openId = realUserInfo.getString("openId");
+
+        //寰俊涓嬪崟PayUtil
+        Map result = java110Payment(orderId, money, openId);
+        responseEntity = new ResponseEntity(JSONObject.toJSONString(result), HttpStatus.OK);
+
+        return responseEntity;
+    }
+
+
+    /**
+     * 棰勪笅鍗�
+     *
+     * @param orderNum
+     * @param money
+     * @param openId
+     * @return
+     * @throws Exception
+     */
+    private Map<String, String> java110Payment(String orderNum, double money, String openId) throws Exception {
+        logger.info("銆愬皬绋嬪簭鏀粯銆� 缁熶竴涓嬪崟寮�濮�, 璁㈠崟缂栧彿=" + orderNum);
+        SortedMap<String, String> resultMap = new TreeMap<String, String>();
+//鐢熸垚鏀粯閲戦锛屽紑鍙戠幆澧冨鐞嗘敮浠橀噾棰濇暟鍒�0.01銆�0.02銆�0.03鍏�
+
+        double payAmount = PayUtil.getPayAmountByEnv("DEV", money);
+//娣诲姞鎴栨洿鏂版敮浠樿褰�(鍙傛暟璺熻繘鑷繁涓氬姟闇�姹傛坊鍔�)
+
+        Map<String, String> resMap = this.java110UnifieldOrder(orderNum, wechatAuthProperties.TRADE_TYPE_JSAPI, payAmount, openId);
+        if ("SUCCESS".equals(resMap.get("return_code")) && "SUCCESS".equals(resMap.get("result_code"))) {
+            resultMap.put("appId", wechatAuthProperties.getAppId());
+            resultMap.put("timeStamp", PayUtil.getCurrentTimeStamp());
+            resultMap.put("nonceStr", PayUtil.makeUUID(32));
+            resultMap.put("package", "prepay_id=" + resMap.get("prepay_id"));
+            resultMap.put("signType", "MD5");
+            resultMap.put("sign", PayUtil.createSign(resultMap, wechatAuthProperties.getKey()));
+            resultMap.put("code", "0");
+            resultMap.put("msg", "涓嬪崟鎴愬姛");
+            logger.info("銆愬皬绋嬪簭鏀粯銆戠粺涓�涓嬪崟鎴愬姛锛岃繑鍥炲弬鏁�:" + resultMap);
+        } else {
+            resultMap.put("code", resMap.get("return_code"));
+            resultMap.put("msg", resMap.get("return_msg"));
+            logger.info("銆愬皬绋嬪簭鏀粯銆戠粺涓�涓嬪崟澶辫触锛屽け璐ュ師鍥�:" + resMap.get("return_msg"));
+        }
+        return resultMap;
+    }
+
+    /**
+     * 灏忕▼搴忔敮浠樼粺涓�涓嬪崟
+     */
+    private Map<String, String> java110UnifieldOrder(String orderNum, String tradeType, double payAmount, String openid) throws Exception {
+//灏佽鍙傛暟
+        SortedMap<String, String> paramMap = new TreeMap<String, String>();
+        paramMap.put("appid", wechatAuthProperties.getAppId());
+        paramMap.put("mch_id", wechatAuthProperties.getMchId());
+        paramMap.put("nonce_str", PayUtil.makeUUID(32));
+        paramMap.put("body", "HC鏅烘収瀹跺洯-鍋滆溅璐�");
+        paramMap.put("out_trade_no", orderNum);
+        paramMap.put("total_fee", PayUtil.moneyToIntegerStr(payAmount));
+        paramMap.put("spbill_create_ip", PayUtil.getLocalIp());
+        paramMap.put("notify_url", wechatAuthProperties.getWxNotifyUrl());
+        paramMap.put("trade_type", tradeType);
+        paramMap.put("openid", openid);
+        paramMap.put("sign", PayUtil.createSign(paramMap, wechatAuthProperties.getKey()));
+//杞崲涓簒ml
+        String xmlData = PayUtil.mapToXml(paramMap);
+
+        logger.debug("璋冪敤鏀粯缁熶竴涓嬪崟鎺ュ彛" + xmlData);
+
+        ResponseEntity<String> responseEntity = restTemplate.postForEntity(
+                wechatAuthProperties.getWxPayUnifiedOrder(), xmlData, String.class);
+
+        logger.debug("缁熶竴涓嬪崟杩斿洖"+responseEntity);
+//璇锋眰寰俊鍚庡彴锛岃幏鍙栭鏀粯ID
+        if (responseEntity.getStatusCode() != HttpStatus.OK) {
+            throw new IllegalArgumentException("鏀粯澶辫触" + responseEntity.getBody());
+        }
+        return PayUtil.xmlStrToMap(responseEntity.getBody());
     }
 }

--
Gitblit v1.8.0