Skip to content

Commit

Permalink
Fix services
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Sep 21, 2018
1 parent 8cfc998 commit fb68732
Show file tree
Hide file tree
Showing 24 changed files with 508 additions and 289 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Stripe
namespace Stripe.Sigma
{
using System;
using Newtonsoft.Json;
Expand Down
24 changes: 16 additions & 8 deletions src/Stripe.net/Infrastructure/Requestor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public static StripeResponse Delete(string url, RequestOptions requestOptions)
return ExecuteRequest(wr);
}

public static StripeResponse PostFile(string url, string fileName, Stream fileStream, string purpose, RequestOptions requestOptions)
public static StripeResponse PostFile(string url, Stream stream, string purpose, RequestOptions requestOptions)
{
var wr = GetRequestMessage(url, HttpMethod.Post, requestOptions);

ApplyMultiPartFileToRequest(wr, fileName, fileStream, purpose);
ApplyMultiPartFileToRequest(wr, stream, purpose);

return ExecuteRequest(wr);
}
Expand Down Expand Up @@ -94,11 +94,11 @@ private static StripeResponse ExecuteRequest(HttpRequestMessage requestMessage)
return ExecuteRequestAsync(wr, cancellationToken);
}

public static Task<StripeResponse> PostFileAsync(string url, string fileName, Stream fileStream, string purpose, RequestOptions requestOptions, CancellationToken cancellationToken = default(CancellationToken))
public static Task<StripeResponse> PostFileAsync(string url, Stream stream, string purpose, RequestOptions requestOptions, CancellationToken cancellationToken = default(CancellationToken))
{
var wr = GetRequestMessage(url, HttpMethod.Post, requestOptions);

ApplyMultiPartFileToRequest(wr, fileName, fileStream, purpose);
ApplyMultiPartFileToRequest(wr, stream, purpose);

return ExecuteRequestAsync(wr, cancellationToken);
}
Expand Down Expand Up @@ -187,16 +187,24 @@ private static string GetAuthorizationHeaderValue(string apiKey)
return $"Bearer {apiKey}";
}

private static void ApplyMultiPartFileToRequest(HttpRequestMessage requestMessage, string fileName, Stream fileStream, string purpose)
private static void ApplyMultiPartFileToRequest(HttpRequestMessage requestMessage, Stream stream, string purpose)
{
requestMessage.Headers.ExpectContinue = true;

if (string.IsNullOrEmpty(fileName))
string fileName = "blob";

#if NET45
// Doing this on .NET Standard would require us to bump the minimum framework version
// to .NET Standard 1.3, which isn't worth it since the filename is basically ignored
// by the server.
FileStream fileStream = stream as FileStream;
if ((fileStream != null) && (!string.IsNullOrEmpty(fileStream.Name)))
{
fileName = "blob";
fileName = fileStream.Name;
}
#endif

var fileContent = new StreamContent(fileStream);
var fileContent = new StreamContent(stream);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "\"file\"",
Expand Down
6 changes: 0 additions & 6 deletions src/Stripe.net/Infrastructure/Urls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ internal static class Urls

internal static string BaseUrl => StripeConfiguration.GetApiBase();

public static string Invoices => BaseUrl + "/invoices";

public static string InvoiceItems => BaseUrl + "/invoiceitems";

public static string Tokens => BaseUrl + "/tokens";
Expand All @@ -18,10 +16,6 @@ internal static class Urls

public static string Plans => BaseUrl + "/plans";

public static string Balance => BaseUrl + "/balance";

public static string BalanceTransactions => BaseUrl + "/balance/history";

public static string Customers => BaseUrl + "/customers";

public static string CustomerSources => BaseUrl + "/customers/{0}/sources";
Expand Down
42 changes: 4 additions & 38 deletions src/Stripe.net/Services/Balance/BalanceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

public class BalanceService : StripeService
{
private static string classUrl = Urls.BaseUrl + "/balance";

public BalanceService()
: base(null)
{
Expand All @@ -18,52 +20,16 @@ public BalanceService(string apiKey)
{
}

public bool ExpandSource { get; set; }

public virtual Balance Get(RequestOptions requestOptions = null)
{
return Mapper<Balance>.MapFromJson(
Requestor.GetString(Urls.Balance, this.SetupRequestOptions(requestOptions)));
}

public virtual BalanceTransaction Get(string id, RequestOptions requestOptions = null)
{
return Mapper<BalanceTransaction>.MapFromJson(
Requestor.GetString(
this.ApplyAllParameters(null, $"{Urls.BalanceTransactions}/{id}", false),
this.SetupRequestOptions(requestOptions)));
}

public virtual StripeList<BalanceTransaction> List(BalanceTransactionListOptions listOptions = null, RequestOptions requestOptions = null)
{
return Mapper<StripeList<BalanceTransaction>>.MapFromJson(
Requestor.GetString(
this.ApplyAllParameters(listOptions, Urls.BalanceTransactions, true),
this.SetupRequestOptions(requestOptions)));
Requestor.GetString(classUrl, this.SetupRequestOptions(requestOptions)));
}

public virtual async Task<Balance> GetAsync(RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return Mapper<Balance>.MapFromJson(
await Requestor.GetStringAsync(Urls.Balance, this.SetupRequestOptions(requestOptions), cancellationToken).ConfigureAwait(false));
}

public virtual async Task<BalanceTransaction> GetAsync(string id, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return Mapper<BalanceTransaction>.MapFromJson(
await Requestor.GetStringAsync(
this.ApplyAllParameters(null, $"{Urls.BalanceTransactions}/{id}", false),
this.SetupRequestOptions(requestOptions),
cancellationToken).ConfigureAwait(false));
}

public virtual async Task<StripeList<BalanceTransaction>> ListAsync(BalanceTransactionListOptions options = null, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return Mapper<StripeList<BalanceTransaction>>.MapFromJson(
await Requestor.GetStringAsync(
this.ApplyAllParameters(options, Urls.BalanceTransactions, true),
this.SetupRequestOptions(requestOptions),
cancellationToken).ConfigureAwait(false));
await Requestor.GetStringAsync(classUrl, this.SetupRequestOptions(requestOptions), cancellationToken).ConfigureAwait(false));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace Stripe
{
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Stripe.Infrastructure;

public class BalanceTransactionService : BasicService<BalanceTransaction>
{
private static string classUrl = Urls.BaseUrl + "/balance/history";

public BalanceTransactionService()
: base(null)
{
}

public BalanceTransactionService(string apiKey)
: base(apiKey)
{
}

public bool ExpandSource { get; set; }

public virtual BalanceTransaction Get(string balanceTransactionId, RequestOptions requestOptions = null)
{
return this.GetEntity($"{classUrl}/{balanceTransactionId}", requestOptions);
}

public virtual Task<BalanceTransaction> GetAsync(string balanceTransactionId, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return this.GetEntityAsync($"{classUrl}/{balanceTransactionId}", requestOptions, cancellationToken);
}

public virtual StripeList<BalanceTransaction> List(BalanceTransactionListOptions listOptions = null, RequestOptions requestOptions = null)
{
return this.GetEntityList(classUrl, requestOptions, listOptions);
}

public virtual Task<StripeList<BalanceTransaction>> ListAsync(BalanceTransactionListOptions listOptions = null, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return this.GetEntityListAsync(classUrl, requestOptions, cancellationToken, listOptions);
}
}
}
24 changes: 24 additions & 0 deletions src/Stripe.net/Services/FileUploads/FileUploadCreateOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Stripe
{
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;

public class FileUploadCreateOptions : BaseOptions
{
/// <summary>
/// REQUIRED. A file to upload. The file should follow the specifications of RFC 2388
/// (which defines file transfers for the <c>multipart/form-data</c> protocol).
/// </summary>
[JsonProperty("file")]
public Stream File { get; set; }

/// <summary>
/// REQUIRED. The purpose of the uploaded file. Possible values are <c>business_logo</c>,
/// <c>customer_signature</c>, <c>dispute_evidence</c>, <c>identity_document</c>,
/// <c>pci_document</c>, or <c>tax_document_user_upload</c>
/// </summary>
[JsonProperty("purpose")]
public string Purpose { get; set; }
}
}
39 changes: 24 additions & 15 deletions src/Stripe.net/Services/FileUploads/FileUploadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,35 @@ public FileUploadService(string apiKey)
{
}

public virtual FileUpload Create(string fileName, Stream fileStream, string purpose, RequestOptions requestOptions = null)
public virtual FileUpload Create(FileUploadCreateOptions options, RequestOptions requestOptions = null)
{
return Mapper<FileUpload>.MapFromJson(
Requestor.PostFile(Urls.FileUploads, fileName, fileStream, purpose, this.SetupRequestOptions(requestOptions)));
Requestor.PostFile(
Urls.FileUploads,
options.File,
options.Purpose,
this.SetupRequestOptions(requestOptions)));
}

public virtual FileUpload Get(string fileUploadId, RequestOptions requestOptions = null)
public virtual async Task<FileUpload> CreateAsync(FileUploadCreateOptions options, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return Mapper<FileUpload>.MapFromJson(
Requestor.GetString(
this.ApplyAllParameters(null, $"{Urls.FileUploads}/{fileUploadId}"),
this.SetupRequestOptions(requestOptions)));
await Requestor.PostFileAsync(
Urls.FileUploads,
options.File,
options.Purpose,
this.SetupRequestOptions(requestOptions),
cancellationToken).ConfigureAwait(false));
}

public virtual StripeList<FileUpload> List(FileUploadListOptions listOptions = null, RequestOptions requestOptions = null)
public virtual FileUpload Get(string fileUploadId, RequestOptions requestOptions = null)
{
return Mapper<StripeList<FileUpload>>.MapFromJson(
return Mapper<FileUpload>.MapFromJson(
Requestor.GetString(
this.ApplyAllParameters(listOptions, Urls.FileUploads, true),
this.ApplyAllParameters(null, $"{Urls.FileUploads}/{fileUploadId}"),
this.SetupRequestOptions(requestOptions)));
}

public virtual async Task<FileUpload> CreateAsync(string fileName, Stream fileStream, string purpose, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return Mapper<FileUpload>.MapFromJson(
await Requestor.PostFileAsync(Urls.FileUploads, fileName, fileStream, purpose, this.SetupRequestOptions(requestOptions), cancellationToken).ConfigureAwait(false));
}

public virtual async Task<FileUpload> GetAsync(string fileUploadId, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return Mapper<FileUpload>.MapFromJson(
Expand All @@ -55,6 +56,14 @@ await Requestor.GetStringAsync(
cancellationToken).ConfigureAwait(false));
}

public virtual StripeList<FileUpload> List(FileUploadListOptions listOptions = null, RequestOptions requestOptions = null)
{
return Mapper<StripeList<FileUpload>>.MapFromJson(
Requestor.GetString(
this.ApplyAllParameters(listOptions, Urls.FileUploads, true),
this.SetupRequestOptions(requestOptions)));
}

public virtual async Task<StripeList<FileUpload>> ListAsync(FileUploadListOptions listOptions = null, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return Mapper<StripeList<FileUpload>>.MapFromJson(
Expand Down
37 changes: 34 additions & 3 deletions src/Stripe.net/Services/Invoices/InvoiceCreateOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,33 @@

public class InvoiceCreateOptions : BaseOptions, ISupportMetadata
{
/// <summary>
/// A fee in cents that will be applied to the invoice and transferred to the application
/// owner’s Stripe account. The request must be made with an OAuth key or the Stripe-Account
/// header in order to take an application fee. For more information, see the application
/// fees <see href="https://stripe.com/docs/connect/subscriptions#working-with-invoices">documentation</see>.
/// </summary>
[JsonProperty("application_fee")]
public int? ApplicationFee { get; set; }

/// <summary>
/// One of <see cref="Billing" />. When charging automatically, Stripe will attempt to pay this invoice using the default source attached to the customer. When sending an invoice, Stripe will email this invoice to the customer with payment instructions. Defaults to charge_automatically.
/// One of <see cref="Billing" />. When charging automatically, Stripe will attempt to pay
/// this invoice using the default source attached to the customer. When sending an invoice,
/// Stripe will email this invoice to the customer with payment instructions. Defaults to
/// <c>charge_automatically</c>.
/// </summary>
[JsonProperty("billing")]
public Billing? Billing { get; set; }

/// <summary>
/// The number of days from which the invoice is created until it is due. Only valid for invoices where billing=send_invoice.
/// REQUIRED
/// </summary>
[JsonProperty("customer")]
public string CustomerId { get; set; }

/// <summary>
/// The number of days from which the invoice is created until it is due. Only valid for
/// invoices where <c>billing=send_invoice</c>.
/// </summary>
[JsonProperty("days_until_due")]
public int? DaysUntilDue { get; set; }
Expand All @@ -26,20 +42,35 @@ public class InvoiceCreateOptions : BaseOptions, ISupportMetadata
public string Description { get; set; }

/// <summary>
/// The date on which payment for this invoice is due. Only valid for invoices where billing=send_invoice.
/// The date on which payment for this invoice is due. Only valid for invoices where
/// <c>billing=send_invoice</c>.
/// </summary>
[JsonProperty("due_date")]
public DateTime? DueDate { get; set; }

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

/// <summary>
/// Extra information about a charge for the customer’s credit card statement. It must
/// contain at least one letter. If not specified and this invoice is part of a
/// subscription, the default <c>statement_descriptor</c> will be set to the first
/// subscription item’s product’s <c>statement_descriptor</c>.
/// </summary>
[JsonProperty("statement_descriptor")]
public string StatementDescriptor { get; set; }

/// <summary>
/// The ID of the subscription to invoice. If not set, the created invoice will include all
/// pending invoice items for the customer. If set, the created invoice will exclude pending
/// invoice items that pertain to other subscriptions.
/// </summary>
[JsonProperty("subscription")]
public string SubscriptionId { get; set; }

/// <summary>
/// The percent tax rate applied to the invoice, represented as a decimal number.
/// </summary>
[JsonProperty("tax_percent")]
public decimal? TaxPercent { get; set; }
}
Expand Down
Loading

0 comments on commit fb68732

Please sign in to comment.