Looking to hire Laravel developers? Try LaraJobs

xendit-laravel maintained by khidirdotid

Description
A Xendit Wrapper for Laravel
Author
Last update
2025/03/18 08:03 (dev-main)
License
Links
Downloads
23
Tags

Comments
comments powered by Disqus

xendit-laravel

A Xendit Wrapper for Laravel

Installation

  1. Install the package

    composer require khidirdotid/xendit-laravel
    
  2. Publish the config file

    php artisan vendor:publish --provider="KhidirDotID\Xendit\Providers\XenditServiceProvider"
    
  3. Add the Facade to your config/app.php into aliases section

    'Xendit' => KhidirDotID\Xendit\Facades\Xendit::class,
    
  4. Add ENV data

    XENDIT_API_KEY=
    

    or you can set it through the controller

    \Xendit::setXenditKey('XENDIT_API_KEY');
    

Usage

Invoice

  1. Get Redirection URL of a Payment Page
    $data = [
        'external_id' => 'invoice-' . time(),
        'amount' => 10000
    ];
    
    try {
        // Get Payment Page URL
        $paymentUrl = \Xendit::createInvoice($data);
    
        // Redirect to Payment Page
        return redirect()->away($paymentUrl['invoice_url']);
    } catch (\Throwable $th) {
        throw $th;
    }
    

Handle HTTP Notification

  1. Create route to handle notifications
    Route::match(['GET', 'POST'], 'xendit.ipn', [PaymentController::class, 'xenditIpn'])->name('xendit.ipn');
    
  2. Create method in controller
    public function xenditIpn(Request $request)
    {
        try {
            $response = \Xendit::getInvoiceById($request->invoice_id);
    
            if (in_array(strtolower($response['status']), ['paid', 'settled'])) {
                // TODO: Set payment status in merchant's database to 'success'
            }
        } catch (\Throwable $th) {
            throw $th;
        }
    }
    
  3. Except verify CSRF token in app/Http/Middleware/VerifyCsrfToken.php
    protected $except = [
        'xendit/ipn'
    ];