Integrating SenangPay with PHP Laravel

  1. Setup Laravel Project:
    Ensure you have a Laravel project set up. If not, you can create one using:
   composer create-project --prefer-dist laravel/laravel your_project_name
  1. Install Required Packages:
    You might need additional packages to handle HTTP requests, such as Guzzle, which is included with Laravel by default. If you need to install it, you can use:
   composer require guzzlehttp/guzzle
  1. Configure SenangPay Settings:
    Add your SenangPay merchant details to the .env file:
   SENANGPAY_MERCHANT_ID=your_merchant_id
   SENANGPAY_SECRET_KEY=your_secret_key
   SENANGPAY_RETURN_URL=https://yourdomain.com/payment/return
   SENANGPAY_CALLBACK_URL=https://yourdomain.com/payment/callback
  1. Create Routes:
    Define routes for handling payment and response from SenangPay in your routes/web.php:
   use App\Http\Controllers\PaymentController;

   Route::get('/payment', [PaymentController::class, 'index']);
   Route::post('/payment/process', [PaymentController::class, 'process']);
   Route::get('/payment/return', [PaymentController::class, 'return']);
   Route::post('/payment/callback', [PaymentController::class, 'callback']);
  1. Create PaymentController:
    Create a controller to handle the payment process:
   php artisan make:controller PaymentController

In PaymentController.php:

   namespace App\Http\Controllers;

   use Illuminate\Http\Request;
   use GuzzleHttp\Client;

   class PaymentController extends Controller
   {
       public function index()
       {
           return view('payment');
       }

       public function process(Request $request)
       {
           $merchantId = config('services.senangpay.merchant_id');
           $secretKey = config('services.senangpay.secret_key');
           $amount = number_format($request->amount, 2);
           $orderId = 'INV' . time();
           $name = $request->name;
           $email = $request->email;
           $phone = $request->phone;
           $desc = $request->description;
           $hash = md5($secretKey . $desc . $orderId . $amount . $name . $email . $phone);

           $paymentData = [
               'merchant_id' => $merchantId,
               'order_id' => $orderId,
               'amount' => $amount,
               'name' => $name,
               'email' => $email,
               'phone' => $phone,
               'desc' => $desc,
               'hash' => $hash,
               'return_url' => config('services.senangpay.return_url'),
               'callback_url' => config('services.senangpay.callback_url'),
           ];

           return view('process_payment', compact('paymentData'));
       }

       public function return(Request $request)
       {
           // Handle the return from SenangPay
           $statusId = $request->status_id;
           $orderId = $request->order_id;
           $msg = $request->msg;

           if ($statusId == '1') {
               // Payment successful
           } else {
               // Payment failed
           }

           return view('payment_return', compact('statusId', 'orderId', 'msg'));
       }

       public function callback(Request $request)
       {
           // Handle the callback from SenangPay
           // This will typically be used to update the payment status in your database
       }
   }
  1. Create Payment View:
    Create a view (resources/views/payment.blade.php) to handle the payment form:
   <form action="/payment/process" method="POST">
       @csrf
       <input type="text" name="amount" placeholder="Amount">
       <input type="text" name="description" placeholder="Description">
       <input type="text" name="name" placeholder="Name">
       <input type="email" name="email" placeholder="Email">
       <input type="text" name="phone" placeholder="Phone">
       <button type="submit">Pay Now</button>
   </form>
  1. Create Process Payment View:
    Create a view (resources/views/process_payment.blade.php) to handle the redirection to SenangPay:
   <form id="senangpay_form" method="POST" action="https://app.senangpay.my/payment/{{ $paymentData['merchant_id'] }}">
       @foreach($paymentData as $key => $value)
           <input type="hidden" name="{{ $key }}" value="{{ $value }}">
       @endforeach
       <button type="submit">Proceed to Payment</button>
   </form>
   <script type="text/javascript">
       document.getElementById('senangpay_form').submit();
   </script>
  1. Handle Return and Callback:
    Create views to handle the return and callback from SenangPay. For example, resources/views/payment_return.blade.php:
   @if($statusId == 1)
       <p>Payment successful! Order ID: {{ $orderId }}</p>
   @else
       <p>Payment failed! Message: {{ $msg }}</p>
   @endif

By following these steps, you should be able to integrate SenangPay with your Laravel application. Make sure to test thoroughly in a sandbox environment provided by SenangPay before going live.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.