# 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"
require'sinatra'# You can find your endpoint's secret in your webhook settings
endpoint_secret ='whsec_...'
post '/webhook'do
event =nil# Verify webhook signature and extract the event# See https://stripe.com/docs/webhooks#verify-events for more information.begin
sig_header = request.env['HTTP_STRIPE_SIGNATURE']
payload = request.body.read
event =Stripe::Webhook.construct_event(payload, sig_header, endpoint_secret)rescueJSON::ParserError=> e
# Invalid payloadreturn status 400rescueStripe::SignatureVerificationError=> e
# Invalid signaturereturn status 400endif event['type']=='checkout.session.completed'
checkout_session = event['data']['object']
line_items =Stripe::Checkout::Session.list_line_items(checkout_session['id'],{limit:100})# Fulfill the purchase...begin
fulfill_order(checkout_session, line_items)rescueNotImplementedError=> e
return status 400endend
status 200enddeffulfill_order(checkout_session, line_items)# TODO: Remove error and implement...raiseNotImplementedError.new(<<~MSG)Given the CheckoutSession"#{checkout_session.id}" load your internal order from the database here.Then you can reconcile your order's quantities with the final line item quantity purchased.You can use `checkout_session.metadata` and `price.metadata` to store and later reference your internal order and item ids.MSGend