Browse Source

Merge remote-tracking branch 'origin/master'

小么熊🐻 3 years ago
parent
commit
ecb3a9cad2

+ 2 - 2
zzjs-admin/src/main/java/com/miaxis/app/controller/film/FilmController.java

@@ -129,7 +129,7 @@ public class FilmController extends BaseController {
         order.setOutTradeNo(orderCode);
         order.setOpenid(student.getOpenid());
         String goodsName = dictDataService.selectDictLabel("goods_type", filmOrderCreateDTO.getGoodsType());
-        order.setGoodsType(goodsName);
+        order.setGoodsType(filmOrderCreateDTO.getGoodsType());
         order.setGoodsPictureUrl(filmOrderCreateDTO.getGoodsPictureUrl());
         FilmOrderJsonData filmOrderJsonData = new FilmOrderJsonData();
         filmOrderCreateDTO.setThirdOrderId(orderCode);
@@ -147,7 +147,7 @@ public class FilmController extends BaseController {
         ObjectNode rootNode = objectMapper.createObjectNode();
         rootNode.put("mchid",wxpayConfig.getMerchantId())
                 .put("appid", appid)
-                .put("description", filmOrderCreateDTO.getGoodsType())
+                .put("description", goodsName)
                 .put("notify_url", notifyUrl)
                 .put("out_trade_no", orderCode);
         rootNode.putObject("amount")

+ 108 - 27
zzjs-admin/src/main/java/com/miaxis/app/controller/film/NotifyController.java

@@ -5,37 +5,33 @@ import com.alibaba.fastjson.JSONObject;
 import com.miaxis.common.config.WxpayConfig;
 import com.miaxis.common.constant.Constants;
 import com.miaxis.common.core.domain.Response;
-import com.miaxis.common.core.domain.entity.UserInfo;
 import com.miaxis.common.exception.CustomException;
 import com.miaxis.common.utils.AesUtil;
-import com.miaxis.common.utils.SecurityUtils;
 import com.miaxis.feign.dto.FilmDTO;
 import com.miaxis.film.domain.FilmOrder;
+import com.miaxis.film.domain.RefundRecord;
 import com.miaxis.film.dto.*;
 import com.miaxis.film.service.IFilmOrderService;
 import com.miaxis.film.service.IRefundRecordService;
 import io.swagger.annotations.Api;
-import io.swagger.annotations.ApiModelProperty;
 import io.swagger.annotations.ApiOperation;
-import io.swagger.annotations.ApiParam;
 import lombok.Data;
 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.beans.factory.annotation.Value;
 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 import org.springframework.transaction.annotation.Transactional;
-import org.springframework.web.bind.annotation.*;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
 
 import javax.servlet.http.HttpServletRequest;
+import java.io.BufferedReader;
 import java.io.IOException;
 import java.security.GeneralSecurityException;
-import java.sql.Date;
-import java.time.ZoneId;
-import java.time.ZonedDateTime;
-import java.util.Locale;
 
 import static com.miaxis.common.utils.OrderCodeFactory.getOrderCode;
 
@@ -69,16 +65,80 @@ public class NotifyController {
      */
     @PostMapping(value = "/wxpay")
     @ApiOperation("微信支付回调")
-    public WxNotifyReturnDTO wxpayNotify(@RequestBody FilmWxpayDTO filmWxpayDTO, HttpServletRequest request) throws GeneralSecurityException, IOException {
-        AesUtil aesUtil = new AesUtil(wxpayConfig.getV3key().getBytes());
-        FilmWxpayDTO.WxpaySource wxpaySource = filmWxpayDTO.getResource();
-        String resourceString = aesUtil.decryptToString(wxpaySource.getAssociated_data().getBytes(), wxpaySource.getNonce().getBytes(), wxpaySource.getCiphertext());
+    public WxNotifyReturnDTO wxpayNotify(@RequestBody FilmWxpayDTO filmWxpayDTO, HttpServletRequest request, BufferedReader br) throws GeneralSecurityException, IOException {
+        String bodyString = getBodyString(br);
+        System.out.println(bodyString);
+        Boolean pass =  validate(request);
+
+        String resourceString = getSourString(filmWxpayDTO);
         log.info(resourceString);
         JSONObject jsonObject = JSONObject.parseObject(resourceString);
         //将回调数据写入数据库
         writeNotifyDataToDb(jsonObject);
 
+        WxNotifyReturnDTO wxNotifyReturnDTO = new WxNotifyReturnDTO();
+        wxNotifyReturnDTO.setCode("SUCCESS");
+        wxNotifyReturnDTO.setMessage("成功");
+        return wxNotifyReturnDTO;
+    }
+
+    private String getBodyString(BufferedReader br) {
+        String inputLine;
+
+        String str = "";
+
+        try {
+            while ((inputLine = br.readLine()) != null) {
+                str += inputLine;
+
+            }
+
+            br.close();
+
+        } catch (IOException e) {
+            System.out.println("IOException: " + e);
+
+        }
+        return str;
+    }
+
+    private Boolean validate(HttpServletRequest request) {
+        String sign = request.getHeader("Wechatpay-Signature");
+        String timestamp = request.getHeader("Wechatpay-Timestamp");
+        String nonce = request.getHeader("Wechatpay-Nonce");
+
+
+        StringBuffer sb = new StringBuffer();
+        sb.append(timestamp + "\n");
+        sb.append(nonce + "\n");
+        System.out.println(sb);
+        return true;
+
+//        File file = new ClassPathResource("wechatpay/apiclient_key.pem").getFile();
+//        String realPath =file.getAbsolutePath();
+//        PrivateKey privateKey = CommonUtils.getPrivateKey(realPath);
+//        // 进行签名服务
+//        Signature signature = Signature.getInstance("SHA256withRSA");
+//        signature.initSign(privateKey);
+//        signature.update(sb.toString().getBytes("UTF-8"));
+//        byte[] signedData = signature.sign();
+//        String base64Str =  Base64.getEncoder().encodeToString(signedData);
+
+
+    }
+
 
+    /**
+     * 微信退款回调
+     */
+    @PostMapping(value = "/refund")
+    @ApiOperation("微信退款回调")
+    public WxNotifyReturnDTO refundNotify(@RequestBody FilmWxpayDTO filmWxpayDTO, HttpServletRequest request) throws GeneralSecurityException, IOException {
+        String resourceString = getSourString(filmWxpayDTO);
+        log.info(resourceString);
+        JSONObject jsonObject = JSONObject.parseObject(resourceString);
+        //将回调数据写入数据库
+        writeRefundNotifyDataToDb(jsonObject);
 
         WxNotifyReturnDTO wxNotifyReturnDTO = new WxNotifyReturnDTO();
         wxNotifyReturnDTO.setCode("SUCCESS");
@@ -86,6 +146,21 @@ public class NotifyController {
 
         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);
+
+
+    }
+
     /**
      * 电影回调接口
      */
@@ -114,27 +189,26 @@ public class NotifyController {
         }
         filmOrderJsonData.setFilmOrderNotifyDTO(dbFilmOrderNotifyDTO);
         filmOrder.setOrderDataJson(JSONObject.toJSONString(filmOrderJsonData));
-        UserInfo student = SecurityUtils.getLoginUser().getStudent();
-        Long id = student.getId();
-        String refundCode = getOrderCode(id);
+        //UserInfo student = SecurityUtils.getLoginUser().getStudent();
         //如果回调回来的订单关闭,则调用退款
         if ("ORDER_CLOSE".equals(filmOrderNotifyDTO.getEventName())){
-        threadPoolTaskExecutor.execute(new Runnable() {
+            String refundCode = getOrderCode(null);
+            filmOrder.setOutRefundNo(refundCode);
+            threadPoolTaskExecutor.execute(new Runnable() {
             @Override
             public void run() {
-                try {
-                    refundRecordService.refund(filmOrder,refundCode);
-                } catch (Exception e) {
-                    log.info(e.getMessage());
+                    try {
+
+                        refundRecordService.refund(filmOrder,refundCode);
+                    } catch (Exception e) {
+                        log.info(e.getMessage());
+                    }
                 }
-            }
-        });
+            });
 
         }
-        filmOrder.setOutRefundNo(refundCode);
-        filmOrderService.updateById(filmOrder);
-
 
+        filmOrderService.updateById(filmOrder);
 
         FilmNotifyReturnDTO filmNotifyReturnDTO = new FilmNotifyReturnDTO();
         filmNotifyReturnDTO.setCode("200");
@@ -241,6 +315,13 @@ public class NotifyController {
         filmOrderService.updateById(filmOrder);
     }
 
+    private String getSourString(FilmWxpayDTO filmWxpayDTO) throws GeneralSecurityException, IOException {
+        AesUtil aesUtil = new AesUtil(wxpayConfig.getV3key().getBytes());
+        FilmWxpayDTO.WxpaySource wxpaySource = filmWxpayDTO.getResource();
+        return aesUtil.decryptToString(wxpaySource.getAssociated_data().getBytes(), wxpaySource.getNonce().getBytes(), wxpaySource.getCiphertext());
+    }
+
+
 
 
     @Data

+ 1 - 0
zzjs-admin/src/main/resources/application-dev.yml

@@ -110,6 +110,7 @@ wxpay:
     serialNumber: 487159E168001CDBA0EFE2C988249A84023AD6CC
     v3key: qqwweerrttyyuuiioopp123456789000
     notifyUrl: http://218.85.55.253:65535/zzjs-admin/open-api/film/notify/wxpay
+    notifyUrlRefund: http://218.85.55.253:65535/zzjs-admin/open-api/film/notify/refund
 
 
 # 电影

+ 1 - 0
zzjs-admin/src/main/resources/application-prod.yml

@@ -110,6 +110,7 @@ wxpay:
     serialNumber: 487159E168001CDBA0EFE2C988249A84023AD6CC
     v3key: qqwweerrttyyuuiioopp123456789000
     notifyUrl: http://admin.zzxcx.net/prod-api/open-api/film/notify/wxpay
+    notifyUrlRefund: http://admin.zzxcx.net/prod-api/open-api/film/notify/refund
 
 
 # 电影

+ 4 - 0
zzjs-common/src/main/java/com/miaxis/common/config/WxpayConfig.java

@@ -10,7 +10,11 @@ import org.springframework.stereotype.Component;
 public class WxpayConfig {
 
     private String v3url;
+    //退款
     private String v3urlRefund;
+    //退款回调
+    private String notifyUrlRefund;
+    //商户id
     private String merchantId;
     private String serialNumber;
     private String v3key;

+ 8 - 7
zzjs-service/src/main/java/com/miaxis/film/domain/RefundRecord.java

@@ -2,6 +2,7 @@ package com.miaxis.film.domain;
 
 import java.util.Date;
 import com.fasterxml.jackson.annotation.JsonFormat;
+import io.swagger.models.auth.In;
 import org.apache.commons.lang3.builder.ToStringBuilder;
 import org.apache.commons.lang3.builder.ToStringStyle;
 import com.miaxis.common.annotation.Excel;
@@ -96,43 +97,43 @@ OTHER_BANKCARD:原银行卡异常退到其他银行卡
     @Excel(name = "订单总金额,单位为分。 示例值:100")
     @TableField("total")
     @ApiModelProperty(value = "订单总金额,单位为分。 示例值:100")
-    private Long total;
+    private Integer total;
 
     /** 退款标价金额,单位为分 */
     @Excel(name = "退款标价金额,单位为分")
     @TableField("refund")
     @ApiModelProperty(value = "退款标价金额,单位为分")
-    private Long refund;
+    private Integer refund;
 
     /** 现金支付金额 */
     @Excel(name = "现金支付金额")
     @TableField("payer_total")
     @ApiModelProperty(value = "现金支付金额")
-    private Long payerTotal;
+    private Integer payerTotal;
 
     /** 退款给用户的金额,不包含所有优惠券金额 */
     @Excel(name = "退款给用户的金额,不包含所有优惠券金额")
     @TableField("payer_refund")
     @ApiModelProperty(value = "退款给用户的金额,不包含所有优惠券金额")
-    private Long payerRefund;
+    private Integer payerRefund;
 
     /** 去掉非充值代金券退款金额后的退款金额 */
     @Excel(name = "去掉非充值代金券退款金额后的退款金额")
     @TableField("settlement_refund")
     @ApiModelProperty(value = "去掉非充值代金券退款金额后的退款金额")
-    private Long settlementRefund;
+    private Integer settlementRefund;
 
     /** 应结订单金额=订单金额-免充值代金券金额 */
     @Excel(name = "应结订单金额=订单金额-免充值代金券金额")
     @TableField("settlement_total")
     @ApiModelProperty(value = "应结订单金额=订单金额-免充值代金券金额")
-    private Long settlementTotal;
+    private Integer settlementTotal;
 
     /** 优惠退款金额<=退款金额,退款金额-代金券或立减优惠退款金额为现金,说明详见 */
     @Excel(name = "优惠退款金额<=退款金额,退款金额-代金券或立减优惠退款金额为现金,说明详见")
     @TableField("discount_refund")
     @ApiModelProperty(value = "优惠退款金额<=退款金额,退款金额-代金券或立减优惠退款金额为现金,说明详见")
-    private Long discountRefund;
+    private Integer discountRefund;
 
     /** 符合ISO 4217标准的三位字母代码,目前只支持人民币:CNY。 */
     @Excel(name = "符合ISO 4217标准的三位字母代码,目前只支持人民币:CNY。")

+ 1 - 0
zzjs-service/src/main/java/com/miaxis/film/mapper/RefundRecordMapper.java

@@ -19,4 +19,5 @@ public interface RefundRecordMapper extends BaseMapper<RefundRecord> {
      */
     public List<RefundRecord> selectRefundRecordList(RefundRecord refundRecord);
 
+    RefundRecord getByRefundId(String refundId);
 }

+ 2 - 0
zzjs-service/src/main/java/com/miaxis/film/service/IRefundRecordService.java

@@ -23,4 +23,6 @@ public interface IRefundRecordService extends IService<RefundRecord>{
     public List<RefundRecord> selectRefundRecordList(RefundRecord refundRecord);
 
     WxNotifyReturnDTO refund(FilmOrder filmOrder, String refundCode) throws Exception;
+
+    RefundRecord getByRefundId(String refundId);
 }

+ 34 - 2
zzjs-service/src/main/java/com/miaxis/film/service/impl/RefundRecordServiceImpl.java

@@ -1,5 +1,6 @@
 package com.miaxis.film.service.impl;
 
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ObjectNode;
@@ -16,6 +17,7 @@ import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.util.EntityUtils;
+import org.joda.time.DateTime;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -61,7 +63,8 @@ public class RefundRecordServiceImpl extends ServiceImpl<RefundRecordMapper, Ref
 
         rootNode.put("out_trade_no", filmOrder.getOutTradeNo())
                 .put("out_refund_no", refundCode)
-                .put("reason", "电影购票失败");
+                .put("reason", "电影购票失败")
+                .put("notify_url",wxpayConfig.getNotifyUrlRefund());
         rootNode.putObject("amount")
                 .put("refund", filmOrder.getTotal())
                 .put("total", filmOrder.getTotal())
@@ -74,7 +77,31 @@ public class RefundRecordServiceImpl extends ServiceImpl<RefundRecordMapper, Ref
         // 返回数据:
         String bodyAsString = EntityUtils.toString(response.getEntity());
         if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
-            //to-do  保存数据到数据库
+            //  保存退款数据到数据库
+            RefundRecord refundRecord = new RefundRecord();
+            JSONObject jsonObject = JSONObject.parseObject(bodyAsString);
+            refundRecord.setOutRefundNo(jsonObject.getString("refund_id"));
+            refundRecord.setTransactionId(jsonObject.getString("out_refund_no"));
+            refundRecord.setOutTradeNo(jsonObject.getString("out_trade_no"));
+            refundRecord.setChannel(jsonObject.getString("channel"));
+            refundRecord.setUserReceivedAccount(jsonObject.getString("user_received_account"));
+            if (jsonObject.getString("success_time")!=null){
+                refundRecord.setSuccessTime(new DateTime(jsonObject.getString("success_time")).toDate());
+            }
+            refundRecord.setCreateTime(new DateTime(jsonObject.getString("create_time")).toDate());
+            refundRecord.setStatus(jsonObject.getString("status"));
+            refundRecord.setFundsAccount(jsonObject.getString("funds_account"));
+            JSONObject amount = jsonObject.getJSONObject("amount");
+            refundRecord.setTotal(amount.getInteger("total"));
+            refundRecord.setRefund(amount.getInteger("refund"));
+            refundRecord.setPayerTotal(amount.getInteger("payer_total"));
+            refundRecord.setPayerRefund(amount.getInteger("payer_refund"));
+            refundRecord.setSettlementRefund(amount.getInteger("settlement_refund"));
+            refundRecord.setSettlementTotal(amount.getInteger("settlement_total"));
+            refundRecord.setDiscountRefund(amount.getInteger("discount_refund"));
+            refundRecord.setCurrency(amount.getString("currency"));
+            this.save(refundRecord);
+
             WxNotifyReturnDTO wxNotifyReturnDTO = new WxNotifyReturnDTO();
             wxNotifyReturnDTO.setCode("SUCCESS");
             wxNotifyReturnDTO.setMessage("成功");
@@ -84,4 +111,9 @@ public class RefundRecordServiceImpl extends ServiceImpl<RefundRecordMapper, Ref
         }
 
     }
+
+    @Override
+    public RefundRecord getByRefundId(String refundId) {
+        return refundRecordMapper.getByRefundId(refundId);
+    }
 }

+ 8 - 0
zzjs-service/src/main/java/com/miaxis/job/test1.java

@@ -0,0 +1,8 @@
+package com.miaxis.job;
+
+
+
+public class test1 {
+}
+
+

+ 4 - 0
zzjs-service/src/main/resources/mapper/film/RefundRecordMapper.xml

@@ -54,4 +54,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         </where>
     </select>
 
+    <select id="getByRefundId" resultType="com.miaxis.film.domain.RefundRecord">
+        select * from refund_record where refund_id =#{refundId}
+    </select>
+
 </mapper>