Search code examples
vb.netstripe-paymentshttp-status-code-400

Stripe Payment with VB - 400 Bad Request


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!


Solution

  • 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