# Using Sinatra.require'sinatra'require'stripe'
set :port,4242# Set your secret key. Remember to switch to your live secret key in production.# See your keys here: https://dashboard.stripe.com/apikeysStripe.api_key =
'sk_test_BQokikJOvBiI2HlWgH4olfQ2'
# If you are testing your webhook locally with the Stripe CLI you# can find the endpoint's secret by running `stripe listen`# Otherwise, find your endpoint's secret in your webhook settings in# the Developer Dashboard
endpoint_secret ='whsec_...'
post '/webhook'do
payload = request.body.read
sig_header = request.env['HTTP_STRIPE_SIGNATURE']
event =nil# Verify webhook signature and extract the event.# See https://stripe.com/docs/webhooks#verify-events for more information.begin
event =Stripe::Webhook.construct_event(
payload, sig_header, endpoint_secret
)rescueJSON::ParserError=> e
# Invalid payload.
status 400returnrescueStripe::SignatureVerificationError=> e
# Invalid Signature.
status 400returnendif event['type']=='checkout.session.completed'
session = event['data']['object']
connected_account_id = event['account']
handle_completed_checkout_session(connected_account_id, session)end
status 200enddefhandle_completed_checkout_session(connected_account_id, session)# Fulfill the purchase
puts 'Connected account ID: '+ connected_account_id
puts session.to_s
end