Using stripe.net in VB.net

2.6k views Asked by At

I have an older application that was written with Visual Studio 2013 using VB.net, framework 4.5

I had to update the stripe.net NuGet package and the API must be different. I used to charge a card (which was populated by the user on a simple form) using the following script:

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)

It no longer works, as I think the logic changed!

I cannot seem to figure out how to charge a card a certain amount using the package.

The package details are:

Created by: Stripe, Jayme Davis

Id: Stripe.net

Version: 13.1.0

Dependency: Newtonsoft.Json (>= 9.0.1)

I think I need to create a charge object like this:

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

There are no options for the credit card number or card details in the StripeCharge object anymore. I cannot seem to understand the logic I need to do in order to charge a simple transaction.

Does anyone have any experience with this in VB.net?

THE FOLLOWING WAS ADDED AFTER I STARTED WORKING ON THE CODE BASED ON COMMENTS:

Based on a suggestion, I "think" I need to create a card token and then assign that to the charge options. Here's what I'm working on so far:

        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)

I am hoping I create the token properly with my code. The next issue I have (if the token is right) is that chargeService.Create(charge) is wrong. I get the following message for the 'charge' part of it:

Value of type 'Stripe.StripeCharge' canot be converted to 'Stripe.StripeChargeCreateOptions'

So, if I change

Dim charge = New StripeCharge 

to

Dim charge = New StripeChargeCreateOptions

... then suddenly the .Amount, .Description, .Currency, .Source.Card.Id are no longer elements to that object.

2

There are 2 answers

0
chris.cavage On BEST ANSWER

I resolved this problem (with the help of a friend) and hope it helps someone else if they should find it.

I needed to create a token of the credit card on the client side using the API. From there I attach the generated token ID to the charge options and then charge the card!

Here's the code that works:

        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)

Thank you all for your comments. I'm happy to put this to rest.

1
mike t On

Thanks for your great input as it helped me a lot. I spent 3 days on this and still could not get it to work. On the 4th day it worked. As well as this great code your figured out I had to do some additional work. I added the following 2 dll files to the Bin folder on the remote server. C:\github\mywebsite\packages\Stripe.net.17.3.1\lib\net45\Stripe.net.dll and C:\github\mywebsite\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll

These 2 files (Stripe.net.dll and Newtonsoft.Json.dll without the path names) should be in the Bin folder on the local site. Just copy them to the Bin folder on the remote server. This worked for me. Hope it helps someone else. I had to use NuGet from visual studio under "Tools" to update Stripe to 17.3.1 and Json to 9.0.1. Thanks

'Stripe.StripeConfiguration.SetApiKey("sk_test_C........SqQM555EezmpE....") test api key Stripe.StripeConfiguration.SetApiKey("sk_live_....24V777ack7m......") live api key

    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