Codementor Events

ASP.NET Integration With Stripe

Published Aug 08, 2017Last updated Aug 09, 2017
ASP.NET Integration With Stripe

Stripe is one of the most popular Online Payment Processing System after Paypal.

While Paypal has been an early bird in the world of Online Payment gateways, Stripe has turned out to be a really strong competitor in the market.
The main reasons being:

1) Better Service Charge Rates
Source: Memberful

## Service PayPal Stripe
Transaction fees [1] 2.9% + 30¢ 2.9% + 30¢
Charge cards from your website $30 / month [2] Free
Chargeback $20 $15
American Express 3.5% [3] Same flat rate
Micropayments (less than $10) 5% + .05¢ Same flat rate
Refund Fixed fee [4] Free
International cards 1% [5] Free [6]
Authorize card 30¢ [7] Free
Recurring Billing $10 / month Free
Advanced Fraud Protection $10 / month [8] Free [9]
Accept Apple Pay © Not available Free

2) Rapid Integration and Startup with Payment Card Industry (PCI) compliant Payment system
You do not have to store your customer's card related information on the server side!

Getting Started

In this post I will introduce the Stripe .NET library and show how to use it with the ASP.NET MVC framework.

To get started, first sign up with Stripe which is a very simple process where all you have to provide is a valid email address and a password for your account.

Once you sign up and confirm your email address, Stripe provides you with a set of Test API keys. Basically you get a "Publishable API" key and a "Secret API" key.

Publishable API keys are meant solely to identify your account with Stripe, they aren't secret. In other words, they can safely be published in places like your Stripe.js JavaScript code, or in an Android or iPhone app. Publishable keys only have the power to create tokens.

Secret API keys should be kept confidential and only stored on your own servers. Your account's secret API key can perform any API request to Stripe without restriction. - Stripe

Stripe Integration with ASP.NET MVC

After you have your keys ready, in your ASP.NET project install Nugget package "Stripe.NET".
stripe.PNG
Next, in your MVC controller, add a method to create a view like below:

public ActionResult Index()
{
var stripePublishKey = ConfigurationManager.AppSettings["stripePublishableKey"];
ViewBag.StripePublishKey = stripePublishKey;
return View();
}

Here, basically I am getting the Publishable API key provided by Stripe and passing it to my View.
Next, add the corresponding view for this method:

<form action="/Payment/Charge" method="POST">
<article>
<label>Amount: $5.00</label>
</article>
<script src="//checkout.stripe.com/v2/checkout.js"
class="stripe-button"
data-key="@ViewBag.StripePublishKey"
data-locale="auto"
data-description="Sample Charge"
data-amount="500">
</script>
</form>

This creates a User Interface view with a label and a Stripe Checkout button for our designated App key.

stripe_btn.PNG

The button generation and the click event is handled by the Stripe's "checkout.js" script. User can click to display the credit card overlay which is automatically validated by Stripe's API error handling.

stripe_2.PNG

If the card is validated, it returns back a token to our application that we should handle on the server side.

public ActionResult Charge(string stripeEmail, string stripeToken)
        {
            var customers = new StripeCustomerService();
            var charges = new StripeChargeService();

            var customer = customers.Create(new StripeCustomerCreateOptions
            {
                Email = stripeEmail,
                SourceToken = stripeToken
            });

            var charge = charges.Create(new StripeChargeCreateOptions
            {
                Amount = 500,//charge in cents
                Description = "Sample Charge",
                Currency = "usd",
                CustomerId = customer.Id
            });
            
            // further application specific code goes here

            return View();
        }

The Charge method handles the incoming POST request that Checkout performs on the frontend. First a Stripe customer is created and then the charges.Create method is invoked, providing the Customer ID to associate the transaction with the customer. Payment amount is specified in cents.

We should write application specific codes to make database adjustment related to the purchase made by our User after the Sripe charge is made.
Finally, we need to configure the Stripe API key which can be done adding the following ling of code in the "Global.asax" file.

StripeConfiguration.SetApiKey(ConfigurationManager.AppSettings["stripeSecretKey"]);

That's all the code we need to get started with Stripe Checkout!

Testing
For testing the API, Stripe has provided a list of sample Credit Cards that you can use for development.

Conclusion

In this post, we saw how easy it is to integrate Stripe Checkout in our application. We were able to create a payment processing system without even having to actually handle user's secure PCI information.

I hope you found this article useful and enjoyed reading it. If you would like to read more article written by me, you can visit my Blog where I frequently write.

Thanks!

Discover and read more posts from Sovit Poudel
get started
post commentsBe the first to share your opinion
Techtolia
6 months ago

If you are looking for a guide that walks you through how to get one-time payments and sell fixed-price subscriptions and provides an example of a well-working integration with Stripe, look at the demo --> https://techtolia.com/Stripe/New/

Abdul Rehman
4 years ago

How do I withdraw money from my stripe account? in asp mvc Application
Can you help?

Ari Sioz
5 years ago

For the record :

They’ve removed the ‘Stripe’ prefix. Meaning that ‘StripeCustomerService’ would be ‘CustomerService’ instead.

https://github.com/stripe/stripe-dotnet/wiki/Migration-guide-for-v20

Show more replies