160 lines
5.5 KiB
Dart
160 lines
5.5 KiB
Dart
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/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<PaymentQuote> _quotation = Resource(data: null, isLoading: false, error: null);
|
|
late OrganizationsProvider _organizations;
|
|
bool _isLoaded = false;
|
|
|
|
void update(
|
|
OrganizationsProvider venue,
|
|
PaymentAmountProvider payment,
|
|
WalletsProvider wallets,
|
|
PaymentFlowProvider flow,
|
|
RecipientsProvider recipients,
|
|
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,
|
|
));
|
|
}
|
|
}
|
|
|
|
PaymentQuote? get quotation => _quotation.data;
|
|
|
|
bool get isReady => _isLoaded && !_quotation.isLoading && _quotation.error == null;
|
|
|
|
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);
|
|
|
|
void _setResource(Resource<PaymentQuote> quotation) {
|
|
_quotation = quotation;
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<PaymentQuote?> getQuotation(PaymentIntent intent) async {
|
|
if (!_organizations.isOrganizationSet) throw StateError('Organization is not set');
|
|
try {
|
|
_quotation = _quotation.copyWith(isLoading: true, error: null);
|
|
final response = await QuotationService.getQuotation(
|
|
_organizations.current.id,
|
|
QuotePaymentRequest(
|
|
idempotencyKey: Uuid().v4(),
|
|
intent: intent.toDTO(),
|
|
),
|
|
);
|
|
_isLoaded = true;
|
|
_setResource(_quotation.copyWith(data: response, isLoading: false, error: null));
|
|
} catch (e) {
|
|
_setResource(_quotation.copyWith(
|
|
data: null,
|
|
error: e is Exception ? e : Exception(e.toString()),
|
|
isLoading: false,
|
|
));
|
|
}
|
|
notifyListeners();
|
|
return _quotation.data;
|
|
}
|
|
|
|
void reset() {
|
|
_setResource(Resource(data: null, isLoading: false, error: null));
|
|
_isLoaded = false;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
Customer? _buildCustomer({
|
|
required Recipient? recipient,
|
|
required PaymentMethod method,
|
|
}) {
|
|
final recipientId = (recipient?.id ?? method.recipientRef).trim();
|
|
if (recipientId.isEmpty) {
|
|
return null;
|
|
}
|
|
|
|
var firstName = '';
|
|
var lastName = '';
|
|
final cardData = method.cardData;
|
|
if (cardData != null) {
|
|
firstName = cardData.firstName.trim();
|
|
lastName = cardData.lastName.trim();
|
|
}
|
|
|
|
if ((firstName.isEmpty || lastName.isEmpty) && recipient != null) {
|
|
final parts = recipient.name.trim().split(RegExp(r'\s+'));
|
|
if (parts.isNotEmpty && firstName.isEmpty) {
|
|
firstName = parts.first;
|
|
}
|
|
if (parts.length > 1 && lastName.isEmpty) {
|
|
lastName = parts.sublist(1).join(' ');
|
|
}
|
|
}
|
|
|
|
if (lastName.isEmpty) {
|
|
lastName = firstName;
|
|
}
|
|
|
|
return Customer(
|
|
id: recipientId,
|
|
firstName: firstName.isEmpty ? null : firstName,
|
|
lastName: lastName.isEmpty ? null : lastName,
|
|
country: cardData?.country,
|
|
);
|
|
}
|