Skip to content
Browse files

Merge pull request #8 from robconery/master

Added CreateChargeWithToken method so a card number does not need to be passed in
  • Loading branch information...
2 parents af9c48f + 8e55a09 commit 59e6130396a3eab1ed60c1ad72395cc5b7feee6d @nberardi committed
Showing with 36 additions and 3 deletions.
  1. +26 −1 src/StripeClient.Charges.cs
  2. +10 −2 test/Stripe.Tests/ChargeTests.cs
View
27 src/StripeClient.Charges.cs
@@ -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);
View
12 test/Stripe.Tests/ChargeTests.cs
@@ -14,7 +14,7 @@ public class ChargeTests
public ChargeTests()
{
_card = new CreditCard {
- Number = "4111111111111111",
+ Number = "4242 4242 4242 4242",
ExpMonth = 3,
ExpYear = 2015
};
@@ -22,7 +22,15 @@ public ChargeTests()
_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()
{

0 comments on commit 59e6130

Please sign in to comment.
Something went wrong with that request. Please try again.