dcsimg
Results 1 to 10 of 10

Thread: [RESOLVED] Stripe.net trying to charge a test card

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Resolved [RESOLVED] Stripe.net trying to charge a test card

    I used stripe.net before to charge a credit card in a simple app I made. It was working okay, but I recently updated Stripe via NuGet. I then was warned to update my .net framework from 4.0 to 4.5.

    When I did that I cannot seem to adapt new code to run a charge.

    I am reading the API documentation here: https://stripe.com/docs/api/dotnet#create_charge

    It says in order to charge a card, I have to have a 'source' for charge.Source in my code. I can attach the information I need to charge a card using a Dictionary, as it says. I've never done a 'Dictionary' before, but I think I am getting somewhere. My first issue is:

    If my Dictionary is correct, how do I attach it to the source?

    Code:
                Dim charge = New StripeCharge
    
                Dim dict = New Dictionary(Of String, String)
                dict.Add("exp_month", lblExpiresMonth.Text)
                dict.Add("exp_year", lblExpiresYear.Text)
                dict.Add("number", lblCardNumber.Text)
                dict.Add("object", "card")
                dict.Add("cvc", lblCVC.Text)
    
                charge.Source = dict
    
                charge.Amount = lblPrice.Text * 100  'put into cents for Stripe
                charge.Currency = "usd"
                charge.Description = "test run"
    
    
                Dim chargeService = New StripeChargeService
                chargeService.Create(charge)
    charge.Source = dict is just not correct. I just don't know what I need to do there.

    Also chargeService.Create(charge) is not correct. The charge shows this error message on hover: value of type Stripe.StripeCharge cannot be converted to Stripe.StripeChargeCreateOptions

    Thank you.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,582

    Re: Stripe.net trying to charge a test card

    I don't know C# much at all, and this API at all, but based on the documentation you pointed out, and the examples they give
    I think charge.Source is a hash of the card, so is the wrong thing to be trying to set.

    The Source you're referring to as a dictionary looks to be part of the chargeOptions argument that should be passed to the chargeService.Create method.
    Perhaps like
    Code:
    var chargeOptions = new StripeChargeCreateOptions()
    {
        Amount = 2000,
        Currency = "usd",
        Source = new Dictionary<string, string>()
        {
            { "exp_month", lblExpiresMonth.Text },
            { "exp_year", lblExpiresYear.Text },
            { "number", lblCardNumber.Text },
            { "object", "card"},
            { "cvc", lblCVC.Text}
        }
    
    };
    
    var chargeService = new StripeChargeService();
    StripeCharge charge = chargeService.Create(chargeOptions);
    I don't know.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Re: Stripe.net trying to charge a test card

    Thanks for looking into this. I think you are correct. I tried that approach.

    I wrote this old project in VB6 and then moved it to .net. I'm embarrassed to say that I don't know why, but I can't use this in my project:

    Code:
    {
        Amount = 2000,
        Currency = "usd",
        Source = new Dictionary<string, string>()
        {
            { "exp_month", lblExpiresMonth.Text },
            { "exp_year", lblExpiresYear.Text },
            { "number", lblCardNumber.Text },
            { "object", "card"},
            { "cvc", lblCVC.Text}
        }
    
    };
    If I do the brackets show syntax errors.

    This is why I tried another method and reached out to the forums.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,118

    Re: Stripe.net trying to charge a test card

    That is C# code, which I assume passel based on the API documentation

    Here is the VB equivalent (with values from your earlier attempt):
    Code:
                Dim chargeOptions = New StripeChargeCreateOptions
    
                Dim dict = New Dictionary(Of String, String)
                dict.Add("exp_month", lblExpiresMonth.Text)
                dict.Add("exp_year", lblExpiresYear.Text)
                dict.Add("number", lblCardNumber.Text)
                dict.Add("object", "card")
                dict.Add("cvc", lblCVC.Text)
    
                chargeOptions.Source = dict
    
                chargeOptions.Amount = lblPrice.Text * 100  'put into cents for Stripe
                chargeOptions.Currency = "usd"
                chargeOptions.Description = "test run"
    
    
                Dim chargeService = New StripeChargeService
                chargeService.Create(charge)
    
                Dim charge as StripeCharge = chargeService.Create(chargeOptions)

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Re: Stripe.net trying to charge a test card

    I see, thanks.

    I think is part is fine in my code:

    Code:
                Dim chargeOptions = New StripeCharge
                chargeOptions.Amount = lblPrice.Text * 100  'put into cents for Stripe
                chargeOptions.Currency = "usd"
                chargeOptions.Description = "test run"
    
    
                Dim chargeService = New StripeChargeService
                Dim charge = chargeService.Create(chargeOptions)
    .Amount, .Currency, .Description are all members of chargeOptions.

    .Source is also.

    But setting .Source = dict shows me:

    Code:
    Dim dict = New Dictionary(Of String, String)
                dict.Add("exp_month", lblExpiresMonth.Text)
                dict.Add("exp_year", lblExpiresYear.Text)
                dict.Add("number", lblCardNumber.Text)
                dict.Add("object", "card")
                dict.Add("cvc", lblCVC.Text)
    
                chargeOptions.Source = dict
    Code:
    Value of type 'System.Collections.Generic.Dictionary(Of String, String)' cannot be converted to 'Stripe.Source'

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,773

    Re: Stripe.net trying to charge a test card

    That means that the .Source property is not a Dictionary object but a Source object... under the hood it may be a dictionary, but it isn't being exposed as one. I'd look to see what methods are available on the .Source object...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Re: Stripe.net trying to charge a test card

    I'm reading the docs.

    They definitely changed the API as I've read from another source.

    This works here:

    Code:
                Dim charge = New StripeCharge
                charge.Amount = 100
                charge.Description = "test"
                charge.Currency = "USD"
                charge.Source.Card.ExpirationYear = txtExpiresYear.Text
                charge.Source.Card.ExpirationMonth = txtExpiresMonth.Text
    However, there is no card number as one of the options! Nor is there a CVC option under .Source.Card.no_card_number_option or .Source.Card.no_card_cvs_option

    You'd think that would be a part of it.

    It's the stripe.net version 13.1.0 under NuGet packages. If anyone ever comes across this who has used it, I'd love to know how to adapt it properly for VB.net

    I'm gonna keep at it for now...

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,773

    Re: Stripe.net trying to charge a test card

    [quote]
    However, there is no card number as one of the options! Nor is there a CVC option under .Source.Card.no_card_number_option or .Source.Card.no_card_cvs_option
    [/quote
    Doesn't surprise me... that's because its called "number" and "cvc" .... I'm starring at it right now in the documentation... well, that's how it is in the API... not sure how it was implemented in the object.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,582

    Re: Stripe.net trying to charge a test card

    Quote Originally Posted by chris.cavage View Post
    ...
    However, there is no card number as one of the options! Nor is there a CVC option under .Source.Card.no_card_number_option or .Source.Card.no_card_cvs_option

    You'd think that would be a part of it.

    ...
    No, I wouldn't think that would be part of it.
    The full card number and CVC values are quite sensitive and should not be readily available. I believe the point is that you should produce an input form using the API or tools provided by Stripe, and the card number entry and cvc entries would be passed directly to the library from the form, and a Source object will be returned. You use that source object. The card number and cvc that was part of the input to generate that source is unavailable to you to prevent easy capture of that information. You can get the last four digits from the source object, but you can't get the full number and cvc. That is verified in some encrypted manner and is part of the source object in that manner.

    I believe it pretty much says that in the documentation, i.e.
    Handling card information

    Card information is sensitive by nature. Card sources must be created client-side using Stripe.js and Elements. This ensures that no sensitive card data passes through your server so your integration can operate in a PCI compliant way.

    When your customer submits their card information using your payment form, it is sent directly to Stripe, and a representative Source object is returned for you to use.
    Just looking around there seems to quite a few posts. Perhaps you want to look at this one if you haven't already.

    It seems like the bottom line of the few that i've scanned is you need to use Stripe.js in order to generate the source object so that you are compliant with regulations that seek to protect card information.
    In the link above, he does mention "If you are PCI-compliant and still want to use the Stripe.NET exclusively to process payments, please check my post here."

    I don't know if you're PCI-compliant already or not. It certainly sounds like "not" to me, so his link is probably not applicable to you. I didn't follow the link since none of this applies to me and my interest has reached its end for now.

    Sorry about posting C# code earlier, I guess sometimes the code looks enough alike to me that I don't notice, and since the documentation was using C# examples, I forgot you were posting in the VB side of the forum and had posted VB code.
    Last edited by passel; Feb 25th, 2018 at 11:55 AM.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Re: Stripe.net trying to charge a test card

    I resolved this problem 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:

    Code:
                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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width