Skip to content

Commit

Permalink
Merge pull request #8 from robconery/master
Browse files Browse the repository at this point in the history
Added CreateChargeWithToken method so a card number does not need to be passed in
  • Loading branch information
nberardi committed Nov 7, 2012
2 parents af9c48f + 8e55a09 commit 59e6130
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
27 changes: 26 additions & 1 deletion src/StripeClient.Charges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,32 @@ namespace Stripe
{
public partial class StripeClient
{
public StripeObject CreateCharge(decimal amount, string currency, string customerId, string description = null)
/// <summary>
/// Creates a charge using a token retrieved via the browser
/// </summary>
/// <returns></returns>
public StripeObject CreateChargeWithToken(decimal amount, string token, string currency="usd", string description = null)
{
Require.Argument("amount", amount);
Require.Argument("currency", currency);
Require.Argument("token", token);

if (amount < 0.5M)
throw new ArgumentOutOfRangeException("amount", amount, "Amount must be at least 50 cents");

var request = new RestRequest();
request.Method = Method.POST;
request.Resource = "charges";

request.AddParameter("amount", Convert.ToInt32(amount * 100M));
request.AddParameter("currency", currency);
request.AddParameter("card", token);
if (description.HasValue()) request.AddParameter("description", description);

return ExecuteObject(request);
}

public StripeObject CreateCharge(decimal amount, string currency, string customerId, string description = null)
{
Require.Argument("amount", amount);
Require.Argument("currency", currency);
Expand Down
12 changes: 10 additions & 2 deletions test/Stripe.Tests/ChargeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,23 @@ public class ChargeTests
public ChargeTests()
{
_card = new CreditCard {
Number = "4111111111111111",
Number = "4242 4242 4242 4242",
ExpMonth = 3,
ExpYear = 2015
};

_client = new StripeClient(Constants.ApiKey);
_customer = _client.CreateCustomer(_card);
}

[Fact]
public void CreateCharge_Using_Token()
{
dynamic token = _client.CreateCardToken(_card);
dynamic response = _client.CreateChargeWithToken(100M,token.Id);
Assert.NotNull(response);
Assert.False(response.IsError);
Assert.True(response.Paid);
}
[Fact]
public void CreateCharge_Card_Test()
{
Expand Down

0 comments on commit 59e6130

Please sign in to comment.