0

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

无论如何,我已经弄清楚了大部分:

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

    routes.MapRoute("pay-by-stripe", "donate/pay-by-stripe", New With{.controller = "Dynamic", .action = "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
    

我在 STR_Response 行收到 400 错误请求错误:

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

我是 VB 和 Stripe 菜鸟,不知道这意味着什么。我现在的主要理论是,这是因为我没有 /donate/pay-by-stripe/ 页面,但我不知道如果我创建它我什至会放在那里。

任何帮助都会很棒!

4

3 回答 3

1

那是您正在调用的网络服务,对吗?

带有 web 服务的 400 错误请求意味着您的 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>是用 a 关闭的</pp:utc2>,这就是您看到 400 bad request 的原因

于 2013-05-24T20:19:25.330 回答
0

我必须将密码输入 System.Net.NetworkCredentials,并且地址国家不是可用字段。提交费用时唯一可用的字段是金额、货币、描述和卡(实际上是令牌)。这是我的动态控制器中 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
于 2013-05-29T10:48:57.183 回答
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 可能会有所帮助。

于 2014-12-29T05:25:04.227 回答