-
Notifications
You must be signed in to change notification settings - Fork 848
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added new example template and instructions on how to create more
- Loading branch information
1 parent
3df1a19
commit 002550f
Showing
3 changed files
with
94 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
## Running an example | ||
|
||
From the examples folder, run: | ||
`php your_example.php` | ||
|
||
## Adding a new example | ||
|
||
1. Clone new_example.php | ||
2. Implement your example | ||
3. Run it (as per above) | ||
4. 👍 |
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,55 @@ | ||
<?php | ||
|
||
require 'vendor/autoload.php'; // Make sure to include Composer's autoload file | ||
|
||
use Stripe\StripeClient; | ||
|
||
class MeterEventManager | ||
{ | ||
private $apiKey; | ||
private $meterEventSession; | ||
|
||
public function __construct($apiKey) | ||
{ | ||
$this->apiKey = $apiKey; | ||
$this->meterEventSession = null; | ||
} | ||
|
||
private function refreshMeterEventSession() | ||
{ | ||
// Check if session is null or expired | ||
if ( | ||
$this->meterEventSession === null || | ||
$this->meterEventSession->expires_at <= time() | ||
) { | ||
// Create a new meter event session in case the existing session expired | ||
$client = new \Stripe\StripeClient($this->apiKey); | ||
$this->meterEventSession = $client->v2->billing->meterEventSession->create(); | ||
} | ||
} | ||
|
||
public function sendMeterEvent($meterEvent) | ||
{ | ||
// Refresh the meter event session, if necessary | ||
$this->refreshMeterEventSession(); | ||
|
||
// Create a meter event with the current session's authentication token | ||
$client = new \Stripe\StripeClient($this->meterEventSession->authentication_token); | ||
$client->v2->billing->meterEventStream->create([ | ||
'events' => [$meterEvent], | ||
]); | ||
} | ||
} | ||
|
||
// Usage | ||
$apiKey = "{{API_KEY}}"; | ||
$customerId = "{{CUSTOMER_ID}}"; | ||
|
||
$manager = new MeterEventManager($apiKey); | ||
$manager->sendMeterEvent([ | ||
'event_name' => 'alpaca_ai_tokens', | ||
'payload' => [ | ||
'stripe_customer_id' => $customerId, | ||
'value' => '26', | ||
], | ||
]); |
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,28 @@ | ||
<?php | ||
|
||
// require 'vendor/autoload.php'; // Make sure to include Composer's autoload file | ||
require '../init.php'; | ||
|
||
use Stripe\StripeClient; | ||
|
||
class NewExample | ||
{ | ||
private $apiKey; | ||
|
||
public function __construct($apiKey) | ||
{ | ||
$this->apiKey = $apiKey; | ||
} | ||
|
||
public function doSomethingGreat() | ||
{ | ||
print("Hello World\n"); | ||
// $client = new \Stripe\StripeClient($this->apiKey); | ||
} | ||
} | ||
|
||
// Usage | ||
$apiKey = "{{API_KEY}}"; | ||
|
||
$example = new NewExample($apiKey); | ||
$example->doSomethingGreat(); |