Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stripe notice about undefined property (quantity) with metered prices #1254

Closed
adamthehutt opened this issue Sep 24, 2021 · 15 comments · Fixed by #1255
Closed

Stripe notice about undefined property (quantity) with metered prices #1254

adamthehutt opened this issue Sep 24, 2021 · 15 comments · Fixed by #1255
Labels

Comments

@adamthehutt
Copy link

  • Cashier Version: 13.5.1
  • Laravel Version: 8.61.0
  • PHP Version: 8.0.10
  • Database Driver & Version: MySQL 8

Description:

When adding a metered price to an existing subscription, Stripe is emitting an error notice about an undefined property (quantity) on the SubscriptionItem object.

Here's the problem line.

Steps To Reproduce:

Create a subscription, then:

$user->subscription()->addMeteredPrice($priceId);

Null is (correctly) being passed as the quantity to the addPrice method, but then when Cashier tries to create a record of the subscription item it references the Stripe object's quantity property, which is undefined.

I believe a fix is as simple as:

    'quantity' => $stripeSubscriptionItem->quantity ?? null

I can produce a PR if you'd like.

@mfauveau
Copy link
Contributor

mfauveau commented Sep 26, 2021

I was coming here to report the same thing. :)

I see it show up in my unit tests as Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity. This leads me to believe it's not a bug in Cashier (also because if you force the SubscriptionBuilder to pass a null quantity, the API call will fail since Stripe doesn't expect quantity to be set for metered pricing).

@driesvints
Copy link
Member

Thanks all. @mfauveau I think this is in fact a bug in Cashier. I've sent in that fix here: #1255

@adamthehutt always feel free to send in PR's with those fixes. Saves a bit of time ;)

@mfauveau
Copy link
Contributor

@driesvints maybe I'm missing something but for me, the notice is triggered by https://github.com/laravel/cashier-stripe/blob/13.x/src/SubscriptionBuilder.php#L251

This is why I was saying this might be a Stripe bug. https://stripe.com/docs/api/subscriptions/create describes quantity as optional but:

  • It's throwing the warning even though quantity is not sent by the payload
  • If I force quantity to be sent as null, the API call fails

@driesvints
Copy link
Member

@mfauveau it's thrown in

'quantity' => $stripeSubscriptionItem->quantity,
and not in the subscription builder.

@mfauveau
Copy link
Contributor

mfauveau commented Sep 27, 2021

@driesvints I think it's a different bug then... This line is not called in my case (I've tried).

This is what my code does:

$paymentMethod = $this->defaultPaymentMethod()->id;

$subscription = $this->newSubscription('default');

// Get the most recent metered prices set
$prices = Pricing::allMeteredPrices();

// Add each price to the subscription
foreach ($prices as $price) {
    $subscription->meteredPrice($price);
}

$subscription->create($paymentMethod, [], [
    'cancel_at_period_end' => false,
    'automatic_tax' => ['enabled' => true],
    'billing_thresholds' => ['amount_gte' => config('billing.billing_thresholds')]
]);

@mfauveau
Copy link
Contributor

@taylorotwell @driesvints note that @adamthehutt is updating an existing subscription. My code creates a subscription from scratch with the subscription builder. In that particular situation, the notice will still show up.

@driesvints
Copy link
Member

I'm sorry @mfauveau but we have tests for the exact code you're using. If you really believe there's a bug please provide a failing test for this library.

@mfauveau
Copy link
Contributor

mfauveau commented Sep 27, 2021

I get that @driesvints but look:

vagrant@homestead:~/Development/cashier-stripe$ phpunit --filter MeteredBillingTest
PHPUnit 9.5.10 by Sebastian Bergmann and contributors.

Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity
Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity
..Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity
Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity
.Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity
Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity
Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity
Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity
.Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity
Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity
Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity
Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity
Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity
Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity
Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity
.Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity
Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity
.Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity
Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity
.Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity
Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity
.                                                            8 / 8 (100%)

Time: 00:54.945, Memory: 30.00 MB

OK (8 tests, 40 assertions)

@driesvints
Copy link
Member

Here's for example a test that covers the behavior: https://github.com/laravel/cashier-stripe/blob/13.x/tests/Feature/MeteredBillingTest.php#L208-L210

I'm wondering what Pricing::allMeteredPrices(); returns? Maybe the culprit lies there?

@mfauveau
Copy link
Contributor

Here's for example a test that covers the behavior: https://github.com/laravel/cashier-stripe/blob/13.x/tests/Feature/MeteredBillingTest.php#L208-L210

I'm wondering what Pricing::allMeteredPrices(); returns? Maybe the culprit lies there?

I'm guessing you posted that before seeing my previous comment?

It returns an array of prices:

array:4 [
  0 => "price_1JMejbIAQWEAWAnbTKgId72x"
  1 => "price_1JMelEIAQWEAWAnbmgXEHh0q"
  2 => "price_1JMeneIAQWEAWAnbI070LpsM"
  3 => "price_1JMen8IAQWEAWAnbwtDxsuGG"
]

@driesvints
Copy link
Member

Hmm no. I'm very sorry @mfauveau but I just don't see an issue. We have tests which very the code is working. If you really think there's a bug please provide a PR.

@mfauveau
Copy link
Contributor

This #1254 (comment) is a fresh clone of a cashier running the MeteredBillingTest and showing the error @driesvints. Not sure what you don't see here. But that's all good. It's just a notice after all.

@driesvints
Copy link
Member

@mfauveau we don't get those notices in our test suite: https://github.com/laravel/cashier-stripe/runs/3720820913

No worries, I'm also just a bit confused here 😅

@mfauveau
Copy link
Contributor

mfauveau commented Sep 27, 2021

https://github.com/laravel/cashier-stripe/runs/3720820913

I had a feeling you were referring to the test suite.

It's confusing indeed. I wonder why a fresh clone is throwing that notice but not the test suite? I'm guessing the test suite doesn't silence notices.

Upon a quick inspection, it looks like the test suite pulls a different version of stripe-php (v7.97.0 vs 7.39). I'm gonna try to upgrade and run the test again.

@mfauveau
Copy link
Contributor

Never mind, still throwing notices with v7.97.0 🤷‍♂️

mfauveau added a commit to mfauveau/cashier-stripe that referenced this issue Oct 1, 2021
This fixes a Stripe Notice (Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity) when using the SubscriptionBuilder with metered prices.
See my comments on laravel#1254.
taylorotwell pushed a commit that referenced this issue Oct 4, 2021
This fixes a Stripe Notice (Stripe Notice: Undefined property of Stripe\SubscriptionItem instance: quantity) when using the SubscriptionBuilder with metered prices.
See my comments on #1254.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants