Skip to content

Commit 8bd8a05

Browse files
Merge pull request #31 from solid-software/subscription-customer-expanded
GET /subscriptions?expand: add customer expansion & JSON parsing
2 parents 270d8b3 + af591fe commit 8bd8a05

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import 'package:stripe/messages.dart';
22
import 'package:stripe/src/expanded.dart';
3+
import 'package:stripe/src/utils/expandable_fields/customer_expandable_field.dart';
34
import 'package:stripe/src/utils/expandable_fields/discounts_expandable_field.dart';
45
import 'package:stripe/src/utils/expandable_fields/latest_invoice_expanded_expandable_field.dart';
56

67
class SubscriptionExpanded {
78
final Subscription subscription;
89
final List<Discount>? discounts;
910
final InvoiceExpanded? latestInvoice;
11+
final Customer? customer;
1012

1113
SubscriptionExpanded({
1214
required this.subscription,
1315
this.discounts,
1416
this.latestInvoice,
17+
this.customer,
1518
});
1619

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

2629
InvoiceExpanded? latestInvoice;
2730
if (expand.contains(SubscriptionExpandableField.latestInvoice)) {
28-
latestInvoice = LatestInvoiceExpandedExpandableField(
31+
latestInvoice = const LatestInvoiceExpandedExpandableField(
2932
expand: {InvoiceExpandableField.paymentIntent},
3033
).extract(json);
3134
}
3235

36+
Customer? customer;
37+
if (expand.contains(SubscriptionExpandableField.customer)) {
38+
customer = const CustomerExpandableField().extract(json);
39+
}
40+
3341
return SubscriptionExpanded(
3442
subscription: Subscription.fromJson(json),
3543
discounts: discounts,
3644
latestInvoice: latestInvoice,
45+
customer: customer,
3746
);
3847
}
3948
}
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'package:stripe/messages.dart';
2+
import 'package:stripe/src/utils/expandable_object_field.dart';
3+
4+
class CustomerExpandableField extends ExpandableObjectField<Customer> {
5+
@override
6+
String get field => 'customer';
7+
8+
const CustomerExpandableField();
9+
10+
@override
11+
Customer parse(Map<String, dynamic> object) => Customer.fromJson(object);
12+
13+
@override
14+
String replacement(Customer parsedValue) => parsedValue.id;
15+
}

0 commit comments

Comments
 (0)