Integrating Paystack with .Net E-Commerce Applications


E-commerce applications require user-friendly mechanisms for payment. Payment processing on the Web is a big industry and there are myriad services and providers allowing you to process credit cards and bank cards over Internet connections.

Introduction

If you are developing an e-commerce application or you already have one running on .net platform and you require some payment gateway integration, then this article will guide you on how you can integrate Paystack payment gateway.

If you are getting to know or hear about Paystack for the first time, then I advise that you visit their website here for full introduction.

How Paystack works

Paystack, like every other payment gateway works the same way in an e-commerce applications.
The process goes like this:
  1. User picks items in the web site
  2. User fills our order info
  3. User accepts the order and final amount
  4. User gets redirected to a Paystack secure payment page
  5. On success Paystack redirects back to site's callback url

A walk through of the process

Before you can start integrating Paystack, you will need a Paystack account. Click here to create a free account.

I recently had a requirement from a client to integrate Paystack with their ASP.Net MVC portal I built for them, as you would expect what came to my mind was that I can just get an API that will speed up the integration process - but guess what.. I got a lot of plugins from PHP, NodeJS etc but got only one for .Net.



The client's portal was built on .Net framework 4.6, so the sole .Net nuget library did not support the framework.

After the integration, I decided to convert the code to a nuget package that support the 4.6 framework.

Below is the screen shot of the sample app on the usage of the plugin on my github page:

Screen shot of the sample mini e-commerce app on my github

How to use the Paystack.Net.SDK plugin

Fire up your Visual Studio, and create a new ASP.Net MVC web application. Right click on the references node and choose Manage Nuget Packages.




Search for Paystack.Net.SDK or alternatively, click here to view the plugin for all optional versions. You can also install the plugin directly from the package manager console by running this command.

Install-Package Paystack.Net.SDK -Version 1.0.2

The plugin which I will continue supporting and adding more feature allows you to perform almost all the actions provided by Paystack like:
  • Transactions
  • Customers
  • Subaccounts
  • Plans
  • Subscriptions
  • Transfers
The plugin is structured with every feature with their own module. 



Transactions Initialization

You can easily perform transaction initialization with the plugin like so:

Using Statements

using Paystack.Net.SDK.Transactions;
using PaystackSampleWebApp.Models.DataModel;
using PaystackSampleWebApp.Models.Interfaces;
using PaystackSampleWebApp.Models.ViewModels;

  public async Task InitializePayment(PaystackCustomerModel model)
        {
            string secretKey = ConfigurationManager.AppSettings["PaystackSecret"];
            var paystackTransactionAPI = new PaystackTransaction(secretKey);
            var response = await paystackTransactionAPI.InitializeTransaction(model.email, model.amount, model.firstName, model.lastName, "http://localhost:17869/order/callback");
            //Note that callback url is optional
            if (response.status == true)
            {
                return Json(new {error=false,result= response }, JsonRequestBehavior.AllowGet);
            }
            return Json(new { error = true, result = response }, JsonRequestBehavior.AllowGet);

        }

I just instantiated the PaystackTransaction and the pass the secret key that can be found in https://dashboard.paystack.com/#/settings/developer


Transaction Verification

Verification can also be done simple like so:

   [Route("order/callback")]
        public async Task Index()
        {
            string secretKey = ConfigurationManager.AppSettings["PaystackSecret"];
            var paystackTransactionAPI = new PaystackTransaction(secretKey);
            var tranxRef = HttpContext.Request.QueryString["reference"];
            if (tranxRef != null)
            {
                var response = await paystackTransactionAPI.VerifyTransaction(tranxRef);
                if (response.status)
                {
                    return View(response);
                }
            }

            return View("PaymentError");
        }


Samples

I’ve spent a fair amount of time talking about how to use my plugin for easy integration in this article, because I think it’s important to keep things in the perspective of a real application environment. I do hope that the code shown was not too specific and easily understandable in the context of the Paystack integration.
I’ve provided a really simple sample project that demonstrates the basic concepts I’ve described here along with the code to the helper classes mentioned, which you can run and play around with. Just remember to use small order amounts.

You can download the sample app from here.

If you have any questions or bug report please let me know.

Happy New Year in Advance and happy coding.

Source Code

1 comment:

Powered by Blogger.