Flutter
Accept card payments in a Flutter app with the Cardencer payment sheet.
The Cardencer Flutter plugin gives you the native payment sheet on iOS and Android from Dart.
Requirements#
- Android 7.0 (API level 24) or later, Android Gradle Plugin 8.5+, Gradle 8.8+, AndroidX
- iOS 13.0 or later with CocoaPods
1. Set up your server#
Your server creates the payment and returns its clientSecret. Use the same endpoint as for Android. Keep your secret key on the server.
2. Install the plugin#
Add the Cardencer Flutter plugin to pubspec.yaml:
dependencies:
flutter_hyperswitch: ^1.5.2Check pub.dev for the latest version, then fetch dependencies and apply the native plugins:
flutter pub get
dart run flutter_hyperswitch:apply_plugins3. Initialise the SDK#
import 'package:flutter_hyperswitch/flutter_hyperswitch.dart';
final _sdk = FlutterHyperswitch();
void initSdk() {
_sdk.init(HyperConfig(
publishableKey: 'pk_snd_REPLACE_WITH_YOUR_KEY',
customBackendUrl: 'https://sandbox.cardencer.com',
));
}Use https://api.cardencer.com and your pk_prd_ key in production.
4. Fetch the payment and start a session#
Future<String> fetchClientSecret() async {
final response = await http.post(Uri.parse('$API_URL/create-payment'));
return jsonDecode(response.body)['clientSecret'];
}
Future<Session?> startSession() async {
final clientSecret = await fetchClientSecret();
return _sdk.initPaymentSession(PaymentMethodParams(clientSecret: clientSecret));
}5. Present the payment sheet#
Call presentPaymentSheet() from your pay button:
Future<void> pay(Session session) async {
final response = await _sdk.presentPaymentSheet(session);
if (response == null) return;
final message = response.message;
setState(() {
_statusText = message.isLeft
? '${response.status.name}\n${message.left!.name}'
: '${response.status.name}\n${message.right}';
});
}The sheet's status is for display. Confirm succeeded on your server, through a webhook or GET /payments/{payment_id}, before you deliver.
Test it#
Use the test cards in the sheet.