small fixes for single payout and big chunck for multiple payouts

This commit is contained in:
Arseni
2026-02-05 21:58:37 +03:00
parent 8034847e46
commit b9748b8ab2
37 changed files with 1708 additions and 224 deletions

View File

@@ -0,0 +1,81 @@
import 'package:flutter/foundation.dart';
import 'package:pshared/models/payment/payment.dart';
import 'package:pshared/provider/organizations.dart';
import 'package:pshared/provider/payment/multiple/quotation.dart';
import 'package:pshared/provider/resource.dart';
import 'package:pshared/service/payment/multiple.dart';
import 'package:pshared/utils/exception.dart';
class MultiPaymentProvider extends ChangeNotifier {
late OrganizationsProvider _organization;
late MultiQuotationProvider _quotation;
Resource<List<Payment>> _payments = Resource(data: []);
bool _isLoaded = false;
List<Payment> get payments => _payments.data ?? [];
bool get isLoading => _payments.isLoading;
Exception? get error => _payments.error;
bool get isReady =>
_isLoaded && !_payments.isLoading && _payments.error == null;
void update(
OrganizationsProvider organization,
MultiQuotationProvider quotation,
) {
_organization = organization;
_quotation = quotation;
}
Future<List<Payment>> pay({
String? idempotencyKey,
Map<String, String>? metadata,
}) async {
if (!_organization.isOrganizationSet) {
throw StateError('Organization is not set');
}
final quoteRef = _quotation.quotation?.quoteRef;
if (quoteRef == null || quoteRef.isEmpty) {
throw StateError('Multiple quotation reference is not set');
}
final expiresAt = _quotation.quoteExpiresAt;
if (expiresAt != null && expiresAt.isBefore(DateTime.now().toUtc())) {
throw StateError('Multiple quotation is expired');
}
_setResource(_payments.copyWith(isLoading: true, error: null));
try {
final response = await MultiplePaymentsService.payByQuote(
_organization.current.id,
quoteRef,
idempotencyKey: idempotencyKey,
metadata: metadata,
);
_isLoaded = true;
_setResource(
_payments.copyWith(data: response, isLoading: false, error: null),
);
} catch (e) {
_setResource(
_payments.copyWith(data: [], isLoading: false, error: toException(e)),
);
}
return _payments.data ?? [];
}
void reset() {
_isLoaded = false;
_setResource(Resource(data: []));
}
void _setResource(Resource<List<Payment>> payments) {
_payments = payments;
notifyListeners();
}
}

View File

@@ -0,0 +1,138 @@
import 'package:flutter/foundation.dart';
import 'package:uuid/uuid.dart';
import 'package:pshared/api/requests/payment/quotes.dart';
import 'package:pshared/data/mapper/payment/intent/payment.dart';
import 'package:pshared/models/payment/intent.dart';
import 'package:pshared/models/payment/quote/quotes.dart';
import 'package:pshared/provider/organizations.dart';
import 'package:pshared/provider/resource.dart';
import 'package:pshared/service/payment/multiple.dart';
import 'package:pshared/utils/exception.dart';
class MultiQuotationProvider extends ChangeNotifier {
OrganizationsProvider? _organizations;
String? _loadedOrganizationRef;
Resource<PaymentQuotes> _quotation = Resource(data: null);
bool _isLoaded = false;
List<PaymentIntent>? _lastIntents;
bool _lastPreviewOnly = false;
Map<String, String>? _lastMetadata;
Resource<PaymentQuotes> get resource => _quotation;
PaymentQuotes? get quotation => _quotation.data;
bool get isLoading => _quotation.isLoading;
Exception? get error => _quotation.error;
bool get canRefresh => _lastIntents != null && _lastIntents!.isNotEmpty;
bool get isReady =>
_isLoaded && !_quotation.isLoading && _quotation.error == null;
DateTime? get quoteExpiresAt {
final quotes = quotation?.quotes;
if (quotes == null || quotes.isEmpty) return null;
int? minExpiresAt;
for (final quote in quotes) {
final expiresAtUnixMs = quote.fxQuote?.expiresAtUnixMs;
if (expiresAtUnixMs == null) continue;
minExpiresAt = minExpiresAt == null
? expiresAtUnixMs
: (expiresAtUnixMs < minExpiresAt ? expiresAtUnixMs : minExpiresAt);
}
if (minExpiresAt == null) return null;
return DateTime.fromMillisecondsSinceEpoch(minExpiresAt, isUtc: true);
}
void update(OrganizationsProvider organizations) {
_organizations = organizations;
if (!organizations.isOrganizationSet) {
reset();
return;
}
final orgRef = organizations.current.id;
if (_loadedOrganizationRef != orgRef) {
_loadedOrganizationRef = orgRef;
reset();
}
}
Future<PaymentQuotes?> quotePayments(
List<PaymentIntent> intents, {
bool previewOnly = false,
String? idempotencyKey,
Map<String, String>? metadata,
}) async {
final organization = _organizations;
if (organization == null || !organization.isOrganizationSet) {
throw StateError('Organization is not set');
}
if (intents.isEmpty) {
throw StateError('At least one payment intent is required');
}
_lastIntents = List<PaymentIntent>.from(intents);
_lastPreviewOnly = previewOnly;
_lastMetadata = metadata == null
? null
: Map<String, String>.from(metadata);
_setResource(_quotation.copyWith(isLoading: true, error: null));
try {
final response = await MultiplePaymentsService.getQuotation(
organization.current.id,
QuotePaymentsRequest(
idempotencyKey: idempotencyKey ?? const Uuid().v4(),
metadata: metadata,
intents: intents.map((intent) => intent.toDTO()).toList(),
previewOnly: previewOnly,
),
);
_isLoaded = true;
_setResource(
_quotation.copyWith(data: response, isLoading: false, error: null),
);
} catch (e) {
_setResource(
_quotation.copyWith(
data: null,
isLoading: false,
error: toException(e),
),
);
}
return _quotation.data;
}
Future<PaymentQuotes?> refreshQuotation() async {
final intents = _lastIntents;
if (intents == null || intents.isEmpty) return null;
return quotePayments(
intents,
previewOnly: _lastPreviewOnly,
metadata: _lastMetadata,
);
}
void reset() {
_isLoaded = false;
_lastIntents = null;
_lastPreviewOnly = false;
_lastMetadata = null;
_quotation = Resource(data: null);
notifyListeners();
}
void _setResource(Resource<PaymentQuotes> quotation) {
_quotation = quotation;
notifyListeners();
}
}

View File

@@ -158,6 +158,28 @@ class PaymentsProvider with ChangeNotifier {
notifyListeners();
}
void addPayments(List<Payment> items, {bool prepend = true}) {
if (items.isEmpty) return;
final current = List<Payment>.from(payments);
final existingRefs = <String>{};
for (final payment in current) {
final ref = payment.paymentRef;
if (ref != null && ref.isNotEmpty) {
existingRefs.add(ref);
}
}
final newItems = items.where((payment) {
final ref = payment.paymentRef;
if (ref == null || ref.isEmpty) return true;
return !existingRefs.contains(ref);
}).toList();
if (newItems.isEmpty) return;
final combined = prepend ? [...newItems, ...current] : [...current, ...newItems];
_applyResource(_resource.copyWith(data: combined, error: null), notify: true);
}
void _applyResource(Resource<List<Payment>> newResource, {required bool notify}) {
_resource = newResource;
if (notify) notifyListeners();

View File

@@ -7,6 +7,7 @@ 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/managed_wallet.dart';
@@ -44,13 +45,17 @@ class QuotationIntentBuilder {
// TODO: adapt to possible other sources
currency: currencyCodeToString(selectedWallet.currency),
);
final fxIntent = FxIntent(
pair: CurrencyPair(
base: currencyCodeToString(selectedWallet.currency),
quote: 'RUB', // TODO: exentd target currencies
),
side: FxSide.sellBaseBuyQuote,
);
final isCryptoToCrypto = paymentData is CryptoAddressPaymentMethod &&
(paymentData.asset?.tokenSymbol ?? '').trim().toUpperCase() == amount.currency;
final fxIntent = isCryptoToCrypto
? null
: FxIntent(
pair: CurrencyPair(
base: currencyCodeToString(selectedWallet.currency),
quote: 'RUB', // TODO: exentd target currencies
),
side: FxSide.sellBaseBuyQuote,
);
return PaymentIntent(
kind: PaymentKind.payout,
amount: amount,