import 'dart:async'; import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:collection/collection.dart'; import 'package:uuid/uuid.dart'; import 'package:pshared/api/requests/payment/quote.dart'; import 'package:pshared/data/mapper/payment/intent/payment.dart'; import 'package:pshared/models/asset.dart'; import 'package:pshared/models/payment/currency_pair.dart'; import 'package:pshared/models/payment/customer.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/managed_wallet.dart'; import 'package:pshared/models/payment/methods/type.dart'; import 'package:pshared/models/payment/money.dart'; import 'package:pshared/models/payment/type.dart'; import 'package:pshared/models/payment/settlement_mode.dart'; import 'package:pshared/models/payment/intent.dart'; import 'package:pshared/models/payment/quote.dart'; import 'package:pshared/models/recipient/recipient.dart'; import 'package:pshared/provider/organizations.dart'; import 'package:pshared/provider/payment/amount.dart'; import 'package:pshared/provider/payment/flow.dart'; import 'package:pshared/provider/payment/wallets.dart'; import 'package:pshared/provider/recipient/provider.dart'; import 'package:pshared/provider/recipient/pmethods.dart'; import 'package:pshared/provider/resource.dart'; import 'package:pshared/service/payment/quotation.dart'; import 'package:pshared/utils/currency.dart'; class QuotationProvider extends ChangeNotifier { Resource _quotation = Resource(data: null, isLoading: false, error: null); late OrganizationsProvider _organizations; bool _isLoaded = false; bool _organizationAttached = false; PaymentIntent? _pendingIntent; String? _lastRequestSignature; Timer? _debounceTimer; Timer? _expirationTimer; bool _autoRefreshEnabled = true; bool _amountEditing = false; static const _inputDebounce = Duration(milliseconds: 500); static const _expiryGracePeriod = Duration(seconds: 1); void update( OrganizationsProvider venue, PaymentAmountProvider payment, WalletsProvider wallets, PaymentFlowProvider flow, RecipientsProvider recipients, PaymentMethodsProvider methods, ) { _organizations = venue; _organizationAttached = true; final wasEditing = _amountEditing; _amountEditing = payment.isEditing; final editingJustEnded = wasEditing && !_amountEditing; _pendingIntent = _buildIntent( payment: payment, wallets: wallets, flow: flow, recipients: recipients, methods: methods, ); if (_pendingIntent == null) { _reset(); return; } if (_amountEditing) { _debounceTimer?.cancel(); return; } if (editingJustEnded) { refreshNow(force: false); return; } _scheduleQuotationRefresh(); } PaymentQuote? get quotation => hasQuoteForCurrentIntent ? _quotation.data : null; bool get isLoading => _quotation.isLoading; Exception? get error => _quotation.error; bool get autoRefreshEnabled => _autoRefreshEnabled; bool get canRequestQuote => _organizationAttached && _pendingIntent != null && _organizations.isOrganizationSet; bool get _isExpired { final remaining = timeToExpire; return remaining != null && remaining <= Duration.zero; } bool get hasQuoteForCurrentIntent { if (_pendingIntent == null || _lastRequestSignature == null) return false; return _lastRequestSignature == _signature(_pendingIntent!); } bool get isReady => _isLoaded && !_isExpired && !_quotation.isLoading && _quotation.error == null; bool get hasLiveQuote => isReady && quotation != null; Duration? get timeToExpire { final expiresAt = _quoteExpiry; if (expiresAt == null) return null; final diff = expiresAt.difference(DateTime.now().toUtc()); return diff.isNegative ? Duration.zero : diff; } Asset? get fee => quotation == null ? null : createAsset( quotation!.expectedFeeTotal!.currency, quotation!.expectedFeeTotal!.amount, ); 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 quotation) { _quotation = quotation; notifyListeners(); } void refreshNow({bool force = true}) { _debounceTimer?.cancel(); if (!canRequestQuote) { if (_pendingIntent == null) { _reset(); } return; } unawaited(_requestQuotation(_pendingIntent!, force: force)); } void setAutoRefresh(bool enabled) { if (!_organizationAttached) return; if (_autoRefreshEnabled == enabled) return; _autoRefreshEnabled = enabled; if (_autoRefreshEnabled && (!hasLiveQuote || _isExpired) && _pendingIntent != null) { unawaited(_requestQuotation(_pendingIntent!, force: true)); } else { _startExpirationTimer(); notifyListeners(); } } @override void dispose() { _debounceTimer?.cancel(); _expirationTimer?.cancel(); super.dispose(); } PaymentIntent? _buildIntent({ required PaymentAmountProvider payment, required WalletsProvider wallets, required PaymentFlowProvider flow, required RecipientsProvider recipients, required PaymentMethodsProvider methods, }) { if (!_organizationAttached || !_organizations.isOrganizationSet) return null; final type = flow.selectedType; final method = methods.methods.firstWhereOrNull((m) => m.type == type); final wallet = wallets.selectedWallet; if (wallet == null || method == null) return null; final customer = _buildCustomer( recipient: recipients.currentObject, method: method, ); return PaymentIntent( kind: PaymentKind.payout, amount: Money( amount: payment.amount.toString(), // TODO: adapt to possible other sources currency: currencyCodeToString(wallet.currency), ), destination: method.data, source: ManagedWalletPaymentMethod( managedWalletRef: wallet.id, ), fx: FxIntent( pair: CurrencyPair( base: currencyCodeToString(wallet.currency), quote: 'RUB', // TODO: exentd target currencies ), side: FxSide.sellBaseBuyQuote, ), settlementMode: payment.payerCoversFee ? SettlementMode.fixReceived : SettlementMode.fixSource, customer: customer, ); } void _scheduleQuotationRefresh() { _debounceTimer?.cancel(); if (_pendingIntent == null) { _reset(); return; } _debounceTimer = Timer(_inputDebounce, () { unawaited(_requestQuotation(_pendingIntent!, force: false)); }); } Future _requestQuotation(PaymentIntent intent, {required bool force}) async { if (!_organizationAttached || !_organizations.isOrganizationSet) { _reset(); return null; } final destinationType = intent.destination?.type; if (destinationType == PaymentType.bankAccount) { _setResource( _quotation.copyWith( data: null, isLoading: false, error: Exception('Unsupported payment endpoint type: $destinationType'), ), ); return null; } final signature = _signature(intent); final isSameIntent = _lastRequestSignature == signature; if (!force && isSameIntent && hasLiveQuote) { _startExpirationTimer(); return _quotation.data; } _setResource(_quotation.copyWith(isLoading: true, error: null)); try { final response = await QuotationService.getQuotation( _organizations.current.id, QuotePaymentRequest( idempotencyKey: Uuid().v4(), intent: intent.toDTO(), ), ); _isLoaded = true; _lastRequestSignature = signature; _setResource(_quotation.copyWith(data: response, isLoading: false, error: null)); _startExpirationTimer(); } catch (e) { _setResource(_quotation.copyWith( data: null, error: e is Exception ? e : Exception(e.toString()), isLoading: false, )); } return _quotation.data; } void _startExpirationTimer() { _expirationTimer?.cancel(); final remaining = timeToExpire; if (remaining == null) return; final triggerOffset = _autoRefreshEnabled ? _expiryGracePeriod : Duration.zero; final duration = remaining > triggerOffset ? remaining - triggerOffset : Duration.zero; _expirationTimer = Timer(duration, () { if (_autoRefreshEnabled && _pendingIntent != null) { unawaited(_requestQuotation(_pendingIntent!, force: true)); } else { notifyListeners(); } }); } void _reset() { _debounceTimer?.cancel(); _expirationTimer?.cancel(); _pendingIntent = null; _lastRequestSignature = null; _isLoaded = false; _setResource(Resource(data: null, isLoading: false, error: null)); } DateTime? get _quoteExpiry { final expiresAt = quotation?.fxQuote?.expiresAtUnixMs; if (expiresAt == null) return null; return DateTime.fromMillisecondsSinceEpoch(expiresAt, isUtc: true); } String _signature(PaymentIntent intent) { try { return jsonEncode(intent.toDTO().toJson()); } catch (_) { return jsonEncode({ 'kind': intent.kind.toString(), 'source': intent.source?.type.toString(), 'destination': intent.destination?.type.toString(), 'amount': { 'value': intent.amount?.amount, 'currency': intent.amount?.currency, }, 'fx': intent.fx == null ? null : { 'pair': { 'base': intent.fx?.pair?.base, 'quote': intent.fx?.pair?.quote, }, 'side': intent.fx?.side.toString(), }, 'settlementMode': intent.settlementMode.toString(), 'customer': intent.customer?.id, }); } } void reset() { _reset(); } }