1

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"]
]);
Designer
  • 477
  • 2
  • 12

2 Answers2

3

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
}
Martin Bean
  • 38,379
  • 25
  • 128
  • 201
  • @Designer Did you put anything inside the foreach loop other than the comment placed by Martin? – RiggsFolly Mar 09 '20 at 16:06
  • It is showing `Catchable fatal error: Object of class GoCardlessPro\Core\ListResponse could not be converted to string i` – Designer Mar 09 '20 at 16:07
  • 1
    @Designer - `echo"
    $record
    ";` you will see the result!
    – Vogal Mar 09 '20 at 16:11
  • Try a `print_r($record);` that will show you what the object looks like so you can then use the actual properties – RiggsFolly Mar 09 '20 at 16:12
  • 1
    @Designer A couple of times it’s been said now that calling that method will _return_ an object. **It will not print anything to the screen**. _You_ have to do something with the object that’s returned! – Martin Bean Mar 09 '20 at 16:16
0

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;
    }
}
karrtojal
  • 796
  • 7
  • 16