-
Notifications
You must be signed in to change notification settings - Fork 681
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Support for New Usage Based Billing (#1727)
* 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
1 parent
a0c4859
commit e1fcac1
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |