Skip to content

Commit

Permalink
place json parsing inside the expanded field class
Browse files Browse the repository at this point in the history
  • Loading branch information
yurii-prykhodko-solid committed Aug 22, 2024
1 parent a9027cf commit 62c3d03
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 26 deletions.
22 changes: 0 additions & 22 deletions lib/src/expanded/invoice_expanded.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,25 +10,4 @@ class InvoiceExpanded {
this.paymentIntent,
this.discounts,
});

factory InvoiceExpanded.fromJson(
Map<String, dynamic> json, {
InvoiceExpandableField? expand,
}) {
PaymentIntent? paymentIntent;
if (expand?.paymentIntentExpansion != null) {
paymentIntent = expand?.paymentIntentExpansion!.extract(json);
}

List<Discount>? discounts;
if (expand?.discountsExpansion != null) {
discounts = expand?.discountsExpansion!.extract(json);
}

return InvoiceExpanded(
invoice: Invoice.fromJson(json),
paymentIntent: paymentIntent,
discounts: discounts,
);
}
}
2 changes: 1 addition & 1 deletion lib/src/resources/invoice.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ class InvoiceResource extends Resource<Invoice> {
},
);

return InvoiceExpanded.fromJson(response, expand: expand);
return expand.parse(response);
}
}
18 changes: 15 additions & 3 deletions lib/src/utils/expandable_fields/invoice_expandable_field.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -23,9 +24,20 @@ class InvoiceExpandableField extends ExpandableObjectField<InvoiceExpanded> {

@override
InvoiceExpanded parse(Map<String, dynamic> object) {
return InvoiceExpanded.fromJson(
object,
expand: this,
PaymentIntent? paymentIntent;
if (paymentIntentExpansion != null) {
paymentIntent = paymentIntentExpansion!.extract(object);
}

List<Discount>? discounts;
if (discountsExpansion != null) {
discounts = discountsExpansion!.extract(object);
}

return InvoiceExpanded(
invoice: Invoice.fromJson(object),
paymentIntent: paymentIntent,
discounts: discounts,
);
}

Expand Down

0 comments on commit 62c3d03

Please sign in to comment.