royryando/laravel-duitku

A simple Duitku payment gateway library for Laravel.

v1.0.6 2021-08-12 03:43 UTC

This package is auto-updated.

Last update: 2024-05-12 09:46:57 UTC


README

A simple Duitku payment gateway library for Laravel.

Requirments

  • PHP ≥ 5.5
  • Laravel ≥ 5.1

Installation

  • Install through composer

    composer require royryando/laravel-duitku
    
  • Add the duitku service provider in config/app.php: (Laravel 5.5+ uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.)

    'providers' => [
      Royryando\Duitku\DuitkuServiceProvider::class
    ];

Configure

  • Add required variable to .env
    DUITKU_MERCHANT_CODE=
    DUITKU_API_KEY=
    DUITKU_CALLBACL_URL=https://example.com/callback/payment
    DUITKU_RETURN_URL=https://example.com/callback/return
    DUITKU_ENV=dev/production

Usage

Get All Available Payment Method

Ref: https://docs.duitku.com/api/id/#payment-method

Call paymentMethods function from Duitku facade with the integer parameter is amount

Duitku::paymentMethods(100000)

The return is an array of array, example:

[
    ...
    [
        'code' => 'M1',
        'name' => 'Bank Mandiri',
        'image' => 'https://example.com/image.jpg',
        'fee' => 0
    ],
    ...
]

Create Invoice

Ref: https://docs.duitku.com/api/id/#request-transaction

Create invoice or inquiry by calling createInvoice from Duitku facade with these parameter:

Order Id, amount, payment method, product name, customer name, cutomer email, expiry in minute

Duitku::createInvoice('ORDER_ID', 100000, 'M1', 'Product Name', 'John Doe', 'john@example.com', 120);

The return if success:

[
  'success' => true,
  'reference' => 'D7999PJ38HNY7TSKHSGX',
  'payment_url' => 'https://url.to.payment.example.com/',
  'va_number' => '0000123123123',
  'amount' => 100000,
  'message' => 'SUCCESS' // message from Duitku
]

The return if not success:

[
  'success' => false,
  'message' => 'The selected payment channel not available' // message from Duitku
]

Check Invoice Status

Ref: https://docs.duitku.com/api/id/#check-transaction

Check invoice or inquiry status by calling

Duitku::checkInvoiceStatus('order ID')

The return is an array, example:

[
  'reference' => 'D7999PJ38HNY7TSKHSGX', // reference code from Duitku
  'amount' => 100000,
  'message' => 'SUCCESS',
  'code' => '00', // 00=>Success, 01=>Pending, 02=>Failed/Expired
]

Handle Callback

Ref: https://docs.duitku.com/api/id/#callback

  • Create a new controller and extend Royryando\Duitku\Http\Controllers\DuitkuBaseController

    use Royryando\Duitku\Http\Controllers\DuitkuBaseController;
    
    class DuitkuController extends DuitkuBaseController
    {
        //
    }

    This controller will handle all callback requests from Duitku and store the success/failed payment function

  • Inside the controller, override onPaymentSuccess function. This function will triggered if receiving a successful transaction callback

    ...
        protected function onPaymentSuccess(
            string $orderId, string $productDetail, int $amount, string $paymentCode,
            string $shopeeUserHash, string $reference, string $additionalParam
        ): void
        {
            // Your code here
        }
    ...
  • Inside the controller, override onPaymentFailed function. This function will triggered if receiving a failed status from callback

    ...
        protected function onPaymentFailed(
            string $orderId, string $productDetail, int $amount, string $paymentCode,
            string $shopeeUserHash, string $reference, string $additionalParam
        ): void
        {
            // Your code here
        }
    ...
  • Add route in your application route web.php with the function of paymentCallback

    Route::post('callback/payment', [\App\Http\Controllers\DuitkuController::class, 'paymentCallback']);
  • Exclude the callback route from CSRF verification

    Edit App\Http\Middleware\VerifyCsrfToken.php

    protected $except = [
        'callback/payment',
    ];

TODO

  • Add tests
  • Add support for Return Callback
  • Add support for Disbursement API