cofee_payment_flutter 2.1.0
cofee_payment_flutter: ^2.1.0 copied to clipboard
CoFee Checkout for Flutter: opens the CoFee checkout overlay in a native WebView and reports payment success, failure and dismiss, matching the CoFee Checkout web SDK.
CoFee Payment Flutter SDK #
CoFee Checkout for Flutter. It opens the CoFee checkout full screen and returns success, failure and dismiss events to your app. It runs the CoFee Checkout web SDK and uses the same option and event names, with a Flutter-style API: set up once, then await each payment.
Always verify the payment on your server. Callbacks in the app are not a source of truth.
Requirements #
- A CoFee merchant publishable key
- A payment order id created on your server before opening checkout
- Dart SDK ^3.6.0, Flutter 3.27 or later
- Android: minSdk 21. The SDK declares the
INTERNETpermission itself, so your manifest needs no changes - iOS: 12.0 or later
Installation #
dependencies:
cofee_payment_flutter: ^2.0.0
flutter pub get
Quick start #
import 'package:cofee_payment_flutter/cofee_payment_flutter.dart';
// 1. Once: your merchant settings. Reuse this instance for every payment.
final cofee = CofeeCheckout(const CofeeCheckoutOptions(
key: 'YOUR_KEY_ID',
environment: CheckoutEnvironment.sandbox, // or CheckoutEnvironment.production
name: 'Acme Corp',
logoUrl: 'https://example.com/logo.png',
));
// 2. Per payment, when the customer chooses to pay:
final result = await cofee.open(orderId: 'ord_…');
if (result is CheckoutSuccess) {
// Payment completed in checkout. Verify it on your server.
print('${result.orderId} ${result.intentId}');
} else if (result is CheckoutFailed) {
print('${result.error.code}: ${result.error.description}');
} else {
// CheckoutDismissed: closed without paying.
}
For a list of orders, call open with each item's order id. Every result carries its orderId.
Payment UI v2 (beta) #
const CofeeCheckoutOptions(
key: 'YOUR_KEY_ID',
environment: CheckoutEnvironment.sandbox,
uiVersion: CheckoutUiVersion.v2,
)
API #
CofeeCheckout(CofeeCheckoutOptions options) #
Merchant settings, shared by every payment. Throws ArgumentError if key is empty or checkoutUrl is not an http(s) URL.
| Option | Web SDK | Required | Description |
|---|---|---|---|
key |
key |
Yes | Merchant publishable key |
environment |
(built in) | Yes | CheckoutEnvironment.sandbox or .production |
currency |
currency |
No | Defaults to INR |
name |
name |
No | Business / display name, shown as the merchant name in checkout |
description |
description |
No | Short description. Sent to the payment page, which does not display it yet |
logoUrl |
logo_url |
No | Merchant logo URL |
uiVersion |
ui_version |
No | CheckoutUiVersion.v2 for the v2 payment UI (default is v1) |
modal.escape |
modal.escape |
No | Whether the Android back button closes checkout (default true). The web SDK uses the Escape key |
theme.color |
theme.color |
No | Accent color for the v2 UI and the loading screen, as #rgb or #rrggbb |
theme.ctaVariant |
theme.ctaVarient |
No | Fill of the solid buttons, such as Pay, in the v2 UI: CheckoutCtaVariant.green (default, #05954e) or .dark (#262626). Buttons stay green with a custom theme.color unless this is dark |
checkoutUrl |
(built in) | No | Overrides the checkout host. For CoFee internal testing |
The amount comes from the order on CoFee's side. You do not pass it into the SDK.
open({required String orderId}) #
Opens checkout for one order and returns a Future<CheckoutResult> that completes when checkout closes. orderId (web SDK order_id) is the payment order id created on your server. Throws ArgumentError if it is empty.
The result is the final outcome of the order:
| Result | When |
|---|---|
CheckoutSuccess(orderId, intentId, amount, status) |
A payment succeeded, including after failed attempts the customer retried |
CheckoutFailed(orderId, error) |
The last attempt failed and the customer closed checkout, or checkout could not open (error.code is CHECKOUT_OPEN_FAILED, for example while another checkout is open) |
CheckoutDismissed(orderId) |
Checkout closed without a payment attempt |
Check the result with is, as in the quick start. CheckoutResult is sealed, so if you prefer a switch, Dart checks that it handles all three.
Events #
Listeners receive every event of every payment opened from the instance, as it happens. Use them for app-wide work such as logging or analytics, and the open result for your payment flow, so the same outcome is not handled twice.
cofee.on(CheckoutEvent.paymentFailed, (failed) {
analytics.log('payment_failed', {'order': failed.orderId, 'code': failed.error.code});
});
| Event | Web SDK | Listener receives |
|---|---|---|
CheckoutEvent.paymentSuccess |
payment.success / handler |
CheckoutSuccess |
CheckoutEvent.paymentFailed |
payment.failed |
CheckoutFailed, for every failed attempt. A success can follow if the customer retries |
CheckoutEvent.dismissed |
modal.ondismiss |
CheckoutDismissed when checkout closes without a successful payment |
Listeners live as long as the instance. If a screen registers them on a shared instance, remove them with off when the screen is disposed.
Other methods #
| Method | Description |
|---|---|
close() |
Closes checkout. open then completes with CheckoutDismissed unless a payment already succeeded or failed |
off(event, [handler]) |
Removes one listener, or all listeners for the event |
isOpen |
Whether checkout is showing |
Only one checkout can be open at a time, across all instances.
UPI apps (Android) #
When checkout opens on Android, the SDK looks up which UPI apps are installed and passes their package names to the payment page, so it can offer those apps directly. You don't call anything for this, and it needs no setup: the SDK declares the upi://pay query that Android 11+ needs. On iOS, no UPI apps are detected.
The package names are sent to CoFee as part of the checkout URL. If you publish on Google Play, declare this in your app's Data safety form as the Installed apps data type (under App activity), collected for app functionality.
Recommended flow #
- Create an order on your backend and receive an order id.
- Create one
CofeeCheckoutwith your merchant settings. - Call
open(orderId: …)when the customer chooses to pay. - On
CheckoutSuccess, confirm the payment with your server before fulfilling the order.
Results can be lost. While the customer is in a UPI or bank app, the system may close your app to free memory, and open never completes. When your app returns to the foreground with an order in progress, ask your server for the order status, or rely on CoFee's server-side notifications.
How it works #
CofeeCheckout runs the CoFee Checkout web SDK in a native WebView (an Android activity or an iOS view controller), so checkout looks and behaves the same as on the web. Bank and 3DS pages open as a window over the checkout, with a close button. UPI links open the matching payment app.
The bundled web SDK lives in assets/checkout/cofee-checkout.js. To update it, run tool/build_web_sdk.sh [git-ref].
Migrating from CoFeePayment (1.x) #
CoFeePayment still works but is deprecated.
| 1.x | 2.x |
|---|---|
CoFeePayment(clientId:, paymentEnvironment:) |
CofeeCheckout(CofeeCheckoutOptions(key:, environment:)) |
pay(orderId, onFinish:, onError:) |
await open(orderId: …) |
onFinish() for any outcome |
CheckoutSuccess, CheckoutFailed or CheckoutDismissed |
onError(PaymentException) |
CheckoutFailed with an error code |
Testing #
flutter test