import 'package:adyen_checkout/adyen_checkout.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:payment_plugin/domain/repositories/adyen_repository.dart';
import 'package:payment_plugin/presentation/app/bloc/payment_processing_state.dart';
class PaymentCubit extends Cubit<PaymentProcessingState> {
final AdyenRepository adyenRepository;
PaymentCubit({required this.adyenRepository}) : super(PaymentProcessingStateNotStarted());
Future<void> createSession(
int price,
String confirmationId,
bool applyClubPoints,
String hotelCode,
) async {
try {
emit(PaymentProcessingStateProcessing());
final paymentConfigurations = await adyenRepository.sessionCheckout(
hotelCode,
confirmationId,
applyClubPoints,
);
if (paymentConfigurations == null) {
emit(PaymentProcessingStateConfirmed());
} else {
emit(PaymentProcessingStateSessionReceived(paymentConfigurations: paymentConfigurations));
}
} catch (e) {
emit(PaymentProcessingStateError(message: "Error creating session"));
await Future<void>.delayed(const Duration(milliseconds: 1000));
}
}
Future<void> createConciergeSession(
String sessionId,
String clientKey,
String sessionData,
) async {
try {
emit(PaymentProcessingStateProcessing());
final paymentConfigurations = await adyenRepository.createSession(
sessionId,
clientKey,
sessionData,
);
emit(PaymentProcessingStateSessionReceived(paymentConfigurations: paymentConfigurations));
} catch (e) {
print("qqq payment Error = $e");
emit(PaymentProcessingStateError(message: "Error creating session"));
await Future<void>.delayed(const Duration(milliseconds: 1000));
}
}
Future<void> onPaymentResult(PaymentResult result) async {
switch (result) {
case PaymentAdvancedFinished _:
case PaymentSessionFinished _:
emit(PaymentProcessingStateConfirmed());
break;
case PaymentCancelledByUser _:
case PaymentError _:
emit(PaymentProcessingStateError(message: "Error processing payment"));
}
}
void emitError(Object e) {
emit(PaymentProcessingStateError(message: e.toString()));
}
void onUserDismissPaymentPopup() {
emit(PaymentProcessingStateNotStarted());
}
}