From 1fa302ae98f8ce700224c783a833ba7fde914249 Mon Sep 17 00:00:00 2001 From: solid-maksymtielnyi Date: Fri, 23 Aug 2024 18:11:36 +0300 Subject: [PATCH] Add update PaymentIntent method --- lib/messages.dart | 4 +- lib/messages.g.dart | 130 ++++++++++++------ .../requests/update_payment_intent.dart | 99 +++++++++++++ lib/src/resources/payment_intent.dart | 15 ++ 4 files changed, 203 insertions(+), 45 deletions(-) create mode 100644 lib/src/messages/requests/update_payment_intent.dart diff --git a/lib/messages.dart b/lib/messages.dart index f4a69fb..e0945b0 100644 --- a/lib/messages.dart +++ b/lib/messages.dart @@ -1,7 +1,6 @@ import 'package:json_annotation/json_annotation.dart'; import 'package:stripe/src/messages/converters.dart'; import 'package:stripe/src/messages/enums.dart'; -import 'package:stripe/src/messages/enums/pause_collection_behavior.dart'; export 'package:stripe/src/messages/enums.dart'; @@ -19,8 +18,8 @@ part 'src/messages/data_list.dart'; part 'src/messages/discount.dart'; part 'src/messages/event.dart'; part 'src/messages/invoice.dart'; -part 'src/messages/payment_intent.dart'; part 'src/messages/pause_collection.dart'; +part 'src/messages/payment_intent.dart'; part 'src/messages/payment_method.dart'; part 'src/messages/portal_session.dart'; part 'src/messages/price.dart'; @@ -48,6 +47,7 @@ part 'src/messages/requests/list_subscription_schedules.dart'; part 'src/messages/requests/list_subscriptions.dart'; part 'src/messages/requests/subscription_payment_settings.dart'; part 'src/messages/requests/update_customer.dart'; +part 'src/messages/requests/update_payment_intent.dart'; part 'src/messages/requests/update_subscription.dart'; part 'src/messages/requests/update_subscription_item.dart'; part 'src/messages/requests/update_subscription_schedule.dart'; diff --git a/lib/messages.g.dart b/lib/messages.g.dart index 520fab2..e26f235 100644 --- a/lib/messages.g.dart +++ b/lib/messages.g.dart @@ -774,6 +774,49 @@ Map _$TotalDiscountAmountToJson( 'discount': instance.discount, }; +PauseCollection _$PauseCollectionFromJson(Map json) => + PauseCollection( + behavior: $enumDecode(_$PauseCollectionBehaviorEnumMap, json['behavior']), + resumesAt: _$JsonConverterFromJson( + json['resumes_at'], const TimestampConverter().fromJson), + ); + +Map _$PauseCollectionToJson(PauseCollection instance) { + final val = { + 'behavior': _$PauseCollectionBehaviorEnumMap[instance.behavior]!, + }; + + void writeNotNull(String key, dynamic value) { + if (value != null) { + val[key] = value; + } + } + + writeNotNull( + 'resumes_at', + _$JsonConverterToJson( + instance.resumesAt, const TimestampConverter().toJson)); + return val; +} + +const _$PauseCollectionBehaviorEnumMap = { + PauseCollectionBehavior.keepAsDraft: 'keep_as_draft', + PauseCollectionBehavior.markUncollectible: 'mark_uncollectible', + PauseCollectionBehavior.void_: 'void', +}; + +Value? _$JsonConverterFromJson( + Object? json, + Value? Function(Json json) fromJson, +) => + json == null ? null : fromJson(json as Json); + +Json? _$JsonConverterToJson( + Value? value, + Json? Function(Value value) toJson, +) => + value == null ? null : toJson(value); + PaymentIntent _$PaymentIntentFromJson(Map json) => PaymentIntent( object: $enumDecode(_$_PaymentIntentObjectEnumMap, json['object']), @@ -862,23 +905,11 @@ const _$_PaymentIntentObjectEnumMap = { _PaymentIntentObject.payment_intent: 'payment_intent', }; -Value? _$JsonConverterFromJson( - Object? json, - Value? Function(Json json) fromJson, -) => - json == null ? null : fromJson(json as Json); - const _$SetupFutureUsageEnumMap = { SetupFutureUsage.on_session: 'on_session', SetupFutureUsage.off_session: 'off_session', }; -Json? _$JsonConverterToJson( - Value? value, - Json? Function(Value value) toJson, -) => - value == null ? null : toJson(value); - AutomaticPaymentMethods _$AutomaticPaymentMethodsFromJson( Map json) => AutomaticPaymentMethods( @@ -891,37 +922,6 @@ Map _$AutomaticPaymentMethodsToJson( 'enabled': instance.enabled, }; -PauseCollection _$PauseCollectionFromJson(Map json) => - PauseCollection( - behavior: $enumDecode(_$PauseCollectionBehaviorEnumMap, json['behavior']), - resumesAt: _$JsonConverterFromJson( - json['resumes_at'], const TimestampConverter().fromJson), - ); - -Map _$PauseCollectionToJson(PauseCollection instance) { - final val = { - 'behavior': _$PauseCollectionBehaviorEnumMap[instance.behavior]!, - }; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull( - 'resumes_at', - _$JsonConverterToJson( - instance.resumesAt, const TimestampConverter().toJson)); - return val; -} - -const _$PauseCollectionBehaviorEnumMap = { - PauseCollectionBehavior.keepAsDraft: 'keep_as_draft', - PauseCollectionBehavior.markUncollectible: 'mark_uncollectible', - PauseCollectionBehavior.void_: 'void', -}; - PaymentMethod _$PaymentMethodFromJson(Map json) => PaymentMethod( id: json['id'] as String, @@ -2372,6 +2372,50 @@ Map _$UpdateCustomerRequestToJson( return val; } +UpdatePaymentIntentRequest _$UpdatePaymentIntentRequestFromJson( + Map json) => + UpdatePaymentIntentRequest( + (json['amount'] as num?)?.toInt(), + json['currency'] as String?, + json['customer'] as String?, + json['description'] as String?, + json['metadata'] as Map?, + json['payment_method'] as String?, + json['receipt_email'] as String?, + $enumDecodeNullable( + _$SetupFutureUsageEnumMap, json['setup_future_usage']), + json['statement_descriptor'] as String?, + json['statement_descriptor_suffix'] as String?, + json['payment_method_configuration'] as String?, + ); + +Map _$UpdatePaymentIntentRequestToJson( + UpdatePaymentIntentRequest instance) { + final val = {}; + + void writeNotNull(String key, dynamic value) { + if (value != null) { + val[key] = value; + } + } + + writeNotNull('amount', instance.amount); + writeNotNull('currency', instance.currency); + writeNotNull('customer', instance.customer); + writeNotNull('description', instance.description); + writeNotNull('metadata', instance.metadata); + writeNotNull('payment_method', instance.paymentMethod); + writeNotNull('receipt_email', instance.receiptEmail); + writeNotNull('setup_future_usage', + _$SetupFutureUsageEnumMap[instance.setupFutureUsage]); + writeNotNull('statement_descriptor', instance.statementDescriptor); + writeNotNull( + 'statement_descriptor_suffix', instance.statementDescriptorSuffix); + writeNotNull( + 'payment_method_configuration', instance.paymentMethodConfiguration); + return val; +} + SubscriptionUpdate _$SubscriptionUpdateFromJson(Map json) => SubscriptionUpdate( cancelAtPeriodEnd: json['cancel_at_period_end'] as bool?, diff --git a/lib/src/messages/requests/update_payment_intent.dart b/lib/src/messages/requests/update_payment_intent.dart new file mode 100644 index 0000000..b829c86 --- /dev/null +++ b/lib/src/messages/requests/update_payment_intent.dart @@ -0,0 +1,99 @@ +part of '../../../messages.dart'; + +@JsonSerializable() +class UpdatePaymentIntentRequest { + /// Amount intended to be collected by this PaymentIntent. A positive integer representing how much to charge in the smallest currency unit (e.g., 100 cents to charge $1.00 or 100 to charge ¥100, a zero-decimal currency). The minimum amount is $0.50 US or equivalent in charge currency. The amount value supports up to eight digits (e.g., a value of 99999999 for a USD charge of $999,999.99). + final int? amount; + + /// Three-letter ISO currency code, in lowercase. Must be a supported currency. + final String? currency; + + /// ID of the Customer this PaymentIntent belongs to, if one exists. + /// Payment methods attached to other Customers cannot be used with this + /// PaymentIntent. + /// + /// If setup_future_usage is set and this PaymentIntent’s payment method is + /// not card_present, then the payment method attaches to the Customer after + /// the PaymentIntent has been confirmed and any required actions from the + /// user are complete. If the payment method is card_present and isn’t a + /// digital wallet, then a generated_card payment method representing the + /// card is created and attached to the Customer instead. + final String? customer; + + /// An arbitrary string attached to the object. Often useful for displaying + /// to users. + final String? description; + + /// 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. Individual keys can be unset by posting an empty value + /// to them. All keys can be unset by posting an empty value to metadata. + final Map? metadata; + + /// ID of the payment method (a PaymentMethod, Card, or compatible Source + /// object) to attach to this PaymentIntent. To unset this field to null, pass + /// in an empty string. + final String? paymentMethod; + + /// Email address that the receipt for the resulting payment will be sent to. + /// If receipt_email is specified for a payment in live mode, a receipt will + /// be sent regardless of your email settings. + final String? receiptEmail; + + /// Indicates that you intend to make future payments with this + /// PaymentIntent’s payment method. + /// + /// If you provide a Customer with the PaymentIntent, you can use this + /// parameter to attach the payment method to the Customer after the + /// PaymentIntent is confirmed and the customer completes any required + /// actions. If you don’t provide a Customer, you can still attach the + /// payment method to a Customer after the transaction completes. + /// + /// If the payment method is card_present and isn’t a digital wallet, Stripe + /// creates and attaches a generated_card payment method representing the + /// card to the Customer instead. + /// + /// When processing card payments, Stripe uses setup_future_usage to help you + /// comply with regional legislation and network rules, such as SCA. + /// + /// If you’ve already set setup_future_usage and you’re performing a request + /// using a publishable key, you can only update the value from on_session to + /// off_session. + final SetupFutureUsage? setupFutureUsage; + + /// Text that appears on the customer’s statement as the statement descriptor + /// for a non-card charge. This value overrides the account’s default + /// statement descriptor. For information about requirements, including the + /// 22-character limit, see the Statement Descriptor docs. + /// + /// Setting this value for a card charge returns an error. For card charges, + /// set the statement_descriptor_suffix instead. + final String? statementDescriptor; + + /// Provides information about a card charge. Concatenated to the account’s + /// statement descriptor prefix to form the complete statement descriptor that + /// appears on the customer’s statement. + final String? statementDescriptorSuffix; + + /// The ID of the payment method configuration to use with this PaymentIntent. + final String? paymentMethodConfiguration; + + const UpdatePaymentIntentRequest( + this.amount, + this.currency, + this.customer, + this.description, + this.metadata, + this.paymentMethod, + this.receiptEmail, + this.setupFutureUsage, + this.statementDescriptor, + this.statementDescriptorSuffix, + this.paymentMethodConfiguration, + ); + + factory UpdatePaymentIntentRequest.fromJson(Map json) => + _$UpdatePaymentIntentRequestFromJson(json); + + Map toJson() => _$UpdatePaymentIntentRequestToJson(this); +} diff --git a/lib/src/resources/payment_intent.dart b/lib/src/resources/payment_intent.dart index e8b76c3..b239102 100644 --- a/lib/src/resources/payment_intent.dart +++ b/lib/src/resources/payment_intent.dart @@ -9,6 +9,8 @@ import '_resource.dart'; final log = Logger('Stripe PaymentIntentResource'); class PaymentIntentResource extends Resource { + static const _resourceName = 'payment_intents'; + PaymentIntentResource(Client client) : super(client); Future create(CreatePaymentIntentRequest request) async { @@ -49,4 +51,17 @@ class PaymentIntentResource extends Resource { return intents; } + + /// https://docs.stripe.com/api/payment_intents/update + Future update( + String id, { + required UpdatePaymentIntentRequest request, + }) async { + final response = await post( + '$_resourceName/$id', + data: request.toJson(), + ); + + return PaymentIntent.fromJson(response); + } }