forked from kidgrow-microservices-platform

houruijun
2020-09-28 e0395c5454534e7cf657b10045d00e4042dc7e71
kidgrow-business/kidgrow-opration-center/kidgrow-opration-center-server/src/main/java/com/kidgrow/oprationcenter/controller/alipay/AlipayController.java
@@ -13,8 +13,9 @@
import com.kidgrow.common.utils.DateUtils;
import com.kidgrow.common.utils.QRCodeUtil;
import com.kidgrow.common.utils.StringUtils;
import com.kidgrow.oprationcenter.config.alipay.AlipayProperties;
import com.kidgrow.oprationcenter.alipay.AlipayProperties;
import com.kidgrow.oprationcenter.model.SaasClientPay;
import com.kidgrow.oprationcenter.service.AlipayService;
import com.kidgrow.oprationcenter.service.ISaasClientPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@@ -42,12 +43,14 @@
    private AlipayProperties alipayProperties;
    @Autowired
    private ISaasClientPayService saasClientPayService;
    @Autowired
    private AlipayService alipayService;
    @ApiOperation(value = "调用预支付的接口,生成二维码")
    @PostMapping("/precreate")
    public ResultBody precreate(HttpServletResponse response) throws Exception{  //商户预创建支付订单,生成二维码
        String outTradeNo = UUID.randomUUID().toString().replaceAll("_", "");
        String outTradeNo = UUID.randomUUID().toString().replaceAll("-", "");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("dia_id","1");
        queryWrapper.eq("pay_status",0);
@@ -72,6 +75,8 @@
        java.security.Security.addProvider(
                new org.bouncycastle.jce.provider.BouncyCastleProvider()
        );
        request.setNotifyUrl(alipayProperties.getNotifyUrl());
        request.setReturnUrl(alipayProperties.getReturnUrl());
        AlipayTradePrecreateResponse alipayTradePrecreateResponse=alipayClient.execute(request);
        if (StringUtils.isBlank(alipayTradePrecreateResponse.getQrCode())) {
            return ResultBody.failed().data("生成二维码失败");
@@ -81,23 +86,20 @@
        System.out.println(base64);
        return ResultBody.ok().data(base64);
    }
    //数据封装
    public SaasClientPay fengData(AlipayTradePrecreateModel model){
        SaasClientPay saasClientPay = new SaasClientPay();
        saasClientPay.setCreateHospitalDepartment("");
        saasClientPay.setCreateHospitalDepartid("");
        saasClientPay.setCreateHospitalId(Long.valueOf(1L));
        saasClientPay.setPayPrice(1);
        saasClientPay.setOutTradeNo(model.getOutTradeNo());
        saasClientPay.setPayStatus(0);
        saasClientPay.setPayMethod(1);
        saasClientPay.setDiaId("1");
        saasClientPay.setId( UUID.randomUUID().toString().replaceAll("_", ""));
        saasClientPay.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        saasClientPay.setCreateTime(new Date());
        saasClientPay.setCreateUserId(Long.valueOf(1L));
        saasClientPay.setCreateUserName("");
        return saasClientPay;
    }
    @ApiOperation(value = "取消订单,支付超时、支付结果未知是可撤销,超过24小时不可撤销-------暂时没有配置")
    @PostMapping("/cancel")
    public ResultBody cancel() throws Exception{  //取消订单,支付超时、支付结果未知是可撤销,超过24小时不可撤销
@@ -113,19 +115,28 @@
    @ApiOperation(value = "trade_success状态下异步通知接口,端口异常的接口")
    @PostMapping("/notify")
    public ResultBody notify(HttpServletRequest request) throws Exception{   //trade_success状态下异步通知接口
    public String notify(HttpServletRequest request) throws Exception {   //trade_success状态下异步通知接口
        if (check(request.getParameterMap())){
            System.out.println(request.getParameter("trade_status"));
            Map<String, String> requestMap = this.getRequestMap(request.getParameterMap());
            log.error("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
            log.error(request.toString());
            log.error(requestMap.toString());
            log.error("eeeeeeeeeeeeeeeewwwwwwwwwwww:");
            if (requestMap.get("trade_status").equals("TRADE_SUCCESS")) {
                log.error("wwwwwwwwwww");
                //更新状态 out_trade_no
            log.error("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
            System.out.println("异步通知 "+ Instant.now());
                return alipayService.notify(requestMap);
            }else {
                return "fail";
            }
        }else {
            System.out.println("验签失败");
            return "fail";
        }
        return ResultBody.ok().data("订单失败");
    }
    @ApiOperation(value = "订单支付成功后同步返回地址")
    @PostMapping("/return")
    public ResultBody returnUrl(HttpServletRequest request, Map<String,Object> map) throws Exception{  //订单支付成功后同步返回地址
@@ -159,6 +170,7 @@
            return ResultBody.failed().data("false");
        }
    }
 private Map<String,String> getRequestMap(Map<String,String[]> requestParams){
     Map<String,String> params = new HashMap<>();
     for (String name : requestParams.keySet()) {
@@ -173,6 +185,7 @@
     }
     return params;
 }
    private boolean check(Map<String,String[]> requestParams) throws Exception{  //对return、notify参数进行验签
        Map<String, String> requestMap = this.getRequestMap(requestParams);
        return AlipaySignature.rsaCheckV1(requestMap, alipayProperties.getAlipayPublicKey(),