Skip to content

Commit 290dba1

Browse files
Merge pull request #42 from solid-software/subscription_payment_method
Add default_payment_method and default_source to Subscription
2 parents 0402c4e + 739de09 commit 290dba1

7 files changed

Lines changed: 83 additions & 0 deletions

File tree

lib/messages.g.dart

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/expanded/subscription_expanded.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import 'package:stripe/messages.dart';
22
import 'package:stripe/src/expanded.dart';
33
import 'package:stripe/src/utils/expandable_fields/customer_expandable_field.dart';
4+
import 'package:stripe/src/utils/expandable_fields/default_payment_method_expandable_field.dart';
5+
import 'package:stripe/src/utils/expandable_fields/default_source_expandable_field.dart';
46
import 'package:stripe/src/utils/expandable_fields/discounts_expandable_field.dart';
57
import 'package:stripe/src/utils/expandable_fields/latest_invoice_expanded_expandable_field.dart';
68

@@ -9,12 +11,16 @@ class SubscriptionExpanded {
911
final List<Discount>? discounts;
1012
final InvoiceExpanded? latestInvoice;
1113
final Customer? customer;
14+
final PaymentMethod? defaultPaymentMethod;
15+
final Source? defaultSource;
1216

1317
SubscriptionExpanded({
1418
required this.subscription,
1519
this.discounts,
1620
this.latestInvoice,
1721
this.customer,
22+
this.defaultPaymentMethod,
23+
this.defaultSource,
1824
});
1925

2026
factory SubscriptionExpanded.fromJson(
@@ -38,11 +44,24 @@ class SubscriptionExpanded {
3844
customer = const CustomerExpandableField().extract(json);
3945
}
4046

47+
PaymentMethod? defaultPaymentMethod;
48+
if (expand.contains(SubscriptionExpandableField.defaultPaymentMethod)) {
49+
defaultPaymentMethod =
50+
const DefaultPaymentMethodExpandableField().extract(json);
51+
}
52+
53+
Source? defaultSource;
54+
if (expand.contains(SubscriptionExpandableField.defaultSource)) {
55+
defaultSource = const DefaultSourceExpandableField().extract(json);
56+
}
57+
4158
return SubscriptionExpanded(
4259
subscription: Subscription.fromJson(json),
4360
discounts: discounts,
4461
latestInvoice: latestInvoice,
4562
customer: customer,
63+
defaultPaymentMethod: defaultPaymentMethod,
64+
defaultSource: defaultSource,
4665
);
4766
}
4867
}

lib/src/messages/enums/expandable_fields/subscription_expandable_field.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ enum SubscriptionExpandableField {
22
discounts,
33
latestInvoice,
44
customer,
5+
defaultPaymentMethod,
6+
defaultSource,
57
}

lib/src/messages/subscription.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@ class Subscription extends Message {
117117
/// updated to `paused`.
118118
final PauseCollection? pauseCollection;
119119

120+
/// ID of the default payment method for the subscription. It must belong to
121+
/// the customer associated with the subscription. This takes precedence over
122+
/// default_source. If neither are set, invoices will use the customer’s
123+
/// invoice_settings.default_payment_method or default_source.
124+
final String? defaultPaymentMethod;
125+
126+
/// ID of the default payment source for the subscription. It must belong to
127+
/// the customer associated with the subscription and be in a chargeable
128+
/// state. If default_payment_method is also set, default_payment_method will
129+
/// take precedence. If neither are set, invoices will use the customer’s
130+
/// invoice_settings.default_payment_method or default_source.
131+
final String? defaultSource;
132+
120133
Subscription({
121134
required this.object,
122135
required this.id,
@@ -135,6 +148,8 @@ class Subscription extends Message {
135148
this.latestInvoice,
136149
this.discounts,
137150
this.pauseCollection,
151+
this.defaultPaymentMethod,
152+
this.defaultSource,
138153
});
139154

140155
factory Subscription.fromJson(Map<String, dynamic> json) =>

lib/src/resources/subscription.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ class SubscriptionResource extends Resource<Subscription> {
4949
return 'latest_invoice.payment_intent';
5050
case SubscriptionExpandableField.customer:
5151
return 'customer';
52+
case SubscriptionExpandableField.defaultPaymentMethod:
53+
return 'default_payment_method';
54+
case SubscriptionExpandableField.defaultSource:
55+
return 'default_source';
5256
}
5357
}).toList();
5458
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import 'package:stripe/messages.dart';
2+
import 'package:stripe/src/utils/expandable_object_field.dart';
3+
4+
class DefaultPaymentMethodExpandableField
5+
extends ExpandableObjectField<PaymentMethod> {
6+
@override
7+
String get field => 'default_payment_method';
8+
9+
const DefaultPaymentMethodExpandableField();
10+
11+
@override
12+
PaymentMethod parse(Map<String, dynamic> object) {
13+
return PaymentMethod.fromJson(object);
14+
}
15+
16+
@override
17+
String replacement(PaymentMethod parsedValue) {
18+
return parsedValue.id;
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'package:stripe/messages.dart';
2+
import 'package:stripe/src/utils/expandable_object_field.dart';
3+
4+
class DefaultSourceExpandableField extends ExpandableObjectField<Source> {
5+
@override
6+
String get field => 'default_source';
7+
8+
const DefaultSourceExpandableField();
9+
10+
@override
11+
Source parse(Map<String, dynamic> object) {
12+
return Source.fromJson(object);
13+
}
14+
15+
@override
16+
String replacement(Source parsedValue) {
17+
return parsedValue.id;
18+
}
19+
}

0 commit comments

Comments
 (0)