Skip to content

Commit

Permalink
Throw payment exception for quantity methods (#1155)
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints authored May 17, 2021
1 parent 683acb2 commit 3e2e806
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 23 deletions.
24 changes: 15 additions & 9 deletions src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,17 +475,23 @@ public function updateQuantity($quantity, $plan = null)

$this->guardAgainstMultiplePlans();

$stripeSubscription = $this->asStripeSubscription();

$stripeSubscription->quantity = $quantity;
$stripeSubscription->payment_behavior = $this->paymentBehavior();
$stripeSubscription->proration_behavior = $this->prorateBehavior();

$stripeSubscription->save();
$stripeSubscription = $this->updateStripeSubscription([
'payment_behavior' => $this->paymentBehavior(),
'proration_behavior' => $this->prorateBehavior(),
'quantity' => $quantity,
'expand' => ['latest_invoice.payment_intent'],
]);

$this->quantity = $quantity;
$this->fill([
'stripe_status' => $stripeSubscription->status,
'quantity' => $quantity,
])->save();

$this->save();
if ($this->hasIncompletePayment()) {
(new Payment(
$stripeSubscription->latest_invoice->payment_intent
))->validate();
}

return $this;
}
Expand Down
32 changes: 18 additions & 14 deletions src/SubscriptionItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,28 @@ public function updateQuantity($quantity)
{
$this->subscription->guardAgainstIncomplete();

$stripeSubscriptionItem = $this->asStripeSubscriptionItem();

$stripeSubscriptionItem->quantity = $quantity;

$stripeSubscriptionItem->payment_behavior = $this->paymentBehavior();

$stripeSubscriptionItem->proration_behavior = $this->prorateBehavior();

$stripeSubscriptionItem->save();

$this->quantity = $quantity;
$stripeSubscriptionItem = $this->updateStripeSubscriptionItem([
'payment_behavior' => $this->paymentBehavior(),
'proration_behavior' => $this->prorateBehavior(),
'quantity' => $quantity,
'expand' => ['subscription.latest_invoice.payment_intent'],
]);

$this->save();
$this->fill([
'quantity' => $quantity,
])->save();

if ($this->subscription->hasSinglePlan()) {
$this->subscription->quantity = $quantity;
$this->subscription->fill([
'stripe_status' => $stripeSubscriptionItem->subscription->status,
'quantity' => $quantity,
])->save();
}

$this->subscription->save();
if ($this->subscription->hasIncompletePayment()) {
(new Payment(
$stripeSubscriptionItem->subscription->latest_invoice->payment_intent
))->validate();
}

return $this;
Expand Down
56 changes: 56 additions & 0 deletions tests/Feature/SubscriptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,62 @@ public function test_swapping_subscription_and_adopting_new_quantity()
$this->assertSame(3, $subscription->asStripeSubscription()->quantity);
}

public function test_declined_card_during_new_quantity()
{
$user = $this->createCustomer('declined_card_during_new_quantity');

$subscription = $user->newSubscription('main', static::$planId)
->quantity(5)
->create('pm_card_visa');

// Set a faulty card as the customer's default payment method.
$user->updateDefaultPaymentMethod('pm_card_chargeCustomerFail');

try {
// Attempt to increment quantity and pay with a faulty card.
$subscription = $subscription->incrementAndInvoice(3);

$this->fail('Expected exception '.IncompletePayment::class.' was not thrown.');
} catch (IncompletePayment $e) {
// Assert that the payment needs a valid payment method.
$this->assertTrue($e->payment->requiresPaymentMethod());

// Assert that the quantity was updated anyway.
$this->assertEquals(8, $subscription->refresh()->quantity);

// Assert subscription is past due.
$this->assertTrue($subscription->pastDue());
}
}

public function test_declined_card_during_new_quantity_for_specific_plan()
{
$user = $this->createCustomer('declined_card_during_new_quantity_for_specific_plan');

$subscription = $user->newSubscription('main', static::$planId)
->quantity(5, static::$planId)
->create('pm_card_visa');

// Set a faulty card as the customer's default payment method.
$user->updateDefaultPaymentMethod('pm_card_chargeCustomerFail');

try {
// Attempt to increment quantity and pay with a faulty card.
$subscription = $subscription->incrementAndInvoice(3);

$this->fail('Expected exception '.IncompletePayment::class.' was not thrown.');
} catch (IncompletePayment $e) {
// Assert that the payment needs a valid payment method.
$this->assertTrue($e->payment->requiresPaymentMethod());

// Assert that the quantity was updated anyway.
$this->assertEquals(8, $subscription->refresh()->quantity);

// Assert subscription is past due.
$this->assertTrue($subscription->pastDue());
}
}

public function test_declined_card_during_subscribing_results_in_an_exception()
{
$user = $this->createCustomer('declined_card_during_subscribing_results_in_an_exception');
Expand Down

0 comments on commit 3e2e806

Please sign in to comment.