How to Integrate Instamojo Payment Gateway in Website

Integrating Payment Gateway in website is a typical stuff and may cause some serious issue if not done in secure manner. When i was doing this , I have no idea and also there is no one to help me out. So, I decided that this thing will not happen to you.

SOURCE CODE

CHECKOUT PAGE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InstamojoAPI;
using System.IO;
using System.Net;

namespace InstamojoIntegration
{
	public partial class InstamojoCheckout : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{

		}

		protected void btnCheckout_Click(object sender, EventArgs e)
		{
			string Insta_client_id = "ClientID";
			string Insta_client_secret = "SecretKey";
			string Insta_Endpoint = InstamojoConstants.INSTAMOJO_API_ENDPOINT;
			string Insta_Auth_Endpoint = InstamojoConstants.INSTAMOJO_AUTH_ENDPOINT;
			
			Instamojo objClass = InstamojoImplementation.getApi(Insta_client_id, Insta_client_secret, Insta_Endpoint, Insta_Auth_Endpoint);
			CreatePaymentOrder(objClass);			
		}

		private void CreatePaymentOrder(Instamojo objClass)
		{
			PaymentOrder objPaymentRequest = new PaymentOrder();
			//Required POST parameters
			objPaymentRequest.name = txtName.Text;
			objPaymentRequest.email = txtEmail.Text;
			objPaymentRequest.phone = txtMobileNumber.Text;
			objPaymentRequest.description = "Test description";
			objPaymentRequest.amount = Convert.ToInt32(txtAmount.Text);
			objPaymentRequest.currency = "INR";

			string randomName = Path.GetRandomFileName();
			randomName = randomName.Replace(".", string.Empty);
			objPaymentRequest.transaction_id = "test" + randomName;

			Session["transID"] = objPaymentRequest.transaction_id;

			objPaymentRequest.redirect_url = "http://localhost:8007/CallBackInstamojo.aspx";
			//Extra POST parameters 

			if (objPaymentRequest.validate())
			{
				if (objPaymentRequest.emailInvalid)
				{
					Response.Write("Email is not valid");
				}
				if (objPaymentRequest.nameInvalid)
				{
					Response.Write("Name is not valid");
				}
				if (objPaymentRequest.phoneInvalid)
				{
					Response.Write("Phone is not valid");
				}
				if (objPaymentRequest.amountInvalid)
				{
					Response.Write("Amount is not valid");
				}
				if (objPaymentRequest.currencyInvalid)
				{
					Response.Write("Currency is not valid");
				}
				if (objPaymentRequest.transactionIdInvalid)
				{
					Response.Write("Transaction Id is not valid");
				}
				if (objPaymentRequest.redirectUrlInvalid)
				{
					Response.Write("Redirect Url Id is not valid");
				}
				if (objPaymentRequest.webhookUrlInvalid)
				{
					Response.Write("Webhook URL is not valid");
				}

			}
			else
			{
				try
				{
					CreatePaymentOrderResponse objPaymentResponse = objClass.createNewPaymentRequest(objPaymentRequest);
					Response.Redirect(objPaymentResponse.payment_options.payment_url);
				}
				catch (ArgumentNullException ex)
				{
					Response.Write(ex.Message);
				}
				catch (WebException ex)
				{
					Response.Write(ex.Message);
				}
				catch (IOException ex)
				{
					Response.Write(ex.Message);
				}
				catch (InvalidPaymentOrderException ex)
				{
					if (!ex.IsWebhookValid())
					{
						Response.Write("Webhook is invalid");
					}

					if (!ex.IsCurrencyValid())
					{
						Response.Write("Currency is Invalid");
					}

					if (!ex.IsTransactionIDValid())
					{
						Response.Write("Transaction ID is Inavlid");
					}
				}
				catch (ConnectionException ex)
				{
					Response.Write(ex.Message);
				}
				catch (BaseException ex)
				{
					Response.Write(ex.Message);
				}
				catch (Exception ex)
				{
					Response.Write("Error:" + ex.Message);
				}
			}
		}
	}
}

RETURN URL PAGE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InstamojoAPI;
using System.IO;
using System.Net;
namespace InstamojoIntegration
{
	public partial class CallBackInstamojo : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			string Insta_client_id = "ClientID";
			string Insta_client_secret = "SecretKey";
			string Insta_Endpoint = InstamojoConstants.INSTAMOJO_API_ENDPOINT;
			string Insta_Auth_Endpoint = InstamojoConstants.INSTAMOJO_AUTH_ENDPOINT;
			
			Instamojo objClass = InstamojoImplementation.getApi(Insta_client_id, Insta_client_secret, Insta_Endpoint, Insta_Auth_Endpoint);
			DetailsofPayment(objClass);
		}

		private void DetailsofPayment(Instamojo objClass)
		{
			PaymentOrderDetailsResponse paymentOrderDetailsResponse = objClass.getPaymentOrderDetailsByTransactionId(Session["transID"].ToString());
			TxnId.InnerText = paymentOrderDetailsResponse.transaction_id;
			h1Message.InnerText = paymentOrderDetailsResponse.status;
			date.InnerText = paymentOrderDetailsResponse.created_at;
		}
	}
}

SOURCE VIDEO

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top