improved logging in callbacks

This commit is contained in:
Stephan D
2026-03-03 01:07:35 +01:00
parent b10ec79fe0
commit bae4cd6e35
45 changed files with 226 additions and 146 deletions

View File

@@ -5,20 +5,22 @@ import 'package:pshared/data/dto/payment/intent/payment.dart';
part 'initiate.g.dart';
@JsonSerializable()
class InitiatePaymentRequest extends PaymentBaseRequest {
final PaymentIntentDTO? intent;
final String? quoteRef;
final String? clientPaymentRef;
const InitiatePaymentRequest({
required super.idempotencyKey,
super.metadata,
this.intent,
this.quoteRef,
this.clientPaymentRef,
});
factory InitiatePaymentRequest.fromJson(Map<String, dynamic> json) => _$InitiatePaymentRequestFromJson(json);
factory InitiatePaymentRequest.fromJson(Map<String, dynamic> json) =>
_$InitiatePaymentRequestFromJson(json);
@override
Map<String, dynamic> toJson() => _$InitiatePaymentRequestToJson(this);
}

View File

@@ -3,18 +3,20 @@ import 'package:pshared/api/requests/payment/base.dart';
part 'initiate_payments.g.dart';
@JsonSerializable()
class InitiatePaymentsRequest extends PaymentBaseRequest {
final String quoteRef;
final String? clientPaymentRef;
const InitiatePaymentsRequest({
required super.idempotencyKey,
super.metadata,
required this.quoteRef,
this.clientPaymentRef,
});
factory InitiatePaymentsRequest.fromJson(Map<String, dynamic> json) => _$InitiatePaymentsRequestFromJson(json);
factory InitiatePaymentsRequest.fromJson(Map<String, dynamic> json) =>
_$InitiatePaymentsRequestFromJson(json);
@override
Map<String, dynamic> toJson() => _$InitiatePaymentsRequestToJson(this);
}

View File

@@ -7,7 +7,6 @@ import 'package:pshared/provider/resource.dart';
import 'package:pshared/service/payment/multiple.dart';
import 'package:pshared/utils/exception.dart';
class MultiPaymentProvider extends ChangeNotifier {
late OrganizationsProvider _organization;
late MultiQuotationProvider _quotation;
@@ -30,6 +29,7 @@ class MultiPaymentProvider extends ChangeNotifier {
Future<List<Payment>> pay({
String? idempotencyKey,
String? clientPaymentRef,
Map<String, String>? metadata,
}) async {
if (!_organization.isOrganizationSet) {
@@ -52,6 +52,7 @@ class MultiPaymentProvider extends ChangeNotifier {
_organization.current.id,
quoteRef,
idempotencyKey: idempotencyKey,
clientPaymentRef: clientPaymentRef,
metadata: metadata,
);

View File

@@ -7,12 +7,15 @@ import 'package:pshared/provider/resource.dart';
import 'package:pshared/service/payment/service.dart';
import 'package:pshared/utils/exception.dart';
class PaymentProvider extends ChangeNotifier {
late OrganizationsProvider _organization;
late QuotationProvider _quotation;
Resource<Payment> _payment = Resource(data: null, isLoading: false, error: null);
Resource<Payment> _payment = Resource(
data: null,
isLoading: false,
error: null,
);
bool _isLoaded = false;
void update(OrganizationsProvider organization, QuotationProvider quotation) {
@@ -23,15 +26,21 @@ class PaymentProvider extends ChangeNotifier {
Payment? get payment => _payment.data;
bool get isLoading => _payment.isLoading;
Exception? get error => _payment.error;
bool get isReady => _isLoaded && !_payment.isLoading && _payment.error == null;
bool get isReady =>
_isLoaded && !_payment.isLoading && _payment.error == null;
void _setResource(Resource<Payment> payment) {
_payment = payment;
notifyListeners();
}
Future<Payment?> pay({String? idempotencyKey, Map<String, String>? metadata}) async {
if (!_organization.isOrganizationSet) throw StateError('Organization is not set');
Future<Payment?> pay({
String? idempotencyKey,
String? clientPaymentRef,
Map<String, String>? metadata,
}) async {
if (!_organization.isOrganizationSet)
throw StateError('Organization is not set');
final quoteRef = _quotation.quotation?.quoteRef;
if (quoteRef == null || quoteRef.isEmpty) {
throw StateError('Quotation reference is not set');
@@ -49,12 +58,17 @@ class PaymentProvider extends ChangeNotifier {
_organization.current.id,
quoteRef,
idempotencyKey: resolvedIdempotencyKey,
clientPaymentRef: clientPaymentRef,
metadata: metadata,
);
_isLoaded = true;
_setResource(_payment.copyWith(data: response, isLoading: false, error: null));
_setResource(
_payment.copyWith(data: response, isLoading: false, error: null),
);
} catch (e) {
_setResource(_payment.copyWith(data: null, error: toException(e), isLoading: false));
_setResource(
_payment.copyWith(data: null, error: toException(e), isLoading: false),
);
}
return _payment.data;
}

View File

@@ -13,7 +13,6 @@ import 'package:pshared/models/payment/quote/quotes.dart';
import 'package:pshared/service/authorization/service.dart';
import 'package:pshared/service/services.dart';
class MultiplePaymentsService {
static final _logger = Logger('service.payment.multiple');
static const String _objectType = Services.payments;
@@ -37,6 +36,7 @@ class MultiplePaymentsService {
String organizationRef,
String quoteRef, {
String? idempotencyKey,
String? clientPaymentRef,
Map<String, String>? metadata,
}) async {
_logger.fine(
@@ -45,6 +45,7 @@ class MultiplePaymentsService {
final request = InitiatePaymentsRequest(
idempotencyKey: idempotencyKey ?? const Uuid().v4(),
quoteRef: quoteRef,
clientPaymentRef: clientPaymentRef,
metadata: metadata,
);

View File

@@ -82,6 +82,7 @@ class PaymentService {
String organizationRef,
String quotationRef, {
String? idempotencyKey,
String? clientPaymentRef,
Map<String, String>? metadata,
}) async {
_logger.fine(
@@ -90,6 +91,7 @@ class PaymentService {
final request = InitiatePaymentRequest(
idempotencyKey: idempotencyKey ?? Uuid().v4(),
quoteRef: quotationRef,
clientPaymentRef: clientPaymentRef,
metadata: metadata,
);
final response = await AuthorizationService.getPOSTResponse(

View File

@@ -158,16 +158,13 @@ void main() {
final request = InitiatePaymentRequest(
idempotencyKey: 'idem-2',
quoteRef: 'q-1',
metadata: const {'client_payment_ref': 'cp-1'},
clientPaymentRef: 'cp-1',
);
final json = request.toJson();
expect(json['idempotencyKey'], equals('idem-2'));
expect(json['quoteRef'], equals('q-1'));
expect(
(json['metadata'] as Map<String, dynamic>)['client_payment_ref'],
equals('cp-1'),
);
expect(json['clientPaymentRef'], equals('cp-1'));
expect(json.containsKey('intent'), isTrue);
expect(json['intent'], isNull);
});
@@ -176,16 +173,13 @@ void main() {
final request = InitiatePaymentsRequest(
idempotencyKey: 'idem-3',
quoteRef: 'q-2',
metadata: const {'client_payment_ref': 'cp-1'},
clientPaymentRef: 'cp-1',
);
final json = request.toJson();
expect(json['idempotencyKey'], equals('idem-3'));
expect(json['quoteRef'], equals('q-2'));
expect(
(json['metadata'] as Map<String, dynamic>)['client_payment_ref'],
equals('cp-1'),
);
expect(json['clientPaymentRef'], equals('cp-1'));
expect(json.containsKey('intentRef'), isFalse);
expect(json.containsKey('intentRefs'), isFalse);
});