Skip to content

Commit

Permalink
Add support for the Checkout Session resource
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-stripe authored and ob-stripe committed Jan 7, 2019
1 parent 39d0348 commit b8b46b2
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 2 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
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.39.0
STRIPE_MOCK_VERSION: 0.40.0

deploy:
- provider: NuGet
Expand Down
19 changes: 19 additions & 0 deletions src/Stripe.net/Entities/CheckoutSessions/CheckoutSession.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
1 change: 1 addition & 0 deletions src/Stripe.net/Infrastructure/StripeTypeRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string> 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<CheckoutSessionLineItemOptions> LineItems { get; set; }

[JsonProperty("payment_intent_data")]
public CheckoutSessionPaymentIntentDataOptions PaymentIntentData { get; set; }

[JsonProperty("success_url")]
public string SuccessUrl { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -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<string> Images { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("quantity")]
public long? Quantity { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -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<string, string> 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; }
}
}
34 changes: 34 additions & 0 deletions src/Stripe.net/Services/CheckoutSessions/CheckoutSessionService.cs
Original file line number Diff line number Diff line change
@@ -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<CheckoutSession>,
ICreatable<CheckoutSession, CheckoutSessionCreateOptions>
{
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<CheckoutSession> CreateAsync(CheckoutSessionCreateOptions options, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return this.CreateEntityAsync(options, requestOptions, cancellationToken);
}
}
}
20 changes: 20 additions & 0 deletions src/StripeTests/Entities/CheckoutSessions/CheckoutSessionTest.cs
Original file line number Diff line number Diff line change
@@ -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<CheckoutSession>(json);
Assert.NotNull(session);
Assert.IsType<CheckoutSession>(session);
Assert.NotNull(session.Id);
Assert.Equal("checkout_session", session.Object);
}
}
}
5 changes: 5 additions & 0 deletions src/StripeTests/Resources/api_fixtures/checkout_session.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "randomid",
"object": "checkout_session",
"livemode": false
}
Original file line number Diff line number Diff line change
@@ -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<string>
{
"card",
},
CancelUrl = "https://stripe.con/cancel",
ClientReferenceId = "1234",
LineItems = new List<CheckoutSessionLineItemOptions>
{
new CheckoutSessionLineItemOptions
{
Amount = 1234,
Currency = "usd",
Description = "item1",
Images = new List<string>
{
"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);
}
}
}
2 changes: 1 addition & 1 deletion src/StripeTests/StripeMockFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class StripeMockFixture : IDisposable
/// <remarks>
/// If you bump this, don't forget to bump `STRIPE_MOCK_VERSION` in `appveyor.yml` as well.
/// </remarks>
private const string MockMinimumVersion = "0.39.0";
private const string MockMinimumVersion = "0.40.0";

private readonly string origApiBase;
private readonly string origFilesBase;
Expand Down

0 comments on commit b8b46b2

Please sign in to comment.