Skip to content

Commit

Permalink
added new example template and instructions on how to create more
Browse files Browse the repository at this point in the history
  • Loading branch information
jar-stripe committed Sep 25, 2024
1 parent 3df1a19 commit 002550f
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/README.md
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. 👍
55 changes: 55 additions & 0 deletions examples/meter_event_stream.php
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',
],
]);
28 changes: 28 additions & 0 deletions examples/new_example.php
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();

0 comments on commit 002550f

Please sign in to comment.