【问题标题】:Using stripe.net in VB.net在 VB.net 中使用 stripe.net
【发布时间】:2018-08-05 05:32:54
【问题描述】:

我有一个使用 VB.net 框架 4.5 使用 Visual Studio 2013 编写的旧应用程序

我必须更新 stripe.net NuGet 包,并且 API 必须不同。我曾经使用以下脚本对一张卡收费(由用户在一个简单的表单上填写):

Dim myCharge = New StripeChargeCreateOptions()

myCharge.Amount = lblPrice.Text * 100  'put into cents for Stripe
myCharge.Currency = "usd"

myCharge.Description = "sample test charge"

myCharge.CardNumber = txtCardNumber.Text
myCharge.CardExpirationYear = txtExpiresYear.Text
myCharge.CardExpirationMonth = txtExpiresMonth.Text

myCharge.CardCvc = txtCVC.Text

myCharge.Capture = True

Dim chargeService = New StripeChargeService
Dim StripeCharge = New StripeCharge
StripeCharge = chargeService.Create(myCharge)

它不再起作用,因为我认为逻辑改变了!

我似乎不知道如何使用该套餐从卡中收取一定的费用。

包裹详情如下:

创建者:Stripe, Jayme Davis

ID:Stripe.net

版本:13.1.0

依赖:Newtonsoft.Json (>= 9.0.1)

我想我需要像这样创建一个收费对象:

Dim charge = New StripeCharge
charge.Amount = lblPrice.Text
charge.Description = "test"
charge.Currency = "USD"

StripeCharge 对象中不再有信用卡号或卡详细信息的选项。我似乎无法理解收取简单交易费用所需执行的逻辑。

有人在 VB.net 中有这方面的经验吗?

以下是我根据评论开始处理代码后添加的:

根据一个建议,我“认为”我需要创建一个卡片令牌,然后将其分配给收费选项。以下是我目前正在做的事情:

        Dim tokenOptions = New StripeTokenCreateOptions
        tokenOptions.Card.Number = txtCardNumber.Text
        tokenOptions.Card.ExpirationMonth = txtExpiresMonth.Text
        tokenOptions.Card.ExpirationYear = txtExpiresYear.Text
        tokenOptions.Card.Cvc = txtCVC.Text

        Dim tokenService = New StripeTokenService
        Dim token = tokenService.Create(tokenOptions)

        Dim charge = New StripeCharge
        charge.Amount = lblPrice.Text
        charge.Description = "test"
        charge.Currency = "USD"
        charge.Source.Card.Id = token.Id

        Dim chargeService = New StripeChargeService
        Dim StripeCharge = chargeService.Create(charge)

我希望我能用我的代码正确地创建令牌。我遇到的下一个问题(如果令牌是正确的)是chargeService.Create(charge) 是错误的。对于其中的“收费”部分,我收到以下消息:

“Stripe.StripeCharge”类型的值无法转换为“Stripe.StripeChargeCreateOptions”

所以,如果我改变了

Dim charge = New StripeCharge 

Dim charge = New StripeChargeCreateOptions

...然后突然之间.Amount、.Description、.Currency、.Source.Card.Id 不再是该对象的元素。

【问题讨论】:

  • 当您查看 Stripe 仪表板日志时,您是否看到请求和相关错误?我很好奇您发送的内容是否到达 Stripe!此外,出于 PCI 合规性原因,您通常希望使用令牌而不是原始卡详细信息,我认为这可能是这里的问题。 stripe.com/docs/api/dotnet#create_charge
  • 嗨鸭。我无法使用我最初拥有的代码构建项目,因此无法使用新 API 发送任何内容。在 VB.net 中,myCharge 似乎没有我可以使用的任何对象元素。所以我想我需要重新创建整个过程。正如您所指出的,我一直在阅读文档,但我认为它们是用 C# 编写的,而且我在理解如何将其转换为 VB.net 时遇到了问题。所以,我需要先根据用户输入的卡信息创建一个令牌,对吗?那么我需要将令牌附加到费用中,对吗?
  • 我根据首先为卡创建令牌的新逻辑,在原始问题中添加了一些新代码。可能是在做某事?希望我越来越接近解决这个问题。

标签: vb.net stripe-payments nuget-package stripe.net


【解决方案1】:

我解决了这个问题(在朋友的帮助下),希望能帮助其他人找到它。

我需要使用 API 在客户端创建信用卡令牌。从那里我将生成的令牌 ID 附加到收费选项,然后向卡收费!

这是有效的代码:

        Stripe.StripeConfiguration.SetApiKey("sk_test_your_key_here")

        Dim tokenOptions = New Stripe.StripeTokenCreateOptions()
        tokenOptions.Card = New Stripe.StripeCreditCardOptions()

        tokenOptions.Card.Number = txtCardNumber.Text
        tokenOptions.Card.ExpirationMonth = Convert.ToInt32(txtExpiresMonth.Text)
        tokenOptions.Card.ExpirationYear = Convert.ToInt32(txtExpiresYear.Text)
        tokenOptions.Card.Cvc = txtCVC.Text

        Dim tokenService = New Stripe.StripeTokenService()
        Dim token = tokenService.Create(tokenOptions)

        Dim charge = New Stripe.StripeChargeCreateOptions()
        charge.Amount = Convert.ToInt32(lblPrice.Text) * 100
        charge.Description = "test"
        charge.Currency = "USD"
        charge.SourceTokenOrExistingSourceId = token.Id

        Dim chargeService = New Stripe.StripeChargeService
        Dim StripeCharge = chargeService.Create(charge)

谢谢大家的cmets。我很高兴能解决这个问题。

【讨论】:

    【解决方案2】:

    感谢您的宝贵意见,因为它对我帮助很大。我花了 3 天的时间,仍然无法让它工作。第4天它起作用了。 除了你发现的这个很棒的代码之外,我还需要做一些额外的工作。 我将以下 2 个 dll 文件添加到远程服务器上的 Bin 文件夹中。 C:\github\mywebsite\packages\Stripe.net.17.3.1\lib\net45\Stripe.net.dll 和 C:\github\mywebsite\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll

    这两个文件(Stripe.net.dll 和 Newtonsoft.Json.dll 没有路径名)应该在本地站点的 Bin 文件夹中。只需将它们复制到远程服务器上的 Bin 文件夹即可。这对我有用。希望它可以帮助别人。我不得不在“工具”下使用 Visual Studio 中的 NuGet 将 Stripe 更新到 17.3.1,将 Json 更新到 9.0.1。谢谢

    'Stripe.StripeConfiguration.SetApiKey("sk_test_C........SqQM555EezmpE....") 测试api密钥 Stripe.StripeConfiguration.SetApiKey("sk_live_....24V777ack7m......") 直播api密钥

        Dim tokenOptions As New Stripe.StripeTokenCreateOptions()
        tokenOptions.Card = New Stripe.StripeCreditCardOptions()
        tokenOptions.Card.Number = TextBox2.Text
        tokenOptions.Card.Name = TextBox3.Text
        tokenOptions.Card.ExpirationMonth = TextBox4.Text
        tokenOptions.Card.ExpirationYear = TextBox5.Text
        tokenOptions.Card.Cvc = TextBox6.Text
    
        Try
            Dim tokenService As New Stripe.StripeTokenService()
            Dim token = tokenService.Create(tokenOptions)
            Dim charge As New Stripe.StripeChargeCreateOptions()
            charge.Amount = 200         ' $2.00
            charge.Description = "live"
            charge.Currency = "USD"
            charge.SourceTokenOrExistingSourceId = token.Id
            Dim chargeService As New Stripe.StripeChargeService
            Dim StripeCharge = chargeService.Create(charge)
    
            'Dim StripePublishableKey As String = "pk_test_....vtfVe666xwLpjuy....."        'test key
            Dim StripePublishableKey As String = "pk_live_.....yntM7888f09iuj7....."         'live key
    
        Catch ex As Exception
            ' Response.Write(ex)
            warning.Visible = True
            warning.Focus()
            warning.Text = "Error  - Credit or Debit card failed  -  Error"
            Exit Sub
        End Try
    

    【讨论】:

    • 不客气。很高兴你能够完成这项工作(对不起,我刚看到这个!)。感谢后续和额外的代码!
    猜你喜欢
    • 2013-09-18
    • 2016-01-03
    • 1970-01-01
    • 2016-12-06
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 2019-09-08
    相关资源
    最近更新 更多