@@ -116,3 +116,40 @@ def authenticate!
116
116
status 200
117
117
return "Charge successfully created"
118
118
end
119
+
120
+ # This endpoint responds to webhooks sent by Stripe. To use it, you'll need
121
+ # to add its URL (https://{your-app-name}.herokuapp.com/stripe-webhook)
122
+ # in the webhook settings section of the Dashboard.
123
+ # https://dashboard.stripe.com/account/webhooks
124
+ post '/stripe-webhook' do
125
+ json = JSON . parse ( request . body . read )
126
+
127
+ # Retrieving the event from Stripe guarantees its authenticity
128
+ event = Stripe ::Event . retrieve ( json [ "id" ] )
129
+ source = event . data . object
130
+
131
+ # For sources that require additional user action from your customer
132
+ # (e.g. authorizing the payment with their bank), you should use webhooks
133
+ # to create a charge after the source becomes chargeable.
134
+ # For more information, see https://stripe.com/docs/sources#best-practices
135
+ WEBHOOK_CHARGE_CREATION_TYPES = [ 'bancontact' , 'giropay' , 'ideal' , 'sofort' , 'three_d_secure' ]
136
+ if event . type == 'source.chargeable' && WEBHOOK_CHARGE_CREATION_TYPES . include? ( source . type )
137
+ begin
138
+ charge = Stripe ::Charge . create (
139
+ :amount => source . amount ,
140
+ :currency => source . currency ,
141
+ :source => source . id ,
142
+ :description => "Example Charge"
143
+ )
144
+ rescue Stripe ::StripeError => e
145
+ p "Error creating charge: #{ e . message } "
146
+ return
147
+ end
148
+ # After successfully creating a charge, you should complete your customer's
149
+ # order and notify them that their order has been fulfilled (e.g. by sending
150
+ # an email). When creating the source in your app, consider storing any order
151
+ # information (e.g. order number) as metadata so that you can retrieve it
152
+ # here and use it to complete your customer's purchase.
153
+ end
154
+ status 200
155
+ end
0 commit comments