SSLCOMMERZ payment gateway(BD)

SSLCOMMERZ is the first payment gateway in Bangladesh opening doors for merchants to receive payments on the internet via their online stores. Know more…

Let’s get started: First of all we may create a database and table, copy the table script and execute it using corresponding database.

CREATE TABLE [dbo].[PaymentLog](
	[PaymentLogID] [int] IDENTITY(1,1) NOT NULL,
	[tran_id] [nvarchar](250) NULL,
	[tran_date] [datetime] NULL,
	[status] [nvarchar](50) NULL,
	[val_id] [nvarchar](50) NULL,
	[amount] [decimal](18, 2) NULL,
	[store_amount] [decimal](18, 2) NULL,
	[currency] [nvarchar](50) NULL,
	[bank_tran_id] [nvarchar](50) NULL,
	[card_type] [nvarchar](50) NULL,
	[card_no] [nvarchar](50) NULL,
	[card_issuer] [nvarchar](50) NULL,
	[card_brand] [nvarchar](50) NULL,
	[card_issuer_country] [nvarchar](50) NULL,
	[card_issuer_country_code] [nvarchar](50) NULL,
	[currency_type] [nvarchar](50) NULL,
	[currency_amount] [decimal](18, 2) NULL,
	[currency_rate] [decimal](18, 2) NULL,
	[base_fair] [decimal](18, 2) NULL,
	[value_a] [nvarchar](50) NULL,
	[value_b] [nvarchar](50) NULL,
	[value_c] [nvarchar](50) NULL,
	[risk_title] [nvarchar](50) NULL,
	[risk_level] [int] NULL,
	[APIConnect] [nvarchar](50) NULL,
	[validated_on] [nvarchar](50) NULL,
	[gw_version] [nvarchar](50) NULL,
 CONSTRAINT [PK_PaymentLog] PRIMARY KEY CLUSTERED 
(
	[PaymentLogID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

In this post we have use ASP.Net MVC application to perform operations. create a new MVC application and copy the view to your Home page/billing page.

pay_1

Home View

@{
    ViewBag.Title = "Payments";
}

<div ng-controller="paymentCtrl">
    <h2>{{pageTitle}}</h2>

    <div class="row">
        @Html.ActionLink("Go back", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
      
        <form id="payment_gw" name="payment_gw" method="POST" action="https://securepay.sslcommerz.com/gwprocess/testbox/v3/process.php">
            <input type="hidden" name="total_amount" value="1500" />
            <input type="hidden" name="store_id" value="test_storeID" />
            <input type="hidden" name="tran_id" value="xxxx" />
            <input type="hidden" name="success_url" value="http://localhost:15135/Home" />
            <input type="hidden" name="fail_url" value="http://localhost:15135/Home" />
            <input type="hidden" name="cancel_url" value="http://localhost:15135/Home" />
            <input type="hidden" name="version" value="2.00" />

            <div class="clearfix"></div>
            <div class="col-md-12">
                @ViewBag.Status
                <h5>Amount to Pay: 1500</h5>
                <br />
                <!-- SUBMIT REQUEST !-->
                <button type="submit" class=" btn success"><i class="fa fa-calendar-check-o"></i> Pay Now</button>
            </div>
            <div class="clearfix"></div>
        </form>
    </div>
</div>

After clicking Pay now button it will redirect to sslcommerz page, select a payment method.

 

pay_2

response message will display like below

pay_3

Home Controller

private PaymentGatewayEntities _ctx = null;
// GET: Home
public ActionResult Index()
{
    string Status = string.Empty;
    vmPaymentPostback result = null;
    try
    {
        result = GetResponseData();
        if (result != null)
        {
            SavePayment(result);
            Status = "Payment Done!";
        }
        else
        {
            Status = "Payment Fails!";
        }
    }
    catch (Exception)
    {
        Status = "";
    }
    ViewBag.Status = Status;
    return View();
}

In our controller we need to reference below .dll

  1. using Newtonsoft.Json;
  2. using System.IO;
  3. using System.Net;

Response Data

public vmPaymentPostback GetResponseData()
{
    vmPaymentPostback objrspParam = null;
    string[] keys = Request.Form.AllKeys;
    var key = keys[1]; //1 = val_id
    var valId = Request.Form[keys[1]]; //1 = val_id
    var storeID = "test_storeID"; //Replace with LiveID
    var storePass = "test_storePass"; //Replace with LivePassword
    var validateurl = "https://securepay.sslcommerz.com/validator/api/testbox/validationserverAPI.php?val_id=" + valId + "&Store_Id=" + storeID + "&Store_Passwd=" + storePass + "&v=1&format=json"; //Replace with LiveValidURL
    try
    {
        //request
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(validateurl);
        request.Method = "GET";

        //response
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        objrspParam = JsonConvert.DeserializeObject<vmPaymentPostback>(reader.ReadToEnd().ToString());
    }
    catch (Exception)
    {
    }
    return objrspParam;
}

Save Transnational data to database

after successful transaction we can now save our transnational data to database.

public int SavePayment(vmPaymentPostback _Payment)
{
    int status = 0;
    try
    {
        PaymentLog objPay = new PaymentLog
        {
            tran_id = _Payment.tran_id,
            tran_date = Convert.ToDateTime(_Payment.tran_date),
            status = _Payment.status,
            val_id = _Payment.status,
            amount = _Payment.amount,
            store_amount = _Payment.store_amount,
            currency = _Payment.currency,
            bank_tran_id = _Payment.bank_tran_id,
            card_type = _Payment.card_type,
            card_no = _Payment.card_no,
            card_issuer = _Payment.card_issuer,
            card_brand = _Payment.card_brand,
            card_issuer_country = _Payment.card_issuer_country,
            card_issuer_country_code = _Payment.card_issuer_country_code,
            currency_type = _Payment.currency_type,
            currency_amount = _Payment.currency_amount,
            currency_rate = _Payment.currency_rate,
            base_fair = _Payment.base_fair,
            value_a = _Payment.value_a,
            value_b = _Payment.value_b,
            value_c = _Payment.value_c,
            risk_title = _Payment.risk_title,
            risk_level = _Payment.risk_level,
            APIConnect = _Payment.APIConnect,
            validated_on = _Payment.validated_on,
            gw_version = _Payment.gw_version,
        };

        using (_ctx = new PaymentGatewayEntities())
        {
            _ctx.PaymentLogs.Add(objPay);
            _ctx.SaveChanges();
            status = 1;
        }
    }
    catch
    {
        status = 0;
    }
    return status;
}

Finally it will redirect to merchant application success page.

pay_4 pay_5

Source Code: I’ve uploaded the full source code to download/clone @github, Hope this will help 🙂

Documentation: Download PDF

Author:

Since March 2011, have 4 years of extensive hands on experience of software development.

7 thoughts on “SSLCOMMERZ payment gateway(BD)”

Leave a Reply

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