SkillAgentSearch skills...

Sslcommerz

SSLCommerz Payment Gateway Package for Laravel 5, 6, 7, 8

Install / Use

/learn @smiftakhairul/Sslcommerz
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

SSLCommerz

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

Official documentation here.

Installation

$ composer require smiftakhairul/sslcommerz

Vendor

$ php artisan vendor:publish --provider="SSLCZ\SSLCommerz\SSLCommerzServiceProvider"

A file sslcommerz.php will be added to config directory after running above command. We need to setup our configuration to .env file as follows:

STORE_ID="your-store-id"
STORE_PASSWORD="your-store-password"
IS_PRODUCTION=false

For deveopment mode we need to set IS_PRODUCTION=false, and for production mode IS_PRODUCTION=true. Please go through the official docs of SSLCommerz for further information.

Usage

Initiate a payment

$sslcommerz = new SSLCommerz();
$sslcommerz->setPaymentDisplayType('hosted'); // enum('hosted', 'checkout')
$sslcommerz->setPrimaryInformation([
    'total_amount' => 1000,
    'currency' => 'BDT',
]);
$sslcommerz->setTranId('your-transaction-id'); // set your transaction id here
$sslcommerz->setSuccessUrl('http://www.example.com/success');
$sslcommerz->setFailUrl('http://www.example.com/fail');
$sslcommerz->setCancelUrl('http://www.example.com/cancel');
$sslcommerz->setCustomerInformation([
    'cus_name' => 'John Doe',
    'cus_email' => 'john.doe@yahoo.com',
    'cus_add1' => 'Dhaka',
    'cus_add2' => 'Dhaka',
    'cus_city' => 'Dhaka',
    'cus_state' => 'Dhaka',
    'cus_postcode' => '1000',
    'cus_country' => 'Bangladesh',
    'cus_phone' => '+880**********',
]);
$sslcommerz->setShipmentInformation([
    'ship_name' => 'Store Test',
    'ship_add1' => 'Dhaka',
    'ship_add2' => 'Dhaka',
    'ship_city' => 'Dhaka',
    'ship_state' => 'Dhaka',
    'ship_postcode' => '1000',
    'ship_country' => 'Bangladesh',
    'shipping_method' => 'NO',
]);
$sslcommerz->setAdditionalInformation([
    'value_a' => 'CPT-112-A',
    'value_b' => 'CPT-112-B',
    'value_c' => 'CPT-112-C',
    'value_d' => 'CPT-112-D',
]);
$sslcommerz->setEmiOption(1); // enum(1, 0)
$sslcommerz->setProductInformation([
    'product_name' => 'Computer',
    'product_category' => 'Goods',
    'product_profile' => 'physical-goods',
]);
$sslcommerz->setCart([
    ['product' => 'Product X', 'amount' => '2000.00'],
    ['product' => 'Product Y', 'amount' => '4000.00'],
    ['product' => 'Product Z', 'amount' => '8000.00'],
]);
$sslcommerz->setProductAmount('1000');
$sslcommerz->setVat('100');
$sslcommerz->setDiscountAmount('0');
$sslcommerz->setConvenienceFee('50');

$response = $sslcommerz->initPayment($sslcommerz);

Set store information dynamically

$sslcommerz = new SSLCommerz([
    'store_id' => 'your-store-id',
    'store_password' => 'your-store-password',
    'is_production' => false
]);

Response

You will get a response after initiating a payment by which you can deal with. You can see a sample response format in the official documentation.

Hosted Payment Integration

// Controller
$sslcommerz = new SSLCommerz();
$sslcommerz->setPaymentDisplayType('hosted');
// ---

$response = $sslcommerz->initPayment($sslcommerz);
return redirect($response['GatewayPageURL']); // redirect to gateway page url

Easy Checkout Integration

// View(js) - Step 1
(function (window, document) {
    var loader = function () {
        var script = document.createElement("script"), tag = document.getElementsByTagName("script")[0];
        script.src = "{{ 'Sandbox or Live(Production) Script' }}" + Math.random().toString(36).substring(7);
        tag.parentNode.insertBefore(script, tag);
    };

    window.addEventListener ? window.addEventListener("load", loader, false) : window.attachEvent("onload", loader);
})(window, document);

/*
Sandbox Script URL: https://sandbox.sslcommerz.com/embed.min.js?
Live or Production Script URL: https://seamless-epay.sslcommerz.com/embed.min.js?
 */
<!-- View(js) - Step 2 -->
<button class="your-button-class" id="sslczPayBtn"
        token="if you have any token validation"
        postdata="your javascript arrays or objects which requires in backend"
        order="If you already have the transaction generated for current order"
        endpoint="{{ 'your-easy-checkout-pay-url' }}"> Pay Now
</button>
// Controller
$sslcommerz = new SSLCommerz();
$sslcommerz->setPaymentDisplayType('checkout');
// ---

$response = $sslcommerz->initPayment($sslcommerz);
echo $sslcommerz->formatCheckoutResponse($response); // show easycheckout pay popup

Disable CSRF Protection

Disable CSRF protection for the following URL's.

  • init-payment-via-ajax url
  • success url
  • fail url
  • cancel url
  • ipn url

Disable them from VerifyCsrfToken middleware.

// VerifyCsrfToken.php
protected $except = [
    '/init-payment-via-ajax', 
    '/success', 
    '/cancel', 
    '/fail', 
    '/ipn'
];

Order Validation

$sslcommerz = new SSLCommerz();
$response = $sslcommerz->orderValidate([
    'val_id' => $request->input('val_id'),
    'store_id' => 'your-store-id', // Optional: by default `$sslcommerz->getStoreId()`
    'store_password' => 'your-store-password', // Optional: by default `$sslcommerz->getStorePassword()`
    'v' => '1', // Optional: by default `1`
    'format' => 'json' // Optional: by default `json`
]);

Transaction Query

$sslcommerz = new SSLCommerz();

// by Transaction Id
$response = $sslcommerz->transactionQueryById([
    'tran_id' => $request->input('tran_id'),
    'store_id' => 'your-store-id', // Optional: by default `$sslcommerz->getStoreId()`
    'store_password' => 'your-store-password', // Optional: by default `$sslcommerz->getStorePassword()`
]);
// by Session Id
$response = $sslcommerz->transactionQueryBySessionId([
    'sessionkey' => 'initiated-session-key',
    'store_id' => 'your-store-id', // Optional: by default `$sslcommerz->getStoreId()`
    'store_password' => 'your-store-password', // Optional: by default `$sslcommerz->getStorePassword()`
]);

Refund

$sslcommerz = new SSLCommerz();

// Initiate
$response = $sslcommerz->refundPayment([
    'bank_tran_id' => $request->input('bank_tran_id'),
    'store_id' => 'your-store-id', // Optional: by default `$sslcommerz->getStoreId()`
    'store_password' => 'your-store-password', // Optional: by default `$sslcommerz->getStorePassword()`
    'refund_amount' => 1000,
    'refund_remarks' => 'your-refund-remarks',
    'refe_id' => 'your-ref-id', // Optional
    'format' => 'json', // Optional: by default `json`
]);
// Status
$response = $sslcommerz->refundStatus([
    'refund_ref_id' => 'refund-ref-id',
    'store_id' => 'your-store-id', // Optional: by default `$sslcommerz->getStoreId()`
    'store_password' => 'your-store-password', // Optional: by default `$sslcommerz->getStorePassword()`
]);

Available Env's & API's

Environments: getApiEnvironment()

  • sandbox (IS_PRODUCTION false)
  • production (IS_PRODUCTION true)

Domains: getApiDomain()

  • sandbox (https://sandbox.sslcommerz.com)
  • production (https://securepay.sslcommerz.com)

APIs:

  • getApiUrl() ([api_domain]/gwprocess/v4/api.php)
  • getOrderValidateApiUrl() ([api_domain]/validator/api/validationserverAPI.php)
  • getTransactionStatusApiUrl() ([api_domain]/validator/api/merchantTransIDvalidationAPI.php)
  • getRefundPaymentApiUrl() ([api_domain]/validator/api/merchantTransIDvalidationAPI.php)
  • getRefundStatusApiUrl() ([api_domain]/validator/api/merchantTransIDvalidationAPI.php)

Available Methods

Environment & domain related configuration:
<table> <thead> <tr> <th>Method Name</th> <th style="text-align: center">Param Info</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td><code>getApiEnvironment()</code></td> <td style="text-align: center"></td> <td>API environment: <b>sandbox</b> or <b>production</b>.</td> </tr> <tr> <td><code>setApiEnvironment()</code></td> <td style="text-align: center"><span><code>string</code></span></td> <td>Set API environment: <b>sandbox</b> or <b>production</b> only.</td> </tr> <tr> <td><code>getApiDomain()</code></td> <td style="text-align: center"></td> <td>API domain: for example <br><a href="https://sandbox.sslcommerz.com">https://sandbox.sslcommerz.com</a> <br>or <br> <a href="https://securepay.sslcommerz.com">https://securepay.sslcommerz.com</a></td> </tr> <tr> <td><code>isProductionMode()</code></td> <td style="text-align: center"></td> <td>Get <b>production_mode</b>.</td> </tr> <tr> <td><code>setProductionMode()</code></td> <td style="text-align: center"><span><code>boolean</code></span></td> <td>Set <b>production_mode</b>. By default, <b>production_mode</b> sets by <code>IS_PRODUCTION</code> value.</td> </tr> </tbody> </table>
API url configuration:
<table> <thead> <tr> <th>Method Name</th> <th style="text-align: center">Param Info</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td><code>getApiUrl()</code></td> <td style="text-align: center"></td> <td>Get payment initiate api url.</td> </tr> <tr> <td><code>setApiUrl()</code></td> <td style="text-align: center"><span><code>string</code></span></td> <td>Set payment initiate api url. By default, api url sets based on <code>IS_PRODUCTION</code> value. If <code>IS_PRODUCTION = true</code>, live api url will be set and for <code>IS_PRODUCTION = false</code> sandbox api url will be set.</td> </tr> <tr> <td><code>getTransactionStatusApiUrl()

Related Skills

View on GitHub
GitHub Stars7
CategoryDevelopment
Updated4y ago
Forks4

Languages

PHP

Security Score

75/100

Audited on Jan 5, 2022

No findings