Integrating iPay88 with PHP Laravel

Here is a step-by-step guide to help you through the process:

  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:
    While Laravel doesn’t have an official package for iPay88, you can use a third-party package or write your own integration code. If you choose to use a third-party package, install it via Composer. For example, a package like hexters/ipay88 can be installed using:
   composer require hexters/ipay88
  1. Publish the Configuration:
    After installing the package, you might need to publish the configuration file:
   php artisan vendor:publish --tag=ipay88-config
  1. Configure iPay88 Settings:
    Update the configuration file (e.g., config/ipay88.php) with your iPay88 Merchant details provided by iPay88.
   return [
       'merchant_code' => env('IPAY88_MERCHANT_CODE', ''),
       'merchant_key'  => env('IPAY88_MERCHANT_KEY', ''),
       'response_url'  => env('IPAY88_RESPONSE_URL', 'your/response/url'),
       'backend_url'   => env('IPAY88_BACKEND_URL', 'your/backend/url'),
   ];
  1. Update Environment File:
    Add the iPay88 credentials to your .env file:
   IPAY88_MERCHANT_CODE=your_merchant_code
   IPAY88_MERCHANT_KEY=your_merchant_key
   IPAY88_RESPONSE_URL=https://yourdomain.com/response
   IPAY88_BACKEND_URL=https://yourdomain.com/backend
  1. Create Routes:
    Define routes for handling payment and response from iPay88 in your routes/web.php:
   use App\Http\Controllers\PaymentController;

   Route::get('/payment', [PaymentController::class, 'index']);
   Route::post('/payment/process', [PaymentController::class, 'process']);
   Route::post('/payment/response', [PaymentController::class, 'response']);
  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;

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

       public function process(Request $request)
       {
           $paymentData = [
               'merchant_code' => config('ipay88.merchant_code'),
               'payment_id'    => $request->payment_id,
               'ref_no'        => 'INV' . time(),
               'amount'        => $request->amount,
               'currency'      => 'MYR',
               'prod_desc'     => $request->description,
               'user_name'     => $request->name,
               'user_email'    => $request->email,
               'user_contact'  => $request->phone,
               'remark'        => 'Remark',
               'lang'          => 'UTF-8',
               'signature'     => $this->generateSignature($request->all()),
               'response_url'  => config('ipay88.response_url'),
               'backend_url'   => config('ipay88.backend_url'),
           ];

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

       protected function generateSignature($data)
       {
           $source = config('ipay88.merchant_key') . config('ipay88.merchant_code') . $data['payment_id'] . $data['ref_no'] . $data['amount'] . $data['currency'];
           return base64_encode(hex2bin(sha1($source)));
       }

       public function response(Request $request)
       {
           // Handle the iPay88 response here
       }
   }
  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="payment_id" placeholder="Payment ID">
       <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 iPay88:
   <form id="ipay88_form" method="POST" action="https://www.mobile88.com/epayment/entry.asp">
       @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('ipay88_form').submit();
   </script>
  1. Handle Response:
    In your PaymentController, implement the logic to handle the response from iPay88. This typically involves verifying the signature and updating your database based on the payment status.
See also  Day 2: Creating Routes and Controllers

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