发布到Square时获取422状态码错误?

时间:2016-05-05 21:29:34

标签: asp.net vb.net square-connect

请帮我完成最后一步,这样我可以完成这个项目:)

我试图将此示例转换为Vb.Net:

https://docs.connect.squareup.com/articles/processing-payment-rest/

用户输入信用卡信息后,我可以成功获取CardOnce ID。我可以成功地从Square进行READ以获取我的位置ID。但是,当我提供CardOnce ID,位置ID和POST数量时,我仍然停留在最后一步,我不断收到错误:"远程服务器返回错误:(422)状态代码422"

我的代码如下所示,错误发生在:

错误在调用它时发生:

response = DirectCast(request.GetResponse(), HttpWebResponse)

完整代码:

Public Function IsSuccessProcess(ByVal sLocationId As String, ByVal sCardOnce As String, ByVal iAmount As Integer)
    Dim request As HttpWebRequest
    Dim response As HttpWebResponse = Nothing
    Dim reader As StreamReader
    Dim address As Uri
    Dim data As StringBuilder
    Dim byteData() As Byte
    Dim postStream As Stream = Nothing

    address = New Uri("https://connect.squareup.com/v2/locations/" & sLocationId & "/transactions")

    ' Create the web request  
    request = DirectCast(WebRequest.Create(address), HttpWebRequest)

    ' Set type to POST  
    request.Method = "POST"
    request.ContentType = "application/json"
    request.Accept = "application/json"
    request.Headers.Add("Authorization", "Bearer " & AccessToken)

    data = New StringBuilder()
    Dim sQuote As String = """"
    sQuote = "'"
    data.Append("{" & sQuote & "card_nonce" & sQuote & ": " & sCardOnce & "," & sQuote & "amount_money" & sQuote & ": {" & sQuote & "amount" & sQuote & ": " & iAmount & "," & sQuote & "currency" & sQuote & ": " & sQuote & "USD" & sQuote & "}")

    Dim didempotency_key As Double = Microsoft.VisualBasic.Timer
    Dim idempotency_key As Integer = CInt(didempotency_key)
    data.Append("'idempotency_key': " & idempotency_key)
    ' Create a byte array of the data we want to send  
    byteData = UTF8Encoding.UTF8.GetBytes(data.ToString())
    ' Set the content length in the request headers  
    request.ContentLength = byteData.Length

    ' Write data  
    Try
        postStream = request.GetRequestStream()
        postStream.Write(byteData, 0, byteData.Length)
    Finally
        If Not postStream Is Nothing Then postStream.Close()
    End Try

    Try
        ' Get response  
        response = DirectCast(request.GetResponse(), HttpWebResponse)

        ' Get the response stream into a reader  
        reader = New StreamReader(response.GetResponseStream())

        ' Console application output  
        Console.WriteLine(reader.ReadToEnd())
    Finally
        If Not response Is Nothing Then response.Close()
    End Try


    Return True

End Function

2 个答案:

答案 0 :(得分:1)

可能会发生此错误,因为您在JSON正文中使用单引号而不是双引号。尝试将其替换为第一步。

请注意,您可能会发现借助第三方库(例如Json.NET)生成和解析JSON对象更加容易。

最后,正如Troy所提到的,端点响应主体中会返回有用的错误消息,这有助于您诊断未来的问题。有关详细信息,请参阅this article

答案 1 :(得分:0)

我一直在玩JSON字符串,最后获得了胜利的组合。我没有利用" amount_money"正确。此外,SDK说您必须使用idempotency_key,我在某些试验期间没有这个。最后,您必须使用实际引号,而不是单个撇号:

Dim sKey As String = Guid.NewGuid().ToString()
Dim sMyJsonString As String
sMyJsonString = "{" & sQuote & "card_nonce" & sQuote & ":" & sQuote & sCardOnce & sQuote & "," & sQuote & "amount_money" & sQuote & ": {" & sQuote & "amount" & sQuote & ": " & iAmount & "," & sQuote & "currency" & sQuote & ": " & sQuote & "USD" & sQuote & "}," & sQuote & "idempotency_key" & sQuote & ":" & sQuote & sKey & sQuote & "}"
data.Append(sMyJsonString)

我最后向我的Square帐户收取了1.00美元的费用。花了很长时间才弄清楚比我希望的更好,但是所有的结果都很好。