import 'package:payment_plugin/domain/repositories/adyen_repository.dart';
import 'package:payment_plugin/payment_config.dart';
/// Main entry point for the Payment Plugin
///
/// Initialize once in your app:
/// ```dart
/// await PaymentPlugin.initialize(
/// config: PaymentConfig(
/// tokenProvider: () async => authService.getToken(),
/// baseUrl: 'https://api.example.com',
/// ),
/// );
/// ```
///
/// Then access anywhere:
/// ```dart
/// final cards = await PaymentPlugin.instance.getCards();
/// ```
class PaymentPlugin {
static PaymentPlugin? _instance;
late final AdyenRepository _repository;
PaymentPlugin._(PaymentConfig config) {
_repository = AdyenRepository(
dio: config.dio,
);
}
/// Initialize the payment plugin with configuration
///
/// This should be called once, typically in your app's initialization
static Future<void> initialize({required PaymentConfig config}) async {
_instance = PaymentPlugin._(config);
}
/// Get the singleton instance of PaymentPlugin
///
/// Throws [StateError] if [initialize] hasn't been called yet
static PaymentPlugin get instance {
if (_instance == null) {
throw StateError(
'PaymentPlugin has not been initialized. '
'Call PaymentPlugin.initialize() first.',
);
}
return _instance!;
}
/// Check if the plugin has been initialized
static bool get isInitialized => _instance != null;
/// Reset the instance (mainly for testing)
static void reset() {
_instance = null;
}
/// Access the Adyen repository for payment operations
AdyenRepository get repository => _repository;
// Convenience methods that delegate to the repository
/// Get all stored payment methods (cards)
Future<Iterable<dynamic>> getCards() => _repository.getCards();
/// Remove a stored payment method
Future<void> removeCard(String cardId) => _repository.removeCard(cardId);
/// Add a new payment method
Future<dynamic> addCard(dynamic paymentMethod) =>
_repository.addCard(paymentMethod);
/// Create a payment session for checkout
Future<dynamic> sessionCheckout(
String hotelCode,
String bookingId,
bool usePoints,
) => _repository.sessionCheckout(hotelCode, bookingId, usePoints);
/// Create a payment session for adding cards
Future<dynamic> fetchSessionForAddingCard() =>
_repository.fetchSessionForAddingCard();
}