Skip to content

Commit

Permalink
Merge pull request #31 from solid-software/subscription-customer-expa…
Browse files Browse the repository at this point in the history
…nded

GET /subscriptions?expand: add customer expansion & JSON parsing
  • Loading branch information
yurii-prykhodko-solid authored Aug 21, 2024
2 parents 270d8b3 + af591fe commit 8bd8a05
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/src/expanded/subscription_expanded.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import 'package:stripe/messages.dart';
import 'package:stripe/src/expanded.dart';
import 'package:stripe/src/utils/expandable_fields/customer_expandable_field.dart';
import 'package:stripe/src/utils/expandable_fields/discounts_expandable_field.dart';
import 'package:stripe/src/utils/expandable_fields/latest_invoice_expanded_expandable_field.dart';

class SubscriptionExpanded {
final Subscription subscription;
final List<Discount>? discounts;
final InvoiceExpanded? latestInvoice;
final Customer? customer;

SubscriptionExpanded({
required this.subscription,
this.discounts,
this.latestInvoice,
this.customer,
});

factory SubscriptionExpanded.fromJson(
Expand All @@ -20,20 +23,26 @@ class SubscriptionExpanded {
) {
List<Discount>? discounts;
if (expand.contains(SubscriptionExpandableField.discounts)) {
discounts = DiscountsExpandableField().extract(json);
discounts = const DiscountsExpandableField().extract(json);
}

InvoiceExpanded? latestInvoice;
if (expand.contains(SubscriptionExpandableField.latestInvoice)) {
latestInvoice = LatestInvoiceExpandedExpandableField(
latestInvoice = const LatestInvoiceExpandedExpandableField(
expand: {InvoiceExpandableField.paymentIntent},
).extract(json);
}

Customer? customer;
if (expand.contains(SubscriptionExpandableField.customer)) {
customer = const CustomerExpandableField().extract(json);
}

return SubscriptionExpanded(
subscription: Subscription.fromJson(json),
discounts: discounts,
latestInvoice: latestInvoice,
customer: customer,
);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
enum SubscriptionExpandableField {
discounts,
latestInvoice,
customer,
}
3 changes: 3 additions & 0 deletions lib/src/resources/subscription.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:async';
import 'package:stripe/messages.dart';
import 'package:stripe/src/expanded.dart';
import 'package:stripe/src/utils/expandable_field.dart';
import 'package:stripe/src/utils/expandable_fields/customer_expandable_field.dart';
import 'package:stripe/src/utils/expandable_fields/discounts_expandable_field.dart';
import 'package:stripe/src/utils/expandable_fields/latest_invoice_expanded_expandable_field.dart';

Expand Down Expand Up @@ -62,6 +63,8 @@ class SubscriptionResource extends Resource<Subscription> {
return LatestInvoiceExpandedExpandableField(
expand: {InvoiceExpandableField.paymentIntent},
);
case SubscriptionExpandableField.customer:
return CustomerExpandableField();
}
}

Expand Down
15 changes: 15 additions & 0 deletions lib/src/utils/expandable_fields/customer_expandable_field.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:stripe/messages.dart';
import 'package:stripe/src/utils/expandable_object_field.dart';

class CustomerExpandableField extends ExpandableObjectField<Customer> {
@override
String get field => 'customer';

const CustomerExpandableField();

@override
Customer parse(Map<String, dynamic> object) => Customer.fromJson(object);

@override
String replacement(Customer parsedValue) => parsedValue.id;
}

0 comments on commit 8bd8a05

Please sign in to comment.