GoCardless API - List Subscriptions

432 views Asked by At

I am using the GoCardless Documentation here to try list all subscriptions for a customer.

I have followed the instructions as you can see below, however nothing at all is displaying when I run this script - does anyone know what I may have done wrong?

require 'vendor/autoload.php';    
$client = new \GoCardlessPro\Client(array(
  'access_token' => 'XXXXXx',
  'environment'  => \GoCardlessPro\Environment::LIVE
));

     $client->subscriptions()->list([
  "params" => ["customer" => "CU000R3B8512345"]
]);
2

There are 2 answers

5
Martin Bean On BEST ANSWER

Calling a method on its own doesn’t do anything. It’ll execute the given method, but it’s not going to print anything to your browser screen on its own.

As RiggsFolly says (and is documented in GoCardless’s API documentation), calling $client->subscriptions()->list() will return a cursor-paginated response object. So you need to do something with this result. What that is, I don’t know as it’s your application’s business logic and only you know that.

<?php

use GoCardlessPro\Client;
use GoCardlessPro\Environment;

require '../vendor/autoload.php';

$client = new Client(array(
    'access_token' => 'your-access-token-here',
    'environment' => Environment::SANDBOX,
));

// Assign results to a $results variable
$results = $client->subscriptions()->list([
    'params' => ['customer' => 'CU000R3B8512345'],
]);

foreach ($results->records as $record) {
    // $record is a variable holding an individual subscription record
}
0
karrtojal On

Pagination with Gocardless:

function AllCustomers($client)
{
    $list = $client->customers()->list(['params'=>['limit'=>100]]);
    $after = $list->after;
    // DO THINGS
    print_r($customers);

    while ($after!="")
    {
        $customers = $list->records;
        // DO THINGS
        print_r($customers);
        
        // NEXT
        $list = $client->customers()->list(['params'=>['after'=>$after,'limit'=>100]]);
        $after = $list->after;
    }
}