In the stripe docs I see some ruby code that conditionally returns a status 400, status 200 etc.
# 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/account/apikeys
Stripe.api_key = 'sk_test_51HYHSFGtUKse83O9J4QeAib3cp8sHzGaOQRrnwvnghEzuYQKUCKEP3CHE3AIHe5ModevMK7TVAUCyJU0ADSwIUoX00qxZmBI9r'
# Uncomment and replace with a real secret. You can find your endpoint's
# secret in your webhook settings.
# webhook_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/signatures for more information.
begin
event = Stripe::Webhook.construct_event(
payload, sig_header, webhook_secret
)
rescue JSON::ParserError => e
# Invalid payload.
status 400
return
rescue Stripe::SignatureVerificationError => e
# Invalid Signature.
status 400
return
end
if event['type'] == 'payment_intent.succeeded'
payment_intent = event['data']['object']
handle_successful_payment_intent(payment_intent)
end
status 200
end
def handle_successful_payment_intent(payment_intent)
# Fulfill the purchase.
puts payment_intent.to_s
end
Is the literal status 200
supposed to be valid ruby code, or pseudocode that the programmer needs to change to whatever they want to have happen when a status 400 occurs? (i.e. should the literal status 400
, status 200
etc be deleted?)
The reason for asking is because when the code gets to the point where it runs status 200
, it returns:
(byebug) status 200
*** ArgumentError Exception: wrong number of arguments (given 1, expected 0)
The status
method can be used to set the returned HTTP status code of an action in Sinatra, which is the web framework used in the example you quoted.
Given that Sinatra is a different web framework from Rails, this works different here. With Rails, the equivalent method call (in this case) would be head
head 400
Note that a lot of other accessors used in the example work differently in Rails, including getting a header value of the request, reading the request body, and setting up e controller and routes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With