PHP code example of smiftakhairul / sslcommerz

1. Go to this page and download the library: Download smiftakhairul/sslcommerz library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

smiftakhairul / sslcommerz example snippets


$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' => '[email protected]',
    '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);

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

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

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

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

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

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

$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`
]);

$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()`
]);

$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()`
]);
bash
$ php artisan vendor:publish --provider="SSLCZ\SSLCommerz\SSLCommerzServiceProvider"