-
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.
Merge changes from stripe/stripe-php master
- Loading branch information
Showing
75 changed files
with
2,636 additions
and
542 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 |
---|---|---|
@@ -1 +1 @@ | ||
v1267 | ||
v1268 |
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,53 @@ | ||
<?php | ||
|
||
require 'vendor/autoload.php'; // Make sure to include Composer's autoload file | ||
|
||
class meter_event_stream | ||
{ | ||
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 ( | ||
null === $this->meterEventSession | ||
|| $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,26 @@ | ||
<?php | ||
|
||
// require 'vendor/autoload.php'; // Make sure to include Composer's autoload file | ||
require '../init.php'; | ||
|
||
class new_example | ||
{ | ||
private $apiKey; | ||
|
||
public function __construct($apiKey) | ||
{ | ||
$this->apiKey = $apiKey; | ||
} | ||
|
||
public function doSomethingGreat() | ||
{ | ||
echo "Hello World\n"; | ||
// $client = new \Stripe\StripeClient($this->apiKey); | ||
} | ||
} | ||
|
||
// Usage | ||
$apiKey = '{{API_KEY}}'; | ||
|
||
$example = new NewExample($apiKey); | ||
$example->doSomethingGreat(); |
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,34 @@ | ||
<?php | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
$api_key = getenv('STRIPE_API_KEY'); | ||
$webhook_secret = getenv('WEBHOOK_SECRET'); | ||
|
||
$app = new \Slim\App(); | ||
$client = new \Stripe\StripeClient($api_key); | ||
|
||
$app->post('/webhook', function ($request, $response) use ($client, $webhook_secret) { | ||
$webhook_body = $request->getBody()->getContents(); | ||
$sig_header = $request->getHeaderLine('Stripe-Signature'); | ||
|
||
try { | ||
$thin_event = $client->parseThinEvent($webhook_body, $sig_header, $webhook_secret); | ||
|
||
// Fetch the event data to understand the failure | ||
$event = $client->v2->core->events->retrieve($thin_event->id); | ||
if ($event instanceof \Stripe\Events\V1BillingMeterErrorReportTriggeredEvent) { | ||
$meter = $event->fetchRelatedObject(); | ||
$meter_id = $meter->id; | ||
|
||
// Record the failures and alert your team | ||
// Add your logic here | ||
} | ||
|
||
return $response->withStatus(200); | ||
} catch (\Exception $e) { | ||
return $response->withStatus(400)->withJson(['error' => $e->getMessage()]); | ||
} | ||
}); | ||
|
||
$app->run(); |
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
Oops, something went wrong.