Getting 422 Status Code Error When Posting to Squa

2019-09-09 21:12发布

Please help me finish this last step so I can be done with this project :)

I'm trying to convert this example to Vb.Net:

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

I can successfully get a CardOnce ID after the user enters credit card information. I can successfully do a READ from Square to get my location ID. However, I'm stuck on the last step when I provide the CardOnce ID, Location ID, and amount to POST, I keep getting back an error: "The remote server returned an error: (422) status code 422."

My Code is listed below and the error occurrs:

Error Occurs when this is called:

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

Full Code Here:

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条回答
我只想做你的唯一
2楼-- · 2019-09-09 21:28

I kept playing around with the JSON string and FINALLY got the winning combination. I wasn't utilizing "amount_money" correctly. Also, the SDK says you HAVE to utilize idempotency_key which I wasn't during some trials. Finally, you HAVE to use actual quotes, not a single apostrophe:

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)

I finally charged $1.00 to myself that showed up in my Square account. That took a lot longer to figure out than I had hoped but All's Well that Ends Well.

查看更多
女痞
3楼-- · 2019-09-09 21:48

This error is likely occurring because you are using single quotes in your JSON body instead of double quotes. Try replacing those as a first step.

Note that you might find it easier to generate and parse JSON objects with the help of a third-party library such as Json.NET.

Finally, as Troy mentioned, helpful error messages are returned in endpoint response bodies, which should help you diagnose future issues. See this article for more information.

查看更多
登录 后发表回答