Skip to content

Commit

Permalink
Add support for new Event Meters
Browse files Browse the repository at this point in the history
  • Loading branch information
Bark-fa committed Jul 30, 2024
1 parent 5d52c8c commit 3108555
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .idea/blade.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/php-test-framework.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 27 additions & 4 deletions src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use InvalidArgumentException;
use Laravel\Cashier\Concerns\AllowsCoupons;
use Laravel\Cashier\Concerns\HandlesPaymentFailures;
Expand Down Expand Up @@ -563,9 +564,12 @@ public function reportUsage($quantity = 1, $timestamp = null, $price = null)
* @return MeterEvent
* @throws ApiErrorException
*/
public function reportMeterUsage(string $meter, int $quantity = 1, ?string $price = null): MeterEvent
public function reportEventUsage(string $meter, int $quantity = 1, ?string $price = null): MeterEvent
{
return $this->findItemOrFail($price ?? $this->stripe_price)->reportMeterUsage($meter, $quantity);
if (! $price) {
$this->guardAgainstMultiplePrices();
}
return $this->findItemOrFail($price ?? $this->stripe_price)->reportEventUsage($meter, $quantity);
}

/**
Expand All @@ -591,9 +595,9 @@ public function reportUsageFor($price, $quantity = 1, $timestamp = null)
* @return MeterEvent
* @throws ApiErrorException
*/
public function reportUsageForMeter(string $meter, string $price, int $quantity = 1): MeterEvent
public function reportUsageForEvent(string $eventName, string $price, int $quantity = 1): MeterEvent
{
return $this->reportMeterUsage($meter, $quantity, $price);
return $this->reportEventUsage($eventName, $quantity, $price);
}

/**
Expand All @@ -612,6 +616,25 @@ public function usageRecords(array $options = [], $price = null)
return $this->findItemOrFail($price ?? $this->stripe_price)->usageRecords($options);
}


/**
* Get the usage records for a meter using its ID (not name).
*
* @param string $meterId
* @param array $options
* @param null $price
* @return Collection
* @throws ApiErrorException
*/
public function meterUsageRecords(string $meterId, array $options = [], $price = null): Collection
{
if (! $price) {
$this->guardAgainstMultiplePrices();
}

return $this->findItemOrFail($price ?? $this->stripe_price)->eventUsageRecord($meterId, $options);
}

/**
* Get the usage records for a specific price of a metered product.
*
Expand Down
43 changes: 42 additions & 1 deletion src/SubscriptionItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function reportUsage($quantity = 1, $timestamp = null)
* @return MeterEvent
* @throws ApiErrorException
*/
public function reportMeterUsage(string $meter, int $quantity = 1): MeterEvent
public function reportEventUsage(string $meter, int $quantity = 1): MeterEvent
{
return $this->subscription->owner->stripe()->billing->meterEvents->create([
'event_name' => $meter,
Expand All @@ -255,6 +255,47 @@ public function usageRecords($options = [])
)->data);
}

/**
* List all the metered prices for the subscription item.
* @see https://stripe.com/docs/api/prices/list
*
* @param array|null $params
* @param array|null $opts
*
* @return Collection
* @throws ApiErrorException
*/
public function listMeters(?array $params = [], ?array $opts = []): Collection
{
return new Collection($this->subscription->owner->stripe()->billing->meters->all($params, $opts)->data);
}

/**
* @param string $meterId
* @param array|null $params
* @param array|null $opts
* @return Collection
* @throws ApiErrorException
*/
public function eventUsageRecord(string $meterId, ?array $params = [], ?array $opts = []): Collection
{
$startTime = $params['start_time'] ?? $this->subscription->created_at->timestamp;
$endTime = $params['end_time'] ?? time();

unset($params['start_time'], $params['end_time']);

$params = [
'customer' => $this->subscription->owner->stripeId(),
'start_time' => $startTime,
'end_time' => $endTime,
...$params
];

return new Collection($this->subscription->owner->stripe()->billing->meters->allEventSummaries(
$meterId, $params, $opts
)->data);
}

/**
* Update the underlying Stripe subscription item information for the model.
*
Expand Down
Loading

0 comments on commit 3108555

Please sign in to comment.