Skip to content

Commit

Permalink
[15.x] Fix payment behavior not being passed (#1649)
Browse files Browse the repository at this point in the history
* Add failing test for #1646

* Pass payment_behavior from Subscription to SubscriptionItem
  • Loading branch information
skeemer committed Feb 8, 2024
1 parent 88d6f6c commit 927d6d5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/Concerns/InteractsWithPaymentBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,17 @@ public function paymentBehavior()
{
return $this->paymentBehavior;
}

/**
* Set the payment behavior for any subscription updates.
*
* @param string $paymentBehavior
* @return $this
*/
public function setPaymentBehavior($paymentBehavior)
{
$this->paymentBehavior = $paymentBehavior;

return $this;
}
}
15 changes: 12 additions & 3 deletions src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,10 @@ public function incrementQuantity($count = 1, $price = null)
$this->guardAgainstIncomplete();

if ($price) {
$this->findItemOrFail($price)->setProrationBehavior($this->prorationBehavior)->incrementQuantity($count);
$this->findItemOrFail($price)
->setPaymentBehavior($this->paymentBehavior)
->setProrationBehavior($this->prorationBehavior)
->incrementQuantity($count);

return $this->refresh();
}
Expand Down Expand Up @@ -478,7 +481,10 @@ public function decrementQuantity($count = 1, $price = null)
$this->guardAgainstIncomplete();

if ($price) {
$this->findItemOrFail($price)->setProrationBehavior($this->prorationBehavior)->decrementQuantity($count);
$this->findItemOrFail($price)
->setPaymentBehavior($this->paymentBehavior)
->setProrationBehavior($this->prorationBehavior)
->decrementQuantity($count);

return $this->refresh();
}
Expand All @@ -502,7 +508,10 @@ public function updateQuantity($quantity, $price = null)
$this->guardAgainstIncomplete();

if ($price) {
$this->findItemOrFail($price)->setProrationBehavior($this->prorationBehavior)->updateQuantity($quantity);
$this->findItemOrFail($price)
->setPaymentBehavior($this->paymentBehavior)
->setProrationBehavior($this->prorationBehavior)
->updateQuantity($quantity);

return $this->refresh();
}
Expand Down
17 changes: 17 additions & 0 deletions tests/Feature/SubscriptionsWithMultiplePricesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,23 @@ public function test_subscription_item_quantity_changes_can_be_prorated()
$this->assertEquals(2000, $user->upcomingInvoice()->rawTotal());
}

public function test_subscription_item_quantity_change_can_be_thrown()
{
$user = $this->createCustomer('subscription_item_quantity_change_can_be_thrown');

$subscription = $user->newSubscription('main', [self::$priceId, self::$otherPriceId])
->quantity(1, self::$otherPriceId)
->create('pm_card_visa');

$this->assertEquals(2000, ($invoice1 = $user->invoices()->first())->rawTotal());

$user->updateDefaultPaymentMethod('pm_card_chargeCustomerFail');

$this->expectException(\Stripe\Exception\CardException::class);

$subscription->errorIfPaymentFails()->alwaysInvoice()->updateQuantity(2, self::$otherPriceId);
}

/**
* Create a subscription with a single price.
*
Expand Down

0 comments on commit 927d6d5

Please sign in to comment.