test_gateway 0.0.1
test_gateway: ^0.0.1 copied to clipboard
A Flutter plugin for test payment gateway integration
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:test_gateway/test_gateway.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'CCAvenue Payment Demo',
theme: ThemeData(primarySwatch: Colors.blue, useMaterial3: true),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final _amountController = TextEditingController(text: "12.00");
String errorText = "";
bool _loading = false;
Future<void> _initiatePayment() async {
setState(() {
// _loading = true;
errorText = "";
});
try {
// Create payment parameters
final params = PaymentParams(
merchantId: "61116",
accessCode: "AVRF42ME06BS33FRSB",
amount: _amountController.text,
currency: "INR",
orderId: "ORDER_${DateTime.now().millisecondsSinceEpoch}",
trackingId: "2130000002050194",
requestHash:
"a08cc4d4f8d51dc9803df3e8bba1def448bca45b6a141af17068e51c3d6a93e4e0fd56076a48dcba7954cbd83ccf06183d9d9018e41fbfbb91d176f00cb7e81c",
// Billing details
billingName: "John Doe",
billingAddress: "Santa Cruz",
billingCity: "Mumbai",
billingState: "Maharashtra",
billingCountry: "India",
billingZip: "400054",
billingTel: "7620237601",
billingEmail: "[email protected]",
// Delivery details
deliveryName: "John Doe",
deliveryAddress: "Santa Cruz",
deliveryCity: "Mumbai",
deliveryState: "Maharashtra",
deliveryCountry: "India",
deliveryZip: "400054",
deliveryTel: "7620237601",
// URLs
redirectUrl: "https://example.com/success",
cancelUrl: "https://example.com/cancel",
// Optional customization
colorPrimary: "#164880",
colorAccent: "#EB2226",
colorFont: "#FFFFFF",
paymentType: "all",
paymentEnvironment: "app_local",
);
// Call the payment gateway
final response = await CcavenuePaymentGateway.pay(params);
// Handle response
if (!mounted) return;
setState(() {
_loading = false;
});
_showResponseDialog(response);
} catch (e) {
setState(() {
_loading = false;
errorText = "Error: $e";
});
}
}
void _showResponseDialog(PaymentResponse response) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(
response.status == 'success'
? 'Payment Successful'
: response.status == 'cancelled'
? 'Payment Cancelled'
: 'Payment Failed',
),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Status: ${response.status}'),
if (response.trackingId != null)
Text('Tracking ID: ${response.trackingId}'),
if (response.orderId != null) Text('Order ID: ${response.orderId}'),
if (response.message != null) Text('Message: ${response.message}'),
],
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('OK'),
),
],
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('CCAvenue Payment Gateway'),
elevation: 2,
),
body: _loading
? const Center(child: CircularProgressIndicator())
: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.payment, size: 80, color: Colors.blue),
const SizedBox(height: 40),
TextField(
controller: _amountController,
keyboardType: const TextInputType.numberWithOptions(
decimal: true,
),
decoration: InputDecoration(
labelText: "Amount (INR)",
prefixIcon: const Icon(Icons.currency_rupee),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
errorText: errorText.isEmpty ? null : errorText,
),
),
const SizedBox(height: 20),
SizedBox(
width: double.infinity,
height: 50,
child: ElevatedButton.icon(
onPressed: () {
if (_amountController.text.isNotEmpty) {
final amount = double.tryParse(
_amountController.text,
);
if (amount != null && amount > 0) {
_initiatePayment();
} else {
setState(() {
errorText = "Please enter a valid amount";
});
}
} else {
setState(() {
errorText = "Please enter an amount";
});
}
},
icon: const Icon(Icons.payment),
label: const Text(
"PAY NOW",
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
),
],
),
),
);
}
@override
void dispose() {
_amountController.dispose();
super.dispose();
}
}