Metered billing plan

Metered billing is useful in cases where you want to charge your customers a granular amount based on their consumption of your service during the billing cycle, instead of explicitly setting quantities. For example, if you are running an email SaaS business, you can track your customersā€™ API calls, then bill them for the total number used at the end of each month.

When theĀ customerĀ subscribesĀ to the plan, Stripe Payments willĀ save theĀ subscription item (opens new window)Ā id, this idĀ is used for usage reporting. You can grab manually this id from the view order page or from the subscription model.

Stripe Payments Metered Subscription

# Reporting usage

To report the usage you can use the next examples:

// PHP EXAMPLE
use Craft;
use enupal\stripe\Stripe;

...

$currentUser = Craft::$app->getUser()->getIdentity();
$orders = Stripe::$app->subscriptions->getSubscriptionsByUser($currentUser->id);
// $orders = Stripe::$app->subscriptions->getSubscriptionsByEmail($currentUser->email);

foreach ($orders as $order) {
    $subscription = $order->getSubscription();
    if ($subscription->status == 'active' && $subscription->meteredId){
        $subscription->reportUsage(25);
    }
}