Implement Stripe Payment Gateway In ASP.NET MVC

Server Side Implementation of Stripe Payment Gateway in ASP.NET MVC. There are many Payment Gateways in market which gives secure and easy implementation. And Stripe is one of them.

There are many Payment Gateways in the market which offer secure and easy implementation. And Stripe is one of them.

There are several steps to integrate Stripe.

Basically there are 2 ways to implement it.

  1. Client Side
    You can use Stripe.js and load the Stripe controls to enter Credit card details and do the payment

  2. Server Side
    If you already have a full fledged Checkout page and everything is in place then you can use library (DLL) to do the payment.

Today we are going to see Server Side implementation of Stripe.

If you follow each and every step you can easily integrate Stripe Payment Gateway.

Step 1

Register on https://dashboard.stripe.com/register with Email, Name and Password.

Step 2

Install Stripe library in your Visual Project using Install-Package Stripe.net. It will download Stripe.net.dll and add a reference to your project.

Step 3

Add Namespace (using Stripe.Infrastructure;) to the Class where you want to implement the payment gateway.

Step 4

To implement you need to play with several classes. Please follow each step to do the payment.
 
Step 5
 
To connect with Stripe you need KEY - “Publishable key”. You can get it from https://dashboard.stripe.com/account/apikeys.
 
Implement Stripe Payment Gateway in ASP.NET MVC

Step 6

Set API key with below function,
  1. Stripe.StripeConfiguration.SetApiKey(“pk_test_FyPZYPyqf8jU6IdG2DONgudS”);  

Step 7

Create Object of Credit Card to generate Token. That Token will be set to Customer object at the time of Creation of Customer.

  1. //Create Card Object to create Token  
  2. Stripe.CreditCardOptions card = new Stripe.CreditCardOptions();  
  3. card.Name = tParams.CardOwnerFirstName + " " + tParams.CardOwnerLastName;  
  4. card.Number = tParams.CardNumber;  
  5. card.ExpYear = tParams.ExpirationYear;  
  6. card.ExpMonth = tParams.ExpirationMonth;  
  7. card.Cvc = tParams.CVV2;  
  8. //Assign Card to Token Object and create Token  
  9. Stripe.TokenCreateOptions token = new Stripe.TokenCreateOptions();  
  10. token.Card = card;  
  11. Stripe.TokenService serviceToken = new Stripe.TokenService();  
  12. Stripe.Token newToken = serviceToken.Create(token);  

Step 8

Assign TokenID to the Customer Object so the at the time of customer creation, a card gets created and linked to the Customer,

  1. //Create Customer Object and Register it on Stripe  
  2. Stripe.CustomerCreateOptions myCustomer = new Stripe.CustomerCreateOptions();  
  3. myCustomer.Email = tParams.Buyer_Email;  
  4. myCustomer.SourceToken = newToken.Id;  
  5. var customerService = new Stripe.CustomerService();  
  6. Stripe.Customer stripeCustomer = customerService.Create(myCustomer);  

Step 9

Create Charge Object. Charge object is the actual object which will do the payment.

  1. //Create Charge Object with details of Charge  
  2. var options = new Stripe.ChargeCreateOptions {  
  3.     Amount = Convert.ToInt32(tParams.Amount),  
  4.         Currency = tParams.CurrencyId == 1 ? "ILS" : "USD",  
  5.         ReceiptEmail = tParams.Buyer_Email,  
  6.         CustomerId = stripeCustomer.Id,  
  7.         Description = Convert.ToString(tParams.TransactionId), //Optional  
  8. };  
  9. //and Create Method of this object is doing the payment execution.  
  10. var service = new Stripe.ChargeService();  
  11. Stripe.Charge charge = service.Create(options); // This will do the Payment  

Step 10

Charge.Status will return the status.
 
Step 11
 
Now you can check Created Customer from https://dashboard.stripe.com/test/customers
 
Implement Stripe Payment Gateway in ASP.NET MVC
 
And Payment using this link - https://dashboard.stripe.com/test/payments
 
Implement Stripe Payment Gateway in ASP.NET MVC
 
-- Happy Coding --