diff --git a/src/main/java/com/example/demo/controller/cash/BankController.java b/src/main/java/com/example/demo/controller/cash/BankController.java index cc9f138..01109c7 100644 --- a/src/main/java/com/example/demo/controller/cash/BankController.java +++ b/src/main/java/com/example/demo/controller/cash/BankController.java @@ -46,7 +46,7 @@ public class BankController { @PostMapping("/getPayment") public Result getPayment(@RequestBody BankDTO bankDTO) { try { - if(bankDTO.getTime().isEmpty() || bankDTO.getOrderNo().isEmpty()){ + if (bankDTO.getTime().isEmpty() || bankDTO.getOrderNo().isEmpty()) { return Result.error("时间或订单号为空"); } return bankService.getPayment(bankDTO); @@ -71,7 +71,7 @@ public class BankController { @PostMapping("/getStripe") public Result getStripe(@RequestBody BankDTO bankDTO) { try { - if(bankDTO.getTime().isEmpty() || bankDTO.getOrderNo().isEmpty()){ + if (bankDTO.getTime().isEmpty() || bankDTO.getOrderNo().isEmpty()) { return Result.error("时间或订单号为空"); } return bankService.getStripe(bankDTO); diff --git a/src/main/java/com/example/demo/controller/cash/SignatureGenerator.java b/src/main/java/com/example/demo/controller/cash/SignatureGenerator.java new file mode 100644 index 0000000..2895c90 --- /dev/null +++ b/src/main/java/com/example/demo/controller/cash/SignatureGenerator.java @@ -0,0 +1,59 @@ +package com.example.demo.controller.cash; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import java.nio.charset.StandardCharsets; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.util.Base64; +import java.util.Random; + +public class SignatureGenerator { + public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException { + // 配置参数 + String key = "3E04ZUCKFmQKrW0uoBa89QKIJWYoU9OX"; + String secret = "ZLtBPgfMIT4HXg25SoVuCyUQZ6GtSv9UFmDmYaoVSKS"; + + // 生成ClientRequestId(1-10000000的随机数) + long clientRequestId = new Random().nextInt(10000000) + 1; + + // 获取当前时间戳(毫秒) + long time = System.currentTimeMillis(); + + // 构造原始签名数据 + String rawSignature = key + clientRequestId + time; + System.out.println("rawdata: " + rawSignature); + + // 计算HMAC-SHA256 + Mac sha256Hmac = Mac.getInstance("HmacSHA256"); + SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"); + sha256Hmac.init(secretKey); + byte[] hmacBytes = sha256Hmac.doFinal(rawSignature.getBytes(StandardCharsets.UTF_8)); + + // 转换为十六进制字符串(对应CryptoJS的toString()) + StringBuilder hmacHex = new StringBuilder(); + for (byte b : hmacBytes) { + hmacHex.append(String.format("%02x", b)); + } + System.out.println("addingHMAC: " + hmacHex.toString()); + + // Base64编码 + String computedHmac = Base64.getEncoder().encodeToString(hmacBytes); + System.out.println("addingBase64: " + computedHmac); + + // Base64解码(转回字节数组) + byte[] decodedBytes = Base64.getDecoder().decode(computedHmac); + StringBuilder decodedHex = new StringBuilder(); + for (byte b : decodedBytes) { + decodedHex.append(String.format("%02x", b)); + } + System.out.println("base64decode: " + decodedHex.toString()); + + // 打印所有环境变量参数 + System.out.println("\n环境变量参数:"); + System.out.println("key: " + key); + System.out.println("time: " + time); + System.out.println("signature: " + computedHmac); + System.out.println("ClientRequestId: " + clientRequestId); + } +} \ No newline at end of file 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 1e9ec1e..8064a2d 100644 --- a/src/main/java/com/example/demo/serviceImpl/cash/BankServiceImpl.java +++ b/src/main/java/com/example/demo/serviceImpl/cash/BankServiceImpl.java @@ -257,12 +257,6 @@ public class BankServiceImpl implements BankService { 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<>(); @@ -300,68 +294,71 @@ public class BankServiceImpl implements BankService { } while (startingAfter != null); - // 在获取的所有记录中查找匹配订单号的记录 - Charge matchedCharge = null; + // 创建StripeDTO列表用于存储所有处理后的数据 + List stripeDTOList = new ArrayList<>(); + + // 处理每一条Stripe数据 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; - } - } - } + try { + // 获取charge对应的余额交易ID + String balanceTransactionId = charge.getBalanceTransaction(); - // 如果未找到匹配的订单,返回错误信息 - if (matchedCharge == null) { - return Result.error("未找到订单号 " + orderNo + " 的支付记录"); - } + // 通过余额交易ID获取详细信息 + BalanceTransaction balanceTransaction = BalanceTransaction.retrieve(balanceTransactionId); - // 获取匹配到的charge对应的余额交易ID - String balanceTransactionId = matchedCharge.getBalanceTransaction(); + // 创建StripeDTO对象并填充所需数据点 + StripeDTO stripeDTO = new StripeDTO(); - // 通过余额交易ID获取详细信息 - BalanceTransaction balanceTransaction = BalanceTransaction.retrieve(balanceTransactionId); + // 从metadata中获取订单号 + if (charge.getMetadata() != null) { + stripeDTO.setOrderNo(charge.getMetadata().get("order_no")); + } - // 创建StripeDTO对象并填充所需数据点 - StripeDTO stripeDTO = new StripeDTO(); - // 设置订单号 - stripeDTO.setOrderNo(matchedCharge.getMetadata().get("order_no")); - // 设置余额交易ID - stripeDTO.setBalanceTransaction(matchedCharge.getBalanceTransaction()); + // 设置余额交易ID + stripeDTO.setBalanceTransaction(charge.getBalanceTransaction()); - // 设置付款币种和金额(来自charge) - stripeDTO.setCurrency(matchedCharge.getCurrency().toUpperCase()); - stripeDTO.setAmount(String.valueOf(matchedCharge.getAmount())); + // 设置付款币种和金额(来自charge) + stripeDTO.setCurrency(charge.getCurrency().toUpperCase()); + stripeDTO.setAmount(String.valueOf(charge.getAmount())); - // 设置收款币种(来自charge) - stripeDTO.setChargeCurrency(matchedCharge.getCurrency().toUpperCase()); + // 设置收款币种(来自charge) + stripeDTO.setChargeCurrency(charge.getCurrency().toUpperCase()); - // 设置到账金额和手续费(来自balanceTransaction) - stripeDTO.setNet(String.valueOf(balanceTransaction.getNet())); - stripeDTO.setFee(String.valueOf(balanceTransaction.getFee())); + // 设置到账金额和手续费(来自balanceTransaction) + stripeDTO.setNet(String.valueOf(balanceTransaction.getNet())); + stripeDTO.setFee(String.valueOf(balanceTransaction.getFee())); - // 设置到账币种(来自balanceTransaction) - stripeDTO.setCurrency(balanceTransaction.getCurrency().toUpperCase()); + // 设置到账币种(来自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); + // 设置available_on日期 + if (balanceTransaction.getAvailableOn() != null) { + long availableOnInSeconds = balanceTransaction.getAvailableOn(); + // 将Unix时间戳转换为Date对象 + Date availableOnDate = new Date(availableOnInSeconds * 1000L); + stripeDTO.setAvailableOn(availableOnDate); + } + + // 添加到列表中 + stripeDTOList.add(stripeDTO); + + // 如果订单号存在,则更新数据库中的记录 + if (stripeDTO.getOrderNo() != null && !stripeDTO.getOrderNo().isEmpty()) { + CashCollection cashCollection = cashCollectionMapper.selectByGoldCoinOrderCode(stripeDTO.getOrderNo()); + if (cashCollection != null) { + cashCollectionMapper.updateByGoldCoinOrderCodeByStripe(stripeDTO); + } + } + } catch (Exception e) { + log.error("处理Stripe数据失败,chargeId: " + charge.getId(), e); + // 继续处理其他数据,不中断整个流程 + } } // 创建响应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()); - } + bankVO.setStripeDTOList(stripeDTOList); + return Result.success(bankVO); } catch (Exception e) { log.error("stripe银行接口处理失败", e); @@ -369,6 +366,7 @@ public class BankServiceImpl implements BankService { } } + //stripe银行接口(单个) @Override public Result getStripe(BankDTO bankDTO) throws StripeException {