#emnaruto07 - rails webhook
1 messages ยท Page 1 of 1 (latest)
I'm not able to save data inside my db
i'm setting the metadat. now i want to search it with metadata
can you please me?
Pasting your code in this thread
protect_from_forgery with: :null_session
def create
event = nil
# Verify webhook signature and extract the event
# See https://stripe.com/docs/webhooks/signatures for more information.
begin
endpoint_secret = Rails.application.credentials.dig(:stripe, :webhook)
sig_header = request.env['HTTP_STRIPE_SIGNATURE']
payload = request.body.read
event = Stripe::Webhook.construct_event(payload, sig_header, endpoint_secret)
rescue JSON::ParserError => e
# Invalid payload
return status 400
rescue Stripe::SignatureVerificationError => e
# Invalid signature
return status 400
end
case event['type']
when event['type'] == 'checkout.session.completed'
checkout_session = event['data']['object']
puts checkout_session
@job = Job.find()
fulfill_order(checkout_session)
when 'checkout.session.completed'
checkout_session = event['data']['object']
# Save an order in your database, marked as 'awaiting payment'
@job.stripe_payment_id = checkout_session.id
if @job.published == false
@job.published = true
@job.save!
end
else
puts "Unhandled event type: #{event.type}"
end
render json: { message: 'success'}
end
end```
What function call is failing? Are you getting a specific Stripe error code here?
Is the metadata problem part of this webhook issue or is that a seperate issue?
This thread has been archived. If you need help with anything else please ask in #dev-help or contact Stripe Support: https://support.stripe.com/contact
Hi
@alpine spoke here is the thread!
Sorry, last night. My internet went off
I still need help with my webhooks
So, my stripe checkout is working fine.
but i'm not able to save any information in my db with webhooks.
This is my webhook code.
Can you share a request id here? Here's how you can find a request ID: https://support.stripe.com/questions/finding-the-id-for-an-api-request so I can further look.
wait
What is the specific issue? What is happening? We'd need way more information here to assist.
Stripe.api_key = Rails.application.credentials.dig(:stripe, :secret)
session = Stripe::Checkout::Session.create({
line_items: [{
# Provide the exact Price ID (e.g. pr_1234) of the product you want to sell
price: 'price_1KwNNHDaN18cbJKshEaO77yS',
quantity: 1,
}],
metadata: {job_id: @job.id},
mode: 'payment',
success_url: root_url + 'success',
cancel_url: root_url + 'cancel'
})
redirect_to session.url, status: 303, allow_other_host: true
end```
i'm sending metadata like this, But i can't see any metadata in my request
it could still be anything
Step 1: Look at the event and check if metadata is there (you can find it in the Dashboard: https://dashboard.stripe.com/test/events/evt_1KxgL6DaN18cbJKsRYGv2xFX)
Step 2: Log the raw JSON you get when you receive the event
yes meta data is there
cool and is it there in ruby? Because the ruby code you worked is unclear, I don't see you accessing metadata anywhere
yeah that notation makes no sense to me at least
did you look at https://stripe.com/docs/webhooks/quickstart it has clear examples on how to parse an event
what is [object][metadata] fairly sure that doesn't work
you want checkout_session.metadata['job_id']
when 'payment_intent.succeeded'
payment_intent = event.data.object # contains a Stripe::PaymentIntent
puts "Payment for #{payment_intent['amount']} succeeded."
# Then define and call a method to handle the successful payment intent.
# handle_payment_intent_succeeded(payment_intent)
when 'payment_method.attached'
payment_method = event.data.object # contains a Stripe::PaymentMethod
# Then define and call a method to handle the successful attachment of a PaymentMethod.
# handle_payment_method_attached(payment_method)``` this is what i found on the page
ok
sorry i'm fairly new
all good, let me know if that unblocks you
sure
still i'm not able to get the id
when event['type'] == 'checkout.session.completed'
checkout_session = event['data']['object']
puts "Job_id: #{checkout_session.metadata['job_id']}"
job = Job.find(checkout_session.metadata['job_id'])
job.update(stripe_payment_id: checkout_session.payment_intent)
# Save an order in your database, marked as 'awaiting payment'
if job.published == false
job.published = true
job.save
end
else
puts "Unhandled event type: #{event.type}"
end
render json: { message: 'success'}
end```
its not even printing the job id.
it worked
๐
yay!
๐ happy to help
Hi
how may I help you?
i have a problem. I can only access metadata in checkout.complete.event
payment_intent.succeeded doesnt have the same metadata
how to fix this problem.
you should probably listen to the checkout.session.completed event instead of payment_intent.succeeded
ok wait
but in all cases if you want to have metadata added to the Payment Intent object created on Checkout Sessions you could use this field https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-payment_intent_data-metadata
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
does both of them have the same data?
no
this is why when you're using the metadata directly on the Checkout Session(https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-metadata) you are not being able to see it when you receive the Payment Intent object from the payment_intent.succeeded event
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
think of it this way, each event type give you access to a certain type of Objects (Payment Intents, Checkout Sessions, etc....)
ok
so depending on where you put the metadata and which type of object you're receiving you will either have access or not to it
i have one question, without payment_intent.succeeded. Checkout event will never completed right?