Skip to content

Commit

Permalink
Add some methods for pending updates
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed May 26, 2020
1 parent 3600800 commit 19c375b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,19 @@ public function downloadAs($filename, array $data)
]);
}

/**
* Void the Stripe invoice.
*
* @param array $options
* @return $this
*/
public function void(array $options = [])
{
$this->invoice = $this->invoice->voidInvoice($options, $this->owner->stripeOptions());

return $this;
}

/**
* Get the Stripe model instance.
*
Expand Down
22 changes: 22 additions & 0 deletions src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,16 @@ public function resume()
return $this;
}

/**
* Determine if the subscription has pending updates.
*
* @return bool
*/
public function pending()
{
return $this->asStripeSubscription()->pending_update !== null;
}

/**
* Invoice the subscription outside of the regular billing cycle.
*
Expand All @@ -920,6 +930,18 @@ public function invoice(array $options = [])
}
}

/**
* Get the latest invoice for the subscription.
*
* @return \Laravel\Cashier\Invoice
*/
public function latestInvoice()
{
$stripeSubscription = $this->asStripeSubscription(['latest_invoice']);

return new Invoice($this->owner, $stripeSubscription->latest_invoice);
}

/**
* Sync the tax percentage of the user to the subscription.
*
Expand Down
1 change: 0 additions & 1 deletion tests/Feature/MultiplanSubscriptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ public function test_subscription_items_can_swap_plans()
$this->assertSame(3, $item->quantity);
}

/** @group Prorate */
public function test_subscription_item_changes_can_be_prorated()
{
$user = $this->createCustomer('subscription_item_changes_can_be_prorated');
Expand Down
9 changes: 9 additions & 0 deletions tests/Feature/PendingUpdatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ public function test_subscription_can_be_pending_if_incomplete()

// Assert subscription is active.
$this->assertTrue($subscription->active());

// Assert subscription has pending updates.
$this->assertTrue($subscription->pending());

// Void the last invoice to cancel any pending updates.
$subscription->latestInvoice()->void();

// Assert subscription has no more pending updates.
$this->assertFalse($subscription->pending());
}
}
}
8 changes: 6 additions & 2 deletions tests/Feature/SubscriptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ public function test_creating_subscription_with_explicit_trial()
$this->assertEquals(Carbon::tomorrow()->hour(3)->minute(15), $subscription->trial_ends_at);
}

/** @group Prorate */
public function test_subscription_changes_can_be_prorated()
{
$user = $this->createCustomer('subscription_changes_can_be_prorated');
Expand All @@ -489,15 +490,18 @@ public function test_subscription_changes_can_be_prorated()

// Assert that no new invoice was created because of no prorating.
$this->assertEquals($invoice->id, $user->invoices()->first()->id);
$this->assertEquals(1000, $user->upcomingInvoice()->rawTotal());

$subscription->swapAndInvoice(static::$premiumPlanId);

// Assert that a new invoice was created because of immediate invoicing.
$this->assertNotSame($invoice->id, $user->invoices()->first()->id);
$this->assertNotSame($invoice->id, ($invoice = $user->invoices()->first())->id);
$this->assertEquals(1000, $invoice->rawTotal());
$this->assertEquals(2000, $user->upcomingInvoice()->rawTotal());

$subscription->prorate()->swap(static::$planId);

// Get back from unused time on premium plan.
// Get back from unused time on premium plan on next invoice.
$this->assertEquals(0, $user->upcomingInvoice()->rawTotal());
}

Expand Down

0 comments on commit 19c375b

Please sign in to comment.