cofee_payment_flutter 2.0.0
cofee_payment_flutter: ^2.0.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.
example/lib/main.dart
import 'package:cofee_payment_flutter/cofee_payment_flutter.dart';
import 'package:flutter/material.dart';
/// Main entry point for the CoFee Checkout example application.
///
/// This example demonstrates how to integrate [CofeeCheckout] in a Flutter
/// application, including success, failure and dismiss handling.
void main() {
runApp(const CoffeePaymentTestApp());
}
/// The root widget of the CoFee Checkout example application.
class CoffeePaymentTestApp extends StatelessWidget {
const CoffeePaymentTestApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'CoFee Checkout Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.green),
useMaterial3: true,
),
home: const PaymentPage(),
);
}
}
/// A demonstration page showing how to use [CofeeCheckout].
///
/// This page shows how to:
/// - Configure checkout with a merchant key, order id and environment
/// - Listen for success, failure and dismiss
/// - Give the user feedback on the outcome
class PaymentPage extends StatefulWidget {
const PaymentPage({super.key});
@override
State<PaymentPage> createState() => _PaymentPageState();
}
class _PaymentPageState extends State<PaymentPage> {
// Text controllers for user input
final TextEditingController _orderIdController =
TextEditingController(text: 'ord_j23ngec8uL8613_test');
final TextEditingController _keyController =
TextEditingController(text: 'YOUR_KEY_ID');
// Toggle for sandbox/production environment
bool isSandbox = true;
String _statusMessage = '';
bool _isError = false;
@override
void dispose() {
_orderIdController.dispose();
_keyController.dispose();
super.dispose();
}
void _setStatus(String message, {bool isError = false}) {
if (!mounted) return;
setState(() {
_statusMessage = message;
_isError = isError;
});
}
/// Opens CoFee Checkout for the entered order and shows the result.
///
/// The order must be created on your server first. Results are not a
/// source of truth: verify the payment on your server before fulfilling
/// the order.
Future<void> _openCheckout() async {
final CofeeCheckout checkout;
try {
// Merchant settings. In an app these are usually fixed, so one
// CofeeCheckout can be created once and reused for every order.
checkout = CofeeCheckout(CofeeCheckoutOptions(
key: _keyController.text.trim(),
environment: isSandbox
? CheckoutEnvironment.sandbox
: CheckoutEnvironment.production,
name: 'CoFee Demo',
description: 'Test transaction',
));
} on ArgumentError catch (e) {
_setStatus('Error: ${e.message}', isError: true);
return;
}
// Optional: listeners see every event, e.g. each failed attempt before
// a successful retry. Useful for logging or analytics.
checkout.on(CheckoutEvent.paymentFailed, (failed) {
debugPrint('Attempt failed for ${failed.orderId}: ${failed.error.code}');
});
_setStatus('');
final CheckoutResult result;
try {
result = await checkout.open(orderId: _orderIdController.text.trim());
} on ArgumentError catch (e) {
_setStatus('Error: ${e.message}', isError: true);
return;
}
if (result is CheckoutSuccess) {
_setStatus('Payment completed for ${result.orderId} '
'(intent ${result.intentId}). Verify it on your server.');
} else if (result is CheckoutFailed) {
_setStatus('Error: ${result.error.code} – ${result.error.description}',
isError: true);
} else {
// CheckoutDismissed: closed without paying.
_setStatus('Checkout closed without paying');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Padding(
padding: const EdgeInsets.all(24.0),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
// Coffee icon
Image.asset(
"assets/images/logo.png",
width: 80,
height: 80,
),
const SizedBox(height: 16),
const Text(
'Coffee Payment',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 24),
TextField(
controller: _keyController,
decoration: const InputDecoration(
labelText: 'Merchant key',
border: OutlineInputBorder(),
hintText: 'Merchant publishable key',
),
),
const SizedBox(height: 16),
Row(
children: [
const Text("isSandbox"),
Switch(
value: isSandbox,
onChanged: (v) {
setState(() {
isSandbox = v;
});
}),
],
),
const SizedBox(height: 16),
TextField(
controller: _orderIdController,
decoration: const InputDecoration(
labelText: 'Order ID',
border: OutlineInputBorder(),
hintText: 'Enter the order ID',
),
),
const SizedBox(height: 16),
// Status message
if (_statusMessage.isNotEmpty)
Padding(
padding: const EdgeInsets.only(bottom: 16.0),
child: Text(
_statusMessage,
style: TextStyle(
color: _isError ? Colors.red : Colors.green,
),
),
),
// Pay button
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: _openCheckout,
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 12),
),
child: const Text(
'Pay Now',
style: TextStyle(fontSize: 16),
),
),
),
],
),
),
),
),
);
}
}