From 0775b09c77bb35a48040061a703257390246ac2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=89=9B=E5=8F=89=E9=97=AA=E9=97=AA?= <18322780655@163.com> Date: Fri, 13 Sep 2024 11:06:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0pcpay=20=E6=94=AF=E4=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/PayController.java | 65 ++++- .../interceptor/WebAppConfigurer.java | 1 + .../cashierservice/service/PayService.java | 262 +++++++++--------- 3 files changed, 184 insertions(+), 144 deletions(-) diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/controller/PayController.java b/src/main/java/com/chaozhanggui/system/cashierservice/controller/PayController.java index 9104fda..1bbec3e 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/controller/PayController.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/controller/PayController.java @@ -1,8 +1,10 @@ package com.chaozhanggui.system.cashierservice.controller; +import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSONObject; import com.alipay.api.AlipayApiException; import com.alipay.api.AlipayClient; +import com.alipay.api.AlipayConfig; import com.alipay.api.DefaultAlipayClient; import com.alipay.api.request.AlipaySystemOauthTokenRequest; import com.alipay.api.request.AlipayUserInfoShareRequest; @@ -17,6 +19,7 @@ import com.chaozhanggui.system.cashierservice.service.PayService; import com.chaozhanggui.system.cashierservice.sign.CodeEnum; import com.chaozhanggui.system.cashierservice.sign.Result; import com.chaozhanggui.system.cashierservice.util.IpUtil; +import com.chaozhanggui.system.cashierservice.util.JSONUtil; import com.chaozhanggui.system.cashierservice.util.RedisCst; import com.chaozhanggui.system.cashierservice.util.WechatUtil; import com.fasterxml.jackson.core.JsonProcessingException; @@ -28,6 +31,7 @@ import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import java.math.BigDecimal; +import java.time.LocalDate; import java.util.List; import java.util.Objects; @@ -304,6 +308,10 @@ public class PayController { } + + + + @RequestMapping("createOrder") public Result createOrder(HttpServletRequest request,@RequestBody PaymentReq paymentReq){ try { @@ -315,8 +323,10 @@ public class PayController { } - - + @GetMapping("queryOrderPay") + public Result queryOrderPay(String orderId){ + return payService.queryOrderPay(orderId); + } @@ -365,15 +375,32 @@ public class PayController { return Result.success(CodeEnum.SUCCESS,openid); }else if("ALIPAY".equals(payType)){ - AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",aliAppId,privateKey,"json","GBK",publicKey,"RSA2"); - AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest(); - AlipayUserInfoShareResponse response = alipayClient.execute(request,code); - log.info("AlipayUserInfoShareResponse:{}",JSONObject.toJSONString(response)); - if(!response.isSuccess()){ + + // 初始化SDK + AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig()); + + // 构造请求参数以调用接口 + AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest(); + + // 设置刷新令牌 +// request.setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b"); + + // 设置授权码 + request.setCode(code); + + // 设置授权方式 + request.setGrantType("authorization_code"); + + AlipaySystemOauthTokenResponse response = alipayClient.execute(request); + + log.info("AlipaySystemOauthTokenResponse:{}", JSONUtil.toJSONString(response)); + + response.setAccessToken(response.getRefreshToken()); + if (!response.isSuccess()) { return Result.fail("获取支付宝userId失败"); } - return Result.success(CodeEnum.SUCCESS,response.getUserId()); + return Result.success(CodeEnum.SUCCESS, ObjectUtil.isNull(response.getUserId())?response.getOpenId():response.getUserId()); } return Result.fail(CodeEnum.FAIL); @@ -381,6 +408,28 @@ public class PayController { + private AlipayConfig getAlipayConfig() { + AlipayConfig alipayConfig = new AlipayConfig(); + alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do"); + alipayConfig.setAppId(aliAppId); + alipayConfig.setPrivateKey(privateKey); + alipayConfig.setFormat("json"); + alipayConfig.setAlipayPublicKey(publicKey); + alipayConfig.setCharset("UTF-8"); + alipayConfig.setSignType("RSA2"); + return alipayConfig; + } + + + public static void main(String[] args){ + LocalDate date=LocalDate.now(); + if(date.isLeapYear()){ + System.out.println(); + } + } + + + diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/interceptor/WebAppConfigurer.java b/src/main/java/com/chaozhanggui/system/cashierservice/interceptor/WebAppConfigurer.java index 2aa9cb5..7a8f40a 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/interceptor/WebAppConfigurer.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/interceptor/WebAppConfigurer.java @@ -31,6 +31,7 @@ public class WebAppConfigurer implements WebMvcConfigurer { .excludePathPatterns("/pay/pcscanpay") .excludePathPatterns("/pay/openId") .excludePathPatterns("/pay/createOrder") + .excludePathPatterns("/pay/queryOrderPay") ; diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/PayService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/PayService.java index 0f03308..4f49752 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/PayService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/PayService.java @@ -40,7 +40,6 @@ import javax.annotation.Resource; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.*; -import java.util.concurrent.atomic.AtomicReference; import static com.chaozhanggui.system.cashierservice.sign.CodeEnum.ACCOUNTEIXST; import static com.chaozhanggui.system.cashierservice.sign.CodeEnum.SUCCESS; @@ -50,43 +49,27 @@ import static com.chaozhanggui.system.cashierservice.sign.CodeEnum.SUCCESS; public class PayService { + private final RedisTemplate redisTemplate; @Autowired TbOrderInfoMapper tbOrderInfoMapper; - @Autowired TbMerchantThirdApplyMapper tbMerchantThirdApplyMapper; - @Autowired TbOrderPaymentMapper tbOrderPaymentMapper; - @Autowired TbShopPayTypeMapper tbShopPayTypeMapper; - @Autowired TbCashierCartMapper tbCashierCartMapper; - @Autowired TbShopUserMapper tbShopUserMapper; - - @Autowired TbShopUserFlowMapper tbShopUserFlowMapper; - @Autowired TbOrderDetailMapper tbOrderDetailMapper; - @Autowired RestTemplate restTemplate; - - @Autowired RabbitProducer producer; - @Resource - private TbGroupOrderInfoMapper tbGroupOrderInfoMapper; - @Resource - private TbGroupOrderCouponMapper couponMapper; - - @Autowired TbUserInfoMapper tbUserInfoMapper; @Autowired @@ -95,22 +78,24 @@ public class PayService { TbShopInfoMapper tbShopInfoMapper; @Autowired TbQuickPayMapper tbQuickPayMapper; - + @Autowired + TbmerchantAccountMapper tbmerchantAccountMapper; + @Autowired + TbPlussShopStaffMapper tbPlussShopStaffMapper; + @Resource + private TbGroupOrderInfoMapper tbGroupOrderInfoMapper; + @Resource + private TbGroupOrderCouponMapper couponMapper; @Autowired private TbProductSkuMapper productSkuMapper; - @Autowired private TbProductMapper productMapper; - @Autowired private TbShopUnitMapper shopUnitMapper; - @Autowired private TbProductStockDetailMapper productStockDetailMapper; - @Autowired private RedisUtils redisUtil; - @Value("${gateway.url}") private String gateWayUrl; @Value("${client.backUrl}") @@ -121,14 +106,6 @@ public class PayService { private String url; @Value("${thirdPay.callBack}") private String callBack; - - - @Autowired - TbmerchantAccountMapper tbmerchantAccountMapper; - - - @Autowired - TbPlussShopStaffMapper tbPlussShopStaffMapper; @Autowired private MpShopUserMapper mpShopUserMapper; @Autowired @@ -136,12 +113,13 @@ public class PayService { @Autowired private MpShopTableMapper mpShopTableMapper; - private final RedisTemplate redisTemplate; - public PayService(RedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; } + public static void main(String[] args) { + System.out.println(MD5Util.encrypt("123456".concat("13718478323").concat("10"))); + } public Result queryPayType(String shopId) { return Result.success(CodeEnum.SUCCESS, tbShopPayTypeMapper.selectByShopId(shopId)); @@ -305,7 +283,7 @@ public class PayService { log.info("更新购物车:{}", cartCount); //更新子单状态 - tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed",orderInfo.getDiscountRatio()); + tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed", orderInfo.getDiscountRatio()); JSONObject jsonObject = new JSONObject(); jsonObject.put("token", token); @@ -387,10 +365,10 @@ public class PayService { int cartCount = tbCashierCartMapper.updateByOrderId(orderId, "final"); log.info("更新购物车:{}", cartCount); - if(ObjectUtil.isNotNull(orderInfo.getDiscountRatio())&&ObjectUtil.isNotEmpty(orderInfo.getDiscountRatio())){ - tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed",orderInfo.getDiscountRatio()); - }else { - tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed",null); + if (ObjectUtil.isNotNull(orderInfo.getDiscountRatio()) && ObjectUtil.isNotEmpty(orderInfo.getDiscountRatio())) { + tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed", orderInfo.getDiscountRatio()); + } else { + tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed", null); } JSONObject jsonObject = new JSONObject(); @@ -509,10 +487,10 @@ public class PayService { int cartCount = tbCashierCartMapper.updateByOrderId(orderId, "final"); log.info("更新购物车:{}", cartCount); - if(ObjectUtil.isNotNull(orderInfo.getDiscountRatio())&&ObjectUtil.isNotEmpty(orderInfo.getDiscountRatio())){ - tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed",orderInfo.getDiscountRatio()); - }else { - tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed",null); + if (ObjectUtil.isNotNull(orderInfo.getDiscountRatio()) && ObjectUtil.isNotEmpty(orderInfo.getDiscountRatio())) { + tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed", orderInfo.getDiscountRatio()); + } else { + tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed", null); } JSONObject jsonObject = new JSONObject(); jsonObject.put("token", token); @@ -595,7 +573,7 @@ public class PayService { if (ObjectUtil.isNotEmpty(memberAccount)) { - if (!memberAccount.substring(0, 2).equals("46")) { + if (!memberAccount.startsWith("46")) { return Result.fail(CodeEnum.ERRORQR); } @@ -647,10 +625,10 @@ public class PayService { //更新购物车状态 int cartCount = tbCashierCartMapper.updateByOrderId(orderId, "final"); - if(ObjectUtil.isNotNull(orderInfo.getDiscountRatio())&&ObjectUtil.isNotEmpty(orderInfo.getDiscountRatio())){ - tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed",orderInfo.getDiscountRatio()); - }else { - tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed",null); + if (ObjectUtil.isNotNull(orderInfo.getDiscountRatio()) && ObjectUtil.isNotEmpty(orderInfo.getDiscountRatio())) { + tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed", orderInfo.getDiscountRatio()); + } else { + tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed", null); } log.info("更新购物车:{}", cartCount); @@ -671,14 +649,13 @@ public class PayService { producer.sendStockSaleMsg(mqData); - - JSONObject baObj=new JSONObject(); - baObj.put("userId",user.getUserId()); - baObj.put("shopId",user.getShopId()); - baObj.put("amount",orderInfo.getPayAmount()); - baObj.put("balance",user.getAmount()); - baObj.put("type","消费"); - baObj.put("time",flow.getCreateTime()); + JSONObject baObj = new JSONObject(); + baObj.put("userId", user.getUserId()); + baObj.put("shopId", user.getShopId()); + baObj.put("amount", orderInfo.getPayAmount()); + baObj.put("balance", user.getAmount()); + baObj.put("type", "消费"); + baObj.put("time", flow.getCreateTime()); producer.balance(baObj.toString()); String tableCartKey = RedisCst.getCurrentOrderKey(orderInfo.getTableId(), @@ -738,7 +715,7 @@ public class PayService { } - TbShopUser user = tbShopUserMapper.selectByShopId(orderInfo.getShopId(), tbUserInfo.getId().toString(),"2").get(0); + TbShopUser user = tbShopUserMapper.selectByShopId(orderInfo.getShopId(), tbUserInfo.getId().toString(), "2").get(0); if (ObjectUtil.isEmpty(user) || !"1".equals(user.getIsVip().toString())) { return Result.fail(CodeEnum.MEMBERNOEXIST); } @@ -778,11 +755,10 @@ public class PayService { int cartCount = tbCashierCartMapper.updateByOrderId(orderId, "final"); - - if(ObjectUtil.isNotNull(orderInfo.getDiscountRatio())&&ObjectUtil.isNotEmpty(orderInfo.getDiscountRatio())){ - tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed",orderInfo.getDiscountRatio()); - }else { - tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed",null); + if (ObjectUtil.isNotNull(orderInfo.getDiscountRatio()) && ObjectUtil.isNotEmpty(orderInfo.getDiscountRatio())) { + tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed", orderInfo.getDiscountRatio()); + } else { + tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed", null); } @@ -804,14 +780,13 @@ public class PayService { producer.sendStockSaleMsg(mqData); - - JSONObject baObj=new JSONObject(); - baObj.put("userId",user.getUserId()); - baObj.put("shopId",user.getShopId()); - baObj.put("amount",orderInfo.getPayAmount()); - baObj.put("balance",user.getAmount()); - baObj.put("type","消费"); - baObj.put("time",flow.getCreateTime()); + JSONObject baObj = new JSONObject(); + baObj.put("userId", user.getUserId()); + baObj.put("shopId", user.getShopId()); + baObj.put("amount", orderInfo.getPayAmount()); + baObj.put("balance", user.getAmount()); + baObj.put("type", "消费"); + baObj.put("time", flow.getCreateTime()); producer.balance(baObj.toString()); clearTableInfoCache(orderInfo); @@ -823,7 +798,7 @@ public class PayService { return Result.fail(CodeEnum.PARAM); } - TbOrderInfo orderInfo = tbOrderInfoMapper.selectByPrimaryKey(Integer.valueOf(orderId)); + TbOrderInfo orderInfo = tbOrderInfoMapper.selectByPrimaryKey(orderId); if (ObjectUtil.isEmpty(orderInfo)) { return Result.fail(CodeEnum.ORDERNOEXIST); @@ -859,8 +834,6 @@ public class PayService { } - - TbShopUserFlow userFlow = new TbShopUserFlow(); userFlow.setAmount((payAmount != null && discountAmount != null) ? payAmount : orderInfo.getOrderAmount()); userFlow.setBalance(shopUser.getAmount().subtract((payAmount != null && discountAmount != null) ? payAmount : orderInfo.getOrderAmount())); @@ -890,10 +863,10 @@ public class PayService { int cartCount = tbCashierCartMapper.updateByOrderId(String.valueOf(orderId), "final"); - if(ObjectUtil.isNotNull(orderInfo.getDiscountRatio())&&ObjectUtil.isNotEmpty(orderInfo.getDiscountRatio())){ - tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed",orderInfo.getDiscountRatio()); - }else { - tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed",null); + if (ObjectUtil.isNotNull(orderInfo.getDiscountRatio()) && ObjectUtil.isNotEmpty(orderInfo.getDiscountRatio())) { + tbOrderDetailMapper.updateStatusByOrderId(orderId, "closed", orderInfo.getDiscountRatio()); + } else { + tbOrderDetailMapper.updateStatusByOrderId(orderId, "closed", null); } log.info("更新购物车:{}", cartCount); @@ -980,10 +953,10 @@ public class PayService { int cartCount = tbCashierCartMapper.updateByOrderId(orderId, "final"); - if(ObjectUtil.isNotNull(orderInfo.getDiscountRatio())&&ObjectUtil.isNotEmpty(orderInfo.getDiscountRatio())){ - tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed",orderInfo.getDiscountRatio()); - }else { - tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed",null); + if (ObjectUtil.isNotNull(orderInfo.getDiscountRatio()) && ObjectUtil.isNotEmpty(orderInfo.getDiscountRatio())) { + tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed", orderInfo.getDiscountRatio()); + } else { + tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed", null); } log.info("更新购物车:{}", cartCount); @@ -1074,10 +1047,10 @@ public class PayService { int cartCount = tbCashierCartMapper.updateByOrderId(orderId, "final"); log.info("更新购物车:{}", cartCount); - if(ObjectUtil.isNotNull(orderInfo.getDiscountRatio())&&ObjectUtil.isNotEmpty(orderInfo.getDiscountRatio())){ - tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed",orderInfo.getDiscountRatio()); - }else { - tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed",null); + if (ObjectUtil.isNotNull(orderInfo.getDiscountRatio()) && ObjectUtil.isNotEmpty(orderInfo.getDiscountRatio())) { + tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed", orderInfo.getDiscountRatio()); + } else { + tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed", null); } JSONObject jsonObject = new JSONObject(); @@ -1216,7 +1189,7 @@ public class PayService { detailPo.setStatus("closed"); } - BigDecimal returnAmount = it.getPriceAmount().divide(new BigDecimal(it.getNum()), 2, RoundingMode.DOWN).multiply(new BigDecimal(map1.get(it.getId())));; + BigDecimal returnAmount = it.getPriceAmount().divide(new BigDecimal(it.getNum()), 2, RoundingMode.DOWN).multiply(new BigDecimal(map1.get(it.getId()))); detailPo.setReturnNum(map1.get(it.getId())); detailPos.add(detailPo); @@ -1266,7 +1239,7 @@ public class PayService { } } else { - TbOrderPayment payment = tbOrderPaymentMapper.selectByOrderId(orderId + ""); + TbOrderPayment payment = tbOrderPaymentMapper.selectByOrderId(String.valueOf(orderId)); PublicResp publicResp = thirdPayService.returnOrder(url, thirdApply.getAppId(), newOrderInfo.getOrderNo(), payment.getTradeNumber(), null, "订单退款", newOrderInfo.getPayAmount().setScale(2, RoundingMode.DOWN).multiply(new BigDecimal(100)).longValue(), callBack, null, thirdApply.getAppToken()); if (ObjectUtil.isNotNull(publicResp) && ObjectUtil.isNotEmpty(publicResp)) { @@ -1318,13 +1291,13 @@ public class PayService { tbShopUserFlowMapper.insert(flow); - JSONObject baObj=new JSONObject(); - baObj.put("userId",user.getUserId()); - baObj.put("shopId",user.getShopId()); - baObj.put("amount",newOrderInfo.getPayAmount()); - baObj.put("balance",user.getAmount()); - baObj.put("type","退款"); - baObj.put("time",flow.getCreateTime()); + JSONObject baObj = new JSONObject(); + baObj.put("userId", user.getUserId()); + baObj.put("shopId", user.getShopId()); + baObj.put("amount", newOrderInfo.getPayAmount()); + baObj.put("balance", user.getAmount()); + baObj.put("type", "退款"); + baObj.put("time", flow.getCreateTime()); producer.balance(baObj.toString()); } @@ -1365,13 +1338,9 @@ public class PayService { producer.putOrderCollect(jsonObject.toJSONString()); - producer.printMechine(newOrderInfo.getId().toString()); - - - //修改耗材数据 JSONObject jsonObject1 = new JSONObject(); jsonObject1.put("orderId", newOrderInfo.getId()); @@ -1692,7 +1661,6 @@ public class PayService { return Result.success(CodeEnum.SUCCESS); } - public Result getOrderDiscount(String staffId, String orderId, String token) { if (ObjectUtil.isNull(staffId) || ObjectUtil.isNull(orderId) || ObjectUtil.isEmpty(staffId) || ObjectUtil.isEmpty(orderId)) { return Result.fail(CodeEnum.PARAM); @@ -1719,7 +1687,7 @@ public class PayService { } if ("master".equals(staff.getType())) { - return Result.success(SUCCESS, new BigDecimal(0.1)); + return Result.success(SUCCESS, new BigDecimal("0.1")); } if (ObjectUtil.isEmpty(staff.getMaxDiscountAmount()) || ObjectUtil.isNull(staff.getMaxDiscountAmount())) { @@ -1729,7 +1697,7 @@ public class PayService { if ("0".equals(staff.getDiscountType())) { if (staff.getMaxDiscountAmount().compareTo(tbOrderInfo.getOrderAmount()) >= 0) { - staff.setMaxDiscountAmount(new BigDecimal(0.1)); + staff.setMaxDiscountAmount(new BigDecimal("0.1")); } else { staff.setMaxDiscountAmount(BigDecimal.ONE.subtract(staff.getMaxDiscountAmount().divide(tbOrderInfo.getOrderAmount(), 2, RoundingMode.HALF_DOWN).setScale(2, RoundingMode.HALF_DOWN))); } @@ -1739,9 +1707,8 @@ public class PayService { return Result.success(SUCCESS, staff.getMaxDiscountAmount()); } - - public Result pcscanpay(String orderId,String ip,String userId,String payType) throws JsonProcessingException { - if (ObjectUtil.isEmpty(orderId) || ObjectUtil.isEmpty(ip)) { + public Result pcscanpay(String orderId, String ip, String userId, String payType) throws JsonProcessingException { + if (ObjectUtil.isEmpty(orderId) || ObjectUtil.isEmpty(ip)) { return Result.fail(CodeEnum.PARAM); } @@ -1757,7 +1724,6 @@ public class PayService { } - List cashierCarts = tbCashierCartMapper.selectByOrderId(orderId, null); if (ObjectUtil.isEmpty(cashierCarts) || ObjectUtil.isNull(cashierCarts)) { return Result.fail(CodeEnum.CARTEXIST); @@ -1778,7 +1744,6 @@ public class PayService { } - TbOrderPayment payment = tbOrderPaymentMapper.selectByOrderId(orderId); if (ObjectUtil.isEmpty(payment) || payment == null) { payment = new TbOrderPayment(); @@ -1814,10 +1779,10 @@ public class PayService { reqbody = body.toString(); } - PublicResp publicResp = thirdPayService.jspay(url,thirdApply.getAppId(),thirdApply.getAppToken(),reqbody,reqbody,orderInfo.getOrderAmount().multiply(new BigDecimal(100)).longValue(),payType,"WECHAT".equals(payType)?thirdApply.getSmallAppid():null,userId,ip,DateUtils.getSsdfTimes(),thirdApply.getStoreId(),backUrl,backUrl); + PublicResp publicResp = thirdPayService.jspay(url, thirdApply.getAppId(), thirdApply.getAppToken(), reqbody, reqbody, orderInfo.getOrderAmount().multiply(new BigDecimal(100)).longValue(), payType, "WECHAT".equals(payType) ? thirdApply.getSmallAppid() : null, userId, ip, DateUtils.getSsdfTimes(), thirdApply.getStoreId(), backUrl, backUrl); - if (ObjectUtil.isNotNull(publicResp) && ObjectUtil.isNotEmpty(publicResp)) { + if (ObjectUtil.isNotNull(publicResp) && ObjectUtil.isNotEmpty(publicResp)) { if ("000000".equals(publicResp.getCode())) { JspayResp scanpayResp = publicResp.getObjData(); if ("TRADE_SUCCESS".equals(scanpayResp.getState())) { @@ -1835,10 +1800,10 @@ public class PayService { int cartCount = tbCashierCartMapper.updateByOrderId(orderId, "final"); log.info("更新购物车:{}", cartCount); - if(ObjectUtil.isNotNull(orderInfo.getDiscountRatio())&&ObjectUtil.isNotEmpty(orderInfo.getDiscountRatio())){ - tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed",orderInfo.getDiscountRatio()); - }else { - tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed",null); + if (ObjectUtil.isNotNull(orderInfo.getDiscountRatio()) && ObjectUtil.isNotEmpty(orderInfo.getDiscountRatio())) { + tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed", orderInfo.getDiscountRatio()); + } else { + tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed", null); } // JSONObject jsonObject = new JSONObject(); @@ -1854,7 +1819,7 @@ public class PayService { ObjectMapper mapper = new ObjectMapper(); return Result.success(CodeEnum.SUCCESS, mapper.readTree(scanpayResp.getPayInfo())); - }else if("TRADE_AWAIT".equals(scanpayResp.getState())){ + } else if ("TRADE_AWAIT".equals(scanpayResp.getState())) { orderInfo.setStatus("paying"); orderInfo.setPayOrderNo(payment.getTradeNumber()); tbOrderInfoMapper.updateByPrimaryKeySelective(orderInfo); @@ -1864,11 +1829,11 @@ public class PayService { tbOrderPaymentMapper.updateByPrimaryKeySelective(payment); - if("WECHAT".equals(scanpayResp.getPayType())){ + if ("WECHAT".equals(scanpayResp.getPayType())) { orderInfo.setPayType("WECHAT"); - }else if("ALIPAY".equals(scanpayResp.getPayType())){ + } else if ("ALIPAY".equals(scanpayResp.getPayType())) { orderInfo.setPayType("ALIPAY"); - }else if("UNIONPAY".equals(scanpayResp.getPayType())){ + } else if ("UNIONPAY".equals(scanpayResp.getPayType())) { orderInfo.setPayType("UNIONPAY"); } @@ -1877,7 +1842,7 @@ public class PayService { ObjectMapper mapper = new ObjectMapper(); - return Result.success(CodeEnum.PAYING,mapper.readTree(scanpayResp.getPayInfo())); + return Result.success(CodeEnum.PAYING, mapper.readTree(scanpayResp.getPayInfo())); } } } @@ -1886,7 +1851,6 @@ public class PayService { } - public String generateOrderNumber() { String date = DateUtils.getSdfTimes(); Random random = new Random(); @@ -1894,28 +1858,23 @@ public class PayService { return "QR" + date + randomNum; } + public Result createOrder(String ip, String userId, String payType, String shopId, BigDecimal amount) throws JsonProcessingException { - public Result createOrder(String ip,String userId,String payType,String shopId,BigDecimal amount) throws JsonProcessingException { + if (ObjectUtil.isNull(userId) || ObjectUtil.isEmpty(userId) || ObjectUtil.isEmpty(payType) || ObjectUtil.isNull(payType) - if(ObjectUtil.isNull(userId)||ObjectUtil.isEmpty(userId)||ObjectUtil.isEmpty(payType)||ObjectUtil.isNull(payType) - - ||ObjectUtil.isNull(shopId)||ObjectUtil.isEmpty(shopId)||ObjectUtil.isNull(shopId)||ObjectUtil.isNull(amount)||ObjectUtil.isEmpty(amount) - ){ + || ObjectUtil.isNull(shopId) || ObjectUtil.isEmpty(shopId) || ObjectUtil.isNull(shopId) || ObjectUtil.isNull(amount) || ObjectUtil.isEmpty(amount) + ) { return Result.fail(CodeEnum.PARAM); } - if(!"WECHAT".equals(payType)||!"ALIPAY".equals(payType)){ - return Result.fail(CodeEnum.PARAM); - } - TbShopInfo shopInfo= tbShopInfoMapper.selectByPrimaryKey(Integer.valueOf(shopId)); + TbShopInfo shopInfo = tbShopInfoMapper.selectByPrimaryKey(Integer.valueOf(shopId)); - if(ObjectUtil.isNull(shopInfo)){ + if (ObjectUtil.isNull(shopInfo)) { return Result.fail(CodeEnum.SHOPINFONOEXIST); } - - TbOrderInfo orderInfo=new TbOrderInfo(); + TbOrderInfo orderInfo = new TbOrderInfo(); String orderNo = generateOrderNumber(); orderInfo.setOrderNo(orderNo); @@ -1963,8 +1922,6 @@ public class PayService { } - - TbMerchantThirdApply thirdApply = tbMerchantThirdApplyMapper.selectByPrimaryKey(Integer.valueOf(orderInfo.getMerchantId())); if (ObjectUtil.isEmpty(thirdApply) || ObjectUtil.isNull(thirdApply)) { return Result.fail(CodeEnum.NOCUSTOMER); @@ -1973,7 +1930,7 @@ public class PayService { String reqbody = "店铺收款码"; - PublicResp publicResp = thirdPayService.jspay(url,thirdApply.getAppId(),thirdApply.getAppToken(),reqbody,reqbody,orderInfo.getOrderAmount().multiply(new BigDecimal(100)).longValue(),payType,"WECHAT".equals(payType)?thirdApply.getSmallAppid():null,userId,ip,DateUtils.getSsdfTimes(),thirdApply.getStoreId(),backUrl,backUrl); + PublicResp publicResp = thirdPayService.jspay(url, thirdApply.getAppId(), thirdApply.getAppToken(), reqbody, reqbody, orderInfo.getOrderAmount().multiply(new BigDecimal(100)).longValue(), payType, "WECHAT".equals(payType) ? thirdApply.getSmallAppid() : null, userId, ip, DateUtils.getSsdfTimes(), thirdApply.getStoreId(), backUrl, backUrl); if (ObjectUtil.isNotNull(publicResp) && ObjectUtil.isNotEmpty(publicResp)) { @@ -2014,22 +1971,55 @@ public class PayService { ObjectMapper mapper = new ObjectMapper(); - return Result.success(CodeEnum.PAYING, mapper.readTree(scanpayResp.getPayInfo())); + + Map map=new HashMap<>(); + + map.put("orderInfo",orderInfo); + map.put("payInfo",mapper.readTree(scanpayResp.getPayInfo())); + return Result.success(CodeEnum.PAYING, map); } } } return Result.fail(CodeEnum.FAIL); } + public Result queryOrderPay(String orderId) { + if (ObjectUtil.isEmpty(orderId)) { + return Result.fail(CodeEnum.PARAM); + } + + TbOrderInfo orderInfo = tbOrderInfoMapper.selectByPrimaryKey(Integer.valueOf(orderId)); + + if (ObjectUtil.isEmpty(orderInfo)) { + return Result.fail(CodeEnum.ORDERNOEXIST); + } + if ("unpaid".equals(orderInfo.getStatus()) || "paying".equals(orderInfo.getStatus())) { + TbMerchantThirdApply thirdApply = tbMerchantThirdApplyMapper.selectByPrimaryKey(Integer.valueOf(orderInfo.getMerchantId())); + if (ObjectUtil.isEmpty(thirdApply) || ObjectUtil.isNull(thirdApply)) { + return Result.fail(CodeEnum.NOCUSTOMER); + } + TbOrderPayment tbOrderPayment = tbOrderPaymentMapper.selectByOrderId(orderId); + if (ObjectUtil.isNotEmpty(tbOrderPayment)) { + PublicResp orderstatus = thirdPayService.queryOrder(url, thirdApply.getAppId(), tbOrderPayment.getTradeNumber(), null, thirdApply.getAppToken()); + if (ObjectUtil.isNotNull(orderstatus) && ObjectUtil.isNotEmpty(orderstatus)) { + if ("000000".equals(orderstatus.getCode())) { + if ("TRADE_SUCCESS".equals(orderstatus.getObjData().getState())) { + orderInfo.setStatus("closed"); + orderInfo.setPayOrderNo(tbOrderPayment.getTradeNumber()); + tbOrderInfoMapper.updateByPrimaryKeySelective(orderInfo); + } + } + } + } + } + return Result.success(CodeEnum.SUCCESS, orderInfo); - public static void main(String[] args) { - System.out.println(MD5Util.encrypt("123456".concat("13718478323").concat("10"))); } }