Question

I'm trying to make a stripe payment work from a VB website. I know, I know, "I should use C#". I can't because the site is already in VB. Nothing I can do about it.

Anyway, I have most of it figured out:

  1. User clicks submit button with valid info
  2. Form submits to Stripe
  3. Stripe sends a token back
  4. A jQuery ajax function posts the data to donate/pay-by-stripe
  5. I have this line of code in my Global.asax.vb

    routes.MapRoute("pay-by-stripe", "donate/pay-by-stripe", New With{.controller = "Dynamic", .action = "PayByStripe"})

  6. So my PayByStripe function in the Dynamic Controller looks like this:

    Function PayByStripe()
    ''The Stripe Account API Token
    Dim STR_Stripe_API_Token As String = "sk_test_*****"
    
    ''The Stripe API URL
    Dim STR_Stripe_API_URL As [String] = "https://api.stripe.com/v1/charges"
    
    ''The Stripe Card Token
    Dim token As String = HttpContext.Request.Form("token")
    Dim description As String = HttpContext.Request.Form("description")
    Dim amount As Single = HttpContext.Request.Form("amount")
    
    ''Creates a Web Client
    Dim OBJ_Webclient As New System.Net.WebClient()
    
    ''Creates Credentials
    Dim OBJ_Credentials As New System.Net.NetworkCredential(STR_Stripe_API_Token, "")
    
    ''Sets the Credentials on the Web Client
    OBJ_Webclient.Credentials = OBJ_Credentials
    
    ''Creates a Transaction with Data that Will be Sent to Stripe
    ''Dim OBJ_Transaction As New System.Collections.Specialized.NameValueCollection()
    Dim OBJ_Transaction As NameValueCollection = New NameValueCollection()
    OBJ_Transaction.Add("amount", amount)
    OBJ_Transaction.Add("currency", "usd")
    OBJ_Transaction.Add("address-country", "US")
    OBJ_Transaction.Add("description", "")
    OBJ_Transaction.Add("card", token)
    
    ''The Stripe Response String
    Dim STR_Response As String = Encoding.ASCII.GetString(OBJ_Webclient.UploadValues(STR_Stripe_API_URL, OBJ_Transaction))
    
    'Response.Redirect("/donate/?transaction=success");
    
    Return STR_Response
    
    End Function
    

I'm getting a 400 bad request error on the STR_Response line:

Dim STR_Response As String = Encoding.ASCII.GetString(OBJ_Webclient.UploadValues(STR_Stripe_API_URL, OBJ_Transaction))

I'm a VB and Stripe noob, and not sure what this means. My main theory now is that it's because I don't have a /donate/pay-by-stripe/ page, but I don't know what I'd even put in there if I did create it.

Any help would be great!

Was it helpful?

Solution 2

I had to put my password in System.Net.NetworkCredentials, and address-country is not a usable field. The only usable fields when submitting a charge are amount, currency, description, and card (which is actually the token). This is the final, working version of my PayByStripe Function in my Dynamic Controller:

Function PayByStripe()


    ''  The Stripe Account API Token - change this for testing 
    Dim STR_Stripe_API_Token As String = ""

    If (this_is_a_test) Then
        ' Test Secret Key
        STR_Stripe_API_Token = "sk_test_***"
    Else
        ' Prod Secret Key
        STR_Stripe_API_Token = "sk_live_***"
    End If

    ''The Stripe API URL
    Dim STR_Stripe_API_URL As [String] = "https://api.stripe.com/v1/charges"

    ''The Stripe Card Token
    Dim token As String = HttpContext.Request.Form("token")
    Dim description As String = HttpContext.Request.Form("description")
    Dim amount As Single = HttpContext.Request.Form("amount")

    ''Creates a Web Client
    Dim OBJ_Webclient As New System.Net.WebClient()

    ''Creates Credentials
    Dim OBJ_Credentials As New System.Net.NetworkCredential(STR_Stripe_API_Token, "YOUR PASSWORD FOR STRIPE")

    ''Sets the Credentials on the Web Client
    OBJ_Webclient.Credentials = OBJ_Credentials

    ''Creates a Transaction with Data that Will be Sent to Stripe
    Dim OBJ_Transaction As New System.Collections.Specialized.NameValueCollection()

    OBJ_Transaction.Add("amount", amount)
    OBJ_Transaction.Add("currency", "usd")
    OBJ_Transaction.Add("description", description)
    OBJ_Transaction.Add("card", token)

    ''The Stripe Response String
    Dim STR_Response As String = Encoding.ASCII.GetString(OBJ_Webclient.UploadValues(STR_Stripe_API_URL, OBJ_Transaction))

    Return STR_Response

End Function

OTHER TIPS

That's a webservice you are calling, right?

A 400 Bad Request with a webservice means your XML request is malformed.

Example, in my request, part of it is a UTC in a certain date format. Example: <pp:utc>2013-05-24 2025</pp:utc>

So, if I were to malform my request to this <pp:utc>2013-05-24 2025</pp:utc2> it would result in:

HTTP/1.1 400 Bad Request
Cache-Control: private
Server: Microsoft-IIS/7.5
X-AspNet-Version: 2.0.5

So, check your request and make sure everything is properly formatted.

EDIT: just noticed I put the "incorrect" utc tags incorrectly. Please notice the opening tag <pp:utc> is being closed with a </pp:utc2>, which is the reason why you see 400 bad request

I've never had to pass in my password when connecting to Stripe's API. Simply pass in your private API Key through an authorization header with no password. It may also help to pass in a version header as well, something Stripe recommends. The following lines of card are in C#, I know your question was in VB, but I'm sure you can easily adaptive this:

webrequest.Headers.Add("Stripe-Version", "2014-12-22");
webrequest.Headers.Add("Authorization", String.Concat("Basic ", (Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:", "sk_test_XXXXXXXXXXXXXXXXXXX"))))));

Also, it may help to know that Stripe sends a 400 Bad Request when an expired or invalid card token is sent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top