added ledger as souec of funds for payouts
This commit is contained in:
@@ -6,7 +6,6 @@ import 'package:pshared/data/dto/ledger/type.dart';
|
||||
|
||||
part 'create.g.dart';
|
||||
|
||||
|
||||
@JsonSerializable()
|
||||
class CreateLedgerAccountRequest {
|
||||
final Map<String, String>? metadata;
|
||||
@@ -27,7 +26,7 @@ class CreateLedgerAccountRequest {
|
||||
this.ownerRef,
|
||||
});
|
||||
|
||||
factory CreateLedgerAccountRequest.fromJson(Map<String, dynamic> json) => _$CreateLedgerAccountRequestFromJson(json);
|
||||
factory CreateLedgerAccountRequest.fromJson(Map<String, dynamic> json) =>
|
||||
_$CreateLedgerAccountRequestFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$CreateLedgerAccountRequestToJson(this);
|
||||
|
||||
}
|
||||
|
||||
159
frontend/pshared/lib/controllers/payment/source.dart
Normal file
159
frontend/pshared/lib/controllers/payment/source.dart
Normal file
@@ -0,0 +1,159 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import 'package:pshared/controllers/balance_mask/wallets.dart';
|
||||
import 'package:pshared/models/ledger/account.dart';
|
||||
import 'package:pshared/models/payment/source_type.dart';
|
||||
import 'package:pshared/models/payment/wallet.dart';
|
||||
import 'package:pshared/provider/ledger.dart';
|
||||
import 'package:pshared/utils/currency.dart';
|
||||
|
||||
|
||||
class PaymentSourceController with ChangeNotifier {
|
||||
WalletsController? _walletsController;
|
||||
|
||||
List<Wallet> _wallets = const <Wallet>[];
|
||||
Map<String, Wallet> _walletsById = const <String, Wallet>{};
|
||||
|
||||
List<LedgerAccount> _ledgerAccounts = const <LedgerAccount>[];
|
||||
Map<String, LedgerAccount> _ledgerByRef = const <String, LedgerAccount>{};
|
||||
|
||||
PaymentSourceType? _selectedType;
|
||||
String? _selectedRef;
|
||||
|
||||
List<Wallet> get wallets => _wallets;
|
||||
List<LedgerAccount> get ledgerAccounts => _ledgerAccounts;
|
||||
bool get hasSources => _wallets.isNotEmpty || _ledgerAccounts.isNotEmpty;
|
||||
|
||||
PaymentSourceType? get selectedType => _selectedType;
|
||||
String? get selectedRef => _selectedRef;
|
||||
|
||||
Wallet? get selectedWallet {
|
||||
if (_selectedType != PaymentSourceType.wallet) return null;
|
||||
final ref = _selectedRef;
|
||||
if (ref == null) return null;
|
||||
return _walletsById[ref];
|
||||
}
|
||||
|
||||
LedgerAccount? get selectedLedgerAccount {
|
||||
if (_selectedType != PaymentSourceType.ledger) return null;
|
||||
final ref = _selectedRef;
|
||||
if (ref == null) return null;
|
||||
return _ledgerByRef[ref];
|
||||
}
|
||||
|
||||
String? get selectedCurrencyCode {
|
||||
final wallet = selectedWallet;
|
||||
if (wallet != null) {
|
||||
return currencyCodeToString(wallet.currency);
|
||||
}
|
||||
|
||||
final ledger = selectedLedgerAccount;
|
||||
if (ledger != null) {
|
||||
final code = ledger.currency.trim().toUpperCase();
|
||||
return code.isEmpty ? null : code;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
void update(WalletsController wallets, LedgerAccountsProvider ledger) {
|
||||
_walletsController = wallets;
|
||||
|
||||
_walletsById = _uniqueWalletsById(wallets.wallets);
|
||||
_wallets = _walletsById.values.toList(growable: false);
|
||||
|
||||
_ledgerByRef = _uniqueLedgerByRef(ledger.accounts);
|
||||
_ledgerAccounts = _ledgerByRef.values.toList(growable: false);
|
||||
|
||||
_syncSelection();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void selectWallet(Wallet wallet) {
|
||||
selectWalletByRef(wallet.id);
|
||||
}
|
||||
|
||||
void selectWalletByRef(String walletRef) {
|
||||
if (!_walletsById.containsKey(walletRef)) return;
|
||||
_walletsController?.selectWalletByRef(walletRef);
|
||||
_setSelection(PaymentSourceType.wallet, walletRef);
|
||||
}
|
||||
|
||||
void selectLedgerByRef(String ledgerAccountRef) {
|
||||
if (!_ledgerByRef.containsKey(ledgerAccountRef)) return;
|
||||
_setSelection(PaymentSourceType.ledger, ledgerAccountRef);
|
||||
}
|
||||
|
||||
bool isWalletSelected(String walletRef) {
|
||||
return _selectedType == PaymentSourceType.wallet &&
|
||||
_selectedRef == walletRef;
|
||||
}
|
||||
|
||||
bool isLedgerSelected(String ledgerAccountRef) {
|
||||
return _selectedType == PaymentSourceType.ledger &&
|
||||
_selectedRef == ledgerAccountRef;
|
||||
}
|
||||
|
||||
void _syncSelection() {
|
||||
final currentType = _selectedType;
|
||||
final currentRef = _selectedRef;
|
||||
|
||||
if (currentType == PaymentSourceType.wallet &&
|
||||
currentRef != null &&
|
||||
_walletsById.containsKey(currentRef)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentType == PaymentSourceType.ledger &&
|
||||
currentRef != null &&
|
||||
_ledgerByRef.containsKey(currentRef)) {
|
||||
return;
|
||||
}
|
||||
|
||||
final selectedWalletRef = _walletsController?.selectedWalletRef;
|
||||
if (selectedWalletRef != null &&
|
||||
_walletsById.containsKey(selectedWalletRef)) {
|
||||
_selectedType = PaymentSourceType.wallet;
|
||||
_selectedRef = selectedWalletRef;
|
||||
return;
|
||||
}
|
||||
|
||||
if (_wallets.isNotEmpty) {
|
||||
_selectedType = PaymentSourceType.wallet;
|
||||
_selectedRef = _wallets.first.id;
|
||||
return;
|
||||
}
|
||||
|
||||
if (_ledgerAccounts.isNotEmpty) {
|
||||
_selectedType = PaymentSourceType.ledger;
|
||||
_selectedRef = _ledgerAccounts.first.ledgerAccountRef;
|
||||
return;
|
||||
}
|
||||
|
||||
_selectedType = null;
|
||||
_selectedRef = null;
|
||||
}
|
||||
|
||||
void _setSelection(PaymentSourceType type, String ref) {
|
||||
if (_selectedType == type && _selectedRef == ref) return;
|
||||
_selectedType = type;
|
||||
_selectedRef = ref;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Map<String, Wallet> _uniqueWalletsById(List<Wallet> wallets) {
|
||||
final result = <String, Wallet>{};
|
||||
for (final wallet in wallets) {
|
||||
result.putIfAbsent(wallet.id, () => wallet);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Map<String, LedgerAccount> _uniqueLedgerByRef(List<LedgerAccount> accounts) {
|
||||
final result = <String, LedgerAccount>{};
|
||||
for (final account in accounts) {
|
||||
result.putIfAbsent(account.ledgerAccountRef, () => account);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,9 @@ import 'package:pshared/models/payment/settlement_mode.dart';
|
||||
|
||||
class PaymentIntent {
|
||||
final PaymentKind kind;
|
||||
final String? sourceRef;
|
||||
final PaymentMethodData? source;
|
||||
final String? destinationRef;
|
||||
final PaymentMethodData? destination;
|
||||
final Money? amount;
|
||||
final FxIntent? fx;
|
||||
@@ -19,7 +21,9 @@ class PaymentIntent {
|
||||
|
||||
const PaymentIntent({
|
||||
this.kind = PaymentKind.unspecified,
|
||||
this.sourceRef,
|
||||
this.source,
|
||||
this.destinationRef,
|
||||
this.destination,
|
||||
this.amount,
|
||||
this.fx,
|
||||
|
||||
1
frontend/pshared/lib/models/payment/source_type.dart
Normal file
1
frontend/pshared/lib/models/payment/source_type.dart
Normal file
@@ -0,0 +1 @@
|
||||
enum PaymentSourceType { wallet, ledger }
|
||||
@@ -15,7 +15,6 @@ import 'package:pshared/provider/resource.dart';
|
||||
import 'package:pshared/service/ledger.dart';
|
||||
import 'package:pshared/utils/exception.dart';
|
||||
|
||||
|
||||
class LedgerAccountsProvider with ChangeNotifier {
|
||||
final LedgerService _service;
|
||||
OrganizationsProvider? _organizations;
|
||||
@@ -25,7 +24,9 @@ class LedgerAccountsProvider with ChangeNotifier {
|
||||
Resource<List<LedgerAccount>> _resource = Resource(data: []);
|
||||
Resource<List<LedgerAccount>> get resource => _resource;
|
||||
|
||||
List<LedgerAccount> get accounts => (_resource.data ?? []).where((la) => la.role == LedgerAccountRole.operating).toList();
|
||||
List<LedgerAccount> get accounts => (_resource.data ?? [])
|
||||
.where((la) => la.role == LedgerAccountRole.operating)
|
||||
.toList();
|
||||
bool get isLoading => _resource.isLoading;
|
||||
Exception? get error => _resource.error;
|
||||
|
||||
@@ -33,11 +34,13 @@ class LedgerAccountsProvider with ChangeNotifier {
|
||||
bool get isRefreshingBalances => _isRefreshingBalances;
|
||||
|
||||
final Set<String> _refreshingAccounts = <String>{};
|
||||
bool isWalletRefreshing(String ledgerAccountRef) => _refreshingAccounts.contains(ledgerAccountRef);
|
||||
bool isWalletRefreshing(String ledgerAccountRef) =>
|
||||
_refreshingAccounts.contains(ledgerAccountRef);
|
||||
|
||||
// Expose current org id so UI controller can reset per-org state if needed.
|
||||
String? get organizationRef =>
|
||||
(_organizations?.isOrganizationSet ?? false) ? _organizations!.current.id : null;
|
||||
String? get organizationRef => (_organizations?.isOrganizationSet ?? false)
|
||||
? _organizations!.current.id
|
||||
: null;
|
||||
|
||||
// Used to ignore stale async results (org changes / overlapping requests).
|
||||
int _opSeq = 0;
|
||||
@@ -69,7 +72,10 @@ class LedgerAccountsProvider with ChangeNotifier {
|
||||
_isRefreshingBalances = false;
|
||||
_refreshingAccounts.clear();
|
||||
|
||||
_applyResource(_resource.copyWith(isLoading: true, error: null), notify: true);
|
||||
_applyResource(
|
||||
_resource.copyWith(isLoading: true, error: null),
|
||||
notify: true,
|
||||
);
|
||||
|
||||
try {
|
||||
final base = await _service.list(orgRef);
|
||||
@@ -78,7 +84,11 @@ class LedgerAccountsProvider with ChangeNotifier {
|
||||
if (seq != _opSeq) return;
|
||||
|
||||
_applyResource(
|
||||
Resource<List<LedgerAccount>>(data: result.wallets, isLoading: false, error: result.error),
|
||||
Resource<List<LedgerAccount>>(
|
||||
data: result.wallets,
|
||||
isLoading: false,
|
||||
error: result.error,
|
||||
),
|
||||
notify: true,
|
||||
);
|
||||
} catch (e) {
|
||||
@@ -129,7 +139,9 @@ class LedgerAccountsProvider with ChangeNotifier {
|
||||
|
||||
if (_refreshingAccounts.contains(ledgerAccountRef)) return;
|
||||
|
||||
final existing = accounts.firstWhereOrNull((w) => w.ledgerAccountRef == ledgerAccountRef);
|
||||
final existing = accounts.firstWhereOrNull(
|
||||
(w) => w.ledgerAccountRef == ledgerAccountRef,
|
||||
);
|
||||
if (existing == null) return;
|
||||
|
||||
final orgRef = org.current.id;
|
||||
@@ -141,12 +153,15 @@ class LedgerAccountsProvider with ChangeNotifier {
|
||||
|
||||
try {
|
||||
final balance = await _service.getBalance(
|
||||
organizationRef: orgRef,
|
||||
organizationRef: orgRef,
|
||||
ledgerAccountRef: ledgerAccountRef,
|
||||
);
|
||||
if ((_accountSeq[ledgerAccountRef] ?? 0) != seq) return;
|
||||
|
||||
final next = _replaceWallet(ledgerAccountRef, (w) => w.copyWith(balance: balance));
|
||||
final next = _replaceWallet(
|
||||
ledgerAccountRef,
|
||||
(w) => w.copyWith(balance: balance),
|
||||
);
|
||||
if (next == null) return;
|
||||
|
||||
_applyResource(_resource.copyWith(data: next), notify: false);
|
||||
@@ -170,7 +185,10 @@ class LedgerAccountsProvider with ChangeNotifier {
|
||||
final org = _organizations;
|
||||
if (org == null || !org.isOrganizationSet) return;
|
||||
|
||||
_applyResource(_resource.copyWith(isLoading: true, error: null), notify: true);
|
||||
_applyResource(
|
||||
_resource.copyWith(isLoading: true, error: null),
|
||||
notify: true,
|
||||
);
|
||||
|
||||
try {
|
||||
await _service.create(
|
||||
@@ -181,20 +199,31 @@ class LedgerAccountsProvider with ChangeNotifier {
|
||||
);
|
||||
await loadAccountsWithBalances();
|
||||
} catch (e) {
|
||||
_applyResource(_resource.copyWith(isLoading: false, error: toException(e)), notify: true);
|
||||
_applyResource(
|
||||
_resource.copyWith(isLoading: false, error: toException(e)),
|
||||
notify: true,
|
||||
);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- internals ----------
|
||||
|
||||
void _applyResource(Resource<List<LedgerAccount>> newResource, {required bool notify}) {
|
||||
void _applyResource(
|
||||
Resource<List<LedgerAccount>> newResource, {
|
||||
required bool notify,
|
||||
}) {
|
||||
_resource = newResource;
|
||||
if (notify) notifyListeners();
|
||||
}
|
||||
|
||||
List<LedgerAccount>? _replaceWallet(String ledgerAccountRef, LedgerAccount Function(LedgerAccount) updater) {
|
||||
final idx = accounts.indexWhere((w) => w.ledgerAccountRef == ledgerAccountRef);
|
||||
List<LedgerAccount>? _replaceWallet(
|
||||
String ledgerAccountRef,
|
||||
LedgerAccount Function(LedgerAccount) updater,
|
||||
) {
|
||||
final idx = accounts.indexWhere(
|
||||
(w) => w.ledgerAccountRef == ledgerAccountRef,
|
||||
);
|
||||
if (idx < 0) return null;
|
||||
|
||||
final next = List<LedgerAccount>.from(accounts);
|
||||
@@ -202,7 +231,10 @@ class LedgerAccountsProvider with ChangeNotifier {
|
||||
return next;
|
||||
}
|
||||
|
||||
Future<_LedgerAccountLoadResult> _withBalances(String orgRef, List<LedgerAccount> base) async {
|
||||
Future<_LedgerAccountLoadResult> _withBalances(
|
||||
String orgRef,
|
||||
List<LedgerAccount> base,
|
||||
) async {
|
||||
Exception? firstError;
|
||||
|
||||
final withBalances = await _mapConcurrent<LedgerAccount, LedgerAccount>(
|
||||
@@ -211,7 +243,7 @@ class LedgerAccountsProvider with ChangeNotifier {
|
||||
(ledgerAccount) async {
|
||||
try {
|
||||
final balance = await _service.getBalance(
|
||||
organizationRef: orgRef,
|
||||
organizationRef: orgRef,
|
||||
ledgerAccountRef: ledgerAccount.ledgerAccountRef,
|
||||
);
|
||||
return ledgerAccount.copyWith(balance: balance);
|
||||
@@ -243,7 +275,10 @@ class LedgerAccountsProvider with ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
final workers = List.generate(min(concurrency, items.length), (_) => worker());
|
||||
final workers = List.generate(
|
||||
min(concurrency, items.length),
|
||||
(_) => worker(),
|
||||
);
|
||||
await Future.wait(workers);
|
||||
|
||||
return results.cast<R>();
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
import 'package:pshared/controllers/balance_mask/wallets.dart';
|
||||
import 'package:pshared/controllers/payment/source.dart';
|
||||
import 'package:pshared/models/payment/asset.dart';
|
||||
import 'package:pshared/models/payment/chain_network.dart';
|
||||
import 'package:pshared/models/payment/customer.dart';
|
||||
import 'package:pshared/models/payment/currency_pair.dart';
|
||||
import 'package:pshared/models/payment/fees/treatment.dart';
|
||||
import 'package:pshared/models/payment/fx/intent.dart';
|
||||
import 'package:pshared/models/payment/fx/side.dart';
|
||||
import 'package:pshared/models/payment/kind.dart';
|
||||
import 'package:pshared/models/payment/methods/card.dart';
|
||||
import 'package:pshared/models/payment/methods/crypto_address.dart';
|
||||
import 'package:pshared/models/payment/methods/data.dart';
|
||||
import 'package:pshared/models/payment/methods/iban.dart';
|
||||
import 'package:pshared/models/payment/methods/ledger.dart';
|
||||
import 'package:pshared/models/payment/methods/managed_wallet.dart';
|
||||
import 'package:pshared/models/payment/methods/russian_bank.dart';
|
||||
import 'package:pshared/models/payment/methods/type.dart';
|
||||
@@ -18,7 +22,6 @@ import 'package:pshared/models/recipient/recipient.dart';
|
||||
import 'package:pshared/provider/payment/amount.dart';
|
||||
import 'package:pshared/provider/payment/flow.dart';
|
||||
import 'package:pshared/provider/recipient/provider.dart';
|
||||
import 'package:pshared/utils/currency.dart';
|
||||
import 'package:pshared/utils/payment/fx_helpers.dart';
|
||||
|
||||
class QuotationIntentBuilder {
|
||||
@@ -26,21 +29,23 @@ class QuotationIntentBuilder {
|
||||
|
||||
PaymentIntent? build({
|
||||
required PaymentAmountProvider payment,
|
||||
required WalletsController wallets,
|
||||
required PaymentSourceController source,
|
||||
required PaymentFlowProvider flow,
|
||||
required RecipientsProvider recipients,
|
||||
}) {
|
||||
final selectedWallet = wallets.selectedWallet;
|
||||
final sourceMethod = _resolveSourceMethod(source);
|
||||
final sourceCurrency = source.selectedCurrencyCode;
|
||||
final paymentData = flow.selectedPaymentData;
|
||||
final selectedMethod = flow.selectedMethod;
|
||||
if (selectedWallet == null || paymentData == null) return null;
|
||||
if (sourceMethod == null || sourceCurrency == null || paymentData == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final customer = _buildCustomer(
|
||||
recipient: recipients.currentObject,
|
||||
method: selectedMethod,
|
||||
data: paymentData,
|
||||
);
|
||||
final sourceCurrency = currencyCodeToString(selectedWallet.currency);
|
||||
final amountCurrency = payment.settlementMode == SettlementMode.fixReceived
|
||||
? _settlementCurrency
|
||||
: sourceCurrency;
|
||||
@@ -48,26 +53,22 @@ class QuotationIntentBuilder {
|
||||
amount: payment.amount.toString(),
|
||||
currency: amountCurrency,
|
||||
);
|
||||
final isLedgerSource = source.selectedLedgerAccount != null;
|
||||
final isCryptoToCrypto =
|
||||
paymentData is CryptoAddressPaymentMethod &&
|
||||
(paymentData.asset?.tokenSymbol ?? '').trim().toUpperCase() ==
|
||||
amount.currency;
|
||||
final fxIntent = FxIntentHelper.buildSellBaseBuyQuote(
|
||||
baseCurrency: sourceCurrency,
|
||||
quoteCurrency: _settlementCurrency, // TODO: exentd target currencies
|
||||
final fxIntent = _buildFxIntent(
|
||||
sourceCurrency: sourceCurrency,
|
||||
settlementMode: payment.settlementMode,
|
||||
isLedgerSource: isLedgerSource,
|
||||
enabled: !isCryptoToCrypto,
|
||||
);
|
||||
return PaymentIntent(
|
||||
kind: PaymentKind.payout,
|
||||
amount: amount,
|
||||
destination: paymentData,
|
||||
source: ManagedWalletPaymentMethod(
|
||||
managedWalletRef: selectedWallet.id,
|
||||
asset: PaymentAsset(
|
||||
tokenSymbol: selectedWallet.tokenSymbol ?? '',
|
||||
chain: selectedWallet.network ?? ChainNetwork.unspecified,
|
||||
),
|
||||
),
|
||||
source: sourceMethod,
|
||||
fx: fxIntent,
|
||||
feeTreatment: payment.payerCoversFee
|
||||
? FeeTreatment.addToSource
|
||||
@@ -77,6 +78,56 @@ class QuotationIntentBuilder {
|
||||
);
|
||||
}
|
||||
|
||||
FxIntent? _buildFxIntent({
|
||||
required String sourceCurrency,
|
||||
required SettlementMode settlementMode,
|
||||
required bool isLedgerSource,
|
||||
required bool enabled,
|
||||
}) {
|
||||
if (!enabled) return null;
|
||||
|
||||
// Ledger sources in fix_received mode need explicit reverse side.
|
||||
// BFF maps only settlement currency + fx side, then quotation derives pair.
|
||||
// For ledger this preserves source debit in ledger currency (e.g. USDT).
|
||||
if (isLedgerSource && settlementMode == SettlementMode.fixReceived) {
|
||||
final base = sourceCurrency.trim();
|
||||
final quote = _settlementCurrency;
|
||||
if (base.isEmpty || base.toUpperCase() == quote.toUpperCase()) {
|
||||
return null;
|
||||
}
|
||||
return FxIntent(
|
||||
pair: CurrencyPair(base: base, quote: quote),
|
||||
side: FxSide.buyBaseSellQuote,
|
||||
);
|
||||
}
|
||||
|
||||
return FxIntentHelper.buildSellBaseBuyQuote(
|
||||
baseCurrency: sourceCurrency,
|
||||
quoteCurrency: _settlementCurrency, // TODO: exentd target currencies
|
||||
enabled: true,
|
||||
);
|
||||
}
|
||||
|
||||
PaymentMethodData? _resolveSourceMethod(PaymentSourceController source) {
|
||||
final wallet = source.selectedWallet;
|
||||
if (wallet != null) {
|
||||
return ManagedWalletPaymentMethod(
|
||||
managedWalletRef: wallet.id,
|
||||
asset: PaymentAsset(
|
||||
tokenSymbol: wallet.tokenSymbol ?? '',
|
||||
chain: wallet.network ?? ChainNetwork.unspecified,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final ledger = source.selectedLedgerAccount;
|
||||
if (ledger != null) {
|
||||
return LedgerPaymentMethod(ledgerAccountRef: ledger.ledgerAccountRef);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Customer? _buildCustomer({
|
||||
required Recipient? recipient,
|
||||
required PaymentMethod? method,
|
||||
|
||||
@@ -7,7 +7,7 @@ import 'package:logging/logging.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
import 'package:pshared/api/requests/payment/quote.dart';
|
||||
import 'package:pshared/controllers/balance_mask/wallets.dart';
|
||||
import 'package:pshared/controllers/payment/source.dart';
|
||||
import 'package:pshared/data/mapper/payment/intent/payment.dart';
|
||||
import 'package:pshared/models/asset.dart';
|
||||
import 'package:pshared/models/payment/intent.dart';
|
||||
@@ -36,6 +36,7 @@ class QuotationProvider extends ChangeNotifier {
|
||||
late OrganizationsProvider _organizations;
|
||||
bool _isLoaded = false;
|
||||
PaymentIntent? _lastIntent;
|
||||
String? _sourceCurrencyCode;
|
||||
final QuotationIntentBuilder _intentBuilder = QuotationIntentBuilder();
|
||||
final AutoRefreshScheduler _autoRefresh = AutoRefreshScheduler();
|
||||
AutoRefreshMode _autoRefreshMode = AutoRefreshMode.on;
|
||||
@@ -43,15 +44,16 @@ class QuotationProvider extends ChangeNotifier {
|
||||
void update(
|
||||
OrganizationsProvider venue,
|
||||
PaymentAmountProvider payment,
|
||||
WalletsController wallets,
|
||||
PaymentSourceController source,
|
||||
PaymentFlowProvider flow,
|
||||
RecipientsProvider recipients,
|
||||
PaymentMethodsProvider _,
|
||||
) {
|
||||
_organizations = venue;
|
||||
_sourceCurrencyCode = source.selectedCurrencyCode;
|
||||
final intent = _intentBuilder.build(
|
||||
payment: payment,
|
||||
wallets: wallets,
|
||||
source: source,
|
||||
flow: flow,
|
||||
recipients: recipients,
|
||||
);
|
||||
@@ -77,7 +79,12 @@ class QuotationProvider extends ChangeNotifier {
|
||||
}
|
||||
|
||||
Asset? get fee => _assetFromMoney(quoteFeeTotal(quotation));
|
||||
Asset? get total => _assetFromMoney(quotation?.amounts?.sourceDebitTotal);
|
||||
Asset? get total => _assetFromMoney(
|
||||
quoteSourceDebitTotal(
|
||||
quotation,
|
||||
preferredSourceCurrency: _sourceCurrencyCode,
|
||||
),
|
||||
);
|
||||
Asset? get recipientGets =>
|
||||
_assetFromMoney(quotation?.amounts?.destinationSettlement);
|
||||
|
||||
@@ -139,6 +146,7 @@ class QuotationProvider extends ChangeNotifier {
|
||||
void reset() {
|
||||
_isLoaded = false;
|
||||
_lastIntent = null;
|
||||
_sourceCurrencyCode = null;
|
||||
_setResource(Resource(data: null, isLoading: false, error: null));
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@ import 'package:pshared/provider/resource.dart';
|
||||
import 'package:pshared/service/payment/wallets.dart';
|
||||
import 'package:pshared/utils/exception.dart';
|
||||
|
||||
|
||||
class WalletsProvider with ChangeNotifier {
|
||||
final WalletsService _service;
|
||||
OrganizationsProvider? _organizations;
|
||||
@@ -31,11 +30,13 @@ class WalletsProvider with ChangeNotifier {
|
||||
bool get isRefreshingBalances => _isRefreshingBalances;
|
||||
|
||||
final Set<String> _refreshingWallets = <String>{};
|
||||
bool isWalletRefreshing(String walletRef) => _refreshingWallets.contains(walletRef);
|
||||
bool isWalletRefreshing(String walletRef) =>
|
||||
_refreshingWallets.contains(walletRef);
|
||||
|
||||
// Expose current org id so UI controller can reset per-org state if needed.
|
||||
String? get organizationRef =>
|
||||
(_organizations?.isOrganizationSet ?? false) ? _organizations!.current.id : null;
|
||||
String? get organizationRef => (_organizations?.isOrganizationSet ?? false)
|
||||
? _organizations!.current.id
|
||||
: null;
|
||||
|
||||
// Used to ignore stale async results (org changes / overlapping requests).
|
||||
int _opSeq = 0;
|
||||
@@ -67,13 +68,25 @@ class WalletsProvider with ChangeNotifier {
|
||||
_isRefreshingBalances = false;
|
||||
_refreshingWallets.clear();
|
||||
|
||||
_applyResource(_resource.copyWith(isLoading: true, error: null), notify: true);
|
||||
_applyResource(
|
||||
_resource.copyWith(isLoading: true, error: null),
|
||||
notify: true,
|
||||
);
|
||||
|
||||
try {
|
||||
final base = await _service.getWallets(orgId);
|
||||
if (seq != _opSeq) return;
|
||||
|
||||
// Publish wallets as soon as the list is available, then hydrate balances.
|
||||
_isRefreshingBalances = true;
|
||||
_applyResource(
|
||||
Resource<List<Wallet>>(data: base, isLoading: false, error: null),
|
||||
notify: true,
|
||||
);
|
||||
|
||||
final result = await _withBalances(orgId, base);
|
||||
if (seq != _opSeq) return;
|
||||
_isRefreshingBalances = false;
|
||||
|
||||
_applyResource(
|
||||
Resource<List<Wallet>>(
|
||||
@@ -85,6 +98,7 @@ class WalletsProvider with ChangeNotifier {
|
||||
);
|
||||
} catch (e) {
|
||||
if (seq != _opSeq) return;
|
||||
_isRefreshingBalances = false;
|
||||
|
||||
_applyResource(
|
||||
_resource.copyWith(isLoading: false, error: toException(e)),
|
||||
@@ -145,7 +159,10 @@ class WalletsProvider with ChangeNotifier {
|
||||
final balance = await _service.getBalance(orgId, walletRef);
|
||||
if ((_walletSeq[walletRef] ?? 0) != seq) return;
|
||||
|
||||
final next = _replaceWallet(walletRef, (w) => w.copyWith(balance: balance));
|
||||
final next = _replaceWallet(
|
||||
walletRef,
|
||||
(w) => w.copyWith(balance: balance),
|
||||
);
|
||||
if (next == null) return;
|
||||
|
||||
_applyResource(_resource.copyWith(data: next), notify: false);
|
||||
@@ -169,7 +186,10 @@ class WalletsProvider with ChangeNotifier {
|
||||
final org = _organizations;
|
||||
if (org == null || !org.isOrganizationSet) return;
|
||||
|
||||
_applyResource(_resource.copyWith(isLoading: true, error: null), notify: true);
|
||||
_applyResource(
|
||||
_resource.copyWith(isLoading: true, error: null),
|
||||
notify: true,
|
||||
);
|
||||
|
||||
try {
|
||||
await _service.create(
|
||||
@@ -180,19 +200,28 @@ class WalletsProvider with ChangeNotifier {
|
||||
);
|
||||
await loadWalletsWithBalances();
|
||||
} catch (e) {
|
||||
_applyResource(_resource.copyWith(isLoading: false, error: toException(e)), notify: true);
|
||||
_applyResource(
|
||||
_resource.copyWith(isLoading: false, error: toException(e)),
|
||||
notify: true,
|
||||
);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- internals ----------
|
||||
|
||||
void _applyResource(Resource<List<Wallet>> newResource, {required bool notify}) {
|
||||
void _applyResource(
|
||||
Resource<List<Wallet>> newResource, {
|
||||
required bool notify,
|
||||
}) {
|
||||
_resource = newResource;
|
||||
if (notify) notifyListeners();
|
||||
}
|
||||
|
||||
List<Wallet>? _replaceWallet(String walletRef, Wallet Function(Wallet) updater) {
|
||||
List<Wallet>? _replaceWallet(
|
||||
String walletRef,
|
||||
Wallet Function(Wallet) updater,
|
||||
) {
|
||||
final idx = wallets.indexWhere((w) => w.id == walletRef);
|
||||
if (idx < 0) return null;
|
||||
|
||||
@@ -201,7 +230,10 @@ class WalletsProvider with ChangeNotifier {
|
||||
return next;
|
||||
}
|
||||
|
||||
Future<_WalletLoadResult> _withBalances(String orgRef, List<Wallet> base) async {
|
||||
Future<_WalletLoadResult> _withBalances(
|
||||
String orgRef,
|
||||
List<Wallet> base,
|
||||
) async {
|
||||
Exception? firstError;
|
||||
|
||||
final withBalances = await _mapConcurrent<Wallet, Wallet>(
|
||||
@@ -239,7 +271,10 @@ class WalletsProvider with ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
final workers = List.generate(min(concurrency, items.length), (_) => worker());
|
||||
final workers = List.generate(
|
||||
min(concurrency, items.length),
|
||||
(_) => worker(),
|
||||
);
|
||||
await Future.wait(workers);
|
||||
|
||||
return results.cast<R>();
|
||||
|
||||
@@ -4,6 +4,7 @@ import 'package:pshared/models/payment/quote/quote.dart';
|
||||
import 'package:pshared/utils/currency.dart';
|
||||
import 'package:pshared/utils/money.dart';
|
||||
|
||||
|
||||
Money? quoteFeeTotal(PaymentQuote? quote) {
|
||||
final preferredCurrency =
|
||||
quote?.amounts?.sourcePrincipal?.currency ??
|
||||
@@ -14,6 +15,36 @@ Money? quoteFeeTotal(PaymentQuote? quote) {
|
||||
);
|
||||
}
|
||||
|
||||
Money? quoteSourceDebitTotal(
|
||||
PaymentQuote? quote, {
|
||||
String? preferredSourceCurrency,
|
||||
}) {
|
||||
final sourceDebitTotal = quote?.amounts?.sourceDebitTotal;
|
||||
final preferredCurrency = _normalizeCurrency(
|
||||
preferredSourceCurrency ?? quote?.amounts?.sourcePrincipal?.currency,
|
||||
);
|
||||
|
||||
if (sourceDebitTotal == null) {
|
||||
return _rebuildSourceDebitTotal(
|
||||
quote,
|
||||
preferredSourceCurrency: preferredCurrency,
|
||||
);
|
||||
}
|
||||
|
||||
final debitCurrency = _normalizeCurrency(sourceDebitTotal.currency);
|
||||
if (preferredCurrency == null ||
|
||||
debitCurrency == null ||
|
||||
debitCurrency == preferredCurrency) {
|
||||
return sourceDebitTotal;
|
||||
}
|
||||
|
||||
final rebuilt = _rebuildSourceDebitTotal(
|
||||
quote,
|
||||
preferredSourceCurrency: preferredCurrency,
|
||||
);
|
||||
return rebuilt ?? sourceDebitTotal;
|
||||
}
|
||||
|
||||
Money? quoteFeeTotalFromLines(
|
||||
List<FeeLine>? lines, {
|
||||
String? preferredCurrency,
|
||||
@@ -74,6 +105,44 @@ List<Money> aggregateMoneyByCurrency(Iterable<Money?> values) {
|
||||
.toList();
|
||||
}
|
||||
|
||||
Money? _rebuildSourceDebitTotal(
|
||||
PaymentQuote? quote, {
|
||||
String? preferredSourceCurrency,
|
||||
}) {
|
||||
final sourcePrincipal = quote?.amounts?.sourcePrincipal;
|
||||
if (sourcePrincipal == null) return null;
|
||||
|
||||
final principalCurrency = _normalizeCurrency(sourcePrincipal.currency);
|
||||
if (principalCurrency == null) return null;
|
||||
if (preferredSourceCurrency != null &&
|
||||
principalCurrency != preferredSourceCurrency) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final principalAmount = parseMoneyAmount(
|
||||
sourcePrincipal.amount,
|
||||
fallback: double.nan,
|
||||
);
|
||||
if (principalAmount.isNaN) return null;
|
||||
|
||||
double totalAmount = principalAmount;
|
||||
final fee = quoteFeeTotalFromLines(
|
||||
quote?.fees?.lines,
|
||||
preferredCurrency: principalCurrency,
|
||||
);
|
||||
if (fee != null && _normalizeCurrency(fee.currency) == principalCurrency) {
|
||||
final feeAmount = parseMoneyAmount(fee.amount, fallback: double.nan);
|
||||
if (!feeAmount.isNaN) {
|
||||
totalAmount += feeAmount;
|
||||
}
|
||||
}
|
||||
|
||||
return Money(
|
||||
amount: amountToString(totalAmount),
|
||||
currency: principalCurrency,
|
||||
);
|
||||
}
|
||||
|
||||
double _lineSign(String? side) {
|
||||
final normalized = side?.trim().toLowerCase() ?? '';
|
||||
switch (normalized) {
|
||||
|
||||
Reference in New Issue
Block a user