user4200323
user4200323

Reputation:

Stripe : list all charges with PHP

I tried to list all charges of my Stripe account to get a list of the statement_descriptor (my products) but I there is an error which says cannot find charge...

<?php
$off = 0;
$has_more = True;
$listeProducts = array();
ini_set('max_execution_time', 2000);
while($has_more){
    $req = \Stripe\Charge::all(array("limit" => 100, "starting_after" => $off));
    $res = json_decode($req->__toJSON(), true);

    foreach($res["data"] as &$prodUni){
        array_push($listeProducts, $prodUni["statement_descriptor"]);
    }

    $has_more = $res["has_more"];
    $off += 100;
}
$_SESSION["products"] = $listeProducts;

Here is the error message :

Fatal error: Uncaught exception 'Stripe\Error\InvalidRequest' with message 'No such charge: 0' in C:\wamp64\www\...\stripe-php-5.1.3\lib\ApiRequestor.php:124 from API request '...' in C:\wamp64\www\...\stripe-php-5.1.3\lib\ApiRequestor.php on line 124

I cant put "offset" because deprecated and it does same thing with ending before.

The issue is that I put 0 but charge ID is required.... I don't want to put charge ID there... I need to do like if I had offset

Upvotes: 3

Views: 1349

Answers (3)

Fabien Snauwaert
Fabien Snauwaert

Reputation: 5651

The Stripe API offers an autopagination feature:

This feature easily handles fetching large lists of resources without having to manually paginate results and perform subsequent requests.

Adapted from the docs, this will do the trick nicely (and is much easier to read):

\Stripe\Stripe::setApiKey("sk_…");

/**
 * Automatically go through _all_ charges, fetching the data in chunks/pages of
 * 100 results.
 */
$charges = \Stripe\Charge::all(["limit" => 100]);
foreach ($charges->autoPagingIterator() as $charge) {
  // Do something with $charge
}

Upvotes: 0

user4200323
user4200323

Reputation:

Fist part of answer to the question is getting the first 100 before you check for has_more.

But after that, I cannt retrieve the first charge, dont forget that when we retrieve with Stripe API the list is in the REVERSE ORDER !!! So I have to get the last or the firt ?? How can I correct my code pease ? And is this line correct ? $lastId = $res["data"]["99"]["id"];

    <?php
    $req = \Stripe\Charge::all(array("limit" => 1, "starting_after" => $lastId));
    $res = json_decode($req->__toJSON(), true);
    $lastId = $res["data"]["99"]["id"];
    $has_more = True;

    while($has_more){

        foreach($res["data"] as &$chargeUni){
            //...
        }
        
        $req = \Stripe\Charge::all(array("limit" => 10, "starting_after" => $lastId));
        $res = json_decode($req->__toJSON(), true);

        $has_more = False;//$res["has_more"];
        $lastId = $res["data"]["99"]["id"];
    }
    ?>

Upvotes: 0

Stuart
Stuart

Reputation: 6780

starting_after takes an object - ideally the last object in the previous fetched result.

See https://stripe.com/docs/api?lang=php#list_charges-starting_after

Upvotes: 2

Related Questions