To integrate SSL Commerce payment in Laravel, you can follow these steps:
Install the SSL Commerce package using Composer. You can do this by running the following command in your terminal:
bashcomposer require sslcommerz/sslcommerz
Add the package's service provider to your
config/app.php
file:arduino'providers' => [ // ... Sslcommerz\SslcommerzServiceProvider::class, ],
Publish the package's configuration file using the following command:
arduinophp artisan vendor:publish --provider="Sslcommerz\SslcommerzServiceProvider"
Update the
config/sslcommerz.php
file with your SSL Commerce account details, including the store ID, store password, and other necessary information.Create a route and a controller method to initiate the payment process:
arduinoRoute::post('/payment', [PaymentController::class, 'initiatePayment']);
phpuse Sslcommerz\SslCommerzNotification; use Illuminate\Http\Request; class PaymentController extends Controller { public function initiatePayment(Request $request) { $postData = [ 'total_amount' => $request->input('amount'), 'currency' => 'BDT', 'tran_id' => uniqid(), 'success_url' => url('/payment/success'), 'fail_url' => url('/payment/fail'), 'cancel_url' => url('/payment/cancel'), ]; $sslcommerz = new SslCommerzNotification(); $sslcommerz->initiate($postData); } }
Create three more routes and methods for handling the success, fail, and cancel callbacks:
arduinoRoute::get('/payment/success', [PaymentController::class, 'paymentSuccess']); Route::get('/payment/fail', [PaymentController::class, 'paymentFail']); Route::get('/payment/cancel', [PaymentController::class, 'paymentCancel']);
phppublic function paymentSuccess(Request $request) { $transactionData = $request->all(); // Process the payment success } public function paymentFail(Request $request) { $transactionData = $request->all(); // Process the payment fail } public function paymentCancel(Request $request) { $transactionData = $request->all(); // Process the payment cancel }
Finally, update your payment form to submit the payment data to the
/payment
route:bash<form action="{{ url('/payment') }}" method="post"> @csrf <input type="hidden" name="amount" value="{{ $amount }}"> <button type="submit">Pay Now</button> </form>
That's it! You should now be able to initiate and process payments using SSL Commerce in Laravel.