payfast_flutter 0.0.2
payfast_flutter: ^0.0.2 copied to clipboard
Flutter SDK for PayFast payment gateway integration with WebView support.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:payfast_flutter/payfast_flutter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: HomeScreen(),
debugShowCheckedModeBanner: false,
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
PayFastResult? _lastResult;
String _paymentStatus = "Ready to pay";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("PayFast SDK Demo"),
elevation: 0,
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Payment Status Card
Card(
elevation: 2,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
Text(
"Payment Status",
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 12),
Text(
_paymentStatus,
style: Theme.of(context).textTheme.bodyMedium,
textAlign: TextAlign.center,
),
if (_lastResult != null) ...[
const SizedBox(height: 16),
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: _lastResult!.success
? Colors.green[100]
: Colors.red[100],
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: _lastResult!.success
? Colors.green
: Colors.red,
),
),
child: Column(
children: [
Text(
_lastResult!.success
? '✓ Payment Successful'
: '✗ Payment Failed',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: _lastResult!.success
? Colors.green[700]
: Colors.red[700],
),
),
const SizedBox(height: 8),
Text(
_lastResult!.message,
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 14),
),
if (_lastResult!.transactionId != null) ...[
const SizedBox(height: 8),
Text(
'Transaction ID: ${_lastResult!.transactionId}',
style: const TextStyle(
fontSize: 12,
fontWeight: FontWeight.w500,
),
),
],
],
),
),
],
],
),
),
),
const SizedBox(height: 32),
// Payment Buttons
ElevatedButton.icon(
onPressed: () => _processPayment(
context,
amount: "100.00",
description: "Premium Package",
),
icon: const Icon(Icons.payment),
label: const Text("Pay 100 PKR"),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
),
),
const SizedBox(height: 12),
ElevatedButton.icon(
onPressed: () => _processPayment(
context,
amount: "500.00",
description: "Deluxe Package",
),
icon: const Icon(Icons.payment),
label: const Text("Pay 500 PKR"),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
),
),
const SizedBox(height: 12),
ElevatedButton.icon(
onPressed: () => _processPayment(
context,
amount: "1000.00",
description: "Enterprise Package",
),
icon: const Icon(Icons.payment),
label: const Text("Pay 1000 PKR"),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
),
),
const SizedBox(height: 32),
// Info Section
Card(
color: Colors.blue[50],
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Demo Credentials",
style: Theme.of(context).textTheme.titleSmall,
),
const SizedBox(height: 8),
const Text(
"Merchant ID: 241665\n"
"Test Mode: Sandbox",
style: TextStyle(fontSize: 12),
),
],
),
),
),
],
),
),
);
}
void _processPayment(
BuildContext context, {
required String amount,
required String description,
}) async {
if (!context.mounted) return;
final config = PayFastConfig(
merchantId: "241665",
securedKey: "tDy0TwuynmeiJP3FiVg-YAOC",
basketId: "ORD-${DateTime.now().millisecondsSinceEpoch}",
amount: amount,
currency: "PKR",
txnDesc: description,
customerEmail: "customer@example.com",
customerMobile: "03001234567",
successUrl: "https://example.com/success",
failureUrl: "https://example.com/failure",
checkoutUrl: "https://example.com/ipn",
environment: "sandbox",
);
setState(() {
_paymentStatus = "Processing payment for $amount PKR...";
});
PayFast.startPayment(
context: context,
config: config,
onResult: (result) {
if (!mounted) return;
setState(() {
_lastResult = result;
_paymentStatus =
result.success ? "Payment Completed" : "Payment Failed";
});
debugPrint("Payment Result: ${result.success}");
debugPrint("Message: ${result.message}");
debugPrint("Transaction ID: ${result.transactionId}");
},
);
}
}