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 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);
    }
}