refactor of money utils with new money2 package

This commit is contained in:
Arseni
2026-03-13 03:17:29 +03:00
parent b4eb1437f6
commit 0091191d97
72 changed files with 453 additions and 982 deletions

View File

@@ -15,6 +15,7 @@ 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;
@@ -179,7 +180,7 @@ class LedgerAccountsProvider with ChangeNotifier {
Future<void> create({
required Describable describable,
required Currency currency,
required CurrencyCode currency,
String? ownerRef,
}) async {
final org = _organizations;

View File

@@ -1,3 +1,5 @@
import 'package:money2/money2.dart';
import 'package:pshared/controllers/payment/source.dart';
import 'package:pshared/models/payment/asset.dart';
import 'package:pshared/models/payment/chain_network.dart';
@@ -13,19 +15,18 @@ import 'package:pshared/models/payment/methods/data.dart';
import 'package:pshared/models/payment/methods/ledger.dart';
import 'package:pshared/models/payment/methods/managed_wallet.dart';
import 'package:pshared/models/payment/methods/type.dart';
import 'package:pshared/models/money.dart';
import 'package:pshared/models/payment/settlement_mode.dart';
import 'package:pshared/models/payment/intent.dart';
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 {
static const String _settlementCurrency = 'RUB';
static const String _addressBookCustomerFallbackId = 'address_book_customer';
PaymentIntent? build({
required PaymentAmountProvider payment,
@@ -38,10 +39,9 @@ class QuotationIntentBuilder {
final paymentData = flow.selectedPaymentData;
final selectedMethod = flow.selectedMethod;
final amountValue = payment.amount;
if (sourceMethod == null || sourceCurrency == null || paymentData == null) {
if (sourceCurrency == null) {
return null;
}
if (amountValue == null) return null;
final customer = _buildCustomer(
recipient: recipients.currentObject,
@@ -51,22 +51,22 @@ class QuotationIntentBuilder {
final amountCurrency = payment.settlementMode == SettlementMode.fixReceived
? _settlementCurrency
: sourceCurrency;
final amount = Money(
amount: amountValue.toString(),
currency: amountCurrency,
);
final currency = money2CurrencyFromCode(amountCurrency);
if (currency == null) return null;
final amount = amountValue == null
? null
: Money.fromNumWithCurrency(amountValue, currency);
final isLedgerSource = source.selectedLedgerAccount != null;
final isCryptoToCrypto =
paymentData is CryptoAddressPaymentMethod &&
(paymentData.asset?.tokenSymbol ?? '').trim().toUpperCase() ==
amount.currency;
sourceCurrency.trim().toUpperCase();
final fxIntent = _buildFxIntent(
sourceCurrency: sourceCurrency,
settlementMode: payment.settlementMode,
isLedgerSource: isLedgerSource,
enabled: !isCryptoToCrypto,
);
final comment = _resolveComment(payment.comment);
return PaymentIntent(
kind: PaymentKind.payout,
amount: amount,
@@ -77,7 +77,7 @@ class QuotationIntentBuilder {
? FeeTreatment.addToSource
: FeeTreatment.deductFromDestination,
settlementMode: payment.settlementMode,
comment: comment,
comment: payment.comment,
customer: customer,
);
}
@@ -94,14 +94,9 @@ class QuotationIntentBuilder {
// 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.sellBaseBuyQuote,
pair: CurrencyPair(base: sourceCurrency, quote: _settlementCurrency),
side: FxSide.buyBaseSellQuote,
);
}
@@ -137,39 +132,59 @@ class QuotationIntentBuilder {
required PaymentMethod? method,
required PaymentMethodData? data,
}) {
final name = recipient?.name.trim();
if (name == null || name.isEmpty) return null;
final id = recipient?.id.trim();
final customerId = id == null || id.isEmpty
? _addressBookCustomerFallbackId
: id;
final customerId = recipient?.id.trim() ?? '';
final card = _resolveCard(method: method, data: data);
final fromRecipient = _buildCustomerFromName(
customerId: customerId,
fullName: recipient?.name,
country: card?.country,
);
if (fromRecipient != null) return fromRecipient;
final parts = name.split(RegExp(r'\s+'));
if (card != null) {
final firstName = _normalizedOrNull(card.firstName);
final lastName = _normalizedOrNull(card.lastName);
if (firstName == null && lastName == null) return null;
return Customer(
id: customerId,
firstName: firstName,
lastName: lastName,
country: card.country,
);
}
return null;
}
CardPaymentMethod? _resolveCard({
required PaymentMethod? method,
required PaymentMethodData? data,
}) => method?.cardData ?? (data is CardPaymentMethod ? data : null);
Customer? _buildCustomerFromName({
required String customerId,
required String? fullName,
String? country,
}) {
final normalizedName = _normalizedOrNull(fullName);
if (normalizedName == null) return null;
final parts = normalizedName.split(RegExp(r'\s+'));
final firstName = parts.isNotEmpty ? parts.first : null;
final lastName = parts.length >= 2 ? parts.last : null;
final middleName = parts.length > 2
? parts.sublist(1, parts.length - 1).join(' ')
: null;
return Customer(
id: customerId,
firstName: firstName,
middleName: middleName,
lastName: lastName,
country: _resolveCustomerCountry(method: method, data: data),
country: country,
);
}
String? _resolveCustomerCountry({
required PaymentMethod? method,
required PaymentMethodData? data,
}) {
final card = method?.cardData ?? (data is CardPaymentMethod ? data : null);
return card?.country;
}
String? _resolveComment(String comment) {
final normalized = comment.trim();
String? _normalizedOrNull(String? value) {
if (value == null) return null;
final normalized = value.trim();
return normalized.isEmpty ? null : normalized;
}
}

View File

@@ -6,13 +6,13 @@ import 'package:logging/logging.dart';
import 'package:uuid/uuid.dart';
import 'package:money2/money2.dart';
import 'package:pshared/api/requests/payment/quote.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';
import 'package:pshared/models/payment/quote/quote.dart';
import 'package:pshared/models/money.dart';
import 'package:pshared/models/auto_refresh_mode.dart';
import 'package:pshared/provider/organizations.dart';
import 'package:pshared/provider/payment/amount.dart';
@@ -79,20 +79,12 @@ class QuotationProvider extends ChangeNotifier {
return DateTime.fromMillisecondsSinceEpoch(expiresAtUnixMs, isUtc: true);
}
Asset? get fee => _assetFromMoney(quoteFeeTotal(quotation));
Asset? get total => _assetFromMoney(
quoteSourceDebitTotal(
quotation,
preferredSourceCurrency: _sourceCurrencyCode,
),
Money? get fee => quoteFeeTotal(quotation);
Money? get total => quoteSourceDebitTotal(
quotation,
preferredSourceCurrency: _sourceCurrencyCode,
);
Asset? get recipientGets =>
_assetFromMoney(quotation?.amounts?.destinationSettlement);
Asset? _assetFromMoney(Money? money) {
if (money == null) return null;
return createAsset(money.currency, money.amount);
}
Money? get recipientGets => quotation?.amounts?.destinationSettlement;
void _setResource(Resource<PaymentQuote> quotation) {
_quotation = quotation;

View File

@@ -5,14 +5,16 @@ import 'package:flutter/foundation.dart';
import 'package:collection/collection.dart';
import 'package:pshared/models/currency.dart';
import 'package:pshared/models/describable.dart';
import 'package:pshared/models/payment/chain_network.dart';
import 'package:pshared/models/payment/wallet.dart';
import 'package:pshared/models/wallet/chain_asset.dart';
import 'package:pshared/provider/organizations.dart';
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;
@@ -180,7 +182,8 @@ class WalletsProvider with ChangeNotifier {
Future<void> create({
required Describable describable,
required ChainAsset asset,
required ChainNetwork chain,
required CurrencyCode currency,
required String? ownerRef,
}) async {
final org = _organizations;
@@ -195,7 +198,8 @@ class WalletsProvider with ChangeNotifier {
await _service.create(
organizationRef: org.current.id,
describable: describable,
asset: asset,
chain: chain,
currency: currency,
ownerRef: ownerRef,
);
await loadWalletsWithBalances();