173 lines
6.0 KiB
Dart
173 lines
6.0 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);
|
|
|
|
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<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();
|
|
}
|
|
}
|