payment_gateway_plugin 5.0.0
payment_gateway_plugin: ^5.0.0 copied to clipboard
Omniware's Payment Gateway Plugin for Flutter
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:payment_gateway_plugin/payment_gateway_plugin.dart';
import 'hash_generator.dart';
void main() {
PaymentGatewayPlugin.init();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: _AppState(),
);
}
}
class _AppState extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<_AppState> {
dynamic response;
String paymentResponse = "";
final _formKey = GlobalKey<FormState>();
final Map<String, TextEditingController> controllers = {
'api_key': TextEditingController(),
'salt_key': TextEditingController(),
'payment_url': TextEditingController(),
'name': TextEditingController(text: 'Tester Sharma'),
'email': TextEditingController(text: 'tester@test.com'),
'phone': TextEditingController(text: '9876543210'),
'amount': TextEditingController(text: '2.00'),
'description': TextEditingController(text: 'test'),
'address_line_1': TextEditingController(text: 'ad1'),
'address_line_2': TextEditingController(text: 'ad2'),
'city': TextEditingController(text: 'Bangalore'),
'state': TextEditingController(text: 'Karnataka'),
'country': TextEditingController(text: 'IND'),
'zip_code': TextEditingController(text: '560043'),
'currency': TextEditingController(text: 'INR'),
'mode': TextEditingController(text: 'LIVE'),
'return_url': TextEditingController(text: 'http://localhost:8888/paymentresponse'),
'enable_auto_refund': TextEditingController(text: 'n'),
};
@override
void dispose() {
controllers.forEach((_, controller) => controller.dispose());
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Payment Gateway'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: ListView(
children: [
const Text(
'Configuration',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
const SizedBox(height: 20),
_buildTextField('API Key', controllers['api_key']!),
_buildTextField('Salt Key', controllers['salt_key']!),
_buildTextField('Payment URL', controllers['payment_url']!),
const SizedBox(height: 20),
const Text(
'Payment Details',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
const SizedBox(height: 20),
_buildTextField('Name', controllers['name']!),
_buildTextField('Email', controllers['email']!, keyboardType: TextInputType.emailAddress),
_buildTextField('Phone', controllers['phone']!, keyboardType: TextInputType.phone),
_buildTextField('Amount', controllers['amount']!, keyboardType: TextInputType.number),
_buildTextField('Description', controllers['description']!),
_buildTextField('Address Line 1', controllers['address_line_1']!),
_buildTextField('Address Line 2', controllers['address_line_2']!),
_buildTextField('City', controllers['city']!),
_buildTextField('State', controllers['state']!),
_buildTextField('Country', controllers['country']!),
_buildTextField('ZIP Code', controllers['zip_code']!),
_buildTextField('Currency', controllers['currency']!),
_buildTextField('Mode', controllers['mode']!),
_buildTextField('Return URL', controllers['return_url']!),
_buildTextField('Enable Auto Refund', controllers['enable_auto_refund']!),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
final userInputs = Map<String, dynamic>.fromEntries(
controllers.entries.map(
(e) => MapEntry(e.key, e.value.text),
),
);
final params = generatePaymentParams(userInputs);
open(params, context);
}
},
child: const Text('Pay Now'),
),
if (paymentResponse.isNotEmpty)
Container(
alignment: Alignment.center,
margin: const EdgeInsets.all(10),
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(8),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
IconButton(
icon: const Icon(Icons.copy, color: Colors.white),
onPressed: () {
Clipboard.setData(ClipboardData(text: paymentResponse));
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Response copied to clipboard')),
);
},
),
],
),
SelectableText(
paymentResponse,
style: const TextStyle(
color: Colors.green,
fontFamily: 'monospace',
fontSize: 16,
),
),
],
),
),
if(paymentResponse.isEmpty)
Container(
alignment: Alignment.center,
padding: const EdgeInsets.all(10),
child: const Text(
'Payment Response is Empty',
style: TextStyle(
color: Colors.blue,
fontWeight: FontWeight.w500,
fontSize: 20,
),
),
),
],
),
),
),
),
);
}
Widget _buildTextField(String label, TextEditingController controller, {TextInputType? keyboardType}) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: TextFormField(
controller: controller,
keyboardType: keyboardType,
decoration: InputDecoration(
labelText: label,
border: const OutlineInputBorder(),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter $label';
}
return null;
},
),
);
}
void open(Map<String, dynamic> request, BuildContext context) async {
try {
print("Request Params => $request");
response = await PaymentGatewayPlugin.open(controllers['payment_url']!.text, request);
if (response != null) {
print("Response => ${response['response']}");
// Format the response as a pretty JSON string
final formattedResponse = _formatResponseJson(response);
setState(() {
paymentResponse = formattedResponse;
});
} else {
setState(() {
paymentResponse = "No response received from the payment gateway.";
});
}
} on PlatformException {
setState(() {
paymentResponse = 'Failed to initiate payment.';
});
}
}
String _formatResponseJson(dynamic response) {
try {
// Create a more copy-friendly format
StringBuffer buffer = StringBuffer();
// Add all response fields in a clean format
response.forEach((key, value) {
buffer.writeln("$key: $value");
});
return buffer.toString();
} catch (e) {
return response.toString();
}
}
}