168 lines
4.7 KiB
Dart
168 lines
4.7 KiB
Dart
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<DetailOrderModel> _detailOrder = [];
|
|
List<DetailOrderModelUnderscore> _detailOrderUnderscore = [];
|
|
|
|
ResultState? get state => _state;
|
|
Process get stateProcess => _stateProcess;
|
|
bool get result => _paymentModel;
|
|
String? get idOrders => _idOrders;
|
|
ZeroPrice? get zeroPrice => _zeroPrice;
|
|
List<DetailOrderModel> get detailOrder => _detailOrder;
|
|
List<DetailOrderModelUnderscore> 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<Map<String, String>?> startSnapPayment({
|
|
required String orderId,
|
|
required int grossAmount,
|
|
required List<Map<String, dynamic>> dataInvoice,
|
|
}) async {
|
|
try {
|
|
_stateProcess = Process.loading;
|
|
notifyListeners();
|
|
|
|
Map<String, String> 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<Map<String, String>> getSnapTransactionToken({
|
|
required String orderId,
|
|
required int grossAmount,
|
|
required List<Map<String, dynamic>> dataInvoice,
|
|
}) async {
|
|
return await paymentServices.getSnapTransactionToken(
|
|
orderId: orderId,
|
|
grossAmount: grossAmount,
|
|
dataInvoice: dataInvoice,
|
|
);
|
|
}
|
|
|
|
// kursus gratis
|
|
Future<bool> 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<bool> 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<dynamic> zeroPayment(List<Map<String, String>> 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;
|
|
}
|
|
}
|
|
}
|