This guide explains how to integrate SSLCommerz payment gateway with the Laravel Payments package.
SSLCommerz is the largest payment gateway in Bangladesh, supporting multiple payment methods including:
composer require raziul/sslcommerz-laravel
php artisan vendor:publish --tag=sslcommerz-config
Add your SSLCommerz credentials to your .env file:
SSLCOMMERZ_STORE_ID=your_store_id_here
SSLCOMMERZ_STORE_PASSWORD=your_store_password_here
SSLCOMMERZ_TEST_MODE=true
SSLCOMMERZ_SUCCESS_URL=https://yoursite.com/payment/success
SSLCOMMERZ_FAIL_URL=https://yoursite.com/payment/fail
SSLCOMMERZ_CANCEL_URL=https://yoursite.com/payment/cancel
SSLCOMMERZ_IPN_URL=https://yoursite.com/payment/ipn
You can obtain these credentials from your SSLCommerz dashboard.
For development/testing:
SSLCOMMERZ_TEST_MODE=true
For production:
SSLCOMMERZ_TEST_MODE=false
use Mdiqbal\LaravelPayments\Facades\Payment;
$paymentRequest = [
'amount' => 100.00,
'currency' => 'BDT',
'email' => 'customer@example.com',
'transaction_id' => 'TXN' . time(),
'redirect_url' => 'https://yoursite.com/payment/callback',
'payment_options' => 'visa,mastercard,mobilebank',
'customer' => [
'name' => 'John Doe',
'phone' => '01712345678',
'address' => '123 Main St, Dhaka, Bangladesh',
'city' => 'Dhaka',
'country' => 'Bangladesh',
'postal_code' => '1000'
],
'metadata' => [
'category' => 'Electronics',
'order_id' => 'ORD123456'
]
];
$payment = Payment::gateway('sslcommerz')->pay($paymentRequest);
$verification = Payment::gateway('sslcommerz')->verify($transactionId);
if ($verification['success']) {
$status = $verification['status'];
$amount = $verification['amount'];
if ($status === 'success') {
// Payment was successful
}
}
$refundData = [
'bank_transaction_id' => 'BANK_TRANSACTION_ID',
'amount' => 50.00,
'reason' => 'Customer requested refund'
];
$refund = Payment::gateway('sslcommerz')->refund($refundData);
$linkData = [
'amount' => 100.00,
'currency' => 'BDT',
'description' => 'Payment for services',
'redirect_url' => 'https://yoursite.com/success',
'customer' => [
'name' => 'John Doe',
'email' => 'customer@example.com',
'phone' => '01712345678'
]
];
$paymentLink = Payment::gateway('sslcommerz')->createPaymentLink($linkData);
$subscriptionData = [
'amount' => 500.00,
'currency' => 'BDT',
'interval' => 'month',
'times' => 12,
'description' => 'Monthly subscription',
'customer' => [
'name' => 'John Doe',
'email' => 'customer@example.com',
'phone' => '01712345678'
],
'success_url' => 'https://yoursite.com/subscription/success',
'fail_url' => 'https://yoursite.com/subscription/fail',
'cancel_url' => 'https://yoursite.com/subscription/cancel'
];
$subscription = Payment::gateway('sslcommerz')->createSubscription($subscriptionData);
$methods = Payment::gateway('sslcommerz')->getPaymentMethodsForCountry('BD');
// Returns:
// [
// 'visa' => 'Visa Card',
// 'mastercard' => 'Mastercard',
// 'amex' => 'American Express',
// 'mobile_banking' => 'Mobile Banking (bKash, Rocket, Nagad, etc.)',
// 'internet_banking' => 'Internet Banking',
// 'others' => 'Other Payment Methods'
// ]
Set up your webhook endpoint in the SSLCommerz dashboard
Create a route to handle webhooks:
// routes/web.php
Route::post('/sslcommerz/webhook', [SslcommerzWebhookController::class, 'handleWebhook']);
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mdiqbal\LaravelPayments\Facades\Payment;
class SslcommerzWebhookController extends Controller
{
public function handleWebhook(Request $request)
{
$payload = $request->all();
// Process the webhook
$result = Payment::gateway('sslcommerz')->processWebhook($payload);
if ($result['success']) {
$eventType = $result['event_type'];
$transactionId = $result['transaction_id'];
switch ($eventType) {
case 'payment.success':
// Handle successful payment
$this->handleSuccessfulPayment($result);
break;
case 'payment.failed':
// Handle failed payment
$this->handleFailedPayment($result);
break;
case 'payment.cancelled':
// Handle cancelled payment
$this->handleCancelledPayment($result);
break;
}
}
return response()->json(['status' => 'received']);
}
protected function handleSuccessfulPayment($data)
{
// Update order status, send confirmation email, etc.
// $data contains: transaction_id, amount, currency, payment_method
}
protected function handleFailedPayment($data)
{
// Log failed payment, notify user, etc.
}
protected function handleCancelledPayment($data)
{
// Handle cancelled payment
}
}
SSLCommerz also supports IPN for real-time payment notifications:
public function handleIpn(Request $request)
{
$val_id = $request->input('val_id');
if (!$val_id) {
return response()->json(['error' => 'Missing val_id'], 400);
}
// Verify the IPN request
$verifyData = [
'val_id' => $val_id,
'store_id' => config('services.sslcommerz.store_id'),
'store_password' => config('services.sslcommerz.store_password'),
'format' => 'json'
];
$response = Http::asForm()->post(
config('services.sslcommerz.test_mode')
? 'https://sandbox.sslcommerz.com/validator/api/validationserverAPI.php'
: 'https://securepay.sslcommerz.com/validator/api/validationserverAPI.php',
$verifyData
);
if ($response->successful() && $response['status'] === 'VALID') {
// Payment verified successfully
$transactionId = $response['tran_id'];
$amount = $response['amount'];
$currency = $response['currency'];
// Update your database, send notifications, etc.
}
return response()->json(['status' => 'processed']);
}
You can specify which payment methods to display:
$paymentRequest = [
'amount' => 100.00,
'currency' => 'BDT',
'email' => 'customer@example.com',
'transaction_id' => 'TXN' . time(),
'payment_options' => [
'visa',
'mastercard',
'mobile_banking',
'internet_banking'
],
// ... other parameters
];
visa - Visa Cardsmastercard - Mastercardamex - American Expressbrac_visa - BRAC Visacity_visa - City Visadutch_bangla_visa - Dutch Bangla Visaebl_visa - EBL Visadbbl_master - DBBL Mastercardbrac_master - BRAC Mastercardcity_master - City Mastercardebl_master - EBL Mastercardmtbl_visa - MBL Visacity_amex - City American Expressbrac_amex - BRAC American Expressdbbl_amex - DBBL American Expressmobile_banking - Mobile Bankinginternet_banking - Internet Bankingothers - Other Payment MethodsThe SSLCommerz gateway provides detailed error messages:
$payment = Payment::gateway('sslcommerz')->pay($paymentRequest);
if (!$payment['success']) {
$error = $payment['error'];
$message = $error['message'];
$code = $error['code'];
// Handle error based on type
if ($code === 'PAYMENT_FAILED') {
// Payment initialization failed
}
}
PAYMENT_FAILED - Payment initialization failedVERIFICATION_FAILED - Transaction verification failedREFUND_FAILED - Refund processing failedSUBSCRIPTION_FAILED - Subscription creation failedWEBHOOK_FAILED - Webhook processing failedTRANSACTION_NOT_FOUND - Transaction not foundFor testing, use these credentials:
SSLCOMMERZ_STORE_ID=testbox
SSLCOMMERZ_STORE_PASSWORD=qwerty
SSLCOMMERZ_TEST_MODE=true
Use these test cards for testing:
Test CVV: 123 Test Expiry: Any future date
For mobile banking testing, select any mobile banking option and use test account numbers provided in the SSLCommerz test environment.
SSLCommerz supports the following currencies:
SSLCommerz primarily serves Bangladesh but supports:
SSLCommerz implements rate limits:
Implement proper rate limiting in your application to avoid being blocked.
pay() - Initialize a paymentverify() - Verify a transactionrefund() - Process a refundgetTransactionStatus() - Get transaction statuscreatePaymentLink() - Create a payment linkcreateSubscription() - Create a subscriptioncancelSubscription() - Cancel a subscription (handled at application level)createCustomer() - Create/record customer informationgetPaymentMethodsForCountry() - Get available payment methods for a countrygetSupportedCurrencies() - Get list of supported currenciesgetGatewayConfig() - Get gateway configuration// In your success URL controller
public function paymentSuccess(Request $request)
{
$transactionId = $request->input('tran_id');
// Verify the transaction
$verification = Payment::gateway('sslcommerz')->verify($transactionId);
if ($verification['success'] && $verification['status'] === 'success') {
// Update order status, show success page
return view('payment.success', [
'transaction_id' => $transactionId,
'amount' => $verification['amount']
]);
}
// Handle verification failure
return redirect('/payment/fail')->with('error', 'Payment verification failed');
}
$paymentRequest = [
'amount' => 100.00,
'currency' => 'BDT',
'email' => 'customer@example.com',
'transaction_id' => 'TXN' . time(),
'metadata' => [
'order_id' => 'ORD123456',
'user_id' => 789,
'product_ids' => [1, 2, 3],
'custom_field' => 'custom_value'
],
// ... other parameters
];
// The metadata will be sent as value_a in the payment request
For SSLCommerz-specific support:
For Laravel Payments package support:
How can I help you explore Laravel packages today?