使用VB的条带支付 - 400错误请求

时间:2013-05-24 19:25:57

标签: vb.net stripe-payments http-status-code-400

我正在尝试从VB网站进行条带支付工作。我知道,我知道,“我应该使用C#”。我不能,因为该网站已经在VB中。我无能为力。

无论如何,我大部分都想通了:

  1. 用户点击提交有效信息的提交按钮
  2. 表单提交给Stripe
  3. Stripe发回令牌
  4. jQuery ajax函数将数据发布到捐赠/按条件支付
  5. 我在Global.asax.vb中有这行代码

    routes.MapRoute(“按条件付费”,“捐赠/按条件付费”,新增{.controller =“动态”,。。或“PayByStripe”})

  6. 因此动态控制器中的PayByStripe函数如下所示:

    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
    
  7. 我在STR_Response行收到400错误请求错误:

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

    我是VB和条纹菜鸟,不知道这意味着什么。我现在的主要理论是,因为我没有/捐赠/付费/页面,但我不知道如果我创建了它,我甚至会把它放在那里。

    任何帮助都会很棒!

3 个答案:

答案 0 :(得分:1)

这是你正在打电话的网络服务,对吗?

带有Web服务的400 Bad Request意味着您的XML请求格式错误。

示例,在我的请求中,部分是特定日期格式的UTC。示例:<pp:utc>2013-05-24 2025</pp:utc>

所以,如果我要将此请求变为<pp:utc>2013-05-24 2025</pp:utc2>,则会导致:

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

因此,请检查您的请求并确保所有内容都已正确格式化。

编辑:刚注意到我错误地输入了“错误”的utc标签。 请注意,正在使用<pp:utc>关闭开始标记</pp:utc2>,这就是您看到400个错误请求的原因

答案 1 :(得分:0)

我必须将我的密码放在System.Net.NetworkCredentials中,而address-country不是可用的字段。提交费用时唯一可用的字段是金额,货币,说明和卡(实际上是令牌)。这是我的动态控制器中PayByStripe函数的最终工作版本:

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

答案 2 :(得分:0)

连接到Stripe的API时,我从未输入过密码。只需通过没有密码的授权标头传递您的私有API密钥。它也可能有助于传入版本标题,Stripe建议。以下几行卡片都在C#中,我知道你的问题是在VB中,但我相信你可以很容易地适应这个:

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"))))));

此外,当发送过期或无效的卡令牌时,Stripe会发送400 Bad Request可能会有所帮助。