Skip to content

Commit

Permalink
Added Support for New Usage Based Billing (#1727)
Browse files Browse the repository at this point in the history
* Fix failing event meter tests

* Add sleep after reporting event usage

* Added Support for New Usage Based Billing

* Refactor $startTime and $endTime into function parameters

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
Bark-fa and taylorotwell authored Nov 27, 2024
1 parent a0c4859 commit e1fcac1
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Billable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Laravel\Cashier\Concerns\ManagesInvoices;
use Laravel\Cashier\Concerns\ManagesPaymentMethods;
use Laravel\Cashier\Concerns\ManagesSubscriptions;
use Laravel\Cashier\Concerns\ManagesUsageBilling;
use Laravel\Cashier\Concerns\PerformsCharges;

trait Billable
Expand All @@ -16,5 +17,6 @@ trait Billable
use ManagesInvoices;
use ManagesPaymentMethods;
use ManagesSubscriptions;
use ManagesUsageBilling;
use PerformsCharges;
}
77 changes: 77 additions & 0 deletions src/Concerns/ManagesUsageBilling.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Laravel\Cashier\Concerns;

use Illuminate\Support\Collection;
use Stripe\Billing\MeterEvent;

trait ManagesUsageBilling
{
/**
* Get all of the defined billing meters.
*
* @param array $options
* @param array $requestOptions
* @return \Illuminate\Support\Collection
*/
public function meters(array $options = [], array $requestOptions = []): Collection
{
return new Collection($this->stripe()->billing->meters->all($options, $requestOptions)->data);
}

/**
* Report usage for a metered product.
*
* @param string $meter
* @param int $quantity
* @param string|null $price
* @param array $options
* @param array $requestOptions
* @return \Stripe\Billing\MeterEvent
*/
public function reportMeterEvent(
string $meter,
int $quantity = 1,
array $options = [],
array $requestOptions = []
): MeterEvent {
$this->assertCustomerExists();

return $this->stripe()->billing->meterEvents->create([
'event_name' => $meter,
'payload' => [
'stripe_customer_id' => $this->stripeId(),
'value' => $quantity,
],
...$options,
], $requestOptions);
}

/**
* Get the usage records for a meter using its ID.
*
* @param string $meterId
* @param array $options
* @param array $requestOptions
* @return \Illuminate\Support\Collection
*/
public function meterEventSummaries(string $meterId, int $startTime = 1, ?int $endTime = null, array $options = [], array $requestOptions = []): Collection
{
$this->assertCustomerExists();

if (!isset($endTime)) {
$endTime = time();
}

return new Collection($this->stripe()->billing->meters->allEventSummaries(
$meterId,
[
'customer' => $this->stripeId(),
'start_time' => $startTime,
'end_time' => $endTime,
...$options,
],
$requestOptions
)->data);
}
}

0 comments on commit e1fcac1

Please sign in to comment.