From f157cede05b8e3ceb2dcac658fa5190078f117fc Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Wed, 29 Nov 2017 13:53:31 +0100 Subject: [PATCH] Add support for listing sources on customers --- .../sources/listing_sources_on_customer.cs | 101 ++++++++++++++++++ .../Sources/StripeSourceListOptions.cs | 13 +++ .../Services/Sources/StripeSourceService.cs | 28 +++++ 3 files changed, 142 insertions(+) create mode 100644 src/Stripe.Tests.XUnit/sources/listing_sources_on_customer.cs create mode 100644 src/Stripe.net/Services/Sources/StripeSourceListOptions.cs diff --git a/src/Stripe.Tests.XUnit/sources/listing_sources_on_customer.cs b/src/Stripe.Tests.XUnit/sources/listing_sources_on_customer.cs new file mode 100644 index 0000000000..3afbddd9f1 --- /dev/null +++ b/src/Stripe.Tests.XUnit/sources/listing_sources_on_customer.cs @@ -0,0 +1,101 @@ +using FluentAssertions; +using Xunit; + +namespace Stripe.Tests.Xunit +{ + public class listing_sources_on_customer + { + public StripeList SourceListAll { get; } + public StripeList SourceListCard { get; } + public StripeList SourceListBitcoin { get; } + + public listing_sources_on_customer() + { + var sourceService = new StripeSourceService(Cache.ApiKey); + var customerService = new StripeCustomerService(Cache.ApiKey); + + // Create customer + var CustomerCreateOptions = new StripeCustomerCreateOptions + { + Email = "source-list@example.com", + }; + var Customer = customerService.Create(CustomerCreateOptions); + + // Create card source and attach it to customer + var SourceCardCreateOptions = new StripeSourceCreateOptions + { + Type = StripeSourceType.Card, + Token = "tok_visa" + }; + var SourceCard = sourceService.Create(SourceCardCreateOptions); + + var SourceAttachOptions = new StripeSourceAttachOptions + { + Source = SourceCard.Id + }; + SourceCard = sourceService.Attach(Customer.Id, SourceAttachOptions); + + // Create bitcoin source and attach it to customer + var SourceBitcoinCreateOptions = new StripeSourceCreateOptions + { + Type = StripeSourceType.Bitcoin, + Amount = 1000, + Currency = "usd", + Owner = new StripeSourceOwner + { + Email = "jenny.rosen+fill_now@example.com", + }, + }; + var SourceBitcoin = sourceService.Create(SourceBitcoinCreateOptions); + + SourceAttachOptions.Source = SourceBitcoin.Id; + SourceBitcoin = sourceService.Attach(Customer.Id, SourceAttachOptions); + + // List sources on customer + SourceListAll = sourceService.List(Customer.Id); + + var SourceListOptions = new StripeSourceListOptions + { + Type = StripeSourceType.Card + }; + SourceListCard = sourceService.List(Customer.Id, SourceListOptions); + + SourceListOptions.Type = StripeSourceType.Bitcoin; + SourceListBitcoin = sourceService.List(Customer.Id, SourceListOptions); + } + + [Fact] + public void list_all_should_have_both_sources() + { + SourceListAll.Data.Count.Should().Be(2); + + var SourceCard = SourceListAll.Data[0]; + SourceCard.Type.Should().Be(StripeSourceType.Card); + SourceCard.Card.Brand.Should().Be("Visa"); + + var SourceBitcoin = SourceListAll.Data[1]; + SourceBitcoin.Type.Should().Be(StripeSourceType.Bitcoin); + SourceBitcoin.Bitcoin.Address.Should().NotBeNull(); + } + + [Fact] + public void list_card_should_have_card_source() + { + SourceListCard.Data.Count.Should().Be(1); + + var SourceCard = SourceListCard.Data[0]; + SourceCard.Type.Should().Be(StripeSourceType.Card); + SourceCard.Card.Brand.Should().Be("Visa"); + } + + [Fact] + public void list_bitcoin_should_have_bitcoin_source() + { + SourceListBitcoin.Data.Count.Should().Be(1); + + var SourceBitcoin = SourceListBitcoin.Data[0]; + SourceBitcoin.Type.Should().Be(StripeSourceType.Bitcoin); + SourceBitcoin.Bitcoin.Address.Should().NotBeNull(); + } + } +} diff --git a/src/Stripe.net/Services/Sources/StripeSourceListOptions.cs b/src/Stripe.net/Services/Sources/StripeSourceListOptions.cs new file mode 100644 index 0000000000..527b4efc26 --- /dev/null +++ b/src/Stripe.net/Services/Sources/StripeSourceListOptions.cs @@ -0,0 +1,13 @@ +using Newtonsoft.Json; + +namespace Stripe +{ + public class StripeSourceListOptions : StripeListOptions + { + [JsonProperty("object")] + internal string Object => "source"; + + [JsonProperty("type")] + public string Type { get; set; } + } +} diff --git a/src/Stripe.net/Services/Sources/StripeSourceService.cs b/src/Stripe.net/Services/Sources/StripeSourceService.cs index 0e283ba0c8..12df403153 100755 --- a/src/Stripe.net/Services/Sources/StripeSourceService.cs +++ b/src/Stripe.net/Services/Sources/StripeSourceService.cs @@ -40,6 +40,19 @@ public virtual StripeSource Detach(string customerId, string sourceId, StripeReq ); } + public virtual StripeList List(string customerId, StripeSourceListOptions listOptions = null, StripeRequestOptions requestOptions = null) + { + var url = string.Format(Urls.CustomerSources, customerId); + + if (listOptions == null) { + listOptions = new StripeSourceListOptions(); + } + + return Mapper>.MapFromJson( + Requestor.GetString(this.ApplyAllParameters(listOptions, url, true), + SetupRequestOptions(requestOptions)) + ); + } // Async @@ -73,5 +86,20 @@ await Requestor.DeleteAsync(url, cancellationToken) ); } + + public virtual async Task> ListAsync(string customerId, StripeSourceListOptions listOptions = null, StripeRequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken)) + { + var url = string.Format(Urls.CustomerSources, customerId); + + if (listOptions == null) { + listOptions = new StripeSourceListOptions(); + } + + return Mapper>.MapFromJson( + await Requestor.GetStringAsync(this.ApplyAllParameters(listOptions, url, true), + SetupRequestOptions(requestOptions), + cancellationToken) + ); + } } }