Credit Card Payment In ASP.NET 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/ to create an account and get API keys.

Step 2. Download the attached project, get Sripe.dll and Newtonsoft.Json.dll from the Bin Foder, and add references to your project.

Step 3. Create a simple ASP.NET Website and create a simple page with a button.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </div>
    </form>
</body>
</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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Stripe;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Page Load logic goes here
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        StripeCustomer current = GetCustomer();

        // Uncomment the following lines if you have a method getaTraildays()
        // int? days = getaTraildays();
        // if (days != null)
        // {
        int chargetotal = 100; // Convert.ToInt32((3.33 * Convert.ToInt32(days) * 100));

        var mycharge = new StripeChargeCreateOptions();
        mycharge.AmountInCents = chargetotal;
        mycharge.Currency = "USD";
        mycharge.CustomerId = current.Id;

        string key = "sk_test_Uvk2cHkpYRTC2Rl4ZUfs4Fvs";
        var chargeservice = new StripeChargeService(key);
        StripeCharge currentcharge = chargeservice.Create(mycharge);

        // }
    }

    private StripeCustomer GetCustomer()
    {
        var mycust = new StripeCustomerCreateOptions();
        mycust.Email = "[email protected]";
        mycust.Description = "Rahul Pandey([email protected])";
        mycust.CardNumber = "4242424242424242";
        mycust.CardExpirationMonth = "10";
        mycust.CardExpirationYear = "2014";
        // mycust.PlanId = "100";
        mycust.CardName = "Rahul Pandey";
        mycust.CardAddressCity = "ABC";
        mycust.CardAddressCountry = "USA";
        mycust.CardAddressLine1 = "asbcd";
        // mycust.TrialEnd = getrialend();

        var customerservice = new StripeCustomerService("sk_test_Uvk2cHkpYRTC2Rl4ZUfs4Fvs");
        return customerservice.Create(mycust);
    }
}

In the above code, we create a 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.

Here is a detailed tutorial: Implement Stripe Payment Gateway In ASP.NET MVC