Browse Source

11月29日银行接口

sunjiabei/feature-20251021102635-银行接口
sunjiabei 11 hours ago
parent
commit
4b86d914a8
  1. 4
      src/main/java/com/example/demo/controller/cash/BankController.java
  2. 59
      src/main/java/com/example/demo/controller/cash/SignatureGenerator.java
  3. 74
      src/main/java/com/example/demo/serviceImpl/cash/BankServiceImpl.java

4
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);

59
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";
// 生成ClientRequestId1-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);
}
}

74
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<Charge> allCharges = new ArrayList<>();
@ -300,43 +294,35 @@ public class BankServiceImpl implements BankService {
} 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;
}
}
}
// 创建StripeDTO列表用于存储所有处理后的数据
List<StripeDTO> stripeDTOList = new ArrayList<>();
// 如果未找到匹配的订单返回错误信息
if (matchedCharge == null) {
return Result.error("未找到订单号 " + orderNo + " 的支付记录");
}
// 获取匹配到的charge对应的余额交易ID
String balanceTransactionId = matchedCharge.getBalanceTransaction();
// 处理每一条Stripe数据
for (Charge charge : allCharges) {
try {
// 获取charge对应的余额交易ID
String balanceTransactionId = charge.getBalanceTransaction();
// 通过余额交易ID获取详细信息
BalanceTransaction balanceTransaction = BalanceTransaction.retrieve(balanceTransactionId);
// 创建StripeDTO对象并填充所需数据点
StripeDTO stripeDTO = new StripeDTO();
// 设置订单号
stripeDTO.setOrderNo(matchedCharge.getMetadata().get("order_no"));
// 从metadata中获取订单号
if (charge.getMetadata() != null) {
stripeDTO.setOrderNo(charge.getMetadata().get("order_no"));
}
// 设置余额交易ID
stripeDTO.setBalanceTransaction(matchedCharge.getBalanceTransaction());
stripeDTO.setBalanceTransaction(charge.getBalanceTransaction());
// 设置付款币种和金额来自charge
stripeDTO.setCurrency(matchedCharge.getCurrency().toUpperCase());
stripeDTO.setAmount(String.valueOf(matchedCharge.getAmount()));
stripeDTO.setCurrency(charge.getCurrency().toUpperCase());
stripeDTO.setAmount(String.valueOf(charge.getAmount()));
// 设置收款币种来自charge
stripeDTO.setChargeCurrency(matchedCharge.getCurrency().toUpperCase());
stripeDTO.setChargeCurrency(charge.getCurrency().toUpperCase());
// 设置到账金额和手续费来自balanceTransaction
stripeDTO.setNet(String.valueOf(balanceTransaction.getNet()));
@ -353,15 +339,26 @@ public class BankServiceImpl implements BankService {
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 {

Loading…
Cancel
Save