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 committed Jan 3, 2019
1 parent cb9318c commit abbf853
Show file tree
Hide file tree
Showing 11 changed files with 240 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 `BaseStripeTest.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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected override Dictionary<string, Func<string, IHasObject>> ObjectsToMapperF
{ "bank_account", Mapper<BankAccount>.MapFromJson },
{ "card", Mapper<Card>.MapFromJson },
{ "charge", Mapper<Charge>.MapFromJson },
{ "checkout_session", Mapper<CheckoutSession>.MapFromJson },
{ "country_spec", Mapper<CountrySpec>.MapFromJson },
{ "coupon", Mapper<Coupon>.MapFromJson },
{ "customer", Mapper<Customer>.MapFromJson },
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);
}
}
}
2 changes: 1 addition & 1 deletion src/StripeTests/BaseStripeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class BaseStripeTest
/// <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 static Mock<HttpClientHandler> mockHandler;

Expand Down
20 changes: 20 additions & 0 deletions src/StripeTests/Entities/Coupons/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,82 @@
namespace StripeTests
{
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

using Stripe;
using Xunit;

public class CheckoutSessionServiceTest : BaseStripeTest
{
private CheckoutSessionService service;
private CheckoutSessionCreateOptions createOptions;

public CheckoutSessionServiceTest()
{
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);
}
}
}

0 comments on commit abbf853

Please sign in to comment.