Skip to content

Commit

Permalink
Add support for the SetupIntent resource and APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-stripe committed Jun 27, 2019
1 parent 0c1b1ba commit 8818fe2
Show file tree
Hide file tree
Showing 17 changed files with 619 additions and 2 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Expand Up @@ -5,7 +5,7 @@ environment:
COVERALLS_REPO_TOKEN:
secure: T0PmP8uyzCseacBCDRBlti2y9Tz5DL6fknea0MKWvbPYrzADmLY2/5kOTfYIsPUk
# If you bump this, don't forget to bump `MinimumMockVersion` in `StripeMockFixture.cs` as well.
STRIPE_MOCK_VERSION: 0.58.0
STRIPE_MOCK_VERSION: 0.60.0

deploy:
- provider: NuGet
Expand Down
15 changes: 15 additions & 0 deletions src/Stripe.net/Constants/Events.cs
Expand Up @@ -578,6 +578,21 @@ public static class Events
/// </summary>
public const string ReviewOpened = "review.opened";

/// <summary>
/// Occurs when a new SetupIntent is created.
/// </summary>
public const string SetupIntentCreated = "setup_intent.created";

/// <summary>
/// Occurs when a SetupIntent has failed the attempt to setup a payment method.
/// </summary>
public const string SetupIntentSetupFailed = "setup_intent.setup_failed";

/// <summary>
/// Occurs when a SetupIntent has successfully setup a payment method.
/// </summary>
public const string SetupIntentSucceeded = "setup_intent.succeeded";

/// <summary>
/// Occurs whenever a Sigma scheduled query run finishes.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions src/Stripe.net/Entities/PaymentIntents/PaymentIntent.cs
Expand Up @@ -192,6 +192,9 @@ public Review Review
internal ExpandableField<Review> InternalReview { get; set; }
#endregion

[JsonProperty("setup_future_usage")]
public string SetupFutureUsage { get; set; }

[JsonProperty("shipping")]
public Shipping Shipping { get; set; }

Expand Down
130 changes: 130 additions & 0 deletions src/Stripe.net/Entities/SetupIntents/SetupIntent.cs
@@ -0,0 +1,130 @@
namespace Stripe
{
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Stripe.Infrastructure;

public class SetupIntent : StripeEntity<SetupIntent>, IHasId, IHasMetadata, IHasObject
{
[JsonProperty("id")]
public string Id { get; set; }

[JsonProperty("object")]
public string Object { get; set; }

#region Expandable Application
[JsonIgnore]
public string ApplicationId
{
get => this.InternalApplication?.Id;
set => this.InternalApplication = SetExpandableFieldId(value, this.InternalApplication);
}

[JsonIgnore]
public Application Application
{
get => this.InternalApplication?.ExpandedObject;
set => this.InternalApplication = SetExpandableFieldObject(value, this.InternalApplication);
}

[JsonProperty("application")]
[JsonConverter(typeof(ExpandableFieldConverter<Application>))]
internal ExpandableField<Application> InternalApplication { get; set; }
#endregion

[JsonProperty("cancellation_reason")]
public string CancellationReason { get; set; }

[JsonProperty("client_secret")]
public string ClientSecret { get; set; }

[JsonProperty("created")]
[JsonConverter(typeof(DateTimeConverter))]
public DateTime? Created { get; set; }

#region Expandable Customer
[JsonIgnore]
public string CustomerId
{
get => this.InternalCustomer?.Id;
set => this.InternalCustomer = SetExpandableFieldId(value, this.InternalCustomer);
}

[JsonIgnore]
public Customer Customer
{
get => this.InternalCustomer?.ExpandedObject;
set => this.InternalCustomer = SetExpandableFieldObject(value, this.InternalCustomer);
}

[JsonProperty("customer")]
[JsonConverter(typeof(ExpandableFieldConverter<Customer>))]
internal ExpandableField<Customer> InternalCustomer { get; set; }
#endregion

[JsonProperty("description")]
public string Description { get; set; }

[JsonProperty("last_setup_error")]
public StripeError LastSetupError { get; set; }

[JsonProperty("livemode")]
public bool Livemode { get; set; }

[JsonProperty("metadata")]
public Dictionary<string, string> Metadata { get; set; }

[JsonProperty("next_action")]
public PaymentIntentNextAction NextAction { get; set; }

#region Expandable OnBehalfOf (Account)
[JsonIgnore]
public string OnBehalfOfId
{
get => this.InternalOnBehalfOf?.Id;
set => this.InternalOnBehalfOf = SetExpandableFieldId(value, this.InternalOnBehalfOf);
}

[JsonIgnore]
public Account OnBehalfOf
{
get => this.InternalOnBehalfOf?.ExpandedObject;
set => this.InternalOnBehalfOf = SetExpandableFieldObject(value, this.InternalOnBehalfOf);
}

[JsonProperty("on_behalf_of")]
[JsonConverter(typeof(ExpandableFieldConverter<Account>))]
internal ExpandableField<Account> InternalOnBehalfOf { get; set; }
#endregion

#region Expandable PaymentMethod
[JsonIgnore]
public string PaymentMethodId
{
get => this.InternalPaymentMethod?.Id;
set => this.InternalPaymentMethod = SetExpandableFieldId(value, this.InternalPaymentMethod);
}

[JsonIgnore]
public PaymentMethod PaymentMethod
{
get => this.InternalPaymentMethod?.ExpandedObject;
set => this.InternalPaymentMethod = SetExpandableFieldObject(value, this.InternalPaymentMethod);
}

[JsonProperty("payment_method")]
[JsonConverter(typeof(ExpandableFieldConverter<PaymentMethod>))]
internal ExpandableField<PaymentMethod> InternalPaymentMethod { get; set; }
#endregion

[JsonProperty("payment_method_types")]
public List<string> PaymentMethodTypes { get; set; }

[JsonProperty("status")]
public string Status { get; set; }

[JsonProperty("usage")]
public string Usage { get; set; }
}
}
11 changes: 11 additions & 0 deletions src/Stripe.net/Entities/StripeError.cs
Expand Up @@ -56,9 +56,20 @@ public class StripeError : StripeEntity<StripeError>
[JsonProperty("payment_intent")]
public PaymentIntent PaymentIntent { get; set; }

/// <summary>
/// The <see cref="Stripe.PaymentMethod"/> object for errors returned on a request
/// involving a <see cref="Stripe.PaymentMethod"/>.
/// </summary>
[JsonProperty("payment_method")]
public PaymentMethod PaymentMethod { get; set; }

/// <summary>
/// The <see cref="Stripe.SetupIntent"/> object for errors returned on a request
/// involving a <see cref="Stripe.SetupIntent"/>.
/// </summary>
[JsonProperty("setup_intent")]
public SetupIntent SetupIntent { get; set; }

/// <summary>
/// The source object for errors returned on a request involving a source.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/Stripe.net/Infrastructure/StripeTypeRegistry.cs
Expand Up @@ -65,6 +65,7 @@ internal static class StripeTypeRegistry
{ "reporting.report_run", typeof(Reporting.ReportRun) },
{ "reporting.report_type", typeof(Reporting.ReportType) },
{ "scheduled_query_run", typeof(Sigma.ScheduledQueryRun) },
{ "setup_intent", typeof(SetupIntent) },
{ "sku", typeof(Sku) },
{ "source", typeof(Source) },
{ "source_mandate_notification", typeof(SourceMandateNotification) },
Expand Down
Expand Up @@ -20,6 +20,9 @@ public class PaymentIntentCreateOptions : PaymentIntentSharedOptions
[JsonProperty("return_url")]
public string ReturnUrl { get; set; }

[JsonProperty("setup_future_usage")]
public string SetupFutureUsage { get; set; }

[JsonProperty("statement_descriptor")]
public string StatementDescriptor { get; set; }
}
Expand Down
11 changes: 11 additions & 0 deletions src/Stripe.net/Services/SetupIntents/SetupIntentCancelOptions.cs
@@ -0,0 +1,11 @@
namespace Stripe
{
using System.Collections.Generic;
using Newtonsoft.Json;

public class SetupIntentCancelOptions : BaseOptions
{
[JsonProperty("cancellation_reason")]
public string CancellationReason { get; set; }
}
}
22 changes: 22 additions & 0 deletions src/Stripe.net/Services/SetupIntents/SetupIntentConfirmOptions.cs
@@ -0,0 +1,22 @@
namespace Stripe
{
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public class SetupIntentConfirmOptions : BaseOptions
{
/// <summary>
/// The client secret of the SetupIntent. Required if a publishable key is used to
/// confirm the setup intent.
/// </summary>
[JsonProperty("client_secret")]
public string ClientSecret { get; set; }

[JsonProperty("payment_method")]
public string PaymentMethodId { get; set; }

[JsonProperty("return_url")]
public string ReturnUrl { get; set; }
}
}
29 changes: 29 additions & 0 deletions src/Stripe.net/Services/SetupIntents/SetupIntentCreateOptions.cs
@@ -0,0 +1,29 @@
namespace Stripe
{
using System.Collections.Generic;
using Newtonsoft.Json;

public class SetupIntentCreateOptions : BaseOptions
{
[JsonProperty("customer")]
public string CustomerId { get; set; }

[JsonProperty("description")]
public string Description { get; set; }

[JsonProperty("metadata")]
public Dictionary<string, string> Metadata { get; set; }

[JsonProperty("on_behalf_of")]
public string OnBehalfOf { get; set; }

[JsonProperty("payment_method")]
public string PaymentMethodId { get; set; }

[JsonProperty("payment_method_types")]
public List<string> PaymentMethodTypes { get; set; }

[JsonProperty("usage")]
public string Usage { get; set; }
}
}
14 changes: 14 additions & 0 deletions src/Stripe.net/Services/SetupIntents/SetupIntentGetOptions.cs
@@ -0,0 +1,14 @@
namespace Stripe
{
using Newtonsoft.Json;

public class SetupIntentGetOptions : BaseOptions
{
/// <summary>
/// The client secret of the SetupIntent. Required if a publishable key is used to
/// retrieve the setup intent.
/// </summary>
[JsonProperty("client_secret")]
public string ClientSecret { get; set; }
}
}
10 changes: 10 additions & 0 deletions src/Stripe.net/Services/SetupIntents/SetupIntentListOptions.cs
@@ -0,0 +1,10 @@
namespace Stripe
{
using Newtonsoft.Json;

public class SetupIntentListOptions : ListOptions
{
[JsonProperty("customer")]
public string CustomerId { get; set; }
}
}
92 changes: 92 additions & 0 deletions src/Stripe.net/Services/SetupIntents/SetupIntentService.cs
@@ -0,0 +1,92 @@
namespace Stripe
{
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

public class SetupIntentService : Service<SetupIntent>,
ICreatable<SetupIntent, SetupIntentCreateOptions>,
IListable<SetupIntent, SetupIntentListOptions>,
IRetrievable<SetupIntent, SetupIntentGetOptions>,
IUpdatable<SetupIntent, SetupIntentUpdateOptions>
{
public SetupIntentService()
: base(null)
{
}

public SetupIntentService(IStripeClient client)
: base(client)
{
}

public override string BasePath => "/v1/setup_intents";

public virtual SetupIntent Cancel(string paymentIntentId, SetupIntentCancelOptions options, RequestOptions requestOptions = null)
{
return this.Request(HttpMethod.Post, $"{this.InstanceUrl(paymentIntentId)}/cancel", options, requestOptions);
}

public virtual Task<SetupIntent> CancelAsync(string paymentIntentId, SetupIntentCancelOptions options, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return this.RequestAsync(HttpMethod.Post, $"{this.InstanceUrl(paymentIntentId)}/cancel", options, requestOptions, cancellationToken);
}

public virtual SetupIntent Confirm(string paymentIntentId, SetupIntentConfirmOptions options, RequestOptions requestOptions = null)
{
return this.Request(HttpMethod.Post, $"{this.InstanceUrl(paymentIntentId)}/confirm", options, requestOptions);
}

public virtual Task<SetupIntent> ConfirmAsync(string paymentIntentId, SetupIntentConfirmOptions options, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return this.RequestAsync(HttpMethod.Post, $"{this.InstanceUrl(paymentIntentId)}/confirm", options, requestOptions, cancellationToken);
}

public virtual SetupIntent Create(SetupIntentCreateOptions options, RequestOptions requestOptions = null)
{
return this.CreateEntity(options, requestOptions);
}

public virtual Task<SetupIntent> CreateAsync(SetupIntentCreateOptions options, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return this.CreateEntityAsync(options, requestOptions, cancellationToken);
}

public virtual SetupIntent Get(string paymentIntentId, SetupIntentGetOptions options = null, RequestOptions requestOptions = null)
{
return this.GetEntity(paymentIntentId, options, requestOptions);
}

public virtual Task<SetupIntent> GetAsync(string paymentIntentId, SetupIntentGetOptions options = null, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return this.GetEntityAsync(paymentIntentId, options, requestOptions, cancellationToken);
}

public virtual StripeList<SetupIntent> List(SetupIntentListOptions options = null, RequestOptions requestOptions = null)
{
return this.ListEntities(options, requestOptions);
}

public virtual Task<StripeList<SetupIntent>> ListAsync(SetupIntentListOptions options = null, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return this.ListEntitiesAsync(options, requestOptions, cancellationToken);
}

public virtual IEnumerable<SetupIntent> ListAutoPaging(SetupIntentListOptions options = null, RequestOptions requestOptions = null)
{
return this.ListEntitiesAutoPaging(options, requestOptions);
}

public virtual SetupIntent Update(string paymentIntentId, SetupIntentUpdateOptions options, RequestOptions requestOptions = null)
{
return this.UpdateEntity(paymentIntentId, options, requestOptions);
}

public virtual Task<SetupIntent> UpdateAsync(string paymentIntentId, SetupIntentUpdateOptions options, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return this.UpdateEntityAsync(paymentIntentId, options, requestOptions, cancellationToken);
}
}
}

0 comments on commit 8818fe2

Please sign in to comment.