Skip to content

feat(flutterwave): Flutterwave payment provider package - #98

Closed
alexasomba wants to merge 2 commits into
emdashCommerce:mainfrom
alexasomba:feat/flutterwave-payment-provider
Closed

alexasomba wants to merge 2 commits into
emdashCommerce:mainfrom
alexasomba:feat/flutterwave-payment-provider

Conversation

@alexasomba

@alexasomba alexasomba commented Nov 24, 2026 •

Copy link
Copy Markdown

Part of the Flutterwave set described in #97. Builds on the merged provider seam (#81–#84, #90); no core changes. Independent of #85 (Paystack): both packages sit on the same merged core seam, and either can merge first.

What

packages/flutterwave: a PaymentProvider for Flutterwave on core's provider interface. It covers card, M-Pesa and mobile money through Flutterwave's hosted checkout, in NGN, USD, GBP, EUR, KES, GHS, ZAR, UGX, TZS, RWF, XOF, XAF, ZMW, MWK and EGP. It registers itself on import "@dashcommerce/flutterwave". The README covers setup: allowedHosts: ["api.flutterwave.com"], keys, the secret hash, and the webhook URL.

How it behaves

  • Amounts: core keeps integer minor units; Flutterwave's v3 API charges major units. The adapter converts at the boundary and nowhere else: a KES 1,500.00 cart is sent as 1500, and Flutterwave's 1500 comes back as 150000. Unit-tested in both directions, including string amounts.
  • Orders: created only from a signed charge.completed with data.status = successful, confirmed with GET /v3/transactions/:id/verify before core writes the order. failed/cancelled become charge.failed and release the stock lock; pending creates nothing. The order stores the numeric transaction id as paymentReference (refunds need it) and tx_ref as checkoutReference.
  • Webhooks: Flutterwave v3 authenticates with the shared secret hash in verif-hash, not an HMAC. The package compares it in constant time, trims whitespace, and explains the fix when it does not match. There is no timestamp, so replay protection is core's event-id dedupe plus the live verify; a replay cannot create a second order or an order Flutterwave does not confirm.
  • Duplicates: Flutterwave sends no event id, so the package builds one: charge.completed:<transaction id>:<status> and refund:<refund id>:<status>. A repeat or overlapping delivery runs once. When confirmation fails, core releases the record, so Flutterwave's retry still creates the order.
  • Refunds: POST /v3/transactions/:id/refund with (refundRequestId=<id>) at the end of comments. Flutterwave takes no idempotency key, so the package lists refunds and returns any non-failed refund already tagged with that request id instead of creating another. A reason cannot pose as a tag (brackets are stripped and the tag is anchored at the end). A tx_ref is resolved to the numeric id first. Every completed* status maps to succeeded, pending-*/processing to pending, failed to failed.
  • Refund webhooks: Flutterwave posts a bare refund object with no event field and capitalised AmountRefunded/FlwRef/TransactionId. That maps to refund.updated by the refund id, with each status a separate event id so pending never blocks completed.
  • Connection check: "Test Flutterwave connection" makes one read-only GET /v3/balances call and reports test or live mode from the key prefix.
  • Payment options: a channel hint maps to Flutterwave's option names, and generic mobile_money resolves by currency (KES → mpesa, GHS → mobilemoneyghana, …). With no hint, Flutterwave shows what the account has enabled.

Deliberately not included

  • On-page checkout. Flutterwave Inline needs the public key (FLWPUBK-…); core stores only secretKey/webhookSecret and gives providers no KV access. The public and secret keys do not share a hash segment (checked with a real pair), so the key cannot be derived. Hosted checkout is the equivalent flow today; Inline needs a core credential field first. The package declares checkoutStyles: ["hosted"] rather than shipping a dead client.
  • Split payments. Flutterwave collection subaccounts need core to hand the provider precomputed allocations; the interface has no such field. connect: false makes checkout refuse rather than silently drop a split.
  • Coupons and subscriptions: declared false for the same reason.

Package

  • Publishable like core: publishConfig.access: public, author "DashCommerce contributors". A minor changeset takes it to 0.1.0 beside core 0.4.0.
  • bun.lock gains only the new workspace entry.

Tests

  • 68 tests, including checkout → signed webhook → verified order → refund through core's real route handlers.
  • A Flutterwave stand-in that only verifies transactions it created, requires the secret key, and enforces the major/minor conversion in both directions — a provider that verifies by the wrong identifier or sends cents fails there.
  • Covered: both charge.completed outcomes, metadata as object and JSON string, draft recovery from tx_ref, wrong verif-hash, amount mismatch (order held with paymentAmountMismatch), verification outage then a successful retry, pending verification (no order), replay, refund retry guard, a failed refund retried, a new request id creating a new refund, the refund webhook moving a pending row to succeeded, and the settings API accepting FLWSECK_TEST- while refusing FLWPUBK_.
  • bun run typecheck passes across the monorepo; biome check is clean on the new files.

End to end in Flutterwave test mode (24 Nov 2026)

Nigerian test account, KES wallet, test keys only.

Check Result
GET /v3/balances via checkConnection 200; reports test mode; a wrong key is refused with a readable message
POST /v3/payments via initCheckout 200; hosted link issued for dc-<draftId>; {CHECKOUT_SESSION_ID} stripped
Real KES 1,500 M-Pesa payment Completed through Flutterwave Inline on checkout-v3.flutterwave.com; callback returned successful, transaction id 10633588
Verify by transaction id and by tx_ref Both returned the payment; getPaymentStatus reported succeeded, 150000 minor units, KES
KES 500 partial refund Succeeded; status: "completed"; the (refundRequestId=…) comment came back exactly as sent
Full settled amount refunded 1,426.5 of 1,500 KES (refund 115050); verify still reports successful
Second partial while the first processes 400 "A partial refund is currently processing on this transaction"; surfaced, not swallowed
GET /v3/refunds 400 {"message":"unauthorized"} on this key, while refund creation works

What the run changed: the refund duplicate check now degrades to a logged warning when refund history is not available to the key, instead of blocking every refund; meta.disburse_status: "failed" is logged because completed can mean "accepted, not yet paid out"; Flutterwave's one-partial-at-a-time rule is documented; and the README records that verify stays successful after a refund.

Still to run before release: the real webhook delivery (a real KES 100 payment and two resend-hook calls were accepted on 24 Nov 2026, but nothing reached the test tunnel yet — the dashboard configuration is under review), a declined payment, and the replay/verif-hash matrix against the starter.

Review notes

  • The refund list is global (GET /v3/refunds) and not filterable by transaction, so the duplicate guard walks up to 20 pages (400 recent refunds) and logs when it stops. A retry is always near the top because the list is newest-first. On keys where the endpoint answers unauthorized, the guard is skipped with a warning, so a retried refund can duplicate there.
  • Flutterwave's documented refund-create example is ambiguous about whether data.id is the refund id or the transaction id; the OpenAPI example, the list endpoint and the refund webhook all treat it as the refund id, which is what the package relies on.

…able

Real test-mode run (24 Nov 2026): GET /v3/refunds answers 400
unauthorized on a key that can create refunds. Degrade the duplicate
check to a logged warning for that case, log a failed disbursement, and
add fixtures from the real verify and refund responses.
@alexasomba

Copy link
Copy Markdown
Author

Superseded by #107 — the Flutterwave package is now reviewed as one PR. The commits are preserved on the consolidated branch, and the core PRs (#99, #101, #103, #105) stay separate.

@alexasomba alexasomba closed this Nov 24, 2026
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.

1 participant