#emnaruto07 - rails webhook

1 messages ยท Page 1 of 1 (latest)

hollow dome
#

Hello, what issue are you running in to with this code?

alpine spoke
#

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?

hollow dome
#

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?

native bladeBOT
alpine spoke
#

Hi

restive river
#

@alpine spoke here is the thread!

alpine spoke
#

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.

restive river
alpine spoke
#

wait

restive river
#

What is the specific issue? What is happening? We'd need way more information here to assist.

alpine spoke
#
    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

knotty sandal
alpine spoke
#

yes meta data is there

knotty sandal
#

cool and is it there in ruby? Because the ruby code you worked is unclear, I don't see you accessing metadata anywhere

alpine spoke
#

yes.

#

@job = Job.find([object][metadata][0]) do i have to access like this?

knotty sandal
#

yeah that notation makes no sense to me at least

alpine spoke
#

which notation?

#

its rails

knotty sandal
#

what is [object][metadata] fairly sure that doesn't work

#

you want checkout_session.metadata['job_id']

alpine spoke
#
  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

knotty sandal
#

all good, let me know if that unblocks you

alpine spoke
#

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

#

๐Ÿ˜„

knotty sandal
#

yay!

patent plinth
#

๐Ÿ‘‹ happy to help

alpine spoke
#

Hi

patent plinth
#

how may I help you?

alpine spoke
#

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.

patent plinth
#

you should probably listen to the checkout.session.completed event instead of payment_intent.succeeded

alpine spoke
#

ok wait

patent plinth
alpine spoke
#

does both of them have the same data?

patent plinth
#

no

#

think of it this way, each event type give you access to a certain type of Objects (Payment Intents, Checkout Sessions, etc....)

alpine spoke
#

ok

patent plinth
#

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

alpine spoke
#

i have one question, without payment_intent.succeeded. Checkout event will never completed right?

patent plinth
#

you should always wait for the checkout.session.completed event to be sure that nothing went wrong and that the session was completed successfully

#

that's our recommendation