如何用Java在Stripe中仅通过客户ID检查ACH贷记转账是否已添加
通过客户ID用Java检查Stripe客户是否添加ACH贷记转账
可以仅通过客户ID实现这个需求,具体实现步骤如下:
集成Stripe Java SDK:先在项目中引入Stripe官方SDK,比如Maven依赖可以用:
<dependency> <groupId>com.stripe</groupId> <artifactId>stripe-java</artifactId> <version>21.12.0</version> <!-- 使用最新稳定版本 --> </dependency>核心实现代码:通过客户ID拉取客户对象,遍历其支付方式判断是否存在ACH贷记转账:
import com.stripe.Stripe; import com.stripe.model.Customer; import com.stripe.model.PaymentMethod; import com.stripe.exception.StripeException; public class AchCheckService { public static boolean hasAchCreditTransfer(String customerId) throws StripeException { Stripe.apiKey = "你的Stripe秘钥"; // 拉取客户对象 Customer customer = Customer.retrieve(customerId); // 过滤检查ACH贷记转账支付方式 return customer.getPaymentMethods().getData().stream() .anyMatch(paymentMethod -> { if (!"us_bank_account".equals(paymentMethod.getType())) { return false; } return "ach_credit_transfer".equals(paymentMethod.getUsBankAccount().getPreferredPaymentMethod()); }); } public static void main(String[] args) { try { boolean result = hasAchCreditTransfer("cus_xxxxxx"); // 替换为目标客户ID System.out.println(result ? "客户已添加ACH贷记转账" : "客户未添加ACH贷记转账"); } catch (StripeException e) { e.printStackTrace(); } } }优化建议:如果客户支付方式较多,可直接调用支付方式列表接口并过滤类型,减少数据传输:
import com.stripe.param.PaymentMethodListParams; // ... PaymentMethodListParams params = PaymentMethodListParams.builder() .setCustomer(customerId) .setType(PaymentMethodListParams.Type.US_BANK_ACCOUNT) .build(); return PaymentMethod.list(params).getData().stream() .anyMatch(pm -> "ach_credit_transfer".equals(pm.getUsBankAccount().getPreferredPaymentMethod()));
关键说明:ACH贷记转账对应的支付方式类型为us_bank_account,且preferred_payment_method字段值为ach_credit_transfer;使用的Stripe秘钥需要具备读取客户支付方式的权限。
内容的提问来源于stack exchange,提问作者Soni
相关产品推荐
相关产品推荐

