Finally Fixed search field in payment page and cleaned up payment flow
This commit is contained in:
@@ -1,73 +1,63 @@
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import 'package:pshared/models/payment/methods/data.dart';
|
||||
import 'package:pshared/models/payment/methods/type.dart';
|
||||
import 'package:pshared/models/payment/type.dart';
|
||||
import 'package:pshared/models/recipient/recipient.dart';
|
||||
import 'package:pshared/provider/recipient/provider.dart';
|
||||
import 'package:pshared/provider/recipient/pmethods.dart';
|
||||
|
||||
|
||||
class PaymentFlowProvider extends ChangeNotifier {
|
||||
PaymentType _selectedType;
|
||||
PaymentType? _preferredType;
|
||||
PaymentMethodData? _manualPaymentData;
|
||||
List<PaymentMethod> _recipientMethods = [];
|
||||
Recipient? _recipient;
|
||||
|
||||
PaymentFlowProvider({
|
||||
required PaymentType initialType,
|
||||
}) : _selectedType = initialType;
|
||||
PaymentType? preferredType,
|
||||
}) : _selectedType = initialType,
|
||||
_preferredType = preferredType ?? initialType;
|
||||
|
||||
PaymentType get selectedType => _selectedType;
|
||||
PaymentMethodData? get manualPaymentData => _manualPaymentData;
|
||||
Recipient? get recipient => _recipient;
|
||||
PaymentMethod? get selectedMethod => hasRecipient
|
||||
? _recipientMethods.firstWhereOrNull((method) => method.type == _selectedType)
|
||||
: null;
|
||||
|
||||
void sync({
|
||||
required Recipient? recipient,
|
||||
required MethodMap availableTypes,
|
||||
PaymentType? preferredType,
|
||||
}) {
|
||||
final resolvedType = _resolveSelectedType(
|
||||
recipient: recipient,
|
||||
availableTypes: availableTypes,
|
||||
preferredType: preferredType,
|
||||
);
|
||||
bool get hasRecipient => _recipient != null;
|
||||
|
||||
var hasChanges = false;
|
||||
if (resolvedType != _selectedType) {
|
||||
_selectedType = resolvedType;
|
||||
hasChanges = true;
|
||||
}
|
||||
MethodMap get availableTypes => hasRecipient
|
||||
? _buildAvailableTypes(_recipientMethods)
|
||||
: {for (final type in PaymentType.values) type: null};
|
||||
|
||||
if (recipient != null && _manualPaymentData != null) {
|
||||
_manualPaymentData = null;
|
||||
hasChanges = true;
|
||||
}
|
||||
PaymentMethodData? get selectedPaymentData =>
|
||||
hasRecipient ? selectedMethod?.data : _manualPaymentData;
|
||||
|
||||
if (hasChanges) notifyListeners();
|
||||
}
|
||||
List<PaymentMethod> get methodsForRecipient => hasRecipient
|
||||
? List<PaymentMethod>.unmodifiable(_recipientMethods)
|
||||
: const [];
|
||||
|
||||
void reset({
|
||||
required Recipient? recipient,
|
||||
required MethodMap availableTypes,
|
||||
PaymentType? preferredType,
|
||||
}) {
|
||||
final resolvedType = _resolveSelectedType(
|
||||
recipient: recipient,
|
||||
availableTypes: availableTypes,
|
||||
preferredType: preferredType,
|
||||
);
|
||||
|
||||
var hasChanges = false;
|
||||
|
||||
if (resolvedType != _selectedType) {
|
||||
_selectedType = resolvedType;
|
||||
hasChanges = true;
|
||||
}
|
||||
|
||||
if (_manualPaymentData != null) {
|
||||
_manualPaymentData = null;
|
||||
hasChanges = true;
|
||||
}
|
||||
|
||||
if (hasChanges) notifyListeners();
|
||||
}
|
||||
void update(
|
||||
RecipientsProvider recipientsProvider,
|
||||
PaymentMethodsProvider methodsProvider,
|
||||
) =>
|
||||
_applyState(
|
||||
recipient: recipientsProvider.currentObject,
|
||||
methods: methodsProvider.methodsForRecipient(recipientsProvider.currentObject),
|
||||
preferredType: _preferredType,
|
||||
forceResetManualData: false,
|
||||
);
|
||||
|
||||
void selectType(PaymentType type, {bool resetManualData = false}) {
|
||||
if (hasRecipient && !availableTypes.containsKey(type)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_selectedType == type && (!resetManualData || _manualPaymentData == null)) {
|
||||
return;
|
||||
}
|
||||
@@ -84,6 +74,20 @@ class PaymentFlowProvider extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void setPreferredType(PaymentType? preferredType) {
|
||||
if (_preferredType == preferredType) {
|
||||
return;
|
||||
}
|
||||
|
||||
_preferredType = preferredType;
|
||||
_applyState(
|
||||
recipient: _recipient,
|
||||
methods: _recipientMethods,
|
||||
preferredType: _preferredType,
|
||||
forceResetManualData: false,
|
||||
);
|
||||
}
|
||||
|
||||
PaymentType _resolveSelectedType({
|
||||
required Recipient? recipient,
|
||||
required MethodMap availableTypes,
|
||||
@@ -107,4 +111,56 @@ class PaymentFlowProvider extends ChangeNotifier {
|
||||
|
||||
return availableTypes.keys.first;
|
||||
}
|
||||
|
||||
void _applyState({
|
||||
required Recipient? recipient,
|
||||
required List<PaymentMethod> methods,
|
||||
required PaymentType? preferredType,
|
||||
required bool forceResetManualData,
|
||||
}) {
|
||||
final availableTypes = _buildAvailableTypes(methods);
|
||||
final resolvedType = _resolveSelectedType(
|
||||
recipient: recipient,
|
||||
availableTypes: availableTypes,
|
||||
preferredType: preferredType,
|
||||
);
|
||||
|
||||
var hasChanges = false;
|
||||
|
||||
if (_recipient != recipient) {
|
||||
_recipient = recipient;
|
||||
hasChanges = true;
|
||||
}
|
||||
|
||||
if (!_hasSameMethods(methods)) {
|
||||
_recipientMethods = methods;
|
||||
hasChanges = true;
|
||||
}
|
||||
|
||||
if (resolvedType != _selectedType) {
|
||||
_selectedType = resolvedType;
|
||||
hasChanges = true;
|
||||
}
|
||||
|
||||
if ((recipient != null || forceResetManualData) && _manualPaymentData != null) {
|
||||
_manualPaymentData = null;
|
||||
hasChanges = true;
|
||||
}
|
||||
|
||||
if (hasChanges) notifyListeners();
|
||||
}
|
||||
|
||||
MethodMap _buildAvailableTypes(List<PaymentMethod> methods) => {
|
||||
for (final method in methods) method.type: method.data,
|
||||
};
|
||||
|
||||
bool _hasSameMethods(List<PaymentMethod> methods) {
|
||||
if (_recipientMethods.length != methods.length) return false;
|
||||
for (var i = 0; i < methods.length; i++) {
|
||||
final current = _recipientMethods[i];
|
||||
final next = methods[i];
|
||||
if (current.id != next.id || current.updatedAt != next.updatedAt) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,6 +83,58 @@ class QuotationProvider extends ChangeNotifier {
|
||||
Asset? get total => quotation == null ? null : createAsset(quotation!.debitAmount!.currency, quotation!.debitAmount!.amount);
|
||||
Asset? get recipientGets => quotation == null ? null : createAsset(quotation!.expectedSettlementAmount!.currency, quotation!.expectedSettlementAmount!.amount);
|
||||
|
||||
Customer _buildCustomer({
|
||||
required Recipient? recipient,
|
||||
required PaymentMethod method,
|
||||
}) {
|
||||
final name = _resolveCustomerName(method, recipient);
|
||||
String? firstName;
|
||||
String? middleName;
|
||||
String? lastName;
|
||||
|
||||
if (name != null && name.isNotEmpty) {
|
||||
final parts = name.split(RegExp(r'\s+'));
|
||||
if (parts.length == 1) {
|
||||
firstName = parts.first;
|
||||
} else if (parts.length == 2) {
|
||||
firstName = parts.first;
|
||||
lastName = parts.last;
|
||||
} else {
|
||||
firstName = parts.first;
|
||||
lastName = parts.last;
|
||||
middleName = parts.sublist(1, parts.length - 1).join(' ');
|
||||
}
|
||||
}
|
||||
|
||||
return Customer(
|
||||
id: recipient?.id ?? method.recipientRef,
|
||||
firstName: firstName,
|
||||
middleName: middleName,
|
||||
lastName: lastName,
|
||||
country: method.cardData?.country,
|
||||
);
|
||||
}
|
||||
|
||||
String? _resolveCustomerName(PaymentMethod method, Recipient? recipient) {
|
||||
final card = method.cardData;
|
||||
if (card != null) {
|
||||
return '${card.firstName} ${card.lastName}'.trim();
|
||||
}
|
||||
|
||||
final iban = method.ibanData;
|
||||
if (iban != null && iban.accountHolder.trim().isNotEmpty) {
|
||||
return iban.accountHolder.trim();
|
||||
}
|
||||
|
||||
final bank = method.bankAccountData;
|
||||
if (bank != null && bank.recipientName.trim().isNotEmpty) {
|
||||
return bank.recipientName.trim();
|
||||
}
|
||||
|
||||
final recipientName = recipient?.name.trim();
|
||||
return recipientName?.isNotEmpty == true ? recipientName : null;
|
||||
}
|
||||
|
||||
void _setResource(Resource<PaymentQuote> quotation) {
|
||||
_quotation = quotation;
|
||||
notifyListeners();
|
||||
@@ -118,42 +170,3 @@ class QuotationProvider extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Customer? _buildCustomer({
|
||||
required Recipient? recipient,
|
||||
required PaymentMethod method,
|
||||
}) {
|
||||
final recipientId = (recipient?.id ?? method.recipientRef).trim();
|
||||
if (recipientId.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var firstName = '';
|
||||
var lastName = '';
|
||||
final cardData = method.cardData;
|
||||
if (cardData != null) {
|
||||
firstName = cardData.firstName.trim();
|
||||
lastName = cardData.lastName.trim();
|
||||
}
|
||||
|
||||
if ((firstName.isEmpty || lastName.isEmpty) && recipient != null) {
|
||||
final parts = recipient.name.trim().split(RegExp(r'\s+'));
|
||||
if (parts.isNotEmpty && firstName.isEmpty) {
|
||||
firstName = parts.first;
|
||||
}
|
||||
if (parts.length > 1 && lastName.isEmpty) {
|
||||
lastName = parts.sublist(1).join(' ');
|
||||
}
|
||||
}
|
||||
|
||||
if (lastName.isEmpty) {
|
||||
lastName = firstName;
|
||||
}
|
||||
|
||||
return Customer(
|
||||
id: recipientId,
|
||||
firstName: firstName.isEmpty ? null : firstName,
|
||||
lastName: lastName.isEmpty ? null : lastName,
|
||||
country: cardData?.country,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:pshared/models/payment/wallet.dart';
|
||||
@@ -38,10 +39,7 @@ class WalletsProvider with ChangeNotifier {
|
||||
throw Exception('update wallet is not implemented');
|
||||
}
|
||||
|
||||
void selectWallet(Wallet wallet) {
|
||||
_selectedWallet = wallet;
|
||||
notifyListeners();
|
||||
}
|
||||
void selectWallet(Wallet wallet) => _setSelectedWallet(wallet);
|
||||
|
||||
Future<void> loadWalletsWithBalances() async {
|
||||
_setResource(_resource.copyWith(isLoading: true, error: null));
|
||||
@@ -98,6 +96,25 @@ class WalletsProvider with ChangeNotifier {
|
||||
|
||||
void _setResource(Resource<List<Wallet>> newResource) {
|
||||
_resource = newResource;
|
||||
_selectedWallet = _resolveSelectedWallet(_selectedWallet, wallets);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Wallet? _resolveSelectedWallet(Wallet? current, List<Wallet> available) {
|
||||
if (available.isEmpty) return null;
|
||||
final currentId = current?.id;
|
||||
if (currentId != null) {
|
||||
final existing = available.firstWhereOrNull((wallet) => wallet.id == currentId);
|
||||
if (existing != null) return existing;
|
||||
}
|
||||
return available.firstWhereOrNull((wallet) => !wallet.isHidden) ?? available.first;
|
||||
}
|
||||
|
||||
void _setSelectedWallet(Wallet wallet) {
|
||||
if (_selectedWallet?.id == wallet.id && _selectedWallet?.isHidden == wallet.isHidden) {
|
||||
return;
|
||||
}
|
||||
_selectedWallet = wallet;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import 'package:pshared/models/describable.dart';
|
||||
import 'package:pshared/models/organization/bound.dart';
|
||||
import 'package:pshared/models/payment/methods/data.dart';
|
||||
import 'package:pshared/models/payment/methods/type.dart';
|
||||
import 'package:pshared/models/payment/type.dart';
|
||||
import 'package:pshared/models/recipient/recipient.dart';
|
||||
import 'package:pshared/models/permissions/bound.dart';
|
||||
import 'package:pshared/models/storable.dart';
|
||||
import 'package:pshared/provider/organizations.dart';
|
||||
@@ -20,6 +22,24 @@ class PaymentMethodsProvider extends GenericProvider<PaymentMethod> {
|
||||
|
||||
List<PaymentMethod> get methods => List<PaymentMethod>.unmodifiable(items.toList()..sort((a, b) => a.storable.createdAt.compareTo(b.storable.createdAt)));
|
||||
|
||||
List<PaymentMethod> methodsForRecipient(Recipient? recipient) {
|
||||
if (recipient == null || !isReady) return [];
|
||||
|
||||
return methods
|
||||
.where((method) => !method.isArchived && method.recipientRef == recipient.id)
|
||||
.toList();
|
||||
}
|
||||
|
||||
MethodMap availableTypesForRecipient(Recipient? recipient) => {
|
||||
for (final method in methodsForRecipient(recipient)) method.type: method.data,
|
||||
};
|
||||
|
||||
PaymentMethod? findMethodByType({
|
||||
required PaymentType type,
|
||||
required Recipient? recipient,
|
||||
}) =>
|
||||
methodsForRecipient(recipient).firstWhereOrNull((method) => method.type == type);
|
||||
|
||||
void updateProviders(OrganizationsProvider organizations, RecipientsProvider recipients) {
|
||||
if (recipients.currentObject != null) loadMethods(organizations, recipients.currentObject?.id);
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ class RecipientsProvider extends GenericProvider<Recipient> {
|
||||
|
||||
RecipientFilter _selectedFilter = RecipientFilter.all;
|
||||
String _query = '';
|
||||
String? _previousRecipientRef;
|
||||
|
||||
RecipientFilter get selectedFilter => _selectedFilter;
|
||||
String get query => _query;
|
||||
@@ -22,6 +23,10 @@ class RecipientsProvider extends GenericProvider<Recipient> {
|
||||
|
||||
RecipientsProvider() : super(service: RecipientService.basicService);
|
||||
|
||||
Recipient? get previousRecipient => _previousRecipientRef == null
|
||||
? null
|
||||
: getItemByRef(_previousRecipientRef!);
|
||||
|
||||
List<Recipient> get filteredRecipients {
|
||||
List<Recipient> filtered = recipients.where((r) {
|
||||
switch (_selectedFilter) {
|
||||
@@ -53,6 +58,24 @@ class RecipientsProvider extends GenericProvider<Recipient> {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
bool setCurrentObject(String? objectRef) {
|
||||
final currentRef = currentObject?.id;
|
||||
final didUpdate = super.setCurrentObject(objectRef);
|
||||
|
||||
if (didUpdate && currentRef != null && currentRef != objectRef) {
|
||||
_previousRecipientRef = currentRef;
|
||||
}
|
||||
|
||||
return didUpdate;
|
||||
}
|
||||
|
||||
void restorePreviousRecipient() {
|
||||
if (_previousRecipientRef != null) {
|
||||
setCurrentObject(_previousRecipientRef);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Recipient> create({
|
||||
required String name,
|
||||
required String email,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import 'package:logging/logging.dart';
|
||||
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
import 'package:pshared/api/requests/payment/initiate.dart';
|
||||
@@ -27,10 +26,10 @@ class PaymentService {
|
||||
metadata: metadata,
|
||||
);
|
||||
final response = await AuthorizationService.getPOSTResponse(
|
||||
_objectType,
|
||||
'/by-quote/$organizationRef',
|
||||
_objectType,
|
||||
'/by-quote/$organizationRef',
|
||||
request.toJson(),
|
||||
);
|
||||
return PaymentResponse.fromJson(response).payment.toDomain();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user