99 lines
3.5 KiB
Dart
99 lines
3.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:pshared/models/asset.dart';
|
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
import 'package:pshared/models/payment/currency_pair.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/card.dart';
|
|
import 'package:pshared/models/payment/methods/managed_wallet.dart';
|
|
import 'package:pshared/models/payment/money.dart';
|
|
import 'package:pshared/models/payment/settlement_mode.dart';
|
|
import 'package:pshared/provider/payment/amount.dart';
|
|
import 'package:pshared/api/requests/payment/quote.dart';
|
|
import 'package:pshared/data/mapper/payment/intent/payment.dart';
|
|
import 'package:pshared/models/payment/intent.dart';
|
|
import 'package:pshared/models/payment/quote.dart';
|
|
import 'package:pshared/provider/organizations.dart';
|
|
import 'package:pshared/provider/resource.dart';
|
|
import 'package:pshared/service/payment/quotation.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) {
|
|
_organizations = venue;
|
|
getQuotation(PaymentIntent(
|
|
kind: PaymentKind.payout,
|
|
amount: Money(
|
|
amount: payment.amount.toString(),
|
|
currency: 'USDT',
|
|
),
|
|
destination: CardPaymentMethod(
|
|
pan: '4000000000000077',
|
|
firstName: 'John',
|
|
lastName: 'Doe',
|
|
),
|
|
source: ManagedWalletPaymentMethod(
|
|
managedWalletRef: '',
|
|
),
|
|
fx: FxIntent(
|
|
pair: CurrencyPair(
|
|
base: 'USDT',
|
|
quote: 'RUB',
|
|
),
|
|
side: FxSide.sellBaseBuyQuote,
|
|
),
|
|
settlementMode: payment.payerCoversFee ? SettlementMode.fixReceived : SettlementMode.fixSource,
|
|
));
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|