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

[12.x] Metered billing #1048

Merged
merged 49 commits into from
Feb 8, 2021
Merged

[12.x] Metered billing #1048

merged 49 commits into from
Feb 8, 2021

Conversation

driesvints
Copy link
Member

@driesvints driesvints commented Feb 2, 2021

This PR adds support for Metered Billing. Thanks to @benjamindoe

Closes #555

Usage

Subscribe user to metered plan:

$subscription = $user->newSubscription('main', [])
    ->meteredPlan('pro-plan')
    ->create('pm_card_visa');

Reporting usage from the subscription:

$user->subscription('default')->reportUsage(); // defaults to quantity of 1
$user->subscription('default')->reportUsage(10);
$user->subscription('default')->reportUsageFor('pro-plan', 10);

Reporting usage from a subscription item:

$item = $$user->subscription('default')->findItemOrFail('pro-plan');

$item->reportUsage();
$item->reportUsage(10);

Stripe gives you the ability to update a usage record at a particular timestamp. The reportUsage & reportUsageFor methods accept a Carbon object to specify the timestamp:

$subscription->reportUsage(100, now()->subHour());
$subscription->reportUsageFor('pro-plan', 100, now()->subHour());

// Or report for a specific subscription item...
$item->reportUsage(100, now()->subHour());

Todos

  • Reporting usage
  • Fetching usage records
  • Swapping behavior
  • Cancelling subscriptions
  • Stripe Checkout integration
  • Tests

Related Stripe Documentation

@driesvints
Copy link
Member Author

@benjamindoe I'm finally done with the PR! Can you give it a review?

@driesvints driesvints marked this pull request as ready for review February 4, 2021 21:18
$item['deleted'] = true;

if ($plan->usage_type == 'metered') {
$item['clear_usage'] = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clearing usage for a metered plan is required when the plan is deleted. Stripe has noted that this param will probably be removed in the future and the behavior would be the default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for Metered billing and creating UsageRecord events
3 participants