Quellcode durchsuchen

抖音小程序 下单

Althars123 vor 3 Jahren
Ursprung
Commit
a50b973db7

+ 22 - 106
sdjk-admin/src/main/java/com/miaxis/app/controller/tt/TtNotifyController.java

@@ -8,6 +8,9 @@ import com.miaxis.common.constant.Constants;
 import com.miaxis.common.core.domain.entity.UserInfo;
 import com.miaxis.common.exception.CustomException;
 import com.miaxis.common.utils.AesUtil;
+import com.miaxis.tt.domain.TtOrder;
+import com.miaxis.tt.dto.TtpayNotifyDTO;
+import com.miaxis.tt.service.ITtOrderService;
 import com.miaxis.user.service.IUserInfoService;
 import com.miaxis.vip.domain.VipCode;
 import com.miaxis.vip.service.IVipCodeService;
@@ -23,6 +26,7 @@ import io.swagger.annotations.ApiOperation;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.joda.time.DateTime;
+import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.Base64Utils;
 import org.springframework.web.bind.annotation.PostMapping;
@@ -54,6 +58,9 @@ public class TtNotifyController {
     @Autowired
     private IWxOrderService wxOrderService;
 
+    @Autowired
+    private ITtOrderService ttOrderService;
+
     @Autowired
     private IVipCodeService vipCodeService;
 
@@ -74,20 +81,14 @@ public class TtNotifyController {
      */
     @PostMapping(value = "/ttpay")
     @ApiOperation("头条支付回调")
-    public WxNotifyReturnDTO wxpayNotify(@RequestBody WxpayNotifyDTO wxpayNotifyDTO, HttpServletRequest request) throws GeneralSecurityException, IOException {
-//        String bodyString = getBodyString(request);
-//        if (!validate(request,bodyString)){
-//            throw new CustomException("签名失败");
-//        }
-        String resourceString = getSourString(wxpayNotifyDTO);
-        log.info(resourceString);
-        JSONObject jsonObject = JSONObject.parseObject(resourceString);
+    public WxNotifyReturnDTO wxpayNotify(@RequestBody TtpayNotifyDTO ttpayNotifyDTO) throws GeneralSecurityException, IOException {
+
         //将回调数据写入数据库
-        String outTradeNo = writeNotifyDataToDb(jsonObject);
+        writeNotifyDataToDb(ttpayNotifyDTO);
         //创建兑换码
         String code = createCode();
         //激活用户
-        avtivityCode(outTradeNo,code);
+        avtivityCode(ttpayNotifyDTO.getMsg().getCp_orderno(),code);
 
         WxNotifyReturnDTO wxNotifyReturnDTO = new WxNotifyReturnDTO();
         wxNotifyReturnDTO.setCode("SUCCESS");
@@ -96,11 +97,11 @@ public class TtNotifyController {
     }
 
     private void avtivityCode(String outTradeNo, String vipCode) {
-        WxOrder wxOrder = wxOrderService.getByOutTradeNo(outTradeNo);
-        if (wxOrder ==  null){
-            throw new CustomException("订单不存在");
+        TtOrder order= ttOrderService.getOne(new QueryWrapper<TtOrder>().eq("cp_orderno",outTradeNo));
+        if (order ==  null){
+            throw new CustomException("订单不存在");
         }
-        UserInfo userInfo = userInfoService.getOne(new QueryWrapper<UserInfo>().eq("app_openid", wxOrder.getAppOpenid()));
+        UserInfo userInfo = userInfoService.getOne(new QueryWrapper<UserInfo>().eq("union_id", order.getUnionId()));
         QueryWrapper<VipCode> queryWrapper = new QueryWrapper<VipCode>();
         queryWrapper.eq("vip_code", vipCode);
         VipCode vipCodeEntity = vipCodeService.getOne(queryWrapper);
@@ -136,40 +137,6 @@ public class TtNotifyController {
         return rVipCode;
     }
 
-    private String getBodyString(HttpServletRequest request) {
-            BufferedReader br = null;
-            StringBuilder sb = new StringBuilder("");
-            try
-            {
-                br = request.getReader();
-                String str;
-                while ((str = br.readLine()) != null)
-                {
-                    sb.append(str);
-                }
-                br.close();
-            }
-            catch (IOException e)
-            {
-                e.printStackTrace();
-            }
-            finally
-            {
-                if (null != br)
-                {
-                    try
-                    {
-                        br.close();
-                    }
-                    catch (IOException e)
-                    {
-                        e.printStackTrace();
-                    }
-                }
-            }
-            return sb.toString();
-
-    }
 
     private Boolean validate(HttpServletRequest request, String bodyString) throws  NoSuchAlgorithmException, InvalidKeyException, SignatureException {
         String sign = request.getHeader("Wechatpay-Signature");
@@ -194,73 +161,22 @@ public class TtNotifyController {
     }
 
 
-    /**
-     * 微信退款回调
-     */
-    @PostMapping(value = "/refund")
-    @ApiOperation("微信退款回调")
-    public WxNotifyReturnDTO refundNotify(@RequestBody WxpayNotifyDTO wxpayNotifyDTO, HttpServletRequest request) throws GeneralSecurityException, IOException {
-        String bodyString = getBodyString(request);
-        if (!validate(request,bodyString)){
-            throw new CustomException("签名失败");
-        }
-        String resourceString = getSourString(wxpayNotifyDTO);
-        log.info(resourceString);
-        JSONObject jsonObject = JSONObject.parseObject(resourceString);
-        //将回调数据写入数据库
-        writeRefundNotifyDataToDb(jsonObject);
-
-        WxNotifyReturnDTO wxNotifyReturnDTO = new WxNotifyReturnDTO();
-        wxNotifyReturnDTO.setCode("SUCCESS");
-        wxNotifyReturnDTO.setMessage("成功");
-
-        return wxNotifyReturnDTO;
-    }
-
-    private void writeRefundNotifyDataToDb(JSONObject jsonObject) {
-        String refundId = jsonObject.getString("refund_id");
-        RefundRecord refundRecord = refundRecordService.getByRefundId(refundId);
-        if (refundRecord == null) {
-            throw new CustomException("该退款订单不存在");
-        }
-        refundRecord.setTransactionId(jsonObject.getString("transaction_id"));
-        refundRecord.setUserReceivedAccount(jsonObject.getString("user_received_account"));
-        refundRecord.setStatus(jsonObject.getString("refund_status"));
-        refundRecordService.updateById(refundRecord);
-
 
-    }
 
 
 
 
 
 
-    public String writeNotifyDataToDb(JSONObject jsonObject) {
-        String outTradeNo = jsonObject.getString("out_trade_no");
-        WxOrder wxOrder = wxOrderService.getByOutTradeNo(outTradeNo);
-        if (wxOrder == null) {
+    public void writeNotifyDataToDb(TtpayNotifyDTO ttpayNotifyDTO) {
+        TtpayNotifyDTO.TTpaySource msg = ttpayNotifyDTO.getMsg();
+        TtOrder order= ttOrderService.getOne(new QueryWrapper<TtOrder>().eq("cp_orderno",msg.getCp_orderno()));
+        if (order == null) {
             throw new CustomException("该订单不存在");
         }
-        wxOrder.setTransactionId(jsonObject.getString("transaction_id"));
-        JSONObject amount = jsonObject.getJSONObject("amount");
-        wxOrder.setPayerTotal(amount.getInteger("payer_total"));
-        wxOrder.setTotal(amount.getInteger("total"));
-        wxOrder.setCurrency(amount.getString("currency"));
-        wxOrder.setPayerCurrency(amount.getString("payer_currency"));
-        wxOrder.setTradeState(jsonObject.getString("trade_state"));
-        wxOrder.setBankType(jsonObject.getString("bank_type"));
-        DateTime dateTime  = new DateTime(jsonObject.getString("success_time"));
-        wxOrder.setSuccessTime(dateTime.toDate());
-        wxOrder.setTradeStateDesc(jsonObject.getString("trade_state_desc"));
-        wxOrder.setTradeType(jsonObject.getString("trade_type"));
-        wxOrder.setAttach(jsonObject.getString("attach"));
-        JSONObject sceneInfo = jsonObject.getJSONObject("scene_info");
-        if (sceneInfo != null){
-            wxOrder.setDeviceId(sceneInfo.getString("device_id"));
-        }
-        wxOrderService.updateById(wxOrder);
-        return outTradeNo;
+        BeanUtils.copyProperties(msg,order,"paidAt");
+        order.setPaidAt(new Date(msg.getPaid_at()));
+        ttOrderService.updateById(order);
 
     }
 

+ 66 - 0
sdjk-service/src/main/java/com/miaxis/tt/dto/TtpayNotifyDTO.java

@@ -0,0 +1,66 @@
+package com.miaxis.tt.dto;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+/**
+ * 微信支付回调 dto
+ */
+@Data
+public class TtpayNotifyDTO {
+
+
+    @ApiModelProperty(value = "Unix 时间戳,10 位,整型数")
+    private Long timestamp;
+
+    @ApiModelProperty(value = "随机数")
+    private String nonce;
+
+    @ApiModelProperty(value = "订单信息的 json 字符串")
+    private TTpaySource msg;
+
+
+    @ApiModelProperty(value = "回调类型标记,支付成功回调为\"payment\"")
+    private String type;
+
+
+    @ApiModelProperty(value = "签名")
+    private String msg_signature;
+
+
+    @Data
+    public class TTpaySource{
+        @ApiModelProperty(value = "当前交易发起的小程序id")
+        private String appid;
+        @ApiModelProperty(value = "开发者侧的订单号")
+        private String cp_orderno;
+        @ApiModelProperty(value = "预下单时开发者传入字段")
+        private String cp_extra;
+        @ApiModelProperty(value = "way 字段中标识了支付渠道1-微信支付,2-支付宝支付,10-抖音支付")
+        private String way;
+        @ApiModelProperty(value = "支付渠道侧单号")
+        private String channel_no;
+
+
+        @ApiModelProperty(value = "支付渠道侧商家订单号")
+        private String payment_order_no;
+        @ApiModelProperty(value = "支付金额,单位为分")
+        private Integer total_amount;
+        @ApiModelProperty(value = "固定SUCCESS")
+        private String status;
+        @ApiModelProperty(value = "订单来源视频对应视频 id")
+        private String item_id;
+        @ApiModelProperty(value = "该笔交易卖家商户号")
+        private String seller_uid;
+        @ApiModelProperty(value = "支付时间,Unix 时间戳,10 位,整型数")
+        private Long paid_at;
+        @ApiModelProperty(value = "抖音侧订单号")
+        private String order_id;
+
+
+
+    }
+
+
+
+}