0
votes

I am using .NET to list the payments from my square account.

I am able to get a list of the payments, but to get the description field I have to go one level deeper and make http end point calls for each payment. This is time consuming.

Question: Can anyone provide me with a sample in Visual C# or Java to make batch calls for retrieving payments (using multiple payment id's)?

Your help is greatly appreciated.

Thanks,

Prashant

@Andrew - Here's what I am using, I am just not sure how to add the headers for batch payments retrieval.

string res = string.Empty;

        string qs = string.Empty;
        foreach (string s in parameters.Keys)
        {
            if (qs == string.Empty)
                qs = "?";
            else
                qs += "&";
            qs += s + "=" + parameters[s];
        }

        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(_connectUrl + "/" + command + qs); ///
        request.Proxy = null;
        request.Headers.Add("Authorization", "Bearer " + _accessToken);// ");
        request.ContentType = "application/json";

        request.Method = method; // "GET";


        try { HttpWebResponse responseGet = (HttpWebResponse)request.GetResponse(); 
        StreamReader reader = new StreamReader(responseGet.GetResponseStream());
        StringBuilder output = new StringBuilder();
        output.Append(reader.ReadToEnd());
        responseGet.Close();

        request = null;

        return output.ToString();
        }
        catch (Exception exp)
1
Maybe give a code example of what you've tried so far. - Andrew
Please check the code above. Thanks - user3705387

1 Answers

1
votes

Looks like I've been able to answer my own query.

We need to be able to send the following POST to the HTTP Endpoint

{"requests":[{"method":"GET","relative_path":"/v1/me/payments/<payment_id>","access_token":"XXXX","request_id":"1"},{"method":"GET","relative_path":"/v1/me/payments/<payment_id>","access_token":"XXXX","request_id":"2"}]}

the following code in .NET achieves the above

        //Convert the body of request into a byte array
        byte[] byteArray = Encoding.UTF8.GetBytes(body);

        //Set the length
        request.ContentLength = byteArray.Length;

        //Write the body to the request by using a datastream
        //This line never returns....
        Stream datastream = request.GetRequestStream();
        datastream.Write(byteArray, 0, byteArray.Length);
        datastream.Close();

And that's all there is to it.

Hope this helps anyone is is set out to use the batch mode.

Thanks