Implement Stripe Payment Gateway In ASP.NET MVC

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

There are several steps to integrate Stripe.

Basically, there are two ways to implement it.

  1. Client Side: You can use Stripe.js and load the Stripe controls to enter Credit card details and make the payment.
  2. Server Side: If you already have a full-fledged Checkout page and everything is in place, then you can use the library (DLL) to make the payment.

Today, we are going to see the 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 your Email, Name, and Password.

Step 2. Install the 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 make 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 the API key with the below function.

Stripe.StripeConfiguration.SetApiKey(“pk_test_FyPZYPyqf8jU6IdG2DONgudS”);  

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

//Create Card Object to create Token  
Stripe.CreditCardOptions card = new Stripe.CreditCardOptions();  
card.Name = tParams.CardOwnerFirstName + " " + tParams.CardOwnerLastName;  
card.Number = tParams.CardNumber;  
card.ExpYear = tParams.ExpirationYear;  
card.ExpMonth = tParams.ExpirationMonth;  
card.Cvc = tParams.CVV2;  
//Assign Card to Token Object and create Token  
Stripe.TokenCreateOptions token = new Stripe.TokenCreateOptions();  
token.Card = card;  
Stripe.TokenService serviceToken = new Stripe.TokenService();  
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.

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

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

//Create Charge Object with details of Charge  
var options = new Stripe.ChargeCreateOptions {  
    Amount = Convert.ToInt32(tParams.Amount),  
        Currency = tParams.CurrencyId == 1 ? "ILS" : "USD",  
        ReceiptEmail = tParams.Buyer_Email,  
        CustomerId = stripeCustomer.Id,  
        Description = Convert.ToString(tParams.TransactionId), //Optional  
};  
//and Create Method of this object is doing the payment execution.  
var service = new Stripe.ChargeService();  
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 --

Blue Ocktopus Technology Systems Private Limited
Enabling revenue generation through brand loyalty and customer retention for enterprise retail and e