Payment Gateway Integration With ASP.NET Core

A step-by-step guide on how to implement payment gateways in ASP.NET Core apps using popular payment services: Braintree.

Pritomsarkar
C# Programming
4 min readOct 31, 2021

--

Payment Gateway

Overview

A payment gateway is an e-commerce software that authorizes payment for online merchants. it’s consumer-facing interfaces used to collect payments. the online stores, the payment gateways are the checkout portals used to enter the credit card information.

Prerequisites

I expect you to have knowledge of object-oriented programming concepts in C#. I assume you know .NET Core concepts especially the MVC pattern also.

To code along with me, you will have to install the .NET Core 2.2, as well as Visual Studio. You can also use another IDE instead of a visual studio. You can find a link to the Github repository end of this article.

Braintree Implementation

1.Set up Braintree account

2.Set up the development environment

3.Generate client tokens

4.Card payments

1. Set up Braintree account

First of all, in our browser, we have to go to Braintreepayment.com for signup. for that Braintree will create us a sandbox account. so we can test the API and before to decide our push our app to production. after successfully all things, we have to login into sandbox.braintreegatway.com.

After the first login, when you scroll down, you will find 3 keys(MarchantId, PublicKey, PrivateKey). we actually will use these keys on configuring the .net core application.

2. Set up the development environment

So above all the key value what we need. we found these key values from our sandbox.braintreegatway account.

Let’s get started by opening the existing application, first of all, we are going to add the Braintree package. Go to NuGet to install Braintree .NET Client Library which is supported by both .NET Framework, .NET Core.

Now we will be going to create a Braintree service. we have to create an interface file in our created services folder.

Above code, don’t forget to make public this interface. Now we are going to implement members of this interface.

Now we have to create a class file in our created services folder.

Above code, To access our key’s from appsettings.json file for that we used the IConfiguration interface.

We have to configure this new service in our startup.cs file. then we are able to inject this service into our controllers.

3. Generates client tokens and Card payments

A client token contains all the necessary information to set up the client SDKs.So before that, I think it’s better to create some model for storing data.

Now we are generating a token and storing some data in our Controller.

Now Index view:-

Above view code, we have actually used some Braintree reference in the script. also, we have first created a reference to the client token and Nonce property is the secured one-time user reference to payment information.

Now after when we run our application, then we will find this output:-

So if we input the test card number and expiration date, nothing will happen because we did not create Create method yet in our Controller.

Now we will be going to Create method in our controller.

After that, you have to create a view for success. I assumed that you will create it easily.

So now if you run the application and will click Confirm Payment. you will see your success view.

And now if you want to see your transaction, then you have to go to sandbox.braintreegatway.com and log in after login, go to the transaction menu and scroll down. then select This Month and after scrolling a little bit, you will find a search button. click here. after that, you will find like below image.

I actually do many transactions for testing purposes and that’s why above image you have seen 5 transactions. but you will see 1 transaction if you are the first time in this.

Conclusion

This article is covered payment gateway for testing purposes using asp.net core. but you learned a lot of things I believe and obviously, you can implement a Braintree payment gateway using this process in a real-world application. you can download this source code in my Github Repository some days later. hope you guys enjoy this article.

--

--