khidirdotid/xendit-laravel

A Xendit Wrapper for Laravel

v1.0.0 2024-09-17 07:34 UTC

This package is auto-updated.

Last update: 2024-12-17 08:14:44 UTC


README

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'
    ];