Prechádzať zdrojové kódy

增加支付、退款初步接口

houweiyu 4 rokov pred
rodič
commit
128ccc6e5f

Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 10 - 0
dinner-core/src/main/java/com/fdage/base/utils/DataUtils.java


+ 89 - 60
dinner-core/src/main/java/com/fdage/controller/app/MapUtils.java

@@ -1,7 +1,8 @@
-package com.fdage.controller.app;
+package com.fdage.base.utils;
 
 import org.apache.commons.beanutils.PropertyUtilsBean;
 import org.apache.commons.lang.ArrayUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.util.Assert;
 
 import java.beans.PropertyDescriptor;
@@ -21,50 +22,66 @@ import java.util.Set;
 public class MapUtils {
 
     public static String getString(String key, Map<String, Object> map) {
-        if (map == null || key == null)
+        if (map == null || key == null){
             throw new IllegalArgumentException();
-        if (!map.containsKey(key))
+        }
+        if (!map.containsKey(key)){
             return null;
+        }
         Object value = map.get(key);
-        if (value == null)
+        if (value == null){
             return null;
+        }
         return value.toString();
     }
 
     public static Integer getInteger(String key, Map<String, Object> map) {
-        if (map == null || key == null)
+        if (map == null || key == null){
             throw new IllegalArgumentException();
-        if (!map.containsKey(key))
+        }
+        if (!map.containsKey(key)){
             return null;
+        }
         Object value = map.get(key);
-        if (value == null)
+        if (value == null){
             return null;
-        if (value instanceof Integer)
+        }
+        if (value instanceof Integer){
             return (Integer) value;
-        if (value instanceof String)
+        }
+        if (value instanceof String){
             return Integer.valueOf((String) value);
+        }
         //Date 不支持变成为date类型
-        if (value instanceof Date)
+        if (value instanceof Date){
             throw new ClassCastException();
-        if (value instanceof Number)
+        }
+        if (value instanceof Number){
             return ((Number) value).intValue();
+        }
         throw new ClassCastException();
     }
 
     public static Long getLong(String key, Map<String, Object> map) {
-        if (map == null || key == null)
+        if (map == null || key == null){
             throw new IllegalArgumentException();
-        if (!map.containsKey(key))
+        }
+        if (!map.containsKey(key)){
             return null;
+        }
         Object value = map.get(key);
-        if (value == null)
+        if (value == null){
             return null;
-        if (value instanceof Long)
+        }
+        if (value instanceof Long){
             return (Long) value;
-        if (value instanceof Number)
+        }
+        if (value instanceof Number){
             return ((Number) value).longValue();
-        if (value instanceof String)
+        }
+        if (value instanceof String){
             return Long.valueOf((String) value);
+        }
         if (value instanceof Date) {
             return (((Date) value).getTime());
         }
@@ -79,44 +96,60 @@ public class MapUtils {
     }
 
     public static Double getDouble(String key, Map<String, Object> map) {
-        if (map == null || key == null)
+        if (map == null || key == null){
             throw new IllegalArgumentException();
-        if (!map.containsKey(key))
+        }
+        if (!map.containsKey(key)){
             return null;
+        }
         Object value = map.get(key);
-        if (value == null)
+        if (value == null){
             return null;
-        if (value instanceof Double)
+        }
+        if (value instanceof Double){
             return (Double) value;
-        if (value instanceof Number)
+        }
+        if (value instanceof Number){
             return ((Number) value).doubleValue();
-        if (value instanceof String)
+        }
+        if (value instanceof String){
             return Double.valueOf((String) value);
+        }
         throw new ClassCastException();
     }
 
     public static BigDecimal getBigDecimal(String key, Map<String, Object> map) {
-        if (map == null || key == null)
+        if (map == null || key == null){
             throw new IllegalArgumentException();
-        if (!map.containsKey(key))
+        }
+        if (!map.containsKey(key)){
             return null;
+        }
         Object value = map.get(key);
-        if (value == null)
+        if (value == null){
             return null;
-        if (value instanceof BigDecimal)
+        }
+        if (value instanceof BigDecimal){
             return (BigDecimal) value;
-        if (value instanceof Integer)
+        }
+        if (value instanceof Integer){
             return new BigDecimal((Integer) value);
-        if (value instanceof Short)
+        }
+        if (value instanceof Short){
             return new BigDecimal((Short) value);
-        if (value instanceof Byte)
+        }
+        if (value instanceof Byte){
             return new BigDecimal((Byte) value);
-        if (value instanceof Long)
+        }
+        if (value instanceof Long){
             return new BigDecimal((Long) value);
-        if (value instanceof Float)
+        }
+        if (value instanceof Float){
             return new BigDecimal((Float) value);
-        if (value instanceof Double)
+        }
+        if (value instanceof Double){
             return new BigDecimal((Double) value);
+        }
         if (value instanceof Date) {
             return new BigDecimal(((Date) value).getTime());
         }
@@ -127,10 +160,11 @@ public class MapUtils {
             return new BigDecimal(((Timestamp) value).getTime());
         }
         if (value instanceof String) {
-            if (!StringUtils.isNullOrEmpty((String) value))
+            if (StringUtils.isNotBlank((String) value)){
                 return new BigDecimal((String) value);
-            else
+            } else{
                 return null;
+            }
         }
         throw new ClassCastException();
     }
@@ -152,11 +186,13 @@ public class MapUtils {
      * @param likeKey
      */
     public static void toLikeValue(Map<String, Object> map, String... likeKey) {
-        if (ArrayUtils.isEmpty(likeKey))
+        if (ArrayUtils.isEmpty(likeKey)){
             return;
+        }
         for (String key : likeKey) {
-            if (map.containsKey(key))
+            if (map.containsKey(key)){
                 map.put(key, "%" + map.get(key) + "%");
+            }
         }
     }
 
@@ -168,14 +204,16 @@ public class MapUtils {
      * @return
      */
     public static Date getDate(String key, Map<String, Object> map) {
-        if (map == null || key == null)
+        if (map == null || key == null){
             throw new IllegalArgumentException();
-        if (!map.containsKey(key))
+        }
+        if (!map.containsKey(key)){
             return null;
+        }
         Object value = map.get(key);
-        if (value == null)
+        if (value == null){
             return null;
-        else {
+        } else {
             if (value instanceof Date) {
                 return (Date) value;
             } else if (value instanceof Timestamp) {
@@ -193,14 +231,16 @@ public class MapUtils {
      * @return
      */
     public static java.util.Date getTimestamp(String key, Map<String, Object> map) {
-        if (map == null || key == null)
+        if (map == null || key == null){
             throw new IllegalArgumentException();
-        if (!map.containsKey(key))
+        }
+        if (!map.containsKey(key)){
             return null;
+        }
         Object value = map.get(key);
-        if (value == null)
+        if (value == null){
             return null;
-        else {
+        } else {
             if (value instanceof Date) {
                 return (Date) value;
             } else if (value instanceof Timestamp) {
@@ -221,8 +261,9 @@ public class MapUtils {
     public static void putIfValueNotNull(Map<String, Object> map, String key, Object value) {
         Assert.notNull(map);
         Assert.hasText(key);
-        if (value != null)
+        if (value != null){
             map.put(key, value);
+        }
     }
 
     /**
@@ -235,23 +276,11 @@ public class MapUtils {
     public static void putIfValueNotEmpty(Map<String, Object> map, String key, String value) {
         Assert.notNull(map);
         Assert.hasText(key);
-        if (!StringUtils.isNullOrEmpty(value))
+        if (StringUtils.isNotBlank(value)){
             map.put(key, value);
+        }
     }
 
-    /**
-     * 将map中指定的key的value值进行处理
-     *
-     * @param key
-     * @param map
-     * @param helper
-     */
-    public static void convertMapValuePattern(String key, Map<String, Object> map, DealMapValueHelper helper) {
-        Assert.hasText(key);
-        Assert.notNull(map);
-        Assert.notNull(helper);
-        helper.dealValue(key, map);
-    }
 
     /**
      * 将javabean实体类转为map类型,然后返回一个map类型的值

+ 0 - 1
dinner-core/src/main/java/com/fdage/base/utils/WechatUtil.java

@@ -2,7 +2,6 @@ package com.fdage.base.utils;
 
 
 import com.fdage.base.dto.WechatRefundApiResult;
-import com.fdage.controller.app.MapUtils;
 import fdage.back.sdk.utils.ResourceUtil;
 import lombok.extern.log4j.Log4j2;
 import org.apache.http.HttpEntity;

+ 153 - 4
dinner-core/src/main/java/com/fdage/controller/app/AppPayController.java

@@ -1,8 +1,10 @@
 package com.fdage.controller.app;
 
+import com.fdage.base.dto.WechatRefundApiResult;
 import com.fdage.base.entity.TmOrder;
 import com.fdage.base.service.impl.TmOrderServiceImpl;
 import com.fdage.base.utils.DataUtils;
+import com.fdage.base.utils.MapUtils;
 import com.fdage.base.utils.WechatUtil;
 import com.fdage.base.utils.XmlUtil;
 import com.fdage.controller.BaseController;
@@ -15,11 +17,14 @@ import io.swagger.annotations.ApiOperation;
 import lombok.extern.log4j.Log4j2;
 import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.bind.annotation.*;
+import springfox.documentation.annotations.ApiIgnore;
 
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
 import java.math.BigDecimal;
 import java.util.HashMap;
 import java.util.Map;
@@ -144,4 +149,148 @@ public class AppPayController extends BaseController {
         }
         return Result.failure("下单失败");
     }
+
+    /**
+     * 订单退款请求
+     */
+    @ApiOperation(value = "订单退款请求")
+    @PostMapping("refund")
+    public Object refund(Integer orderId) {
+        //
+        TmOrder orderInfo = tmOrderService.getById(orderId);
+
+        if (null == orderInfo) {
+            return Result.failure( "订单已取消");
+        }
+
+        if (orderInfo.getStatus() == 401 || orderInfo.getStatus() == 402) {
+            return Result.failure("订单已退款");
+        }
+        WechatRefundApiResult result = WechatUtil.wxRefund(orderInfo.getId(),
+                orderInfo.getOrderPrice().doubleValue(), orderInfo.getOrderPrice().doubleValue());
+        if (result.getResult_code().equals("SUCCESS")) {
+            if (orderInfo.getStatus() == 201) {
+                orderInfo.setStatus(3);
+            } else if (orderInfo.getStatus() == 300) {
+                orderInfo.setStatus(3);
+            }
+            tmOrderService.updateById(orderInfo);
+            return Result.success( "成功退款");
+        } else {
+            return Result.failure( "退款失败");
+        }
+    }
+
+
+    /**
+     * 微信查询订单状态
+     */
+    @ApiOperation(value = "查询订单状态")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "orderId", value = "订单号", paramType = "query", required = true, dataType = "String")
+    })
+    @PostMapping("query")
+    @Transactional(rollbackFor = Exception.class)
+    public Object orderQuery(@RequestParam(name = "orderId") String orderId) {
+        if (orderId == null) {
+            return Result.failure("订单不存在");
+        }
+
+        TmOrder orderDetail = tmOrderService.getById(orderId);
+        Map<Object, Object> parame = new TreeMap<Object, Object>();
+        parame.put("appid", ResourceUtil.getConfigByName("wx.appId"));
+        // 商家账号。
+        parame.put("mch_id", ResourceUtil.getConfigByName("wx.mchId"));
+        String randomStr = DataUtils.getRandomNum(18).toUpperCase();
+        // 随机字符串
+        parame.put("nonce_str", randomStr);
+        // 商户订单编号
+        parame.put("out_trade_no", orderDetail.getOrderNo());
+
+        String sign = WechatUtil.arraySign(parame, ResourceUtil.getConfigByName("wx.paySignKey"));
+        // 数字签证
+        parame.put("sign", sign);
+
+        String xml = MapUtils.convertMap2Xml(parame);
+        log.info("xml:" + xml);
+        Map<String, Object> resultUn = null;
+        try {
+            resultUn = XmlUtil.xmlStrToMap(WechatUtil.requestOnce(ResourceUtil.getConfigByName("wx.orderquery"), xml));
+        } catch (Exception e) {
+            e.printStackTrace();
+            return Result.failure("查询失败,error=" + e.getMessage());
+        }
+        // 响应报文
+        String return_code = MapUtils.getString("return_code", resultUn);
+        String return_msg = MapUtils.getString("return_msg", resultUn);
+
+        if (!"SUCCESS".equals(return_code)) {
+            return  Result.failure("查询失败,error=" + return_msg);
+        }
+
+        String trade_state = MapUtils.getString("trade_state", resultUn);
+        if ("SUCCESS".equals(trade_state)) {
+            //更新订单的状态为已支付,更新库存
+
+
+            return  Result.success("支付成功");
+        } else if ("USERPAYING".equals(trade_state)) {
+            // 重新查询 正在支付中
+
+
+        } else {
+            // 失败
+            return Result.failure("查询失败,error=" + trade_state);
+        }
+
+        return  Result.failure("查询失败,未知错误");
+    }
+
+    /**
+     * 微信订单回调接口
+     *
+     * @return
+     */
+    @ApiIgnore
+    @RequestMapping(value = "/notify", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
+    public void notify(HttpServletRequest request, HttpServletResponse response) {
+        try {
+            request.setCharacterEncoding("UTF-8");
+            response.setCharacterEncoding("UTF-8");
+            response.setContentType("text/html;charset=UTF-8");
+            response.setHeader("Access-Control-Allow-Origin", "*");
+            InputStream in = request.getInputStream();
+            ByteArrayOutputStream out = new ByteArrayOutputStream();
+            byte[] buffer = new byte[1024];
+            int len = 0;
+            while ((len = in.read(buffer)) != -1) {
+                out.write(buffer, 0, len);
+            }
+            out.close();
+            in.close();
+            //xml数据
+            String reponseXml = new String(out.toByteArray(), "utf-8");
+            log.info("收到微信支付结果通知notify:" + reponseXml);
+            WechatRefundApiResult result = (WechatRefundApiResult) XmlUtil.xmlStrToBean(reponseXml, WechatRefundApiResult.class);
+            String result_code = result.getResult_code();
+            if (result_code.equalsIgnoreCase("FAIL")) {
+                //订单编号
+                String out_trade_no = result.getOut_trade_no();
+                log.info("订单" + out_trade_no + "支付失败");
+                response.getWriter().write(DataUtils.setXml("SUCCESS", "OK"));
+            } else if (result_code.equalsIgnoreCase("SUCCESS")) {
+                //订单编号
+                String out_trade_no = result.getOut_trade_no();
+                log.info("订单" + out_trade_no + "支付成功");
+                //更新订单的状态为已支付,并更新库存
+
+
+                response.getWriter().write(DataUtils.setXml("SUCCESS", "OK"));
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+
 }