Skip to content

Commit cdef993

Browse files
GET /subscriptions?expand: add customer expansion & JSON parsing
1 parent 270d8b3 commit cdef993

4 files changed

Lines changed: 31 additions & 0 deletions

File tree

lib/src/expanded/subscription_expanded.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1+
import 'dart:convert';
2+
13
import 'package:stripe/messages.dart';
24
import 'package:stripe/src/expanded.dart';
5+
import 'package:stripe/src/utils/expandable_fields/customer_expandable_field.dart';
36
import 'package:stripe/src/utils/expandable_fields/discounts_expandable_field.dart';
47
import 'package:stripe/src/utils/expandable_fields/latest_invoice_expanded_expandable_field.dart';
58

69
class SubscriptionExpanded {
710
final Subscription subscription;
811
final List<Discount>? discounts;
912
final InvoiceExpanded? latestInvoice;
13+
final Customer? customer;
1014

1115
SubscriptionExpanded({
1216
required this.subscription,
1317
this.discounts,
1418
this.latestInvoice,
19+
this.customer,
1520
});
1621

1722
factory SubscriptionExpanded.fromJson(
@@ -30,10 +35,20 @@ class SubscriptionExpanded {
3035
).extract(json);
3136
}
3237

38+
Customer? customer;
39+
if (expand.contains(SubscriptionExpandableField.customer)) {
40+
const customerExpander = CustomerExpandableField();
41+
customer = customerExpander.extract(json);
42+
43+
/// A shim so that Subscription gets parsed correctly
44+
json[customerExpander.field] = customer.id;
45+
}
46+
3347
return SubscriptionExpanded(
3448
subscription: Subscription.fromJson(json),
3549
discounts: discounts,
3650
latestInvoice: latestInvoice,
51+
customer: customer,
3752
);
3853
}
3954
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
enum SubscriptionExpandableField {
22
discounts,
33
latestInvoice,
4+
customer,
45
}

lib/src/resources/subscription.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:async';
33
import 'package:stripe/messages.dart';
44
import 'package:stripe/src/expanded.dart';
55
import 'package:stripe/src/utils/expandable_field.dart';
6+
import 'package:stripe/src/utils/expandable_fields/customer_expandable_field.dart';
67
import 'package:stripe/src/utils/expandable_fields/discounts_expandable_field.dart';
78
import 'package:stripe/src/utils/expandable_fields/latest_invoice_expanded_expandable_field.dart';
89

@@ -62,6 +63,8 @@ class SubscriptionResource extends Resource<Subscription> {
6263
return LatestInvoiceExpandedExpandableField(
6364
expand: {InvoiceExpandableField.paymentIntent},
6465
);
66+
case SubscriptionExpandableField.customer:
67+
return CustomerExpandableField();
6568
}
6669
}
6770

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'package:stripe/messages.dart';
2+
import 'package:stripe/src/utils/expandable_field.dart';
3+
4+
class CustomerExpandableField extends ExpandableField<Customer> {
5+
@override
6+
String get field => 'customer';
7+
8+
const CustomerExpandableField();
9+
10+
@override
11+
Customer extract(Map<String, dynamic> json) => Customer.fromJson(json[field]);
12+
}

0 commit comments

Comments
 (0)