VS2013 VB.NET(.NET4.5)升级Stripe.net13.1.0后刷卡扣费失败求助
嘿,我刚好熟悉Stripe.net 13.x版本的API变更,帮你搞定这个VB.NET环境下的刷卡扣费问题:
Stripe.net 13.1.0版本VB.NET实现指定金额刷卡扣费方案
首先得明确:Stripe.net 13.x版本已经全面弃用了旧版的Charge API,转而使用Payment Intent API来处理支付流程,这就是你原有代码失效的核心原因。下面是适配你的VS2013 + .NET Framework 4.5环境的具体实现方法:
第一步:初始化Stripe客户端
首先需要用你的Stripe密钥初始化客户端,所有支付操作都基于这个客户端:
Imports Stripe ' 替换为你的Stripe Secret Key(测试环境用测试密钥,生产用生产密钥) Dim stripeClient As New StripeClient("sk_your_secret_key_here")
两种常见的扣费场景
场景1:使用用户已保存的PaymentMethod扣费
如果用户之前已经绑定了支付方式(有对应的PaymentMethod ID),可以直接用这个ID创建并确认支付意向:
Try Dim paymentIntentOptions As New PaymentIntentCreateOptions() With { .Amount = 1000, ' 金额单位为「分」,这里代表10美元 .Currency = "usd", ' 货币代码,根据你的业务调整 .PaymentMethod = "pm_xxxxxx", ' 替换为实际的PaymentMethod ID .Confirm = True, ' 直接触发扣费确认 .AutomaticPaymentMethods = New PaymentIntentAutomaticPaymentMethodsOptions() With { .Enabled = True ' 启用自动支付方法处理 } } Dim paymentIntentService As New PaymentIntentService(stripeClient) Dim paymentIntent As PaymentIntent = paymentIntentService.Create(paymentIntentOptions) ' 检查支付结果 If paymentIntent.Status = "succeeded" Then ' 扣费成功,执行你的业务逻辑(比如更新订单状态) Console.WriteLine($"扣费成功!PaymentIntent ID: {paymentIntent.Id}") Else ' 支付未完成,可能需要用户进一步验证(比如3D Secure) Console.WriteLine($"支付状态: {paymentIntent.Status},请引导用户完成验证") End If Catch ex As StripeException ' 捕获Stripe专属异常,根据错误代码处理 Console.WriteLine($"支付出错: {ex.Message},错误代码: {ex.StripeError.Code}") End Try
场景2:一次性使用银行卡号直接扣费
如果是临时刷卡(没有保存PaymentMethod),需要先创建PaymentMethod,再用它发起支付:
Try ' 1. 创建PaymentMethod(模拟用户输入的银行卡信息) Dim paymentMethodOptions As New PaymentMethodCreateOptions() With { .Type = "card", .Card = New PaymentMethodCardOptions() With { .Number = "4242424242424242", ' 测试卡号,生产环境替换为用户输入的真实卡号 .ExpMonth = 12, .ExpYear = 2025, .Cvc = "123" ' 测试CVC } } Dim paymentMethodService As New PaymentMethodService(stripeClient) Dim paymentMethod As PaymentMethod = paymentMethodService.Create(paymentMethodOptions) ' 2. 创建并确认PaymentIntent Dim paymentIntentOptions As New PaymentIntentCreateOptions() With { .Amount = 1500, ' 15美元 .Currency = "usd", .PaymentMethod = paymentMethod.Id, .Confirm = True, .AutomaticPaymentMethods = New PaymentIntentAutomaticPaymentMethodsOptions() With { .Enabled = True } } Dim paymentIntentService As New PaymentIntentService(stripeClient) Dim paymentIntent As PaymentIntent = paymentIntentService.Create(paymentIntentOptions) ' 结果判断 If paymentIntent.Status = "succeeded" Then Console.WriteLine($"一次性扣费成功!PaymentIntent ID: {paymentIntent.Id}") Else Console.WriteLine($"支付状态: {paymentIntent.Status}") End If Catch ex As StripeException Console.WriteLine($"支付出错: {ex.Message},错误代码: {ex.StripeError.Code}") End Try
关键注意事项
- 兼容性验证:Stripe.net 13.1.0是支持.NET Framework 4.5的,确保你的NuGet包安装过程没有报错,项目引用正常。
- 密钥权限:确保你的Stripe Secret Key拥有
payment_intent:create和payment_intent:confirm的权限。 - 错误处理:一定要捕获
StripeException,它会返回具体的错误代码(比如card_declined、insufficient_funds),方便你做针对性的用户提示。
内容的提问来源于stack exchange,提问作者chris.cavage
相关产品推荐
相关产品推荐

