fix(mpesa): land the Daraja callback in production and fix admin display - #3
Conversation
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.
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
There was a problem hiding this comment.
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/callbackto/api/v1/mpesa/callback. - Add
default_name(“Lipa na M-Pesa”) and add a spec for the callback URL generation. - Register
payment_icons/mpesa.svgfor asset precompilation so the admin icon renders; bump gem version to1.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.
| 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.
abdff6d to
eca5f6a
Compare
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
proxy delivers it to Rails.