Alan Denke
Alan Denke

Reputation: 59

Need help getting PayPal transactions using PayPal REST API or any other method in C#

I have been researching over and over again how to get a list of transactions (to be clear, I am a PayPal Merchant and the transactions I want are payments made TO US) and I keep finding old, deprecated, and misleading information.

Backing up, my problem is this:

We use PayPal's IPN system to notify us when we have orders. This works great 99% of the time. Every once in a while, PayPal drops the ball and never sends the IPN. I have an old app I wrote in VB6 that I use to query our PayPal account and create my own, simulated IPN entries, but it's clunky and crashes sometimes. I don't like it anyway because it has to run on a PC used as a webserver and calls a bunch of PHP scripts that I personally did not write. For some reason I am unable to make changes to the program now so I need to replace it with something in C#.NET

I know you folks like code examples, so here's what I have. I was easily able to get the OAuth credentials to work, but I cannot for the life of me figure out how to see payments made to us (or refunds issued by us). This example was one of the rabbit holes I went down and it turned out that Payment.List gives me payments WE MADE, not payments made TO US. I have researched so many different functions/methods/whatever you folks like to call them, and I can't find ANYTHING that will yield a list of payments made to us where you only have to specify a date range (it doesn't help to have a function where I need to specify the transaction ID because the transaction IDs are one of the things I'm looking for.

    private void cmdPayPalTest_Click(object sender, EventArgs e)
    {
        var config = ConfigManager.Instance.GetProperties();
        config.Add("clientId","<<PayPalclientId Goes here>>");                 
        config.Add("clientSecret", "<<PayPal clientSecret Goes here>>");
        var accessToken = new OAuthTokenCredential(config).GetAccessToken();
        var apiContext = new APIContext(accessToken);
        var bob = PayPal.Api.Payment.List(apiContext, 20, "", 0);
        var sam = bob.ConvertToJson();
    }

Does anyone know the modern, non-deprecated, approved method of getting a list of transactions paid to us as a merchant? I am frustrated and have hit a wall multiple times.

Edit:

Because I couldn't find anything in the documentation for the REST API for searching transactions, I attempted to add some code to translate the Curl that PayPal provides for TransactionSearch (which I am entirely unclear whether that is REST or NVP/SOAP). I see from the answer below from Preston PHX that perhaps TransactionSearch is my only option, but the code I have written gives me an error - "StatusCode:403, ReasonPhrase: 'Forbidden'..." Does this have something to do with the 9 hours referenced in Preston PHX's answer? I have another application that is constantly running on another computer that is using TransactionSearch in some PHP scripts. I have temporarily shut that down in the event that my problem is whatever this "9 hours" refers to and I will try my new code in the morning. In the meantime, if anyone has another explanation for my 403 error, I would love to hear it. Here is my code that I am feeding the Oauth token accessToken from my code above:

      private async void CurlRequest(string MrToken)
    {
        using (var httpClient = new HttpClient())
        {
            using (var request = new HttpRequestMessage(new HttpMethod("GET"), "https://api-m.paypal.com/v1/reporting/transactions?start_date=2020-01-01T00:00:00-0700&end_date=2021-05-25T23:59:59-0700&fields=all&page_size=100&page=1"))
            {
                request.Headers.TryAddWithoutValidation("Authorization", MrToken);

                var response = await httpClient.SendAsync(request);
                MessageBox.Show(response.ToString());
            }
        }

    }

The response string displays the 403 error I spoke of. Happy to provide the rest of the error message as well if that means anything, but I think perhaps it really doesn't.

Upvotes: 0

Views: 1331

Answers (1)

Preston PHX
Preston PHX

Reputation: 30359

The PayPal-NET-SDK mention in comments is deprecated and should not be used. If you are going to make API calls for anything other than the Checkout-NET-SDK or Payouts-NET-SDK use cases, use direct REST API calls.

The best method to obtain a list of transactions is to download an activity log report in CSV format from www.paypal.com

However, if for some reason you require an API for this accounting task, the only publicly available one is the Transaction Search API: https://developer.paypal.com/docs/api/transaction-search/v1 -- which you must enable for the client ID you are using, and wait up to 9 hours (or terminate the token) if you have already requested an access token with it.

Upvotes: 2

Related Questions