Facebook iconWebhook integration with Stripe Payment Intents
Blogs/Technology

WebHook integration with Stripe Payment Intents

Apr 15, 20223 Min Read
by Murtuza Kutub
WebHook integration with Stripe Payment Intents Hero

You may have heard about Webhooks when integrating payment providers Stripe, Razorpay, Dwolla, etc in your application. I was working on one of the rails applications and had to integrate Stripe payment in that. So I read about webhook, what it does and integrated with my application.

In this blog, I am going to cover the following things:

  1. What is a webhook?
  2. Webhook integration with Stripe payment intent.

What is a WebHook?

A WebHook is an HTTP callback: an HTTP POST that occurs when something happens. With all APIs, there’s a request followed by a response. Even for webhook, the request is made by the payment provider. just the difference is here request is initiated by another server instead of a client-side browser.

WebHook integration with Stripe Payment Intent

To use a webhook, you’ll have to register a URL with the Payment Gateway. This URL is a place within your application that will accept the data and process it. Whenever any change to your payment application happens eg: a customer creating a transfer, adding a new bank account, withdrawing funds, etc., the Payment provider will notify your application server using this webhook endpoint present in your server. It is generally a good practice to record all requests sent to this endpoint by the payment provider and process only the topics that are of application’s interests such as you may only have to process a topic customer_bank_transfer_completed or customer_bank_transfer_failed to mark your payment record with complete / failed.

In my case, I was using the Stripe payment Intent API and integrated the webhook for it.

Here are the following steps.

  1. Create a model called Webhook and add attributes based on your requirements (like event, event_type).
  2. Create a controller called webhook_controllers.rb
  3. Write an action called create.
  4. Add route in config/routes.rb file
resources :webhooks, only :create

The migration will look like:

Partner with Us for Success

Experience seamless collaboration and exceptional results.

class CreateWebhooks < ActiveRecord::Migration[6.0]
  def change
    create_table :webhooks do |t|
      t.json :event
      t.string :event_type
      t.timestamps
    end
  end
end

Paste the following code inside webhook_controllers.rb

def create
      payload = request.body.read
      begin
        event = Stripe::Webhook.construct_event(
          payload, signature_header, endpoint_secret
        )
      rescue JSON::ParserError => e
        # Invalid payload
        render json: {success: false, message: "Invalid payload"}, status: 400 and return
      rescue Stripe::SignatureVerificationError => e
        # Invalid signature
        render json: {success: false, message: "Invalid signature"}, status: 400 and return
      end
json_data = JSON.parse(event.to_json)
      stripe_webhook = Webhook.create!(event: json_data, event_type: json_data["type"])
      render json: {success: true, message: "successfully stored"}, status: :ok
  end
private
def signature_header
      request.env['HTTP_STRIPE_SIGNATURE']
  end
def endpoint_secret
       Rails.application.credentials.stripe.dig(:webhook_endpoint_secret)
  end

You will find endpoint_secret in the stripe dashboard. First, log in to the dashboard, click on test mode then go to Developers -> Webhooks -> signing secret as shown in the image. Click to reveal.

Webhooks Signing

Add your endpoint to the stripe dashboard. suppose your URL is https://mydomain.com/webhooks and select the event that you want to receive data for. You can see in the below image.

Partner with Us for Success

Experience seamless collaboration and exceptional results.

stripe dashboard

stripe dashboard setting

After setting up everything, you can send a test webhook and test your endpoint whether it is working perfectly or not.

Conclusion

And that’s it! Did this work for you? Please leave any questions and comments below!

Thank you for reading!

If you found this article helpful, 👏👏👏

Author-Murtuza Kutub
Murtuza Kutub

A product development and growth expert, helping founders and startups build and grow their products at lightning speed with a track record of success. Apart from work, I love to network & Travel.

Phone

Next for you

How to Integrate Google Maps into Your Flutter App Cover

Technology

Nov 20, 20244 min read

How to Integrate Google Maps into Your Flutter App

In our smartphone-driven world, location services are transforming the app landscape. The global location-based services market is projected to hit a staggering reach USD 129.71 billion by 2032, exhibiting a CAGR of 19.5% during the forecast period (2024-2032) This explosion stems from our smartphone addiction and hunger for personalized experiences. It's only natural that developers are rushing to integrate Google Maps into their apps, aiming to deliver an engaging, user-friendly mapping experi

How To Implement Dark Mode in Your Flutter App Cover

Technology

Oct 24, 20244 min read

How To Implement Dark Mode in Your Flutter App

In today's app-driven world, dark mode has become a must-have feature. A 2021 Android Authority survey found that an overwhelming 81.9% of users prefer dark mode in their apps. This trend highlights the need for Flutter developers to incorporate this feature to enhance user satisfaction and stay competitive. Understanding Theming in Flutter Flutter theming is the foundation for implementing dark mode. It provides a systematic way to manage your app's visual elements, ensuring consistency and

How to Implement Drag-and-Drop in Flutter? Cover

Technology

Oct 22, 20245 min read

How to Implement Drag-and-Drop in Flutter?

Have you ever marvelled at how smoothly you can rearrange apps on your smartphone's home screen? That's the magic of drag-and-drop at work. As a Flutter developer, you have the power to bring this same intuitive interaction to your apps. Let's dive into the world of Flutter drag and drop and discover how you can create interfaces that users will love to touch, drag, and interact with. Why Drag-and-Drop Matters in Modern Apps Picture this: you're scrolling through a to-do list app, and instea