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,31 @@
import 'package:pshared/api/requests/payment/base.dart';
class InitiatePaymentsRequest extends PaymentBaseRequest {
final String quoteRef;
const InitiatePaymentsRequest({
required super.idempotencyKey,
super.metadata,
required this.quoteRef,
});
factory InitiatePaymentsRequest.fromJson(Map<String, dynamic> json) {
return InitiatePaymentsRequest(
idempotencyKey: json['idempotencyKey'] as String,
metadata: (json['metadata'] as Map<String, dynamic>?)?.map(
(key, value) => MapEntry(key, value as String),
),
quoteRef: json['quoteRef'] as String,
);
}
@override
Map<String, dynamic> toJson() {
return <String, dynamic>{
'idempotencyKey': idempotencyKey,
'metadata': metadata,
'quoteRef': quoteRef,
};
}
}

View File

@@ -13,6 +13,8 @@ class PaymentDTO {
final String? failureCode;
final String? failureReason;
final PaymentQuoteDTO? lastQuote;
final Map<String, String>? metadata;
final String? createdAt;
const PaymentDTO({
this.paymentRef,
@@ -21,6 +23,8 @@ class PaymentDTO {
this.failureCode,
this.failureReason,
this.lastQuote,
this.metadata,
this.createdAt,
});
factory PaymentDTO.fromJson(Map<String, dynamic> json) => _$PaymentDTOFromJson(json);

View File

@@ -5,19 +5,21 @@ import 'package:pshared/data/dto/payment/payment_quote.dart';
part 'quotes.g.dart';
@JsonSerializable()
class PaymentQuotesDTO {
final String quoteRef;
final String? idempotencyKey;
final PaymentQuoteAggregateDTO? aggregate;
final List<PaymentQuoteDTO>? quotes;
const PaymentQuotesDTO({
required this.quoteRef,
this.idempotencyKey,
this.aggregate,
this.quotes,
});
factory PaymentQuotesDTO.fromJson(Map<String, dynamic> json) => _$PaymentQuotesDTOFromJson(json);
factory PaymentQuotesDTO.fromJson(Map<String, dynamic> json) =>
_$PaymentQuotesDTOFromJson(json);
Map<String, dynamic> toJson() => _$PaymentQuotesDTOToJson(this);
}

View File

@@ -11,6 +11,8 @@ extension PaymentDTOMapper on PaymentDTO {
failureCode: failureCode,
failureReason: failureReason,
lastQuote: lastQuote?.toDomain(),
metadata: metadata,
createdAt: createdAt == null ? null : DateTime.tryParse(createdAt!),
);
}
@@ -22,5 +24,7 @@ extension PaymentMapper on Payment {
failureCode: failureCode,
failureReason: failureReason,
lastQuote: lastQuote?.toDTO(),
metadata: metadata,
createdAt: createdAt?.toUtc().toIso8601String(),
);
}

View File

@@ -3,11 +3,10 @@ import 'package:pshared/data/mapper/payment/payment_quote.dart';
import 'package:pshared/data/mapper/payment/quote/aggregate.dart';
import 'package:pshared/models/payment/quote/quotes.dart';
extension PaymentQuotesDTOMapper on PaymentQuotesDTO {
PaymentQuotes toDomain({String? idempotencyKey}) => PaymentQuotes(
quoteRef: quoteRef,
idempotencyKey: idempotencyKey,
idempotencyKey: idempotencyKey ?? this.idempotencyKey,
aggregate: aggregate?.toDomain(),
quotes: quotes?.map((quote) => quote.toDomain()).toList(),
);
@@ -16,6 +15,7 @@ extension PaymentQuotesDTOMapper on PaymentQuotesDTO {
extension PaymentQuotesMapper on PaymentQuotes {
PaymentQuotesDTO toDTO() => PaymentQuotesDTO(
quoteRef: quoteRef,
idempotencyKey: idempotencyKey,
aggregate: aggregate?.toDTO(),
quotes: quotes?.map((quote) => quote.toDTO()).toList(),
);

View File

@@ -8,6 +8,8 @@ class Payment {
final String? failureCode;
final String? failureReason;
final PaymentQuote? lastQuote;
final Map<String, String>? metadata;
final DateTime? createdAt;
const Payment({
required this.paymentRef,
@@ -16,5 +18,13 @@ class Payment {
required this.failureCode,
required this.failureReason,
required this.lastQuote,
required this.metadata,
required this.createdAt,
});
bool get isFailure {
if ((failureCode ?? '').trim().isNotEmpty) return true;
final normalized = (state ?? '').trim().toLowerCase();
return normalized.contains('fail') || normalized.contains('cancel');
}
}

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,

View File

@@ -0,0 +1,60 @@
import 'package:logging/logging.dart';
import 'package:uuid/uuid.dart';
import 'package:pshared/api/requests/payment/initiate_payments.dart';
import 'package:pshared/api/requests/payment/quotes.dart';
import 'package:pshared/api/responses/payment/payments.dart';
import 'package:pshared/api/responses/payment/quotes.dart';
import 'package:pshared/data/mapper/payment/payment_response.dart';
import 'package:pshared/data/mapper/payment/quote/quotes.dart';
import 'package:pshared/models/payment/payment.dart';
import 'package:pshared/models/payment/quote/quotes.dart';
import 'package:pshared/service/authorization/service.dart';
import 'package:pshared/service/services.dart';
class MultiplePaymentsService {
static final _logger = Logger('service.payment.multiple');
static const String _objectType = Services.payments;
static Future<PaymentQuotes> getQuotation(
String organizationRef,
QuotePaymentsRequest request,
) async {
_logger.fine('Quoting multiple payments for organization $organizationRef');
final response = await AuthorizationService.getPOSTResponse(
_objectType,
'/multiquote/$organizationRef',
request.toJson(),
);
final parsed = PaymentQuotesResponse.fromJson(response);
return parsed.quote.toDomain();
}
static Future<List<Payment>> payByQuote(
String organizationRef,
String quoteRef, {
String? idempotencyKey,
Map<String, String>? metadata,
}) async {
_logger.fine(
'Executing multiple payments for quote $quoteRef in $organizationRef',
);
final request = InitiatePaymentsRequest(
idempotencyKey: idempotencyKey ?? const Uuid().v4(),
quoteRef: quoteRef,
metadata: metadata,
);
final response = await AuthorizationService.getPOSTResponse(
_objectType,
'/by-multiquote/$organizationRef',
request.toJson(),
);
final parsed = PaymentsResponse.fromJson(response);
return parsed.payments.map((payment) => payment.toDomain()).toList();
}
}

View File

@@ -16,18 +16,24 @@ class QuotationService {
static final _logger = Logger('service.payment.quotation');
static const String _objectType = Services.payments;
static Future<PaymentQuote> getQuotation(String organizationRef, QuotePaymentRequest request) async {
static Future<PaymentQuote> getQuotation(
String organizationRef,
QuotePaymentRequest request,
) async {
_logger.fine('Quoting payment for organization $organizationRef');
final response = await AuthorizationService.getPOSTResponse(
_objectType,
'/quote/$organizationRef',
_objectType,
'/quote/$organizationRef',
request.toJson(),
);
final parsed = PaymentQuoteResponse.fromJson(response);
return parsed.quote.toDomain(idempotencyKey: parsed.idempotencyKey);
}
static Future<PaymentQuotes> getMultiQuotation(String organizationRef, QuotePaymentsRequest request) async {
static Future<PaymentQuotes> getMultiQuotation(
String organizationRef,
QuotePaymentsRequest request,
) async {
_logger.fine('Quoting payments for organization $organizationRef');
final response = await AuthorizationService.getPOSTResponse(
_objectType,
@@ -35,7 +41,6 @@ class QuotationService {
request.toJson(),
);
final parsed = PaymentQuotesResponse.fromJson(response);
final idempotencyKey = response['idempotencyKey'] as String?;
return parsed.quote.toDomain(idempotencyKey: idempotencyKey);
return parsed.quote.toDomain();
}
}