McTaba Labs logo
Software EngineeringThe WhiteboardProjectsEventsMasterclassesWebinarsLearn
Book Call

500+ developers already in the community

Get answers, find accountability partners, and stay ahead

Join Discord →
McTaba Labs logo

Educating 1 million students in tech by 2030.

Programs

  • HighSchool Graduates Launchpad
  • AI & Business Automation
  • Corporate Training
  • McTaba Story & Mission
  • Full-Stack Curriculum
  • Course Pricing & Plans
  • Frequently Asked Questions

Resources

  • Join Discord Community
  • Tech Events in Nairobi
  • Masterclasses
  • Free Resources
  • Learn
  • Campus
  • Africa Tech Talent Report
  • Partner With Us
  • Start a Project
  • Our Work

Contact Us

  • 124 Manyani East Road, Lavington, Nairobi
  • +254727795603
  • Book a Discovery Call

Office Hours:

Mon-Fri: 9am - 2pm EAT

Sat: 10am - 12pm EAT

© 2027 McTaba Labs - A product of Edtech Industries. All rights reserved.

Privacy PolicyTerms of ServiceRefund Policy
  1. Learn
  2. Paystack for African Developers
  3. Handle Paystack Webhooks in Ruby on Rails
Bonaventure OgetoBy Bonaventure Ogeto|Updated July 20, 2026

Handle Paystack Webhooks in Ruby on Rails

To handle Paystack webhooks in Rails, add skip_forgery_protection to your webhook action, read request.body.read for the raw bytes, compute an HMAC SHA-512 hash with your secret key, compare it to the X-Paystack-Signature header, and process the event. Return head :ok.

In this article

  1. 1. Webhook Controller
  2. 2. ActiveJob for Processing
  3. 3. Route
  4. 4. Testing
  5. 5. Production Notes
  6. 6. Ship Payments Faster

Webhook Controller

# app/controllers/webhooks_controller.rb
class WebhooksController < ApplicationController
  skip_forgery_protection only: :paystack

  def paystack
    signature = request.headers['X-Paystack-Signature']
    return head :bad_request unless signature.present?

    body = request.body.read
    secret = ENV['PAYSTACK_SECRET_KEY']

    computed_hash = OpenSSL::HMAC.hexdigest('sha512', secret, body)

    unless ActiveSupport::SecurityUtils.secure_compare(computed_hash, signature)
      return head :bad_request
    end

    event = JSON.parse(body)

    ProcessPaystackWebhookJob.perform_later(event)

    head :ok
  end
end

ActiveJob for Processing

# app/jobs/process_paystack_webhook_job.rb
class ProcessPaystackWebhookJob < ApplicationJob
  queue_as :default

  def perform(event)
    event_type = event['event']
    data = event['data'] || {}

    case event_type
    when 'charge.success'
      PaystackService.new.verify_and_fulfill(data['reference']) if data['reference']
    when 'subscription.create'
      handle_subscription_create(data)
    when 'invoice.payment_failed'
      handle_invoice_failed(data)
    end
  end

  private

  def handle_subscription_create(data)
    # Create or update subscription record
  end

  def handle_invoice_failed(data)
    # Update subscription status to past_due
  end
end

Get weekly developer tips

Join 25,000+ developers. Practical guides, job tips, and new content — straight to your inbox.

No spam. Unsubscribe anytime.

Route

# config/routes.rb
post '/webhooks/paystack', to: 'webhooks#paystack'

Testing

# test/controllers/webhooks_controller_test.rb
test 'valid webhook returns 200' do
  payload = { event: 'charge.success', data: { reference: 'test_123' } }.to_json
  signature = OpenSSL::HMAC.hexdigest('sha512', ENV['PAYSTACK_SECRET_KEY'], payload)

  post webhooks_paystack_url,
    params: payload,
    headers: { 'Content-Type' => 'application/json', 'X-Paystack-Signature' => signature }

  assert_response :ok
end

Production Notes

  1. Use Sidekiq or another background job backend. Do not use inline jobs in production.
  2. HTTPS required. Your webhook URL must use HTTPS.
  3. Log events. Store every webhook in a database for debugging.
  4. Monitor. Check Paystack dashboard for failed deliveries.

Ship Payments Faster

This guide is part of the Paystack integration guides by language and framework series.

Sign up for the McTaba newsletter

Key Takeaways

  • ✓Add skip_forgery_protection to your webhook controller action.
  • ✓Use request.body.read for the raw bytes needed for HMAC verification.
  • ✓Use OpenSSL::HMAC.hexdigest for SHA-512 hash computation.
  • ✓Use ActiveSupport::SecurityUtils.secure_compare for constant-time comparison.
  • ✓Return head :ok immediately. Process events in ActiveJob for reliability.
  • ✓Make handlers idempotent. Paystack retries failed deliveries.

Frequently Asked Questions

Why use skip_forgery_protection instead of protect_from_forgery?
skip_forgery_protection is the Rails 7+ way to disable CSRF for specific actions. Paystack webhook requests do not include Rails CSRF tokens.
Can I use request.raw_post instead of request.body.read?
Yes. request.raw_post also returns the raw request body as a string. Both work for HMAC verification.
Should I use ActiveJob or Sidekiq directly?
ActiveJob is the Rails abstraction. Use it with Sidekiq as the backend adapter. This keeps your code framework-agnostic while getting Sidekiq performance.

Learn Paystack Integration

KES 2,999

The definitive guide to building payment systems with Paystack — from your first transaction to production-grade payment infrastructure across Africa. 9 modules, 47 lessons. Free preview available.

Start Paystack Course

Not ready? Create Free Account

Keep reading

Paystack Integration Guides by Language and Framework

A complete directory of Paystack integration guides organized by programming language and framework. Each guide covers accepting payments, verifying transactions, handling webhooks, and building subscriptions in your specific stack.

Read more

Accept Payments with Paystack in Svelte and SvelteKit

Accept Paystack payments in SvelteKit using server endpoints for backend logic and Svelte components for the frontend. This guide covers initializing transactions, inline popup checkout, redirect checkout, and verification in a single full-stack SvelteKit project.

Read more

Verify Paystack Payments in Flask

Verify Paystack payments server-side in your Flask application. This guide covers calling the Paystack verify endpoint, validating amounts and currencies, building reusable verification helpers, and preventing double-granting.

Read more

Build Paystack Subscriptions in ASP.NET Core

Implement Paystack recurring billing in ASP.NET Core. This guide covers creating plans, subscribing customers through transaction initialization, handling renewal webhooks with C#, and cancelling subscriptions.

Read more

Paystack Transaction Timeline API for Debugging Customer Complaints

A customer says they paid but did not get their product. Another says they were charged twice. Your support team needs answers. The Paystack Transaction Timeline API shows you exactly what happened at each step of a transaction, from initialization to settlement. This guide shows you how to use it for debugging and customer support.

Read more

Debugging Paystack with the Paystack CLI

The Paystack CLI gives you terminal access to your Paystack account. Instead of clicking through the Dashboard to check a transaction or waiting for a webhook to arrive, you run a command and get the answer in seconds. For developers who live in the terminal, it is the fastest way to debug payment issues.

Read more

Explore related topics

M-Pesa & African Stack

M-Pesa Daraja API tutorials, USSD development, and WhatsApp Business API guides. The African Stack that global bootcamps do not teach.

Learn to Code in Kenya

Everything a Kenyan needs to know before writing their first line of code. Career paths, real costs, hardware requirements, and honest answers about what it takes.

Learning to Code

Roadmaps, language guides, and practical advice for learning to code and launching a programming career.

Breaking Into Tech

Practical, no-nonsense guides for anyone looking to start or change their career in tech. From choosing your first language to acing interviews.

The African Stack: Skills for This Market

The skills that make Kenyan developers hard to replace: M-Pesa, USSD, WhatsApp, fintech, and building for the African market.

Bonaventure Ogeto

Bonaventure Ogeto

Founder, McTaba Labs

Bonaventure is the founder of McTaba Labs, a coding bootcamp in Nairobi, Kenya. He has spent years building products for the African market and mentoring the next generation of developers across the continent.