Credit Card Payment In ASP.NET using Stripe

Here are the steps required to implement credit card payment processing in an ASP.NET application using Stripe.

Stripe is one of the most popular gateways to implement credit card payment processing in web applications. Stripe provides an API that is used in a Web application. In this blog, we will see how to use the Stripe API to integrate credit card payment processing in an ASP.NET Web app using C#.
 
Here are the steps. Keep in mind, I'm using a test account. 
 
Step 1: Go to https://stripe.com/ and create an account and get API Keys.
 
Step 2: Download attached project and get Sripe.dll and Newtonsoft.Json.dll from the Bin Foder and add referances to your project.
 
Step 3: Create a simple ASP.NET Website and create a simple page with a button.  
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head id="Head1" runat="server">  
  5. <title></title>  
  6. </head>  
  7. <body>  
  8. <form id="form1" runat="server">  
  9. <div>  
  10. <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />  
  11. </div>  
  12. </form>  
  13. </body>  
  14. </html>  
On the button click event handler, we will execute Stripe API code. 
 
Step 4. Add code to .cs file
 
Here is the code-behind looks like: 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using Stripe;  
  8. public partial class _Default : System.Web.UI.Page  
  9. {  
  10. protected void Page_Load(object sender, EventArgs e)  
  11. {  
  12. }  
  13. protected void Button1_Click(object sender, EventArgs e)  
  14. {  
  15. StripeCustomer current = GetCustomer();  
  16. // int? days = getaTraildays();  
  17. //if (days != null)  
  18. //{  
  19. int chargetotal = 100;//Convert.ToInt32((3.33*Convert.ToInt32(days)*100));  
  20. var mycharge = new StripeChargeCreateOptions();  
  21. mycharge.AmountInCents = chargetotal;  
  22. mycharge.Currency = "USD";  
  23. mycharge.CustomerId = current.Id;  
  24. string key = "sk_test_Uvk2cHkpYRTC2Rl4ZUfs4Fvs";  
  25. var chargeservice = new StripeChargeService(key);  
  26. StripeCharge currentcharge = chargeservice.Create(mycharge);  
  27. //}  
  28. }  
  29. private StripeCustomer GetCustomer()  
  30. {  
  31. var mycust = new StripeCustomerCreateOptions();  
  32. mycust.Email = "pankaj@hotmail.com";  
  33. mycust.Description = "Rahul Pandey(rahul@gmail.com)";  
  34. mycust.CardNumber = "4242424242424242";  
  35. mycust.CardExpirationMonth = "10";  
  36. mycust.CardExpirationYear = "2014";  
  37. // mycust.PlanId = "100";  
  38. mycust.CardName = "Rahul Pandey";  
  39. mycust.CardAddressCity = "ABC";  
  40. mycust.CardAddressCountry = "USA";  
  41. mycust.CardAddressLine1 = "asbcd";  
  42. //mycust.TrialEnd = getrialend();  
  43. var customerservice = new StripeCustomerService("sk_test_Uvk2cHkpYRTC2Rl4ZUfs4Fvs");  
  44. return customerservice.Create(mycust);  
  45. }  
  46. }  
In the above code, we create Customer and provide customer details including credit card details. StripeCharge is the object responsible for charging.
 
Step 5. Build and run and you should be able to execute a test payment transaction. 
 
For more please download the code.