Integrating Billplz with PHP Laravel

Step-by-Step Guide to Integrating Billplz 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 Billplz Settings:
    Add your Billplz API key and other settings to the .env file:
   BILLPLZ_API_KEY=your_billplz_api_key
   BILLPLZ_COLLECTION_ID=your_collection_id
   BILLPLZ_RETURN_URL=https://yourdomain.com/payment/return
   BILLPLZ_CALLBACK_URL=https://yourdomain.com/payment/callback
  1. Create Routes:
    Define routes for handling payment and response from Billplz 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)
       {
           $client = new Client();

           $response = $client->request('POST', 'https://www.billplz.com/api/v3/bills', [
               'auth' => [env('BILLPLZ_API_KEY'), ''],
               'form_params' => [
                   'collection_id' => env('BILLPLZ_COLLECTION_ID'),
                   'email' => $request->email,
                   'name' => $request->name,
                   'amount' => $request->amount * 100, // Convert to cents
                   'callback_url' => env('BILLPLZ_CALLBACK_URL'),
                   'description' => $request->description,
                   'redirect_url' => env('BILLPLZ_RETURN_URL'),
               ],
           ]);

           $bill = json_decode($response->getBody(), true);

           return redirect($bill['url']);
       }

       public function return(Request $request)
       {
           $billId = $request->billplz['id'];
           $paid = $request->billplz['paid'];
           $amount = $request->billplz['amount'];
           $transactionId = $request->billplz['transaction_id'];

           if ($paid) {
               // Payment successful
           } else {
               // Payment failed
           }

           return view('payment_return', compact('billId', 'paid', 'amount', 'transactionId'));
       }

       public function callback(Request $request)
       {
           // Handle the callback from Billplz
           // 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">
       <button type="submit">Pay Now</button>
   </form>
  1. Create Return View:
    Create a view (resources/views/payment_return.blade.php) to display the payment status:
   @if($paid)
       <p>Payment successful! Bill ID: {{ $billId }}, Amount: {{ $amount }}, Transaction ID: {{ $transactionId }}</p>
   @else
       <p>Payment failed!</p>
   @endif

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