From 62c3d038abd3faf48e3a089e1fb02d8be5bbc95a Mon Sep 17 00:00:00 2001 From: Yurii Prykhodko Date: Thu, 22 Aug 2024 11:20:59 +0300 Subject: [PATCH] place json parsing inside the expanded field class --- lib/src/expanded/invoice_expanded.dart | 22 ------------------- lib/src/resources/invoice.dart | 2 +- .../invoice_expandable_field.dart | 18 ++++++++++++--- 3 files changed, 16 insertions(+), 26 deletions(-) diff --git a/lib/src/expanded/invoice_expanded.dart b/lib/src/expanded/invoice_expanded.dart index d5988c0..071f533 100644 --- a/lib/src/expanded/invoice_expanded.dart +++ b/lib/src/expanded/invoice_expanded.dart @@ -1,5 +1,4 @@ import 'package:stripe/messages.dart'; -import 'package:stripe/src/utils/expandable_fields/invoice_expandable_field.dart'; class InvoiceExpanded { final Invoice invoice; @@ -11,25 +10,4 @@ class InvoiceExpanded { this.paymentIntent, this.discounts, }); - - factory InvoiceExpanded.fromJson( - Map json, { - InvoiceExpandableField? expand, - }) { - PaymentIntent? paymentIntent; - if (expand?.paymentIntentExpansion != null) { - paymentIntent = expand?.paymentIntentExpansion!.extract(json); - } - - List? discounts; - if (expand?.discountsExpansion != null) { - discounts = expand?.discountsExpansion!.extract(json); - } - - return InvoiceExpanded( - invoice: Invoice.fromJson(json), - paymentIntent: paymentIntent, - discounts: discounts, - ); - } } diff --git a/lib/src/resources/invoice.dart b/lib/src/resources/invoice.dart index 5bc9a19..38eb691 100644 --- a/lib/src/resources/invoice.dart +++ b/lib/src/resources/invoice.dart @@ -31,6 +31,6 @@ class InvoiceResource extends Resource { }, ); - return InvoiceExpanded.fromJson(response, expand: expand); + return expand.parse(response); } } diff --git a/lib/src/utils/expandable_fields/invoice_expandable_field.dart b/lib/src/utils/expandable_fields/invoice_expandable_field.dart index 3dedd44..8bb9fba 100644 --- a/lib/src/utils/expandable_fields/invoice_expandable_field.dart +++ b/lib/src/utils/expandable_fields/invoice_expandable_field.dart @@ -1,3 +1,4 @@ +import 'package:stripe/messages.dart'; import 'package:stripe/src/expanded.dart'; import 'package:stripe/src/utils/expandable_fields/discounts_expandable_field.dart'; import 'package:stripe/src/utils/expandable_fields/payment_intent_expandable_field.dart'; @@ -23,9 +24,20 @@ class InvoiceExpandableField extends ExpandableObjectField { @override InvoiceExpanded parse(Map object) { - return InvoiceExpanded.fromJson( - object, - expand: this, + PaymentIntent? paymentIntent; + if (paymentIntentExpansion != null) { + paymentIntent = paymentIntentExpansion!.extract(object); + } + + List? discounts; + if (discountsExpansion != null) { + discounts = discountsExpansion!.extract(object); + } + + return InvoiceExpanded( + invoice: Invoice.fromJson(object), + paymentIntent: paymentIntent, + discounts: discounts, ); }