PHP code example of revoltify / sslcommerz-php-library

1. Go to this page and download the library: Download revoltify/sslcommerz-php-library 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/ */

    

revoltify / sslcommerz-php-library example snippets




SLCommerz\Exceptions\SSLCommerzException;
use SSLCommerz\PaymentParams;
use SSLCommerz\SSLCommerz;

try {
    // Initialize SSLCommerz with your store ID, store password, and sandbox mode
    $sslcommerz = new SSLCommerz('your_store_id', 'your_store_password', true);

    // Set up payment parameters
    $params = (new PaymentParams())
        ->setAmount(1000)  // Amount in BDT
        ->setCurrency('BDT')
        ->setTransactionId(uniqid())  // Unique transaction ID
        ->setSuccessUrl('https://yourdomain.com/success.php')
        ->setFailUrl('https://yourdomain.com/fail.php')
        ->setCancelUrl('https://yourdomain.com/cancel.php')
        ->setCustomerInfo('Customer Name', '[email protected]', '01700000000', 'Customer Address', 'Dhaka', 'Bangladesh')
        ->setProductInfo('Product Name', 'Electronics', 'general')
        ->setCustomValues('Ref001', 'Ref002', 'Ref003', 'Ref004');

    // Initiate payment and get the response
    $response = $sslcommerz->initiatePayment($params);

    // Redirect to the payment gateway
    header("location:" . $response['GatewayPageURL']);

} catch (SSLCommerzException $e) {
    // Handle any errors that occur during the payment initiation
    echo $e->getMessage();
}



SLCommerz\Exceptions\SSLCommerzException;
use SSLCommerz\SSLCommerz;

try {
    // Initialize SSLCommerz with your store ID, store password, and sandbox mode
    $sslcommerz = new SSLCommerz('your_store_id', 'your_store_password', true);
    
    // Validate the payment
    $response = $sslcommerz->validatePayment();
    
    // Output the validation response
    echo "<pre>";
    print_r($response);

} catch (SSLCommerzException $e) {
    // Handle any errors during payment validation
    echo $e->getMessage();
}



SLCommerz\Exceptions\SSLCommerzException;
use SSLCommerz\SSLCommerz;

try {
    // Initialize SSLCommerz with your store ID, store password, and sandbox mode
    $sslcommerz = new SSLCommerz('your_store_id', 'your_store_password', true);
    
    // Validate the payment received via IPN
    $response = $sslcommerz->validatePayment();
    
    // Output the IPN validation response
    echo "<pre>";
    print_r($response);

} catch (SSLCommerzException $e) {
    // Handle any errors during IPN validation
    echo $e->getMessage();
}



SLCommerz\Exceptions\SSLCommerzException;
use SSLCommerz\SSLCommerz;

try {
    // Initialize SSLCommerz with your store ID, store password, and sandbox mode
    $sslcommerz = new SSLCommerz('your_store_id', 'your_store_password', true);

    // Refund a payment
    $bank_tran_id = 'TRANSACTION_ID';  // Replace with the actual bank_tran_id
    $refund_amount = 500;  // Amount to refund in BDT
    $refund_remarks = 'Customer request for refund';  // Optional refund remarks

    $response = $sslcommerz->refundPayment($bank_tran_id, $refund_amount, $refund_remarks);
    
    // Output the refund response
    echo "<pre>";
    print_r($response);

} catch (SSLCommerzException $e) {
    // Handle any errors that occur during the refund process
    echo $e->getMessage();
}
bash
composer