Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .changeset/flutterwave-provider.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"@dashcommerce/flutterwave": minor
---

New package: a Flutterwave payment provider for DashCommerce, built on core's PaymentProvider interface.

- Card, M-Pesa and mobile money through Flutterwave's hosted checkout. Currencies: NGN, USD, GBP, EUR, KES, GHS, ZAR, UGX, TZS, RWF, XOF, XAF, ZMW, MWK and EGP.
- Amounts are converted at the boundary: core keeps minor units, Flutterwave's v3 API charges major units.
- Orders are created only from a signed `charge.completed`, confirmed against Flutterwave before the order is written. `verif-hash` is checked in constant time against the secret hash saved in Settings → Webhooks.
- Repeat and overlapping webhook deliveries run once: each charge and refund event carries an event id core can deduplicate on.
- Refunds by transaction id or `tx_ref`, guarded against duplicates on retry, with Flutterwave's refund webhook moving each refund to succeeded or failed.
- "Test Flutterwave connection" in Settings checks the saved key in test or live mode with a read-only balances call.

Needs `@dashcommerce/core` 0.4.0 or later. Register it with `import "@dashcommerce/flutterwave"` in a server module, add `api.flutterwave.com` to `allowedHosts`, and point Flutterwave's webhook at `…/checkout/webhook?provider=flutterwave`; see the package README.
14 changes: 14 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 100 additions & 0 deletions packages/flutterwave/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# @dashcommerce/flutterwave

A Flutterwave `PaymentProvider` for DashCommerce. Customers pay on Flutterwave's hosted checkout by card, M-Pesa or mobile money. DashCommerce creates the order when Flutterwave's signed `charge.completed` webhook arrives and the transaction is confirmed with Flutterwave.

> **Needs `@dashcommerce/core` 0.4.0 or later.** 0.4.0 is the first core release that can call `api.flutterwave.com`, save a second provider's keys, pass its webhook header to the provider and take refund updates. This package's first release, 0.1.0, goes out with it.

## What it does

| Area | Behaviour |
|---|---|
| Checkout | `POST /v3/payments`, then a redirect to the hosted link in `data.link`. The reference is `dc-<orderDraftId>`, so the draft can be recovered from `tx_ref` even without metadata |
| Amounts | Core works in integer minor units; Flutterwave's v3 API works in major units. The provider converts at the boundary: a KES 1,500.00 cart is sent as `1500`, and Flutterwave's `1500` comes back as `150000` |
| Webhook | Compares the `verif-hash` header with the secret hash saved in Settings → Webhooks, in constant time. See [Webhook verification](#webhook-verification) |
| Confirmation | Core calls `getPaymentStatus` before creating the order. A numeric reference is verified with `GET /v3/transactions/:id/verify`; anything else through `GET /v3/transactions/verify_by_reference?tx_ref=…`. The amount and currency come from Flutterwave's record, not the webhook body. `successful` counts as paid, `failed`/`cancelled` as failed, `pending` as pending. An unknown status is reported as pending, never as paid. Flutterwave keeps reporting `successful` after a refund (there is no `reversed` state), so a payment refunded in full before its order exists cannot be told apart from an unrefunded one; core's unique index on the payment reference still refuses a second order |
| Duplicates | Flutterwave webhooks carry no event id, so the provider builds one: `charge.completed:<transaction id>:successful`, `charge.completed:<id>:failed`, and `refund:<refund id>:<status>`. Core records it before the handler runs, so a repeat or overlapping delivery is skipped, and a failed refund is reversed once. If confirming with Flutterwave fails (Flutterwave down, or the payment not yet successful), core releases the record, so Flutterwave's retry still creates the order. Any other failure keeps the record, as it does for Stripe |
| Draft lookup | From `meta.orderDraftId` (object or JSON string), or from the `dc-<draftId>` reference |
| Return URL | Core's success URL minus Stripe's `{CHECKOUT_SESSION_ID}` placeholder; Flutterwave appends `status`, `tx_ref` and `transaction_id` |
| Refunds | `POST /v3/transactions/:id/refund`, with `(refundRequestId=<id>)` at the end of `comments`. Flutterwave takes no idempotency key for refunds, so the provider lists refunds and returns any non-failed refund already tagged with this `refundRequestId` instead of creating another. The tag is read only from the end of the comment, and brackets are stripped from the reason, so a reason cannot pose as another request. This guards retries, not two simultaneous calls with the same id: both could pass the check before either refund exists, so core must not run the same refund request concurrently. Flutterwave allows one partial refund to process per transaction at a time; a second answers `400 "A partial refund is currently processing on this transaction"`. `GET /v3/refunds` is not available to every key: a real test-mode key answered `400 {"message":"unauthorized"}` while it could create refunds. The provider logs that the retry check is unavailable and continues, so on such an account a retried refund can duplicate |
| Refund statuses | `completed`, `completed-bank-transfer`, `completed-momo`, `completed-mpgs`, `completed-offline` and `completed-preauth` → succeeded; `pending`, `pending-momo`, `pending-bank-transfer`, `pending-mpgs` and `processing` → pending; `failed` → failed. Flutterwave's minimum refund is NGN 100 / KES 10; a smaller request is refused by Flutterwave with a 400. `completed` can arrive while `meta.disburse_status` is `failed`, meaning Flutterwave accepted the refund but the customer has not been paid yet; the provider logs a warning in that case |
| Refund webhooks | Flutterwave posts the refund object itself, with no `event` field and capitalised `AmountRefunded`, `FlwRef` and `TransactionId`. The provider turns it into core's `refund.updated`, matched by the refund id |
| Currencies | NGN, USD, GBP, EUR, KES, GHS, ZAR, UGX, TZS, RWF, XOF, XAF, ZMW, MWK and EGP. Your Flutterwave account must be enabled for the currency you sell in |
| Payment options | With no channel hint, Flutterwave shows every method enabled on the account. A hint is mapped to Flutterwave's option names, and generic `mobile_money` is resolved by currency (`KES` → `mpesa`, `GHS` → `mobilemoneyghana`, …) |
| Customer details | Flutterwave's hosted page collects neither a shipping address nor a name for the store, so the provider leaves `collectsCustomerDetails` unset and DashCommerce's checkout asks for email and address before payment. The email is also required by `POST /v3/payments` |
| Not supported | Coupons, split payments and subscriptions. `capabilities` declares all three as `false` |
| On-page checkout | Not offered. Flutterwave Inline needs the account's public key (`FLWPUBK-…`), which core's provider settings do not store; hosted checkout is the equivalent flow today |

## Webhook verification

Flutterwave authenticates v3 webhooks with a shared secret, not an HMAC ([docs](https://developer.flutterwave.com/docs/webhooks)):

1. Set a **secret hash** under Settings → Webhooks in the Flutterwave dashboard. Any random value works.
2. Flutterwave sends it back verbatim in the `verif-hash` header on every delivery.
3. The provider compares the two in constant time and rejects a missing or different value.

There is no timestamp and no signature over the body, so a captured delivery can be replayed. That cannot create a second order: core deduplicates on the event id above, its unique index on the payment reference refuses a second order for the same transaction, and every order is confirmed against Flutterwave before it is written. A replay also cannot create an order for money that was never captured.

Enable **webhook retries** in the Flutterwave dashboard. Flutterwave retries a failed delivery three times, 30 minutes apart. A delivery refused with a 400 (wrong secret hash) is also retried, so fixing the setting recovers it.

Flutterwave sends `charge.completed` for failed attempts too, with `data.status` of `failed`. Those events release the checkout's stock lock; they never create an order.

## Test-mode notes (24 Nov 2026)

Verified against Flutterwave's real test API with a Nigerian test account (KES wallet):

- `GET /v3/balances`, `POST /v3/payments` and a real KES 1,500 M-Pesa payment all worked. The payment was completed through Flutterwave Inline on `checkout-v3.flutterwave.com`, which proves the Inline path this package does not ship yet.
- `GET /v3/transactions/:id/verify` and `verify_by_reference?tx_ref=` both returned the payment, and the provider's `getPaymentStatus` reported `succeeded` with `150000` minor units for a `1500` KES charge.
- A KES 500 partial refund succeeded with `status: "completed"`, and the `comments` tag came back exactly as sent. `meta.disburse_status` was `failed` in test mode, which is why the provider logs it.
- The hosted link Flutterwave issued pointed at `checkout-v2.dev-flutterwave.com`, which Cloudflare blocked from this network. The production host `checkout.flutterwave.com` did not recognise that test link. If your test link is blocked too, use a different network or ask Flutterwave support; the API side is unaffected.
- The public key and the secret key do **not** share their hash segment, so the public key cannot be derived from the secret key. Inline needs the public key stored, which is the core change described in the issue.

## Use

1. **Install** both packages:

```sh
npm install @dashcommerce/core@^0.4.0 @dashcommerce/flutterwave
```

2. **Register the provider at runtime.** Import the package in a module the server loads before any request, such as `src/middleware.ts`:

```ts
import "@dashcommerce/flutterwave"; // registers the "flutterwave" provider
```

Importing it in `astro.config.mjs` is not enough: that file runs only at build time. Importing twice is harmless. If a different provider already holds the `flutterwave` id, the import throws instead of silently replacing it.

3. **Allow Flutterwave's API host:**

```ts
emdash({ plugins: [dashcommerce({ allowedHosts: ["api.flutterwave.com"] })] });
```

4. **Choose Flutterwave and enter its keys** in Settings → Payment provider:

| Field | Value |
|---|---|
| Secret key | Settings → API Keys. Test keys start `FLWSECK_TEST-`; live keys start `FLWSECK-` |
| Secret hash | The secret hash you set under Settings → Webhooks. Flutterwave sends it back in `verif-hash` |

Save, then press **Test Flutterwave connection**. It makes one read-only call (`GET /v3/balances`) and says whether Flutterwave accepts the key, in test or live mode.

5. **Set the webhook URL** in the Flutterwave dashboard, including `?provider=flutterwave`:

```
https://<your-site>/_emdash/api/plugins/dashcommerce/checkout/webhook?provider=flutterwave
```

Without the query, core picks the provider from the `verif-hash` header. That works today, but the query keeps Flutterwave's webhooks going to Flutterwave even if another provider later uses the same header. Enable **refund** webhooks as well, so `refund.updated` reaches the store.

6. **Use the same secret hash for every environment you point at the same site.** Flutterwave's dashboard has one webhook configuration; a test-mode delivery and a live-mode delivery both carry that value.

## Develop

```sh
bun test # unit tests, plus checkout → webhook → order → refund through core's real routes
bun run typecheck
bun run build
```

The integration tests use a Flutterwave stand-in that only answers verification for transactions it created, and that enforces the major/minor conversion in both directions. A provider that verifies by the wrong identifier or sends cents to Flutterwave fails there.
48 changes: 48 additions & 0 deletions packages/flutterwave/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "@dashcommerce/flutterwave",
"version": "0.0.0",
"description": "Flutterwave payment provider for @dashcommerce/core: card, M-Pesa and mobile money through Flutterwave's hosted checkout, verified webhooks, live status checks and refunds.",
"type": "module",
"main": "dist/index.mjs",
"module": "dist/index.mjs",
"types": "dist/index.d.mts",
"exports": {
".": {
"import": "./dist/index.mjs",
"types": "./dist/index.d.mts"
}
},
"files": ["dist", "README.md"],
"engines": {
"node": ">=20"
},
"keywords": ["dashcommerce", "flutterwave", "payments", "mpesa", "mobile-money", "africa"],
"author": "DashCommerce contributors",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/emdashCommerce/dashcommerce.git",
"directory": "packages/flutterwave"
},
"homepage": "https://dashcommerce.dev",
"bugs": {
"url": "https://github.com/emdashCommerce/dashcommerce/issues"
},
"publishConfig": {
"access": "public"
},
"scripts": {
"build": "tsdown",
"dev": "tsdown --watch",
"typecheck": "tsc --noEmit",
"test": "bun test"
},
"peerDependencies": {
"@dashcommerce/core": "^0.4.0"
},
"devDependencies": {
"@dashcommerce/core": "workspace:*",
"tsdown": "^0.20.0",
"typescript": "^5.9.3"
}
}
106 changes: 106 additions & 0 deletions packages/flutterwave/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* Flutterwave API client — sandbox-safe, mirrors the calling convention
* established by @dashcommerce/core:
*
* - Uses the runtime context's `http.fetch` (not global fetch) so
* `allowedHosts` is honoured when this runs inside an EmDash sandbox.
* - JSON request/response bodies (Flutterwave REST API v3).
* - Auto-injects `Authorization: Bearer {secretKey}`.
* - Throws `FlutterwaveApiError` on a non-2xx or a body whose
* `status` is not `"success"`.
*/

import type { PaymentProviderRuntimeContext } from "@dashcommerce/core/payment-provider";

export const FLUTTERWAVE_API_BASE = "https://api.flutterwave.com/v3";

export class FlutterwaveApiError extends Error {
readonly status: number;
readonly body: Record<string, unknown>;
constructor(status: number, body: Record<string, unknown>, message: string) {
super(message);
this.name = "FlutterwaveApiError";
this.status = status;
this.body = body;
}
}

export interface FlutterwaveClientOptions {
secretKey: string;
/** Override for tests. */
apiBase?: string;
}

export interface FlutterwaveCallOptions {
method?: "GET" | "POST";
path: string; // leading slash, e.g. /payments
body?: Record<string, unknown>;
client: FlutterwaveClientOptions;
}

export interface FlutterwavePageInfo {
total?: number;
current_page?: number;
total_pages?: number;
page_size?: number;
}

export interface FlutterwaveResponse<T> {
status: "success" | "error";
message: string;
data: T;
meta?: { page_info?: FlutterwavePageInfo };
}

export async function call<T = Record<string, unknown>>(
ctx: PaymentProviderRuntimeContext,
opts: FlutterwaveCallOptions,
): Promise<T> {
return (await callEnvelope<T>(ctx, opts)).data;
}

/** As `call`, but returns the whole response, including list `meta`. */
export async function callEnvelope<T = Record<string, unknown>>(
ctx: PaymentProviderRuntimeContext,
opts: FlutterwaveCallOptions,
): Promise<FlutterwaveResponse<T>> {
const method = opts.method ?? "POST";
const base = opts.client.apiBase ?? FLUTTERWAVE_API_BASE;
const headers: Record<string, string> = {
Authorization: `Bearer ${opts.client.secretKey}`,
"Content-Type": "application/json",
};

// Only the runtime's fetch, which enforces the plugin's allowedHosts.
// Never fall back to global fetch: that would bypass the sandbox.
if (!ctx.http?.fetch) {
throw new Error("Flutterwave provider needs ctx.http.fetch (network:request capability)");
}
const res = await ctx.http.fetch(`${base}${opts.path}`, {
method,
headers,
...(opts.body ? { body: JSON.stringify(opts.body) } : {}),
});

const rawText = await res.text();
let parsed: FlutterwaveResponse<T>;
try {
parsed = JSON.parse(rawText) as FlutterwaveResponse<T>;
} catch {
throw new FlutterwaveApiError(
res.status,
{ raw: rawText },
`Flutterwave returned unparseable response (${res.status})`,
);
}

if (!res.ok || parsed.status !== "success") {
const msg =
parsed.message ||
(typeof parsed.data === "string" ? parsed.data : undefined) ||
`Flutterwave API error (${res.status})`;
throw new FlutterwaveApiError(res.status, parsed as unknown as Record<string, unknown>, msg);
}

return parsed;
}
49 changes: 49 additions & 0 deletions packages/flutterwave/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @dashcommerce/flutterwave
*
* Flutterwave PaymentProvider for DashCommerce: hosted checkout (card,
* M-Pesa and mobile money), `verif-hash` webhook verification, live status
* checks and refunds.
*
* Importing this module registers the provider with core's registry.
*/

import { getPaymentProvider, registerPaymentProvider } from "@dashcommerce/core/payment-provider";
import { flutterwavePaymentProvider } from "./provider";

export {
flutterwavePaymentProvider,
flutterwaveReferenceFor,
toMajorUnits,
toMinorUnits,
} from "./provider";
export {
verifyFlutterwaveSecretHash,
type VerifyInput,
type VerifyResult,
} from "./webhook-verify";
export {
call as callFlutterwave,
FlutterwaveApiError,
FLUTTERWAVE_API_BASE,
type FlutterwaveClientOptions,
type FlutterwaveCallOptions,
type FlutterwaveResponse,
} from "./client";

/**
* Register the Flutterwave provider. Safe to call more than once. If a
* different provider already holds the `flutterwave` id this throws, unless
* `{ override: true }` is passed, so a clash is never silent.
*/
export function registerFlutterwaveProvider(opts: { override?: boolean } = {}): void {
if (
!opts.override &&
getPaymentProvider(flutterwavePaymentProvider.id) === flutterwavePaymentProvider
) {
return;
}
registerPaymentProvider(flutterwavePaymentProvider, opts);
}

registerFlutterwaveProvider();
Loading