Integrating Razer Merchant Services (formerly known as MOLPay) with PHP Laravel

Step-by-Step Guide to Integrating Razer Merchant Services with 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:
    Laravel comes with Guzzle, an HTTP client that can be used to make API requests. Ensure it is installed:
   composer require guzzlehttp/guzzle
  1. Configure Razer Merchant Services Settings:
    Add your Razer Merchant Services API key and other settings to the .env file:
   RAZER_MERCHANT_ID=your_merchant_id
   RAZER_VERIFY_KEY=your_verify_key
   RAZER_SECRET_KEY=your_secret_key
   RAZER_PAYMENT_GATEWAY_URL=https://www.onlinepayment.com.my/MOLPay/pay/your_merchant_id
   RAZER_RETURN_URL=https://yourdomain.com/payment/return
   RAZER_CALLBACK_URL=https://yourdomain.com/payment/callback
  1. Create Routes:
    Define routes for handling payment and response from Razer Merchant Services 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 = env('RAZER_MERCHANT_ID');
           $verifyKey = env('RAZER_VERIFY_KEY');
           $amount = number_format($request->amount, 2, '.', '');
           $orderId = 'INV' . time();
           $name = $request->name;
           $email = $request->email;
           $phone = $request->phone;
           $desc = $request->description;
           $currency = 'MYR';
           $returnUrl = env('RAZER_RETURN_URL');
           $callbackUrl = env('RAZER_CALLBACK_URL');
           $vcode = md5($amount . $merchantId . $orderId . $verifyKey);

           $paymentData = [
               'amount' => $amount,
               'orderid' => $orderId,
               'bill_name' => $name,
               'bill_email' => $email,
               'bill_mobile' => $phone,
               'bill_desc' => $desc,
               'currency' => $currency,
               'returnurl' => $returnUrl,
               'callbackurl' => $callbackUrl,
               'vcode' => $vcode,
               'country' => 'MY',
               'channel' => 'Fpx',
           ];

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

       public function return(Request $request)
       {
           $status = $request->status;
           $orderId = $request->orderid;
           $message = $request->message;
           $amount = $request->amount;

           if ($status == '00') {
               // Payment successful
           } else {
               // Payment failed
           }

           return view('payment_return', compact('status', 'orderId', 'message', 'amount'));
       }

       public function callback(Request $request)
       {
           // Handle the callback from Razer Merchant Services
           // 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 Razer Merchant Services:
   <form id="razer_form" method="POST" action="{{ env('RAZER_PAYMENT_GATEWAY_URL') }}">
       @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('razer_form').submit();
   </script>
  1. Handle Return and Callback:
    Create views to handle the return and callback from Razer Merchant Services. For example, resources/views/payment_return.blade.php:
   @if($status == '00')
       <p>Payment successful! Order ID: {{ $orderId }}, Amount: {{ $amount }}</p>
   @else
       <p>Payment failed! Message: {{ $message }}</p>
   @endif

By following these steps, you should be able to integrate Razer Merchant Services with your Laravel application. Make sure to test thoroughly in a sandbox environment provided by Razer Merchant Services 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.