From ef507faa04889fc4d6f05ad3ff2cf4496db37300 Mon Sep 17 00:00:00 2001 From: solid-maksymtielnyi Date: Tue, 30 Jul 2024 02:27:26 +0300 Subject: [PATCH] Add Coupon model --- lib/messages.dart | 3 +- lib/messages.g.dart | 127 +++++++++++++++++++++++++++++------ lib/src/messages/coupon.dart | 125 ++++++++++++++++++++++++++++++++++ 3 files changed, 232 insertions(+), 23 deletions(-) create mode 100644 lib/src/messages/coupon.dart diff --git a/lib/messages.dart b/lib/messages.dart index be857fd..c667202 100644 --- a/lib/messages.dart +++ b/lib/messages.dart @@ -11,6 +11,7 @@ part 'src/messages/balance_transaction.dart'; part 'src/messages/billing_details.dart'; part 'src/messages/charge.dart'; part 'src/messages/checkout_session.dart'; +part 'src/messages/coupon.dart'; part 'src/messages/customer.dart'; part 'src/messages/data_list.dart'; part 'src/messages/event.dart'; @@ -32,8 +33,8 @@ part 'src/messages/requests/create_subscription_schedule.dart'; part 'src/messages/requests/list_prices.dart'; part 'src/messages/requests/list_products.dart'; part 'src/messages/requests/list_subscription_items.dart'; -part 'src/messages/requests/list_subscriptions.dart'; part 'src/messages/requests/list_subscription_schedules.dart'; +part 'src/messages/requests/list_subscriptions.dart'; part 'src/messages/requests/update_customer.dart'; part 'src/messages/requests/update_subscription.dart'; part 'src/messages/requests/update_subscription_item.dart'; diff --git a/lib/messages.g.dart b/lib/messages.g.dart index 327ef37..6da13cd 100644 --- a/lib/messages.g.dart +++ b/lib/messages.g.dart @@ -237,6 +237,89 @@ const _$PaymentMethodTypeEnumMap = { PaymentMethodType.wechat_pay: 'wechat_pay', }; +CouponAppliesTo _$CouponAppliesToFromJson(Map json) => + CouponAppliesTo( + products: (json['products'] as List?) + ?.map((e) => e as String) + .toList(), + ); + +Map _$CouponAppliesToToJson(CouponAppliesTo instance) { + final val = {}; + + void writeNotNull(String key, dynamic value) { + if (value != null) { + val[key] = value; + } + } + + writeNotNull('products', instance.products); + return val; +} + +Coupon _$CouponFromJson(Map json) => Coupon( + object: $enumDecode(_$_CouponObjectEnumMap, json['object']), + id: json['id'] as String, + duration: $enumDecode(_$CouponDurationEnumMap, json['duration']), + created: (json['created'] as num).toInt(), + livemode: json['livemode'] as bool, + timesRedeemed: (json['times_redeemed'] as num).toInt(), + valid: json['valid'] as bool, + amountOff: (json['amount_off'] as num?)?.toInt(), + currency: json['currency'] as String?, + durationInMonths: (json['duration_in_months'] as num?)?.toInt(), + metadata: json['metadata'] as Map?, + name: json['name'] as String?, + percentOff: (json['percent_off'] as num?)?.toDouble(), + appliesTo: json['applies_to'] == null + ? null + : CouponAppliesTo.fromJson( + json['applies_to'] as Map), + currencyOptions: json['currency_options'] as Map?, + maxRedemptions: (json['max_redemptions'] as num?)?.toInt(), + redeemBy: (json['redeem_by'] as num?)?.toInt(), + ); + +Map _$CouponToJson(Coupon instance) { + final val = { + 'object': _$_CouponObjectEnumMap[instance.object]!, + 'id': instance.id, + 'duration': _$CouponDurationEnumMap[instance.duration]!, + 'created': instance.created, + 'livemode': instance.livemode, + 'times_redeemed': instance.timesRedeemed, + 'valid': instance.valid, + }; + + void writeNotNull(String key, dynamic value) { + if (value != null) { + val[key] = value; + } + } + + writeNotNull('amount_off', instance.amountOff); + writeNotNull('currency', instance.currency); + writeNotNull('duration_in_months', instance.durationInMonths); + writeNotNull('metadata', instance.metadata); + writeNotNull('name', instance.name); + writeNotNull('percent_off', instance.percentOff); + writeNotNull('applies_to', instance.appliesTo?.toJson()); + writeNotNull('currency_options', instance.currencyOptions); + writeNotNull('max_redemptions', instance.maxRedemptions); + writeNotNull('redeem_by', instance.redeemBy); + return val; +} + +const _$_CouponObjectEnumMap = { + _CouponObject.coupon: 'coupon', +}; + +const _$CouponDurationEnumMap = { + CouponDuration.forever: 'forever', + CouponDuration.once: 'once', + CouponDuration.repeating: 'repeating', +}; + Customer _$CustomerFromJson(Map json) => Customer( object: $enumDecode(_$_CustomerObjectEnumMap, json['object']), id: json['id'] as String, @@ -1534,6 +1617,28 @@ Map _$ListSubscriptionItemsRequestToJson( return val; } +ListSubscriptionSchedulesRequest _$ListSubscriptionSchedulesRequestFromJson( + Map json) => + ListSubscriptionSchedulesRequest( + customer: json['customer'] as String?, + limit: (json['limit'] as num?)?.toInt(), + ); + +Map _$ListSubscriptionSchedulesRequestToJson( + ListSubscriptionSchedulesRequest instance) { + final val = {}; + + void writeNotNull(String key, dynamic value) { + if (value != null) { + val[key] = value; + } + } + + writeNotNull('customer', instance.customer); + writeNotNull('limit', instance.limit); + return val; +} + ListSubscriptionsRequest _$ListSubscriptionsRequestFromJson( Map json) => ListSubscriptionsRequest( @@ -1570,28 +1675,6 @@ const _$SubscriptionStatusEnumMap = { SubscriptionStatus.ended: 'ended', }; -ListSubscriptionSchedulesRequest _$ListSubscriptionSchedulesRequestFromJson( - Map json) => - ListSubscriptionSchedulesRequest( - customer: json['customer'] as String?, - limit: (json['limit'] as num?)?.toInt(), - ); - -Map _$ListSubscriptionSchedulesRequestToJson( - ListSubscriptionSchedulesRequest instance) { - final val = {}; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull('customer', instance.customer); - writeNotNull('limit', instance.limit); - return val; -} - UpdateCustomerRequest _$UpdateCustomerRequestFromJson( Map json) => UpdateCustomerRequest( diff --git a/lib/src/messages/coupon.dart b/lib/src/messages/coupon.dart new file mode 100644 index 0000000..3dce5df --- /dev/null +++ b/lib/src/messages/coupon.dart @@ -0,0 +1,125 @@ +part of '../../messages.dart'; + +enum _CouponObject { coupon } + +/// https://docs.stripe.com/api/coupons/object#coupon_object-duration +enum CouponDuration { + /// Applies to all charges from a subscription with this coupon applied. + forever, + + /// Applies to the first charge from a subscription with this coupon applied. + once, + + /// Applies to charges in the first duration_in_months months from a + /// subscription with this coupon applied. + repeating, +} + +/// https://docs.stripe.com/api/coupons/object#coupon_object-applies_to +@JsonSerializable() +class CouponAppliesTo { + /// A list of product IDs this coupon applies to. + final List? products; + + CouponAppliesTo({ + this.products, + }); + + factory CouponAppliesTo.fromJson(Map json) => + _$CouponAppliesToFromJson(json); + + Map toJson() => _$CouponAppliesToToJson(this); +} + +/// https://docs.stripe.com/api/coupons/object +@JsonSerializable() +class Coupon extends Message { + final _CouponObject object; + + /// Unique identifier for the object. + final String id; + + /// One of forever, once, and repeating. Describes how long a customer who + /// applies this coupon will get the discount. + final CouponDuration duration; + + /// Time at which the object was created. Measured in seconds since the Unix + /// epoch. + final int created; + + /// Has the value true if the object exists in live mode or the value false if + /// the object exists in test mode. + final bool livemode; + + /// Number of times this coupon has been applied to a customer. + final int timesRedeemed; + + /// Taking account of the above properties, whether this coupon can still be + /// applied to a customer. + final bool valid; + + /// Amount (in the [currency] specified) that will be taken off the subtotal + /// of any invoices for this customer. + final int? amountOff; + + /// If [amountOff] has been set, the three-letter ISO code for the currency + /// of the amount to take off. + final String? currency; + + /// If [duration] is repeating, the number of months the coupon applies. + /// Null if coupon [duration] is forever or once. + final int? durationInMonths; + + /// Set of key-value pairs that you can attach to an object. This can be + /// useful for storing additional information about the object in a structured + /// format. + final Map? metadata; + + /// Name of the coupon displayed to customers on for instance invoices or + /// receipts. + final String? name; + + /// Percent that will be taken off the subtotal of any invoices for this + /// customer for the duration of the coupon. For example, a coupon with + /// percent_off of 50 will make a $100 invoice $50 instead. + final double? percentOff; + + /// Contains information about what this coupon applies to. + final CouponAppliesTo? appliesTo; + + /// Coupons defined in each available currency option. Each key must be a + /// three-letter ISO currency code and a supported currency. + final Map? currencyOptions; + + /// Maximum number of times this coupon can be redeemed, in total, across all + /// customers, before it is no longer valid. + final int? maxRedemptions; + + /// Date after which the coupon can no longer be redeemed. + final int? redeemBy; + + Coupon({ + required this.object, + required this.id, + required this.duration, + required this.created, + required this.livemode, + required this.timesRedeemed, + required this.valid, + this.amountOff, + this.currency, + this.durationInMonths, + this.metadata, + this.name, + this.percentOff, + this.appliesTo, + this.currencyOptions, + this.maxRedemptions, + this.redeemBy, + }); + + factory Coupon.fromJson(Map json) => _$CouponFromJson(json); + + @override + Map toJson() => _$CouponToJson(this); +}