Skip to content

fix(mpesa): land the Daraja callback in production and fix admin display - #3

Merged
willymwai merged 2 commits into
mainfrom
fix/safaricom-callback
Jul 1, 2026
Merged

fix(mpesa): land the Daraja callback in production and fix admin display#3
willymwai merged 2 commits into
mainfrom
fix/safaricom-callback

Conversation

@Elias-3817

Copy link
Copy Markdown
Contributor

Problem

M-Pesa charged customers but left orders unpaid.

Root cause

The Safaricom callback was sent to /mpesa/callback. The platform proxy
routes only /api and /admin to Rails; everything else goes to the Next.js
storefront, which 307-redirects (country/locale) then 404s. Safaricom does
not follow callback redirects, so the confirmation never reached Rails.

Fix

  • Callback now uses /api/v1/mpesa/callback (matches spree_ipay), so the
    proxy delivers it to Rails.
  • default_name presents the method as "Lipa na M-Pesa".
  • Register the bundled mpesa.svg for precompilation so the icon renders.

The STK callback was sent to a bare /mpesa/callback path. On the hosted
platform a reverse proxy forwards only /api and /admin to Rails; other
paths go to the Next.js storefront, which 307-redirects to add a
country/locale prefix and then 404s. Safaricom does not follow callback
redirects, so payment confirmations were lost and orders stayed unpaid
after the customer was charged. Move the route and callback_url to
/api/v1/mpesa/callback, matching the convention spree_ipay already uses.

Also fix admin presentation: override default_name so the method shows as
"Lipa na M-Pesa", and register payment_icons/mpesa.svg for precompilation
so inline_svg resolves it in production instead of the generic fallback.

Update specs for the new path; add callback_url and default_name specs.
Bump to 1.0.2.
Copilot AI review requested due to automatic review settings June 30, 2026 16:08
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses missed M-Pesa payment confirmations by moving the Daraja callback endpoint onto an /api/... path that the platform proxy forwards to Rails, and it fixes admin presentation details for the payment method.

Changes:

  • Move the callback route and all request-spec posts from /mpesa/callback to /api/v1/mpesa/callback.
  • Add default_name (“Lipa na M-Pesa”) and add a spec for the callback URL generation.
  • Register payment_icons/mpesa.svg for asset precompilation so the admin icon renders; bump gem version to 1.0.2.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
spree_mpesa.gemspec Bumps gem version for the release containing the callback/admin fixes.
config/routes.rb Moves the Daraja callback endpoint under /api/v1/....
app/models/spree/payment_method/mpesa.rb Updates callback URL generation and adds default_name.
spec/requests/spree/mpesa_callbacks_spec.rb Updates request specs to hit the new callback endpoint.
spec/models/spree/payment_method/mpesa_spec.rb Adds coverage for callback_url and default_name.
lib/spree_mpesa/engine.rb Adds asset precompile entry for the M-Pesa payment icon SVG.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread config/routes.rb
Spree::Core::Engine.routes.draw do
# Safaricom Daraja STK Push result callback
post '/mpesa/callback', to: 'mpesa_callbacks#confirm', defaults: { format: 'json' }
post '/api/v1/mpesa/callback', to: 'mpesa_callbacks#confirm', defaults: { format: 'json' }
…api path

The assets precompile initializer called app.config.assets on hosts
without Sprockets (the dummy app in CI), raising NoMethodError at boot and
failing db:test:prepare. Guard it so it runs only when the asset config is
present.

Also follow the callback path move to /api/v1/mpesa/callback through the
rest of the gem: throttle the new path in the Rack::Attack initializer and
update the README references.
@Elias-3817
Elias-3817 force-pushed the fix/safaricom-callback branch from abdff6d to eca5f6a Compare July 1, 2026 03:20
Copilot AI review requested due to automatic review settings July 1, 2026 03:20

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.

@willymwai
willymwai merged commit 4534030 into main Jul 1, 2026
2 checks passed
@willymwai
willymwai deleted the fix/safaricom-callback branch July 1, 2026 03:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants