Created
October 14, 2016 21:10
-
-
Save iloveitaly/ff95a97a4d425ef0d6211d27e146d623 to your computer and use it in GitHub Desktop.
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
# Mike Bianco <mike@suitesync.io> | |
# Description: Example of using Stripe webhooks to copy a parent subscription | |
# metadata field to a Stripe-generated charge's metadata | |
require 'stripe' | |
require 'sinatra' | |
Stripe.api_key = "sk_test_" | |
# Listen to Stripe events using webhooks | |
# | |
# - https://stripe.com/docs/webhooks | |
# - https://dashboard.stripe.com/account/webhooks | |
# This example uses sinatra. You can easily use rails. | |
# post '/stripe/webhooks' => 'stripe_webhooks#consume' | |
post "/stripe/webhooks" do | |
event_json = JSON.parse(request.body.read) | |
# https://stripe.com/docs/api#list_events | |
# charge.pending is fired whenever a charge is created | |
if event_json['type'] == 'charge.pending' | |
stripe_charge = Stripe::Charge.retrieve(event_json['id']) | |
if stripe_charge.invoice | |
stripe_invoice = Stripe::Invoice.retrieve(stripe_charge.invoice) | |
stripe_subscription = Stripe::Subscription.retrieve(stripe_invoice.subscription) | |
stripe_charge.metadata['order_number'] = stripe_subscription.metadata['order_number'] | |
stripe_charge.save | |
end | |
end | |
status 200 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment