stripe / stripe-php

PHP library for the Stripe API.
https://stripe.com
MIT License
3.75k stars 848 forks source link

AutoPagingIterator does not work with an invoice that has more than 10 line items that use tax rates #1422

Open padre opened 1 year ago

padre commented 1 year ago

Describe the bug

https://github.com/stripe/stripe-php/issues/1400#issuecomment-1344272956

To Reproduce

$stripe = new \Stripe\StripeClient('stripe-secret');

$invoice = $stripe->invoices->retrieve('in_xxx', [
    'expand' => ['lines.data.tax_amounts.tax_rate'],
]);

foreach ($invoice->lines->autoPagingIterator() as $line) {
    foreach ($line->tax_amounts as $tax_amount) {
        $tax_rate = $tax_amount->tax_rate;
        echo $tax_rate->display_name . ' ' . $tax_rate->percentage . '%' . PHP_EOL;
    }
}

Expected behavior

All lines on an invoice should be expanded, but it'll fail with the following error:

Attempt to read property "display_name" on string

Code snippets

No response

OS

Ubuntu / Centos

PHP version

PHP 8.1

Library version

stripe/stripe-php v10.3.0

API version

2022-11-15

Additional context

No response

pakrym-stripe commented 1 year ago

I'm sorry for the confusion, the service fix didn't cover all the cases we assumed. I'll work on getting this resolved.

driesvints commented 1 year ago

@pakrym-stripe can't we just merge my PR in the meantime?

pakrym-stripe commented 1 year ago

I'm sorry @driesvints, while merging your PR will unblock this issue, the ability to change filter parameters is not a feature we'd like to support long-term on all collections.

Is it possible for you to use manual pagination as a temporary workaround?

driesvints commented 1 year ago

No worries. Unfortunately not as we just need to get all line items to display on the custom invoice.

pakrym-stripe commented 1 year ago

You can get all items by manually iterating pages:


$invoice = $stripe->invoices->retrieve('in_....', [
    'expand' => ['lines.data.price.product'],
]);

$page = $invoice->lines;
while (true) {
    foreach ($page as $item) {
        echo($item["price"]);
    }
    $page = $page->nextPage([
        'expand' => ['data.price.product'],
    ]);

    if ($page->isEmpty()) {
        break;
    }
}
driesvints commented 1 year ago

@pakrym-stripe thank you for that example. I've sent this in for Cashier: https://github.com/laravel/cashier-stripe/pull/1503