Last active
November 4, 2016 12:56
-
-
Save gma/f5262c9ab550b31c8fba to your computer and use it in GitHub Desktop.
Creating fixtures for Stripe's JSON events
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
customer_events = %w( | |
charge.captured.json | |
charge.failed.json | |
charge.refunded.json | |
charge.succeeded.json | |
charge.updated.json | |
customer.card.created.json | |
customer.card.deleted.json | |
customer.card.updated.json | |
customer.created.json | |
customer.deleted.json | |
customer.discount.created.json | |
customer.discount.deleted.json | |
customer.discount.updated.json | |
customer.subscription.created.json | |
customer.subscription.deleted.json | |
customer.subscription.trial_will_end.json | |
customer.subscription.updated.json | |
customer.updated.json | |
invoice.created.json | |
invoice.payment_failed.json | |
invoice.payment_succeeded.json | |
invoice.updated.json | |
invoiceitem.created.json | |
invoiceitem.deleted.json | |
invoiceitem.updated.json | |
) | |
def get_customer_id(event) | |
object = event.data.object | |
if object.object == "customer" | |
object.id | |
else | |
object.customer | |
end | |
end | |
Dir.foreach('test/fixtures/stripe') do |filename| | |
next unless customer_events.include?(filename) | |
path = File.join(%w(test fixtures stripe), filename) | |
event = Stripe::Event.construct_from(JSON.parse(File.read(path))) | |
printf "%-45s%s\n", filename, get_customer_id(event) | |
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 would normally go in config/initializers/stripe.rb. | |
StripeEvent.configure do |events| | |
events.all StripeFixture.new | |
# This block disables StripeEvent's retrieval and verification of | |
# incoming events. It's a security feature, protecting you from | |
# attackers forging events. | |
# | |
# When sending test events to your webhook from Stripe's dashboard | |
# it causes all requests to fail with 401 (unauthorized), so while | |
# we're creating our features, we need to turn it off. | |
# | |
# REMOVE THIS BLOCK AFTER MAKING YOUR FIXTURES! | |
events.event_retriever = lambda { |params| | |
Stripe::Event.construct_from(params.deep_symbolize_keys) | |
} | |
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
# You can put this in app/models, but it doesn't matter where you put it | |
# so long as Rails can find it. | |
class StripeFixture | |
def call(stripe_event) | |
dir = File.join(Rails.root, %w(test fixtures stripe)) | |
File.open(File.join(dir, "#{stripe_event.type}.json"), 'w') do |f| | |
f.write stripe_event.to_json | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist is referenced from commit
5677e57
on Planner, whose commit message mentions that the customer ID is stored in different properties in the payload for different events.I'm about to attach a script (
stripe-fixtures-customer-ids.rb
) to this gist that inspects each fixture's event for the location of the customer id.