Added quote expiry-aware flows with auto-refresh
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
@@ -15,6 +18,7 @@ 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';
|
||||
@@ -34,9 +38,18 @@ class QuotationProvider extends ChangeNotifier {
|
||||
Resource<PaymentQuote> _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;
|
||||
|
||||
static const _inputDebounce = Duration(milliseconds: 500);
|
||||
static const _expiryGracePeriod = Duration(seconds: 1);
|
||||
|
||||
void update(
|
||||
OrganizationsProvider venue,
|
||||
OrganizationsProvider venue,
|
||||
PaymentAmountProvider payment,
|
||||
WalletsProvider wallets,
|
||||
PaymentFlowProvider flow,
|
||||
@@ -44,44 +57,65 @@ class QuotationProvider extends ChangeNotifier {
|
||||
PaymentMethodsProvider methods,
|
||||
) {
|
||||
_organizations = venue;
|
||||
final t = flow.selectedType;
|
||||
final method = methods.methods.firstWhereOrNull((m) => m.type == t);
|
||||
if ((wallets.selectedWallet != null) && (method != null)) {
|
||||
final customer = _buildCustomer(
|
||||
recipient: recipients.currentObject,
|
||||
method: method,
|
||||
);
|
||||
getQuotation(PaymentIntent(
|
||||
kind: PaymentKind.payout,
|
||||
amount: Money(
|
||||
amount: payment.amount.toString(),
|
||||
// TODO: adapt to possible other sources
|
||||
currency: currencyCodeToString(wallets.selectedWallet!.currency),
|
||||
),
|
||||
destination: method.data,
|
||||
source: ManagedWalletPaymentMethod(
|
||||
managedWalletRef: wallets.selectedWallet!.id,
|
||||
),
|
||||
fx: FxIntent(
|
||||
pair: CurrencyPair(
|
||||
base: currencyCodeToString(wallets.selectedWallet!.currency),
|
||||
quote: 'RUB', // TODO: exentd target currencies
|
||||
),
|
||||
side: FxSide.sellBaseBuyQuote,
|
||||
),
|
||||
settlementMode: payment.payerCoversFee ? SettlementMode.fixReceived : SettlementMode.fixSource,
|
||||
customer: customer,
|
||||
));
|
||||
}
|
||||
_organizationAttached = true;
|
||||
_pendingIntent = _buildIntent(
|
||||
payment: payment,
|
||||
wallets: wallets,
|
||||
flow: flow,
|
||||
recipients: recipients,
|
||||
methods: methods,
|
||||
);
|
||||
_scheduleQuotationRefresh();
|
||||
}
|
||||
|
||||
PaymentQuote? get quotation => _quotation.data;
|
||||
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 isReady => _isLoaded && !_quotation.isLoading && _quotation.error == null;
|
||||
bool get _isExpired {
|
||||
final remaining = timeToExpire;
|
||||
return remaining != null && remaining <= Duration.zero;
|
||||
}
|
||||
|
||||
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);
|
||||
bool get hasQuoteForCurrentIntent {
|
||||
if (_pendingIntent == null || _lastRequestSignature == null) return false;
|
||||
return _lastRequestSignature == _signature(_pendingIntent!);
|
||||
}
|
||||
|
||||
bool get hasLiveQuote =>
|
||||
_isLoaded &&
|
||||
!_isExpired &&
|
||||
!_quotation.isLoading &&
|
||||
_quotation.error == null &&
|
||||
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,
|
||||
@@ -140,19 +174,127 @@ class QuotationProvider extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<PaymentQuote?> getQuotation(PaymentIntent intent) async {
|
||||
if (!_organizations.isOrganizationSet) throw StateError('Organization is not set');
|
||||
void refreshNow({bool force = true}) {
|
||||
_debounceTimer?.cancel();
|
||||
if (!_organizationAttached) return;
|
||||
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<PaymentQuote?> _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 {
|
||||
_quotation = _quotation.copyWith(isLoading: true, error: null);
|
||||
final response = await QuotationService.getQuotation(
|
||||
_organizations.current.id,
|
||||
_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,
|
||||
@@ -160,13 +302,68 @@ class QuotationProvider extends ChangeNotifier {
|
||||
isLoading: false,
|
||||
));
|
||||
}
|
||||
notifyListeners();
|
||||
return _quotation.data;
|
||||
}
|
||||
|
||||
void reset() {
|
||||
_setResource(Resource(data: null, isLoading: false, error: null));
|
||||
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;
|
||||
notifyListeners();
|
||||
_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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user