Chargily Pay Flutter SDK ๐ฉ๐ฟ
A production-ready Flutter SDK for integrating Chargily Pay V2, the leading payment gateway in Algeria.
This package provides a seamless way to accept EDAHABIA and CIB payments in your Flutter app. It handles the API complexity and provides a secure, drop-in UI for the payment flow.
โ ๏ธ Security Warning
NEVER ship your Chargily Secret Key inside your mobile app code.
- Production: Your mobile app should call your own backend, which then communicates with Chargily using the Secret Key.
- Development: You may use the Secret Key temporarily for testing, but remove it before release.
โจ Features
- ๐ Easy Integration: Simple
ChargilyClientwrapper for V2 API. - ๐ฑ Drop-in UI:
ChargilyCheckoutViewhandles the payment webview and redirects automatically. - ๐ก๏ธ Type-Safe: Fully typed Request & Response models (No more
Map<String, dynamic>). - ๐ฉ๐ฟ Localization: Supports
ar,en,frlocales. - ๐งช Test Mode: Built-in support for Chargily's Test API.
๐ฆ Installation
Add this to your pubspec.yaml:
dependencies:
chargily_pay: ^0.0.1
Or run:
flutter pub add chargily_pay
๐ Getting Started
1. Initialize the Client
You can initialize the client in Test or Live mode.
import 'package:chargily_pay/chargily_pay.dart';
// ๐งช FOR TESTING ONLY
final client = ChargilyClient(ChargilyConfig.test(
apiKey: 'test_pk_YOUR_API_KEY',
));
// ๐ FOR PRODUCTION (Recommended)
// In production, you typically don't need the API key in the Flutter app
// if you create checkouts on your backend.
final client = ChargilyClient(ChargilyConfig.live());
2. Create a Checkout
Ideally, fetch this from your backend. If you are prototyping, you can create it directly:
final request = CreateCheckoutRequest(
amount: 2000, // 2000 DZD
currency: 'dzd',
successUrl: 'https://myapp.com/success', // Deep link to your app
failureUrl: 'https://myapp.com/failure',
locale: 'ar', // Arabic checkout interface
description: 'Payment for Premium Subscription',
);
final checkout = await client.createCheckout(request);
3. Show the Payment Screen
Use the ChargilyCheckoutView widget to show the payment page. It automatically detects when the user finishes payment (success) or cancels (failure) based on the redirect URLs.
import 'package:chargily_pay/chargily_pay.dart';
// ... inside your button's onPressed:
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => ChargilyCheckoutView(
checkout: checkout,
onPaymentSuccess: () {
// Handle success (e.g., show success dialog)
Navigator.pop(context);
print("โ
Payment Successful!");
},
onPaymentFailure: () {
// Handle failure
Navigator.pop(context);
print("โ Payment Failed.");
},
onPaymentCancel: () {
// User closed the modal
Navigator.pop(context);
},
),
),
);
๐๏ธ Architecture
This package is designed with a Backend-First mindset:
- Mobile App: Requests a checkout from Your Backend.
- Your Backend: Calls Chargily API (
POST /checkouts) using the Secret Key. - Your Backend: Returns the
checkout_urlto the Mobile App. - Mobile App: Opens
ChargilyCheckoutViewwith the URL. - User: Pays via EDAHABIA/CIB.
- Chargily: Sends a Webhook to your backend to confirm payment.
๐งช Error Handling
The SDK throws specific exceptions to help you handle errors gracefully:
try {
final checkout = await client.createCheckout(request);
} on ChargilyNetworkException {
// Internet connection lost
} on ChargilyApiException catch (e) {
// API returned an error (e.g. 401 Unauthorized, 422 Validation)
print(e.message);
print(e.statusCode);
} catch (e) {
// Other errors
}
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the project
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
๐ License
Distributed under the MIT License. See LICENSE for more information.