diff --git a/src/main/java/com/example/demo/serviceImpl/cash/BankServiceImpl.java b/src/main/java/com/example/demo/serviceImpl/cash/BankServiceImpl.java index 1e79880..1e9ec1e 100644 --- a/src/main/java/com/example/demo/serviceImpl/cash/BankServiceImpl.java +++ b/src/main/java/com/example/demo/serviceImpl/cash/BankServiceImpl.java @@ -254,7 +254,119 @@ public class BankServiceImpl implements BankService { //stripe银行接口(批量) @Override public Result stripeAuto(BankDTO bankDTO) { - return Result.success(bankDTO); + try { + // 设置Stripe API密钥 + Stripe.apiKey = "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"; + // 方式一:通过订单号查找最近数据 + String orderNo = bankDTO.getOrderNo(); + + if (orderNo == null || orderNo.isEmpty()) { + return Result.error("订单号为空"); + } + + // 从Stripe获取最近的收费记录(最多200条) + List allCharges = new ArrayList<>(); + String startingAfter = null; + int totalLimit = 200; // 目标获取条数 + int pageSize = 100; // 单次最大获取条数 + + do { + // 计算当前页需查询的条数(最后一页可能不足100条) + int currentPageSize = Math.min(pageSize, totalLimit - allCharges.size()); + if (currentPageSize <= 0) { + break; // 已获取足够条数,停止 + } + + // 构建分页参数 + ChargeListParams params = ChargeListParams.builder() + .setLimit((long) currentPageSize) + .setStartingAfter(startingAfter) + .build(); + + try { + // 执行分页查询 + ChargeCollection charges = Charge.list(params); + List currentPageData = charges.getData(); + allCharges.addAll(currentPageData); + + // 更新分页游标:若有下一页且未达200条,继续 + boolean hasMore = charges.getHasMore() && allCharges.size() < totalLimit; + startingAfter = hasMore ? currentPageData.get(currentPageData.size() - 1).getId() : null; + + } catch (StripeException e) { + log.error("Stripe 分页查询失败:" + e.getMessage()); + break; // 异常时停止查询,返回已获取的数据 + } + + } while (startingAfter != null); + + // 在获取的所有记录中查找匹配订单号的记录 + Charge matchedCharge = null; + for (Charge charge : allCharges) { + // 从metadata中获取订单号进行匹配 + if (charge.getMetadata() != null) { + String chargeOrderNo = charge.getMetadata().get("order_no"); + if (chargeOrderNo != null && orderNo.equals(chargeOrderNo)) { + matchedCharge = charge; + break; + } + } + } + + // 如果未找到匹配的订单,返回错误信息 + if (matchedCharge == null) { + return Result.error("未找到订单号 " + orderNo + " 的支付记录"); + } + + // 获取匹配到的charge对应的余额交易ID + String balanceTransactionId = matchedCharge.getBalanceTransaction(); + + // 通过余额交易ID获取详细信息 + BalanceTransaction balanceTransaction = BalanceTransaction.retrieve(balanceTransactionId); + + // 创建StripeDTO对象并填充所需数据点 + StripeDTO stripeDTO = new StripeDTO(); + // 设置订单号 + stripeDTO.setOrderNo(matchedCharge.getMetadata().get("order_no")); + // 设置余额交易ID + stripeDTO.setBalanceTransaction(matchedCharge.getBalanceTransaction()); + + // 设置付款币种和金额(来自charge) + stripeDTO.setCurrency(matchedCharge.getCurrency().toUpperCase()); + stripeDTO.setAmount(String.valueOf(matchedCharge.getAmount())); + + // 设置收款币种(来自charge) + stripeDTO.setChargeCurrency(matchedCharge.getCurrency().toUpperCase()); + + // 设置到账金额和手续费(来自balanceTransaction) + stripeDTO.setNet(String.valueOf(balanceTransaction.getNet())); + stripeDTO.setFee(String.valueOf(balanceTransaction.getFee())); + + // 设置到账币种(来自balanceTransaction) + stripeDTO.setCurrency(balanceTransaction.getCurrency().toUpperCase()); + + // 设置available_on日期 + if (balanceTransaction.getAvailableOn() != null) { + long availableOnInSeconds = balanceTransaction.getAvailableOn(); + // 将Unix时间戳转换为Date对象 + Date availableOnDate = new Date(availableOnInSeconds * 1000L); + stripeDTO.setAvailableOn(availableOnDate); + } + + // 创建响应VO对象 + BankVO bankVO = new BankVO(); + bankVO.setStripeDTO(stripeDTO); + CashCollection cashCollection = cashCollectionMapper.selectByGoldCoinOrderCode(orderNo); + if (cashCollection == null) { + return Result.error("金币系统当前日期 " + " 该订单号 " + orderNo + " 未查到"); + } else { + cashCollectionMapper.updateByGoldCoinOrderCodeByStripe(bankVO.getStripeDTO()); + } + return Result.success(bankVO); + } catch (Exception e) { + log.error("stripe银行接口处理失败", e); + throw new RuntimeException("处理失败: " + e.getMessage()); + } } //stripe银行接口(单个)