chargily_pay 0.0.1
chargily_pay: ^0.0.1 copied to clipboard
A production-ready Flutter SDK for integrating Chargily Pay V2 (EDAHABIA & CIB). Secure, typed, and easy to use.
import 'package:flutter/material.dart';
import 'package:chargily_pay/chargily_pay.dart';
void main() {
runApp(const MaterialApp(home: ChargilyDemoPage()));
}
class ChargilyDemoPage extends StatefulWidget {
const ChargilyDemoPage({super.key});
@override
State<ChargilyDemoPage> createState() => _ChargilyDemoPageState();
}
class _ChargilyDemoPageState extends State<ChargilyDemoPage> {
// ⚠️ API KEY WARNING:
// In a real app, NEVER put this key here.
// You should create the checkout on your Backend Server, not the mobile app.
// We use it here only for the sake of this demo.
final _client = ChargilyClient(ChargilyConfig.test(
apiKey: 'test_pk_YOUR_TEST_KEY_HERE', // <-- REPLACE THIS IF YOU HAVE ONE
));
bool _isLoading = false;
Future<void> _startPayment() async {
setState(() => _isLoading = true);
try {
// 1. Create Checkout
final checkout = await _client.createCheckout(const CreateCheckoutRequest(
amount: 2000, // 2000 DZD
currency: 'dzd',
successUrl: 'https://example.com/success',
// In this demo, we can use the same URL for failure or a different one
failureUrl: 'https://example.com/failure',
description: 'Payment for Premium Plan',
));
if (!mounted) return;
// 2. Launch Payment View
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => ChargilyCheckoutView(
checkout: checkout,
onPaymentSuccess: () {
Navigator.pop(context); // Close WebView
_showResultDialog("Success!", "Payment completed successfully.");
},
onPaymentFailure: () {
Navigator.pop(context); // Close WebView
_showResultDialog("Failed", "Payment was failed or declined.");
},
onPaymentCancel: () {
Navigator.pop(context); // Close WebView
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("Payment canceled by user")),
);
},
),
),
);
} catch (e) {
_showResultDialog("Error", e.toString());
} finally {
if (mounted) setState(() => _isLoading = false);
}
}
void _showResultDialog(String title, String message) {
showDialog(
context: context,
builder: (_) => AlertDialog(
title: Text(title),
content: Text(message),
actions: [
TextButton(
onPressed: () => Navigator.pop(context), child: const Text("OK"))
],
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Chargily Pay Flutter Demo")),
body: Center(
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.shopping_cart, size: 64, color: Colors.blue),
const SizedBox(height: 24),
const Text(
"Premium Plan",
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
const Text("2000 DZD",
style: TextStyle(fontSize: 18, color: Colors.grey)),
const SizedBox(height: 48),
if (_isLoading)
const CircularProgressIndicator()
else
SizedBox(
width: double.infinity,
height: 50,
child: ElevatedButton(
onPressed: _startPayment,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
),
child: const Text("Pay with Chargily"),
),
),
],
),
),
),
);
}
}