eps_pg_flutter 0.0.1
eps_pg_flutter: ^0.0.1 copied to clipboard
A Flutter package to integrate **EPS Bangladesh Payment Gateway** into Flutter apps. Supports token generation, checkout via WebView, and payment callback handling.
import 'package:flutter/material.dart';
import 'package:eps_pg_flutter/eps_pg_flutter.dart';
void main() => runApp(const MaterialApp(debugShowCheckedModeBanner: false, home: EpsExampleApp()));
class EpsExampleApp extends StatefulWidget {
const EpsExampleApp({super.key});
@override
State<EpsExampleApp> createState() => _EpsExampleAppState();
}
class _EpsExampleAppState extends State<EpsExampleApp> {
EpsPGEnvironment _selectedEnv = EpsPGEnvironment.testbox;
String _statusLog = "Ready to pay...";
// 1. Text Controllers for Live Credentials
final _userEmailController = TextEditingController();
final _passwordController = TextEditingController();
final _hashKeyController = TextEditingController();
final _merchantIdController = TextEditingController();
final _storeIdController = TextEditingController();
// Elegant Colors
static const Color colorLive = Color(0xFF009456);
static const Color colorTest = Color(0xFFEE2D43);
Future<void> _initiatePayment() async {
final String txnId = DateTime.now().millisecondsSinceEpoch.toString();
// 2. Initialize Config (Uses Controllers for Live, Defaults for Test)
final initializer = EpsPGInitialization(
environment: _selectedEnv,
userName: _selectedEnv == EpsPGEnvironment.live ? _userEmailController.text : 'Epsdemo@gmail.com',
password: _selectedEnv == EpsPGEnvironment.live ? _passwordController.text : 'Epsdemo258@',
hashKey: _selectedEnv == EpsPGEnvironment.live ? _hashKeyController.text : 'FHZxyzeps56789gfhg678ygu876o=',
merchantId: _selectedEnv == EpsPGEnvironment.live ? _merchantIdController.text : "29e86e70-0ac6-45eb-ba04-9fcb0aaed12a",
storeId: _selectedEnv == EpsPGEnvironment.live ? _storeIdController.text : "d44e705f-9e3a-41de-98b1-1674631637da",
);
final eps = EpsPGController(initializer: initializer);
final request = EpsPGPaymentRequest(
merchantId: initializer.merchantId,
storeId: initializer.storeId,
customerOrderId: "ORD-$txnId",
merchantTransactionId: txnId,
totalAmount: 5.0,
successUrl: "https://example.com/success",
failUrl: "https://example.com/fail",
cancelUrl: "https://example.com/cancel",
customerName: "Roman Hossen",
customerEmail: "example@gmail.com",
customerAddress: "Dhaka",
customerCity: "Dhaka",
customerPostcode: "1212",
customerCountry: "BD",
customerPhone: "01777777777",
productName: "Demo Product",
);
setState(() => _statusLog = "Initializing...");
final response = await eps.initializePayment(request);
if (response.paymentUrl != null) {
if (!mounted) return;
Navigator.push(context, MaterialPageRoute(
builder: (_) => EpsPGWebView(
url: response.paymentUrl!,
onPaymentFinished: (status) async {
if (status == 'SUCCESS') {
final verifyRes = await eps.verifyTransaction(txnId);
setState(() => _statusLog = "SUCCESS: ${verifyRes.merchantTransactionId} (${verifyRes.status})");
} else {
setState(() => _statusLog = "Payment $status");
}
},
),
));
} else {
setState(() => _statusLog = "Error: ${response.message}");
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("EPS Gateway Configuration"), centerTitle: true),
body: SingleChildScrollView( // Allow scrolling when keyboard appears
padding: const EdgeInsets.all(24.0),
child: Column(
children: [
Container(
padding: const EdgeInsets.all(4),
decoration: BoxDecoration(color: Colors.grey.shade200, borderRadius: BorderRadius.circular(12)),
child: Row(
children: [
_buildEnvButton("Test ", EpsPGEnvironment.testbox),
_buildEnvButton("Live ", EpsPGEnvironment.live),
],
),
),
const SizedBox(height: 20),
// 3. Conditional Input Fields for Live Mode
if (_selectedEnv == EpsPGEnvironment.live) ...[
const Text("Edit Live Credentials", style: TextStyle(fontWeight: FontWeight.bold, color: colorLive)),
const SizedBox(height: 15),
_buildTextField(_userEmailController, "User Email / Name"),
_buildTextField(_passwordController, "Password", isObscure: true),
_buildTextField(_hashKeyController, "Hash Key"),
_buildTextField(_merchantIdController, "Merchant ID"),
_buildTextField(_storeIdController, "Store ID"),
] else ...[
const SizedBox(height: 40),
const Icon(Icons. science_outlined, size: 60, color: colorTest),
const Text("Running on Sandbox Environment"),
const Text("Using Epsdemo@gmail.com", style: TextStyle(color: Colors.grey)),
],
const SizedBox(height: 30),
Text(_statusLog, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w500)),
const SizedBox(height: 30),
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: _initiatePayment,
style: ElevatedButton.styleFrom(
backgroundColor: _selectedEnv == EpsPGEnvironment.live ? colorLive : colorTest,
padding: const EdgeInsets.symmetric(vertical: 16),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
),
child: Text("PAY WITH ${_selectedEnv.name}", style: const TextStyle(color: Colors.white)),
),
),
],
),
),
);
}
// 4. Helper Widget for TextFields
Widget _buildTextField(TextEditingController controller, String label, {bool isObscure = false}) {
return Padding(
padding: const EdgeInsets.only(bottom: 12),
child: TextField(
controller: controller,
obscureText: isObscure,
decoration: InputDecoration(
labelText: label,
border: const OutlineInputBorder(),
contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
),
),
);
}
Widget _buildEnvButton(String label, EpsPGEnvironment env) {
bool isSelected = _selectedEnv == env;
return Expanded(
child: GestureDetector(
onTap: () => setState(() => _selectedEnv = env),
child: AnimatedContainer(
duration: const Duration(milliseconds: 200),
padding: const EdgeInsets.symmetric(vertical: 12),
decoration: BoxDecoration(
color: isSelected ? (env == EpsPGEnvironment.live ? colorLive : colorTest) : Colors.transparent,
borderRadius: BorderRadius.circular(10),
),
child: Center(
child: Text(label, style: TextStyle(color: isSelected ? Colors.white : Colors.black54, fontWeight: FontWeight.bold)),
),
),
),
);
}
}