From b8b46b2b4411a4a52352258cff8df5274e8fec14 Mon Sep 17 00:00:00 2001 From: Remi Jannel Date: Thu, 20 Dec 2018 14:42:02 +0100 Subject: [PATCH] Add support for the Checkout Session resource --- appveyor.yml | 2 +- .../CheckoutSessions/CheckoutSession.cs | 19 +++++ .../Infrastructure/StripeTypeRegistry.cs | 1 + .../CheckoutSessionCreateOptions.cs | 28 +++++++ .../CheckoutSessionLineItemOptions.cs | 26 ++++++ ...CheckoutSessionPaymentIntentDataOptions.cs | 23 +++++ .../CheckoutSessionService.cs | 34 ++++++++ .../CheckoutSessions/CheckoutSessionTest.cs | 20 +++++ .../api_fixtures/checkout_session.json | 5 ++ .../CheckoutSessionServiceTest.cs | 83 +++++++++++++++++++ src/StripeTests/StripeMockFixture.cs | 2 +- 11 files changed, 241 insertions(+), 2 deletions(-) create mode 100644 src/Stripe.net/Entities/CheckoutSessions/CheckoutSession.cs create mode 100644 src/Stripe.net/Services/CheckoutSessions/CheckoutSessionCreateOptions.cs create mode 100644 src/Stripe.net/Services/CheckoutSessions/CheckoutSessionLineItemOptions.cs create mode 100644 src/Stripe.net/Services/CheckoutSessions/CheckoutSessionPaymentIntentDataOptions.cs create mode 100644 src/Stripe.net/Services/CheckoutSessions/CheckoutSessionService.cs create mode 100644 src/StripeTests/Entities/CheckoutSessions/CheckoutSessionTest.cs create mode 100644 src/StripeTests/Resources/api_fixtures/checkout_session.json create mode 100644 src/StripeTests/Services/CheckoutSessions/CheckoutSessionServiceTest.cs diff --git a/appveyor.yml b/appveyor.yml index 0638fd9b7e..7e9e630f06 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -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.39.0 + STRIPE_MOCK_VERSION: 0.40.0 deploy: - provider: NuGet diff --git a/src/Stripe.net/Entities/CheckoutSessions/CheckoutSession.cs b/src/Stripe.net/Entities/CheckoutSessions/CheckoutSession.cs new file mode 100644 index 0000000000..574594f77f --- /dev/null +++ b/src/Stripe.net/Entities/CheckoutSessions/CheckoutSession.cs @@ -0,0 +1,19 @@ +namespace Stripe +{ + using System; + using System.Collections.Generic; + using Newtonsoft.Json; + using Stripe.Infrastructure; + + public class CheckoutSession : StripeEntity, IHasId, IHasObject + { + [JsonProperty("id")] + public string Id { get; set; } + + [JsonProperty("object")] + public string Object { get; set; } + + [JsonProperty("livemode")] + public bool Livemode { get; set; } + } +} diff --git a/src/Stripe.net/Infrastructure/StripeTypeRegistry.cs b/src/Stripe.net/Infrastructure/StripeTypeRegistry.cs index 87ca87fad2..224ce6ac04 100644 --- a/src/Stripe.net/Infrastructure/StripeTypeRegistry.cs +++ b/src/Stripe.net/Infrastructure/StripeTypeRegistry.cs @@ -22,6 +22,7 @@ internal static class StripeTypeRegistry { "bank_account", typeof(BankAccount) }, { "card", typeof(Card) }, { "charge", typeof(Charge) }, + { "checkout_session", typeof(CheckoutSession) }, { "country_spec", typeof(CountrySpec) }, { "coupon", typeof(Coupon) }, { "customer", typeof(Customer) }, diff --git a/src/Stripe.net/Services/CheckoutSessions/CheckoutSessionCreateOptions.cs b/src/Stripe.net/Services/CheckoutSessions/CheckoutSessionCreateOptions.cs new file mode 100644 index 0000000000..20d904fbdb --- /dev/null +++ b/src/Stripe.net/Services/CheckoutSessions/CheckoutSessionCreateOptions.cs @@ -0,0 +1,28 @@ +namespace Stripe +{ + using System; + using System.Collections.Generic; + using Newtonsoft.Json; + using Stripe.Infrastructure; + + public class CheckoutSessionCreateOptions : BaseOptions + { + [JsonProperty("allowed_source_types")] + public List AllowedSourceTypes { get; set; } + + [JsonProperty("cancel_url")] + public string CancelUrl { get; set; } + + [JsonProperty("client_reference_id")] + public string ClientReferenceId { get; set; } + + [JsonProperty("line_items")] + public List LineItems { get; set; } + + [JsonProperty("payment_intent_data")] + public CheckoutSessionPaymentIntentDataOptions PaymentIntentData { get; set; } + + [JsonProperty("success_url")] + public string SuccessUrl { get; set; } + } +} diff --git a/src/Stripe.net/Services/CheckoutSessions/CheckoutSessionLineItemOptions.cs b/src/Stripe.net/Services/CheckoutSessions/CheckoutSessionLineItemOptions.cs new file mode 100644 index 0000000000..7f12bf51f0 --- /dev/null +++ b/src/Stripe.net/Services/CheckoutSessions/CheckoutSessionLineItemOptions.cs @@ -0,0 +1,26 @@ +namespace Stripe +{ + using System.Collections.Generic; + using Newtonsoft.Json; + + public class CheckoutSessionLineItemOptions : INestedOptions + { + [JsonProperty("amount")] + public long? Amount { get; set; } + + [JsonProperty("currency")] + public string Currency { get; set; } + + [JsonProperty("description")] + public string Description { get; set; } + + [JsonProperty("images")] + public List Images { get; set; } + + [JsonProperty("name")] + public string Name { get; set; } + + [JsonProperty("quantity")] + public long? Quantity { get; set; } + } +} diff --git a/src/Stripe.net/Services/CheckoutSessions/CheckoutSessionPaymentIntentDataOptions.cs b/src/Stripe.net/Services/CheckoutSessions/CheckoutSessionPaymentIntentDataOptions.cs new file mode 100644 index 0000000000..e3424fd744 --- /dev/null +++ b/src/Stripe.net/Services/CheckoutSessions/CheckoutSessionPaymentIntentDataOptions.cs @@ -0,0 +1,23 @@ +namespace Stripe +{ + using System.Collections.Generic; + using Newtonsoft.Json; + + public class CheckoutSessionPaymentIntentDataOptions : INestedOptions + { + [JsonProperty("description")] + public string Description { get; set; } + + [JsonProperty("metadata")] + public Dictionary Metadata { get; set; } + + [JsonProperty("receipt_email")] + public string ReceiptEmail { get; set; } + + [JsonProperty("shipping")] + public ChargeShippingOptions Shipping { get; set; } + + [JsonProperty("statement_descriptor")] + public string StatementDescriptor { get; set; } + } +} diff --git a/src/Stripe.net/Services/CheckoutSessions/CheckoutSessionService.cs b/src/Stripe.net/Services/CheckoutSessions/CheckoutSessionService.cs new file mode 100644 index 0000000000..4b3d88b47c --- /dev/null +++ b/src/Stripe.net/Services/CheckoutSessions/CheckoutSessionService.cs @@ -0,0 +1,34 @@ +namespace Stripe +{ + using System.Collections.Generic; + using System.Net; + using System.Threading; + using System.Threading.Tasks; + using Stripe.Infrastructure; + + public class CheckoutSessionService : Service, + ICreatable + { + public CheckoutSessionService() + : base(null) + { + } + + public CheckoutSessionService(string apiKey) + : base(apiKey) + { + } + + public override string BasePath => "/checkout_sessions"; + + public virtual CheckoutSession Create(CheckoutSessionCreateOptions options, RequestOptions requestOptions = null) + { + return this.CreateEntity(options, requestOptions); + } + + public virtual Task CreateAsync(CheckoutSessionCreateOptions options, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken)) + { + return this.CreateEntityAsync(options, requestOptions, cancellationToken); + } + } +} diff --git a/src/StripeTests/Entities/CheckoutSessions/CheckoutSessionTest.cs b/src/StripeTests/Entities/CheckoutSessions/CheckoutSessionTest.cs new file mode 100644 index 0000000000..919090772e --- /dev/null +++ b/src/StripeTests/Entities/CheckoutSessions/CheckoutSessionTest.cs @@ -0,0 +1,20 @@ +namespace StripeTests +{ + using Newtonsoft.Json; + using Stripe; + using Xunit; + + public class CheckoutSessionTest : BaseStripeTest + { + [Fact] + public void Deserialize() + { + var json = GetResourceAsString("api_fixtures.checkout_session.json"); + var session = JsonConvert.DeserializeObject(json); + Assert.NotNull(session); + Assert.IsType(session); + Assert.NotNull(session.Id); + Assert.Equal("checkout_session", session.Object); + } + } +} diff --git a/src/StripeTests/Resources/api_fixtures/checkout_session.json b/src/StripeTests/Resources/api_fixtures/checkout_session.json new file mode 100644 index 0000000000..835dc48ec0 --- /dev/null +++ b/src/StripeTests/Resources/api_fixtures/checkout_session.json @@ -0,0 +1,5 @@ +{ + "id": "randomid", + "object": "checkout_session", + "livemode": false +} \ No newline at end of file diff --git a/src/StripeTests/Services/CheckoutSessions/CheckoutSessionServiceTest.cs b/src/StripeTests/Services/CheckoutSessions/CheckoutSessionServiceTest.cs new file mode 100644 index 0000000000..13c8a621af --- /dev/null +++ b/src/StripeTests/Services/CheckoutSessions/CheckoutSessionServiceTest.cs @@ -0,0 +1,83 @@ +namespace StripeTests +{ + using System.Collections.Generic; + using System.Net.Http; + using System.Threading.Tasks; + + using Stripe; + using Xunit; + + public class CheckoutSessionServiceTest : BaseStripeTest + { + private readonly CheckoutSessionService service; + private readonly CheckoutSessionCreateOptions createOptions; + + public CheckoutSessionServiceTest(MockHttpClientFixture mockHttpClientFixture) + : base(mockHttpClientFixture) + { + this.service = new CheckoutSessionService(); + + this.createOptions = new CheckoutSessionCreateOptions + { + AllowedSourceTypes = new List + { + "card", + }, + CancelUrl = "https://stripe.con/cancel", + ClientReferenceId = "1234", + LineItems = new List + { + new CheckoutSessionLineItemOptions + { + Amount = 1234, + Currency = "usd", + Description = "item1", + Images = new List + { + "https://stripe.com/image1", + }, + Name = "item name", + Quantity = 2, + }, + }, + PaymentIntentData = new CheckoutSessionPaymentIntentDataOptions + { + Description = "description", + Shipping = new ChargeShippingOptions + { + Name = "name", + Phone = "555-555-5555", + Address = new AddressOptions + { + State = "CA", + City = "City", + Line1 = "Line1", + Line2 = "Line2", + PostalCode = "90210", + Country = "US", + }, + } + }, + SuccessUrl = "https://stripe.con/success", + }; + } + + [Fact] + public void Create() + { + var session = this.service.Create(this.createOptions); + this.AssertRequest(HttpMethod.Post, "/v1/checkout_sessions"); + Assert.NotNull(session); + Assert.Equal("checkout_session", session.Object); + } + + [Fact] + public async Task CreateAsync() + { + var session = await this.service.CreateAsync(this.createOptions); + this.AssertRequest(HttpMethod.Post, "/v1/checkout_sessions"); + Assert.NotNull(session); + Assert.Equal("checkout_session", session.Object); + } + } +} diff --git a/src/StripeTests/StripeMockFixture.cs b/src/StripeTests/StripeMockFixture.cs index d2f1d2cdd6..4e8d18f78c 100644 --- a/src/StripeTests/StripeMockFixture.cs +++ b/src/StripeTests/StripeMockFixture.cs @@ -12,7 +12,7 @@ public class StripeMockFixture : IDisposable /// /// If you bump this, don't forget to bump `STRIPE_MOCK_VERSION` in `appveyor.yml` as well. /// - private const string MockMinimumVersion = "0.39.0"; + private const string MockMinimumVersion = "0.40.0"; private readonly string origApiBase; private readonly string origFilesBase;