如何在Stripe PaymentSheet中禁用卡片删除功能?
问题:禁用Stripe PaymentSheet的卡片删除功能
我使用以下代码展示Stripe SDK的PaymentSheet界面,用于添加并绑定银行卡至系统:
PaymentSheet.Address address = new PaymentSheet.Address( PersistentManager.getCityForId(HVariant.getCityVariant()).getDisplayName(), HVariant.getCountryVariant(), null, null, null, HVariant.getCountryVariant()); PaymentSheet.BillingDetails.Builder billingDetails = new PaymentSheet.BillingDetails.Builder().address(address); PaymentSheet.CustomerConfiguration customerConfiguration = new PaymentSheet.CustomerConfiguration( resp.getStripe().getCustomer(), resp.getStripe().getEphemeralKey() ); mPaymentSheet.presentWithSetupIntent( resp.getStripe().getClientSecret(), new PaymentSheet.Configuration( "Avara", customerConfiguration, null, null, billingDetails.build() ) );
当用户无银行卡时,界面会打开输入卡片并绑定的页面;当用户已有银行卡时,界面会展示现有卡片并允许添加新卡,但存在一个“Edit”按钮,点击可删除Stripe中的卡片,这破坏了用户必须始终绑定至少一张卡的核心业务逻辑。我发现PaymentSessionConfig类中有setCanDeletePaymentOptions方法,但不知道如何将其配置到PaymentSheet中,请问该如何禁用卡片删除功能?
解决方案
你提到的PaymentSessionConfig是旧版Stripe SDK中PaymentSession相关的配置类,当前使用的PaymentSheet有独立的配置项控制删除权限。要禁用卡片删除功能,需通过PaymentSheet.Configuration.Builder构建配置,并设置SetupIntentConfiguration的allowDeletion为false。
修改后的代码如下:
PaymentSheet.Address address = new PaymentSheet.Address( PersistentManager.getCityForId(HVariant.getCityVariant()).getDisplayName(), HVariant.getCountryVariant(), null, null, null, HVariant.getCountryVariant()); PaymentSheet.BillingDetails billingDetails = new PaymentSheet.BillingDetails.Builder().address(address).build(); PaymentSheet.CustomerConfiguration customerConfiguration = new PaymentSheet.CustomerConfiguration( resp.getStripe().getCustomer(), resp.getStripe().getEphemeralKey() ); // 构建SetupIntent配置,禁用删除功能 PaymentSheet.SetupIntentConfiguration setupIntentConfig = new PaymentSheet.SetupIntentConfiguration.Builder() .setAllowDeletion(false) .build(); // 使用Builder构建PaymentSheet.Configuration PaymentSheet.Configuration paymentSheetConfig = new PaymentSheet.Configuration.Builder("Avara") .setCustomerConfiguration(customerConfiguration) .setSetupIntentConfiguration(setupIntentConfig) .setBillingDetails(billingDetails) .build(); mPaymentSheet.presentWithSetupIntent( resp.getStripe().getClientSecret(), paymentSheetConfig );
关键改动点:
- 改用
PaymentSheet.Configuration.Builder构建配置对象,替代直接构造的方式 - 创建
SetupIntentConfiguration并调用setAllowDeletion(false),这会隐藏编辑按钮并禁用卡片删除功能 - 将所有配置项通过Builder方法传入,保持代码可读性和扩展性
配置完成后,PaymentSheet将不再显示用于删除卡片的“Edit”按钮,确保用户始终至少绑定一张卡,符合业务逻辑。
内容的提问来源于stack exchange,提问作者AndreiBogdan
相关产品推荐
相关产品推荐

