import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:initial_folder/models/detail_order_model.dart'; import 'package:initial_folder/models/detail_order_model_underscore.dart'; import 'package:initial_folder/models/zero_price_model.dart'; import 'package:initial_folder/services/payment_service.dart'; import '../screens/checkout/snap_payment_page.dart'; enum ResultState { error, success, gagal, loading } enum Process { uninitialized, loading } class PaymentsProvider with ChangeNotifier { PaymentsProvider({required this.paymentServices}); final PaymentServices paymentServices; ResultState? _state; Process _stateProcess = Process.uninitialized; bool _paymentModel = false; ZeroPrice? _zeroPrice; String? _idOrders; List _detailOrder = []; List _detailOrderUnderscore = []; ResultState? get state => _state; Process get stateProcess => _stateProcess; bool get result => _paymentModel; String? get idOrders => _idOrders; ZeroPrice? get zeroPrice => _zeroPrice; List get detailOrder => _detailOrder; List get detailOrderUnderscore => _detailOrderUnderscore; String get orderId { if (_detailOrder.isNotEmpty) { return _detailOrder[0].idOrder; } else { return ''; } } set selectedIdOrders(String value) { _idOrders = value; notifyListeners(); } // mulai Snap Payment Future?> startSnapPayment({ required String orderId, required int grossAmount, required List> dataInvoice, }) async { try { _stateProcess = Process.loading; notifyListeners(); Map transactionToken = await paymentServices.getSnapTransactionToken( orderId: orderId, grossAmount: grossAmount, dataInvoice: dataInvoice, ); if (transactionToken.isNotEmpty) { _stateProcess = Process.uninitialized; _state = ResultState.success; print('Transaction token didapatkan: $transactionToken'); notifyListeners(); return transactionToken; } else { _state = ResultState.gagal; _stateProcess = Process.uninitialized; notifyListeners(); return null; } } catch (e) { print('Error Snap Payment -> $e'); _state = ResultState.error; _stateProcess = Process.uninitialized; notifyListeners(); return null; } } Future> getSnapTransactionToken({ required String orderId, required int grossAmount, required List> dataInvoice, }) async { return await paymentServices.getSnapTransactionToken( orderId: orderId, grossAmount: grossAmount, dataInvoice: dataInvoice, ); } // kursus gratis Future freeCourse(int _idCourse) async { try { _stateProcess = Process.loading; notifyListeners(); bool result = await paymentServices.freeCoure(_idCourse); if (result) { _stateProcess = Process.uninitialized; _state = ResultState.success; notifyListeners(); return true; } else { _state = ResultState.gagal; _stateProcess = Process.uninitialized; notifyListeners(); return false; } } catch (e) { print('Error -> $e'); _state = ResultState.error; _stateProcess = Process.uninitialized; notifyListeners(); return false; } } // kursus dengan kupon gratis Future freeCourseCoupon(int _idCourse, String coupon) async { try { _stateProcess = Process.loading; notifyListeners(); bool result = await paymentServices.freeCoureCoupon(_idCourse, coupon); if (result) { _stateProcess = Process.uninitialized; _state = ResultState.success; notifyListeners(); return true; } else { _state = ResultState.gagal; _stateProcess = Process.uninitialized; notifyListeners(); return false; } } catch (e) { print('Error -> $e'); _state = ResultState.error; _stateProcess = Process.uninitialized; notifyListeners(); return false; } } //pembayaran dengan harga 0 (ZeroPrice) Future zeroPayment(List> dataInvoice, String totalPayment) async { try { notifyListeners(); var result = await paymentServices.zeroPayment( dataInvoice: dataInvoice, totalPayment: totalPayment); if (result.isNotEmpty) { notifyListeners(); return true; } else { notifyListeners(); return false; } } catch (e) { print('Error -> $e'); notifyListeners(); return false; } } }