-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for APIs in the new API version 2024-09-30.acacia (#1458)
- Loading branch information
1 parent
07ee576
commit 39d8736
Showing
256 changed files
with
17,774 additions
and
3,166 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
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 @@ | ||
v1255 | ||
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
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: | ||
`RUBYLIB=../lib ruby your_example.rb` | ||
|
||
## Adding a new example | ||
|
||
1. Clone new_example.rb | ||
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,47 @@ | ||
# frozen_string_literal: true | ||
|
||
require "stripe" | ||
require "date" | ||
|
||
class MeterEventManager | ||
attr_accessor :api_key, :meter_event_session | ||
|
||
def initialize(api_key) | ||
@api_key = api_key | ||
@meter_event_session = nil | ||
end | ||
|
||
def refresh_meter_event_session | ||
return unless @meter_event_session.nil? || DateTime.parse(@meter_event_session.expires_at) <= DateTime.now | ||
|
||
# Create a new meter event session in case the existing session expired | ||
client = Stripe::StripeClient.new(api_key) | ||
@meter_event_session = client.v2.billing.meter_event_session.create | ||
end | ||
|
||
def send_meter_event(meter_event) | ||
# Refresh the meter event session if necessary | ||
refresh_meter_event_session | ||
|
||
# Create a meter event with the current session's authentication token | ||
client = Stripe::StripeClient.new(meter_event_session.authentication_token) | ||
client.v2.billing.meter_event_stream.create( | ||
events: [meter_event] | ||
) | ||
end | ||
end | ||
|
||
# Send meter events | ||
api_key = "{{API_KEY}}" | ||
customer_id = "{{CUSTOMER_ID}}" | ||
|
||
manager = MeterEventManager.new(api_key) | ||
manager.send_meter_event( | ||
{ | ||
event_name: "alpaca_ai_tokens", | ||
payload: { | ||
"stripe_customer_id" => customer_id, | ||
"value" => "25", | ||
}, | ||
} | ||
) |
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,24 @@ | ||
# frozen_string_literal: true | ||
|
||
require "stripe" | ||
require "date" | ||
|
||
class NewExample | ||
attr_accessor :api_key | ||
|
||
def initialize(api_key) | ||
@api_key = api_key | ||
end | ||
|
||
def do_something_great | ||
puts "Hello World" | ||
# client = Stripe::StripeClient.new(api_key) | ||
# client.v1 | ||
end | ||
end | ||
|
||
# Send meter events | ||
api_key = "{{API_KEY}}" | ||
|
||
example = NewExample.new(api_key) | ||
example.do_something_great |
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 @@ | ||
# frozen_string_literal: true | ||
# typed: false | ||
|
||
require "stripe" | ||
require "sinatra" | ||
|
||
api_key = ENV.fetch("STRIPE_API_KEY", nil) | ||
# Retrieve the webhook secret from the environment variable | ||
webhook_secret = ENV.fetch("WEBHOOK_SECRET", nil) | ||
|
||
client = Stripe::StripeClient.new(api_key) | ||
|
||
post "/webhook" do | ||
webhook_body = request.body.read | ||
sig_header = request.env["HTTP_STRIPE_SIGNATURE"] | ||
thin_event = client.parse_thin_event(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.instance_of? Stripe::V1BillingMeterErrorReportTriggeredEvent | ||
meter = event.fetch_related_object | ||
meter_id = meter.id | ||
puts "Success!", meter_id | ||
end | ||
|
||
# Record the failures and alert your team | ||
status 200 | ||
end |
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.