package com.stylefeng.guns.modular.system.controller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.amazonaws.services.dynamodbv2.xspec.B; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.plugins.Page; import com.stripe.exception.StripeException; import com.stripe.model.PaymentIntent; import com.stylefeng.guns.modular.system.model.*; import com.stylefeng.guns.modular.system.service.*; import com.stylefeng.guns.modular.system.service.impl.TQuoteServiceImpl; import com.stylefeng.guns.modular.system.utils.PaymentProcessor; import com.stylefeng.guns.modular.system.utils.Server; import com.stylefeng.guns.modular.system.utils.StripePayUtils; import com.stylefeng.guns.modular.system.utils.tips.ErrorTip; import com.stylefeng.guns.modular.system.utils.tips.SuccessTip; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.apache.poi.ss.formula.functions.T; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; @Controller @Api(tags = "用户端-财务信息") @RequestMapping("/api/tFinance") public class FinanceController { @Autowired private ITOrderService orderService; @Autowired private ITGoodsService goodsService; @Autowired private ITUserService itUserService; @Autowired private ITQuoteService tQuoteService; @Autowired private TUserBankService userBankService; @Autowired private ITPriceService priceService; @ApiOperation(value = "用户端-invocie",notes="用户端-invocie") @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."), @ApiImplicitParam(name = "pageNumber", value = "pageNumber", required = true, dataType = "int",paramType = "query"), @ApiImplicitParam(name = "pageSize", value = "pageSize", required = true, dataType = "int",paramType = "query"), @ApiImplicitParam(name = "time", value = "时间 (时间 - 时间)", required = false, dataType = "String"), @ApiImplicitParam(name = "id", value = "订单id", required = false, dataType = "Long"), @ApiImplicitParam(name = "state", value = "状态 1支付完 2未支付完", required = false, dataType = "int"), @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "int"), }) @PostMapping(value = "/list") @ResponseBody public Object list(int pageNumber,int pageSize,String time,Long id,Integer state,int userId) { Page receivableVoPage = new Page<>(pageNumber, pageSize); List receivable = orderService.getReceivable(receivableVoPage, time, id, state, userId); receivableVoPage.setRecords(receivable); return new SuccessTip(receivableVoPage); } @ApiOperation(value = "用户端-payImmediately",notes="用户端-payImmediately") @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."), @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "int"), }) @PostMapping(value = "/payImmediately") @ResponseBody public Object payImmediately(int userId) { // 找出所有未支付完成的订单 List orders = orderService.getNoPayOrder(userId); BigDecimal all = new BigDecimal(0); ArrayList goods = new ArrayList<>(); for (TOrder order : orders) { BigDecimal allTotal = priceService.selectList(new EntityWrapper().eq("order_id", order.getId())).stream().map(TPrice::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal paid = order.getPaid(); BigDecimal subtract = allTotal.subtract(paid); all = all.add(subtract); TGoods tGoods = goodsService.selectOne(new EntityWrapper().eq("order_id", order.getId())); goods.add(tGoods); } HashMap map = new HashMap<>(); map.put("money",all); map.put("data",goods); return new SuccessTip(map); } @ApiOperation(value = "应收款票据详情",notes="应收款票据详情") @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."), @ApiImplicitParam(name = "number", value = "订单id", required = true, dataType = "Long"), }) @GetMapping(value = "/listOfReceivablesInfo") @ResponseBody public Object listOfReceivablesInfo(Long number) { return new SuccessTip(orderService.listOfReceivablesInfo(number)); } @ApiOperation(value = "支付调用",notes="支付调用") @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."), @ApiImplicitParam(name = "id", value = "userid", required = true, dataType = "int"), @ApiImplicitParam(name = "quoteId", value = "需求id", required = false, dataType = "Long"), @ApiImplicitParam(name = "cardId", value = "cardId", required = false, dataType = "Integer"), @ApiImplicitParam(name = "type", value = "type 1额度支付 2全款支付", required = true, dataType = "int"), @ApiImplicitParam(name = "category", value = " 1需求支付 2所有支付", required = true, dataType = "int"), }) @GetMapping(value = "/payInfo") @ResponseBody @Transactional(propagation = Propagation.REQUIRES_NEW) public Object payInfo(int id, Long quoteId, Integer cardId, int type, int category) throws StripeException { TUser tUser = itUserService.selectById(id); try { if (category == 1) { TQuote quote = tQuoteService.selectById(quoteId); // 找出状态为0的订单 List orders = orderService.selectList(new EntityWrapper().eq("e_zip_z", quote.getOrderId()).eq("status", 0)); // 总金额 BigDecimal bigDecimal = new BigDecimal(0); for (TOrder order : orders) { List prices = priceService.selectList(new EntityWrapper().eq("order_id", order.getId())); bigDecimal = bigDecimal.add(prices.stream().map(TPrice::getPrice).reduce(BigDecimal.ZERO,BigDecimal::add)); } if (type == 1) { // 可用额度 BigDecimal bigDecimal1 = null; String residueLimit = tUser.getResidueLimit(); if (residueLimit == null) { bigDecimal1 = new BigDecimal(residueLimit); } else { bigDecimal1 = new BigDecimal(residueLimit); } // 剩余额度 BigDecimal subtract = bigDecimal1.subtract(bigDecimal); tUser.setResidueLimit(subtract.toString()); orders.forEach(e -> { e.setStatus("2"); e.setPayTime(new Date()); }); orderService.updateBatchById(orders); itUserService.updateById(tUser); quote.setState(4); tQuoteService.updateById(quote); } else { if (tUser.getUserNumber() == null) { return new ErrorTip(5006, "Please bind a card first"); } // 找出这张卡 TUserBank tUserBank = userBankService.selectById(cardId); String cardToken = tUserBank.getCardToken(); // 修改默认支付卡 StripePayUtils.updateStripeDefaultCard(tUser.getUserNumber(), cardToken); // String method = PaymentProcessor.getMethod(tUser.getUserNumber()); // JSONObject jsonObject = JSON.parseObject(method); // Object id1 = jsonObject.get("id"); Boolean pay = StripePayUtils.charge(tUser.getUserNumber(), bigDecimal, "","Continental Cargo Association, Inc."); if (pay) { orders.forEach(e -> { e.setStatus("2"); e.setPaid(e.getAllTotal()); e.setPayTime(new Date()); }); orderService.updateBatchById(orders); quote.setState(4); tQuoteService.updateById(quote); } } } else { if (tUser.getUserNumber() == null) { return new ErrorTip(5006, "Please bind a card first"); } // 找出所有未支付完成的订单 List orders = orderService.getNoPayOrder(id); HashSet tQuotes = new HashSet<>(); BigDecimal all = new BigDecimal(0); for (TOrder order : orders) { String s = order.geteZipZ(); TQuote quote = tQuoteService.selectOne(new EntityWrapper().eq("order_id", s)); quote.setState(4); tQuotes.add(quote); // BigDecimal allTotal = order.getAllTotal(); List prices = priceService.selectList(new EntityWrapper().eq("order_id", order.getId())); BigDecimal allTotal = prices.stream().map(TPrice::getPrice).reduce(BigDecimal.ZERO,BigDecimal::add); BigDecimal paid = order.getPaid(); BigDecimal subtract = allTotal.subtract(paid); all = all.add(subtract); } // 找出这张卡 TUserBank tUserBank = userBankService.selectById(cardId); String cardToken = tUserBank.getCardToken(); // 修改默认支付卡 StripePayUtils.updateStripeDefaultCard(tUser.getUserNumber(), cardToken); String method = PaymentProcessor.getMethod(tUser.getUserNumber()); JSONObject jsonObject = JSON.parseObject(method); Object id1 = jsonObject.get("id"); long l = all.multiply(new BigDecimal(100)).longValue(); Boolean pay = StripePayUtils.charge(tUser.getUserNumber(), all, "","Continental Cargo Association, Inc."); if (pay) { orders.forEach(e -> { e.setStatus("2"); e.setPaid(e.getAllTotal()); e.setPayTime(new Date()); }); orderService.updateBatchById(orders); List collect = tQuotes.stream().collect(Collectors.toList()); tQuoteService.updateBatchById(collect); }else { return new ErrorTip(5010, "payment failure"); } } return new SuccessTip(); } catch (Exception e) { e.printStackTrace(); return new ErrorTip(500, "ERROR"); } } }