eghlflutter 1.5.8
eghlflutter: ^1.5.8 copied to clipboard
eGHL Flutter plugin facilitate a seamless integration experience.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:eghlflutter/eghlflutter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'eGHL Flutter Plugin',
theme: ThemeData(
primarySwatch: Colors.blue,
scaffoldBackgroundColor: Colors.white,
),
home: const PaymentPage(),
);
}
}
class PaymentPage extends StatefulWidget {
const PaymentPage({super.key});
@override
State<PaymentPage> createState() => _PaymentPageState();
}
class _PaymentPageState extends State<PaymentPage> {
final TextEditingController _amountController = TextEditingController(text: '1.00');
final TextEditingController _merchantController = TextEditingController(text: 'eGHL Payment Testing');
final TextEditingController _emailController = TextEditingController(text: 'johndoe@test.com');
final TextEditingController _customerController = TextEditingController(text: 'Testing');
final TextEditingController _serviceIdController = TextEditingController(text: 'GHL');
final TextEditingController _passwordController = TextEditingController(text: 'ghl12345');
final TextEditingController _currencyController = TextEditingController(text: 'MYR');
String _selectedPayMethod = 'ANY';
bool _isProduction = false;
String _paymentResult = '';
final List<String> _payMethods = ['ANY', 'CC', 'DD', 'OTC'];
Future<void> _executePayment(String transactionType) async {
String paymentId = 'SIT${DateTime.now().millisecondsSinceEpoch}';
try {
Map<String, dynamic> payment = {
'TransactionType': transactionType,
'Amount': _amountController.text,
'CurrencyCode': _currencyController.text,
'PaymentId': paymentId,
'OrderNumber': paymentId,
'PaymentDesc': 'Testing Payment',
'PymtMethod': _selectedPayMethod,
'CustName': _customerController.text,
'CustEmail': _emailController.text,
'CustPhone': '01112345678',
'MerchantName': _merchantController.text,
'MerchantReturnURL': 'https://pay.e-ghl.com/IPGSimulator/RespFrmGW.aspx',
'MerchantCallBackURL': 'https://pay.e-ghl.com/IPGSimulator/RespFrmGW.aspx',
'ServiceId': _serviceIdController.text,
'Password': _passwordController.text,
'LanguageCode': 'EN',
'PageTimeout': '600',
'PaymentGateway': !_isProduction,
'EnableCardPage': false,
'TriggerReturnURL': false,
'WebViewZoom': false,
'NumOfRequery': 1,
'ForceClosePayment': false,
'Loggable': true,
'PreloadView': true,
'PopupEnabled': true,
};
String result = await Eghlflutter.executePayment(payment);
setState(() {
_paymentResult = result;
});
} catch (e) {
setState(() {
_paymentResult = 'Error: $e';
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('eGHL Flutter Plugin'),
centerTitle: true,
),
body: SingleChildScrollView(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildTextField('Amount:', _amountController),
_buildTextField('Merchant:', _merchantController),
_buildTextField('Email:', _emailController),
_buildTextField('Customer:', _customerController),
_buildTextField('ServiceID:', _serviceIdController),
_buildTextField('Password:', _passwordController),
_buildTextField('Currency:', _currencyController),
const SizedBox(height: 12),
_buildPayMethodSelector(),
const SizedBox(height: 16),
_buildHostSwitch(),
const SizedBox(height: 24),
_buildActionButtons(),
const SizedBox(height: 24),
if (_paymentResult.isNotEmpty) _buildResultSection(),
],
),
),
);
}
Widget _buildTextField(String label, TextEditingController controller) {
return Padding(
padding: const EdgeInsets.only(bottom: 12),
child: Row(
children: [
SizedBox(
width: 100,
child: Text(
label,
style: const TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
),
),
Expanded(
child: TextField(
controller: controller,
decoration: InputDecoration(
filled: true,
fillColor: Colors.black,
contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(6),
borderSide: BorderSide.none,
),
),
style: const TextStyle(color: Colors.white, fontSize: 14),
),
),
],
),
);
}
Widget _buildPayMethodSelector() {
return Row(
children: [
const SizedBox(
width: 100,
child: Text(
'PayMethod:',
style: TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
),
),
Expanded(
child: Wrap(
spacing: 8,
runSpacing: 8,
children: _payMethods.map((method) {
final isSelected = _selectedPayMethod == method;
return GestureDetector(
onTap: () => setState(() => _selectedPayMethod = method),
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
decoration: BoxDecoration(
color: isSelected ? Colors.cyan : Colors.grey[700],
borderRadius: BorderRadius.circular(6),
),
child: Text(
method,
style: const TextStyle(color: Colors.white, fontSize: 13),
),
),
);
}).toList(),
),
),
],
);
}
Widget _buildHostSwitch() {
return Row(
children: [
const SizedBox(
width: 100,
child: Text(
'RealHost:',
style: TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
),
),
Switch(
value: _isProduction,
onChanged: (value) => setState(() => _isProduction = value),
),
Text(
_isProduction ? 'Production' : 'Staging',
style: TextStyle(
fontSize: 13,
color: _isProduction ? Colors.red : Colors.green,
fontWeight: FontWeight.w500,
),
),
],
);
}
Widget _buildActionButtons() {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
_buildActionButton('Query', 'QUERY', Colors.cyan),
_buildActionButton('Sale', 'SALE', Colors.cyan),
_buildActionButton('Pre-Auth', 'AUTH', Colors.cyan),
_buildActionButton('Capture', 'CAPTURE', Colors.cyan),
],
);
}
Widget _buildActionButton(String label, String transactionType, Color color) {
return TextButton(
onPressed: () => _executePayment(transactionType),
child: Text(
label,
style: TextStyle(color: color, fontSize: 14, fontWeight: FontWeight.w600),
),
);
}
Widget _buildResultSection() {
return Container(
width: double.infinity,
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Colors.grey[100],
borderRadius: BorderRadius.circular(8),
border: Border.all(color: Colors.grey[300]!),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Result:',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14),
),
const SizedBox(height: 6),
Text(
_paymentResult,
style: const TextStyle(fontSize: 12),
),
],
),
);
}
@override
void dispose() {
_amountController.dispose();
_merchantController.dispose();
_emailController.dispose();
_customerController.dispose();
_serviceIdController.dispose();
_passwordController.dispose();
_currencyController.dispose();
super.dispose();
}
}