Count active subscriptions with PayPal API?
Asked Answered
S

4

8

I need to use PHP to count the number of active subscriptions in my PayPal account (people subscribed to my service, not the counts of services I'm subscribed to).

Does anyone know how to do this or can point me in the right direction?

I can't find any documentation that allows you to see a list of your active subscriptions via the API, but I'm assuming that PayPal has this option and I'm just missing it...

Superfecundation answered 12/8, 2018 at 15:21 Comment(4)
It may be "List billing plans" in the paypal developer api guide: developer.paypal.com/docs/api/payments.billing-plans/v1Personate
Hmm it looks like that may be it, PayPal makes it confusing because they have so many different names like Subscriptions, Billing Plans, Recurring Payments, Billing Agreements, etc. I'll post an update back here shortly about if that works or not.Superfecundation
Exactly :( ... and not only that, SOME of those are 'depreciated' ... making it even more confusing! I dread when I have to dig into the paypal api, or when things change... we've wasted so many man hours with paypal integrations lol.Personate
Well it looks like billing-plans isn't what shows subscriptions :/ Any other ideas?Superfecundation
S
6

This option cannot be found because it's apparently not available:

https://github.com/paypal/PayPal-REST-API-issues/issues/5

Superfecundation answered 12/8, 2018 at 18:42 Comment(1)
Figures huh! Not the first time I've run across a deficiency in their APIs.Personate
P
4

Apparently you cannot get a list of subscriptions regardless the status for the time being, as you can see here https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions there is no such feature available so let's hope Paypal adds it in the future.

However if you are storing the subscription ids in a database then you can loop them and foreach subscription id you can get the details of the subscription https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_get and check its status and finally display only the ones with an active status.

Pulchritude answered 30/10, 2019 at 10:20 Comment(1)
Absolute garbage. How much turnover does PayPal have per year? And yet they don't have simple full CRUD operations for each of their entities...Pierides
C
3

There doesn't appear to be a direct way to list all of your subscriptions, but you can use the Transaction Search API to retrieve a list of your transactions (one month at a time, max 500 results per page) and grab the paypal_reference_id of each transaction object, which will be a valid subscription ID if the paypal_reference_id_type is "SUB".

Here's an example of the GET URL, which requires an Oauth token:

https://api-m.sandbox.paypal.com/v1/reporting/transactions?start_date=2022-07-01T00:00:00-0700&end_date=2022-07-31T23:59:59-0700&fields=all&page_size=500&page=1

Here's a simplified example of the response data:

{
    "transaction_details":
    [
        "transaction_info":
        {
            "paypal_reference_id": "I-ABCDEFGHIJKL",
            "paypal_reference_id_type": "SUB",
        }
    ]

}

Once you've retrieved all of your subscription IDs, you can use the Subscriptions API to retrieve their details (such as its status) via an authenticated GET request:

https://api-m.sandbox.paypal.com/v1/billing/subscriptions/I-ABCDEFGHIJKL

Here's a simplified example of the response data:

{
    "status": "ACTIVE",
}
Consensus answered 27/7, 2022 at 8:10 Comment(0)
H
1
https://www.paypal.com/billing/api/orchestrator/subscriptions?data=%7B%22total_required%22%3Atrue%2C%22sort_order%22%3A%22desc%22%2C%22page_size%22%3A%2220%22%2C%22page%22%3A%221%22%2C%22plan_ids%22%3A%22P-123456789ABCDE%22%2C%22statuses%22%3A%22ACTIVE%22%2C%22tab%22%3A%22all%22%7D

Just pass a query string like:

{"plan_ids":"P-123456789ABCDE","statuses":"ACTIVE"}

You can GET this, and specify a page size. It seems the max is 20, but you can change the page number too.

Change the plan_ids parameter that I have as "P-123456789ABCDE" to your plan ID and you'll get a JSON response. I'm sure this isn't advertised, but I found it digging through the PayPal site.

Hambletonian answered 11/11, 2021 at 13:47 Comment(1)
Is this the same auth scheme as the normal API URLs that live at https://api-m.sandbox.paypal.com and https://api-m.paypal.com? I'm getting NEEDS_LOGIN errors, even with a valid token...Pierides

© 2022 - 2024 — McMap. All rights reserved.