-
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.
Merge changes from stripe/stripe-ruby master and fix tests
- Loading branch information
Showing
272 changed files
with
18,540 additions
and
3,424 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
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 | ||
v1267 |
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 |
Oops, something went wrong.