package com.szwl.controller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.google.gson.Gson; import com.szwl.feign.bean.SzwlFeign; import com.szwl.model.bo.JsonMessage; import com.szwl.model.bo.R; import com.szwl.model.bo.ResponseModel; import com.szwl.model.entity.*; import com.szwl.constant.JoinpayConstant; import com.szwl.model.utils.PushUtils; import com.szwl.service.TEquipmentService; import com.szwl.service.TOrderService; import com.szwl.service.TWeixinPayConfigService; import com.szwl.utils.IDGenerator; import com.szwl.weixin.refund.model.CreateRequest; import com.szwl.weixin.util.HttpUtils; import com.wechat.pay.java.service.refund.model.AmountReq; import com.wechat.pay.java.service.refund.model.Refund; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import static com.wechat.pay.java.service.refund.model.Status.PROCESSING; /** *

* 前端控制器 *

* * @author wuhs * @since 2023-04-08 */ @RestController @RequestMapping("/tWeixinPay") public class TWeixinPayController { @Autowired SzwlFeign szwlFeign; @Autowired TOrderService orderService; @Autowired TWeixinPayConfigService weixinPayConfigService; @Autowired TEquipmentService equipmentService; @ApiOperation(value = "购物车 请求在线支付 ") @PostMapping("/carsPay") public Object carsPay(String clientId, @RequestBody Map productNameMap) { if(productNameMap==null){ return JsonMessage.error("数据出错"); } StringBuffer productName1 = new StringBuffer(); StringBuffer note = new StringBuffer(); StringBuffer productName = new StringBuffer(); TEquipment equipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(clientId)); if(equipment==null||equipment.getId()==null){ return JsonMessage.error("找不到设备"); } BigDecimal price = new BigDecimal("0.00"); // JSONObject mapList = JSON.parseObject(productNameMap); TEquipmentDesc equipmentDesc = R.getDataIfSuccess(szwlFeign.findEquipmentById(equipment.getId())); if(equipmentDesc==null||equipmentDesc.getEquipmentId()==null||equipmentDesc.getPayType()==null||equipmentDesc.getPayType().equals("0")){ //1,type=0,原始设定,没有第二件半价 for (String key : productNameMap.keySet()) { String entryValue = String.valueOf(productNameMap.get(key)); List value = JSON.parseArray(entryValue, String.class); List promoCodeList = new ArrayList<>(); //1,校验优惠码 if(value.size()>0){ for(String code:value){ if(!code.equals("0")){ TPromoCode promoCode = new TPromoCode(); ResponseModel tPromoCodeResponseModel = szwlFeign.selectTPromoCode(code, equipment.getAdminId().toString()); TPromoCode tPromoCode = tPromoCodeResponseModel.getData(); // TPromoCode tPromoCode = R.getDataIfSuccess(szwlFeign.selectTPromoCode(code, equipment.getAdminId().toString())); promoCode = tPromoCode; if (tPromoCode == null||tPromoCode.getId()==null) { //不存在 return JsonMessage.success("1"); } Date lastUseDate = null; if (promoCode != null) { lastUseDate = promoCode.getLastUseDate(); } if (lastUseDate != null && lastUseDate.getTime() < ((new Date()).getTime())) { promoCode.setIsUse("2"); szwlFeign.updatePromoCode(String.valueOf(promoCode.getId()),"2"); //过期 return JsonMessage.success("4"); } if (promoCode.getId() == null) { //不存在 return JsonMessage.success("1"); } if (promoCode.getIsUse().equals("1")) { //被使用 return JsonMessage.success("2"); } if(!promoCode.getAdminId().equals("1")){ if (String.valueOf(equipment.getAdminId()).equals(promoCode.getAdminId())) { } else { //不是本机 return JsonMessage.success("3"); } } if (promoCode.getDiscount() == null) { //旧优惠码 return JsonMessage.success("0"); } if (promoCode.getDiscount() != null && promoCode.getDiscount() == 0) { //0折 return JsonMessage.success("0"); } promoCodeList.add(promoCode); } } } String[] productNum = key.split("-"); String productNamea = productNum[0]; String num = productNum[1]; productName.append(productNamea).append(num); note.append(productNamea).append(num).append(":"); productName1.append(productNamea).append(num); //确定价格,然后叠加R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipmentId), productName)); TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipment.getId()), productNamea)); if (product == null) { return JsonMessage.error("找不到商品"); } BigDecimal productPrice = product.getRmbPrice(); if(promoCodeList.size()>0){ //有优惠码 BigDecimal onePrice = new BigDecimal("0.00"); int i; for( i = 0;i= 0) { return JsonMessage.error("商品价格异常"); } String sn1 = orderService.initSn(equipmentId); //获取分销 关联设备 TAdmin admin = R.getDataIfSuccess(szwlFeign.getAdmin(String.valueOf(equipment.getAdminId()))); if (admin == null) { return JsonMessage.error("找不到设备商家"); } JSONArray altInfo = new JSONArray(); String orderNo1 = sn1; BigDecimal amount = price.setScale(2, RoundingMode.HALF_DOWN); amount = amount.multiply(new BigDecimal(100)); String notifyUrl = JoinpayConstant.Notify_Url; String frpCode1 = "WEIXIN_NATIVE"; TOrder order = new TOrder(); order.setId(IDGenerator.orderID()); order.setCreateDate(new Date()); order.setModifyDate(new Date()); order.setAdminId(admin.getId()); order.setSn(sn1); order.setProductName(productName.toString()); order.setPrice(price); order.setClientId(equipment.getClientId()); order.setEquipmentId(equipmentId); order.setFrpCode(frpCode1); order.setAltInfo(altInfo.toString()); order.setStatus(0); String payPlatform = "3"; order.setPayPlatform(payPlatform); if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){ order.setIsSettlement("0"); } order.setNote(note.toString()); orderService.save(order); Iterator iterator = altInfo.iterator(); while (iterator.hasNext()) { JSONObject jsonObject = (JSONObject) iterator.next(); String altAmount = jsonObject.getString("altAmount"); if (altAmount.equals("0.00")) { iterator.remove(); } } String result = null; try { result = orderService.weixinPay( orderNo1, amount, productName.toString(), admin.getId(), notifyUrl ); } catch (Exception e) { e.printStackTrace(); return JsonMessage.error("申请支付失败"); } // 微信支付支付申请返回支付二维码图片 JSONObject kindData = new JSONObject(); kindData.put("sn", sn1); kindData.put("code_url", result); return JsonMessage.success(kindData.toString()); } /** * 成功回调 微信 * * @return */ @RequestMapping(value = "/notify", method = RequestMethod.POST) @ResponseBody public Object notify(HttpServletRequest request, HttpServletResponse response) { Gson gson = new Gson(); //应答对象 Map map = new HashMap<>(); String body = HttpUtils.readData(request); Map bodyMap = gson.fromJson(body, HashMap.class); //处理支付回调成功的订单 String plainText = weixinPayConfigService.decryptFromResource(bodyMap); if(StringUtils.isNotEmpty(plainText)){ JSONObject resultJson = JSONObject.parseObject(plainText); String sn = resultJson.getString("out_trade_no"); if(StringUtils.isNotEmpty(sn)){ LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TOrder::getSn,sn); query.eq(TOrder::getStatus,0); TOrder order = orderService.getOne(query); order.setStatus(1); String success_time = resultJson.getString("success_time"); Date date = new Date(); try { date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(success_time); } catch (ParseException e) { //LOGGER.error("时间转换错误, string = {}", s, e); } order.setPayDate(date); order.setTrxNo(resultJson.getString("transaction_id")); orderService.updateById(order); TEquipment byClientId = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId())); JSONObject kindData = new JSONObject(); kindData.put("sn", order.getSn()); kindData.put("productName", order.getProductName()); equipmentService.sentMessage(byClientId.getClientId(), PushUtils.buildJson("pay_success", kindData.toString()).toString()); } } return "success"; } @ApiOperation(value = "退款") @GetMapping("/refund") ResponseModel refund(@RequestParam String sn,@RequestParam BigDecimal refusePrice){ if(StringUtils.isEmpty(sn)){ return R.fail("订单为空/error"); } LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TOrder::getSn,sn); query.eq(TOrder::getStatus,1); TOrder order = orderService.getOne(query); if(order==null){ return R.fail("订单为空/error"); } CreateRequest createRequest = new CreateRequest(); createRequest.setOutTradeNo(order.getSn()); AmountReq amount = new AmountReq(); amount.setRefund(refusePrice.multiply(new BigDecimal(100)).longValue()); if(order.getRefundAmount()!=null){ BigDecimal refusep = order.getPrice().add(order.getRefundAmount()).multiply(new BigDecimal(100)); amount.setTotal(refusep.longValue()); }else { BigDecimal multiply = order.getPrice().multiply(new BigDecimal(100)); amount.setTotal(multiply.longValue()); } amount.setCurrency("CNY"); createRequest.setAmount(amount); String refundNo = orderService.initSn(order.getEquipmentId()); createRequest.setOutRefundNo(refundNo); createRequest.setNotifyUrl("https://app.sunzee.com.cn/PAY-SERVER/tWeixinPay/refundNotify"); // createRequest.setNotifyUrl("http://mianhuatang.gz2vip.91tunnel.com/tWeixinPay/refundNotify"); // createRequest.setNotifyUrl("http://soobin.5gzvip.91tunnel.com/tWeixinPay/refundNotify"); Refund refund = orderService.weixinRefundApi(createRequest,order.getAdminId()); System.out.println("退款参数"+refund.toString()); if(refund.getStatus()==PROCESSING){ return R.ok("退款申请中,请30秒后再查询"); }else { return R.ok("退款失败,请重试"); } }; @ApiOperation(value = "退款成功回调") @PostMapping("/refundNotify") ResponseModel refundNotify(HttpServletRequest request, HttpServletResponse response){ String result = orderService.weixinRefund(request,response); return R.ok(result); }; }