获得422状态code错误投递坊? [英] Getting 422 Status Code Error When Posting to Square?

查看:745
本文介绍了获得422状态code错误投递坊?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想这个例子转换为Vb.Net:

https://docs.connect.sq​​uareup.com/articles/processing - 付款 - 休息/

我能顺利拿到CardOnce ID用户输入信用卡信息后。我可以成功地做一个读广场,让我的位置ID。不过,我被困在最后一步时,我提供CardOnce ID,位置ID和金额为POST,我不断收到回一个错误:远程服务器返回错误:(422)状态code 422。

我的code是下面列出和错误occurrs:

错误发生在这个叫做:

  =响应DirectCast(request.GetResponse(),HttpWebResponse)

全部code这里:

 公共功能IsSuccessProcess(BYVAL sLocationId作为字符串,BYVAL sCardOnce作为字符串,BYVAL iAmount作为整数)
    昏暗的要求,因为HttpWebRequest的
    昏暗的响应作为HttpWebResponse =无
    昏暗的阅读器的StreamReader
    昏暗的地址作为乌里
    昏暗的数据作为的StringBuilder
    昏暗byteData()为字节
    昏暗postStream作为流=无    地址=新的URI(https://connect.sq​​uareup.com/v2/locations/&放大器; sLocationId&安培;/交易)    创建Web请求
    请求= DirectCast(WebRequest.Create(地址),HttpWebRequest的)    将类型设置为POST
    request.Method =POST
    request.ContentType =应用/ JSON
    request.Accept =应用/ JSON
    request.Headers.Add(授权,承载&放大器;的accessToken)    数据=新的StringBuilder()
    昏暗sQuote作为字符串=
    sQuote ='
    data.Append({&放大器; sQuote&放大器;card_nonce与& sQuote&放大器;:&放大器; sCardOnce&放大器;,&放大器; sQuote&放大器;amount_money与& sQuote&放大器;:{&放大器; sQuote&安培;量和功放; sQuote&安培;:&放大器; iAmount&安培;,&放大器; sQuote&安培;货币和放大器; sQuote&安培;:&放大器; sQuote&安培;USD&放大器; sQuote&放;})    昏暗didempotency_key为双= Microsoft.VisualBasic.Timer
    昏暗idempotency_key作为整数= CINT(didempotency_key)
    data.Append('idempotency_key':与& idempotency_key)
    创建数据的字节数组,我们想送
    byteData = UTF8Encoding.UTF8.GetBytes(data.ToString())
    设置内容长度的请求头
    request.ContentLength = byteData.Length    写数据
    尝试
        postStream = request.GetRequestStream()
        postStream.Write(byteData,0,byteData.Length)
    最后
        如果没有postStream是Nothing然后postStream.Close()
    结束Try    尝试
        '获取响应
        响应= DirectCast(request.GetResponse(),HttpWebResponse)        '获取响应流中插入读卡器
        读者=新的StreamReader(response.GetResponseStream())        控制台应用程序的输出
        Console.WriteLine(reader.ReadToEnd())
    最后
        如果不回应就是没有那么response.Close()
    结束Try
    返回TRUE结束功能


解决方案

我不停地用JSON字符串玩弄终于拿到了最佳组合。我没有使用amount_money正确。另外,SDK说你必须利用idempotency_key我在一些试验没有。最后,你必须使用实际的报价,而不是一个单引号:

 昏暗SKEY的String = Guid.NewGuid()的ToString()
昏暗sMyJsonString作为字符串
sMyJsonString ={&放大器; sQuote&安培; card_nonce与& sQuote&安培; :&放大器; sQuote&安培; sCardOnce&安培; sQuote&安培; 与& sQuote&安培; amount_money与& sQuote&安培; :{&放大器; sQuote&安培; 量和功放; sQuote&安培; :&放大器; iAmount&安培; 与& sQuote&安培; 货币和放大器; sQuote&安培; :&放大器; sQuote&安培; USD&放大器; sQuote&安培; },&放大器; sQuote&安培; idempotency_key与& sQuote&安培; :&放大器; sQuote&安培; SKEY&安培; sQuote&安培; }
data.Append(sMyJsonString)

我终于收取$ 1.00到自己,在我的帐户广场出现了。这花了很多时间来找出比我所希望的,但所有的好就一切都好,

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

解决方案

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.

这篇关于获得422状态code错误投递坊?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆