susutawar / laravel-duitku
DuitKu Api implementation for laravel
Requires
- php: ^7.0|^8.0
README
DuitKu API implementation in laravel. Initially made for personal use, so it’s a bit biased.
Installation
composer install susutawar/duitku-laravel
php artisan vendor:publish --tag="duitku"
Configuring
add the following to your .env
file.
DUITKU_MERCHANT_ID= DUITKU_API_KEY= DUITKU_SANDBOX_MODE= DUITKU_RETURN_URL=
- Fill out
DUITKU_MERCHANT_ID
,DUITKU_API_KEY
with credentials you obtain from duitku. - Set
DUITKU_SANDBOX_MODE
to1
to use DuitKu in sandbox mode, or0
to use it in production mode. - Setup
DUITKU_RETURN_URL
to any route you have configured. this will be used by DuitKu (see documentation). The value can be a route name or a URL.
You can check config/duitku.php
for more setting/information.
Usage
Get Payments
Get available payment methods from DuitKu. see documentation
use SusuTawar\Facade\Duitku; // ... // the result is a Laravel Collection $paymentMethods = Duitku::getPayments($paymentAmount);
Make Transaction
Start a transaction using DuitKu. see documentation
use SusuTawar\Facade\Duitku; // ... $paymentMethods = Duitku::makeTransaction(/* params */);
The resulting object contains trx
, url
, qr
, and va
:
trx
: The transaction number generated by DuitKu. Save this to your system.url
: A link to the transaction page on DuitKu.qr
: A string representing QRIS. You must generate QR images from this string yourself.va
: The virtual account number for payment methods that support it.
Check Transaction
Check the status of a transaction using your transaction ID. see documentation
use SusuTawar\Facade\Duitku; // ... $checkResult = Duitku::check($yourTransactionId);
Error Handling
Instead of returning false
or null
, when something unexpected occurred we will throw an Exception
instead.
You can catch the exception by its specific type or catch all exceptions with Exception, then use the provided message.
DuitKuAuthException
: Occurs when the provided authentication credentials cannot be authenticated by DuitKu.DuitKuBadRequestException
: Occurs when the provided parameters are not valid according to DuitKu.DuitKuNotFoundException
: Occurs when the specified resource is not available in DuitKu.DuitKuPaymentException
: Occurs when something goes wrong with the payment.
Callback Routing
We automatically register the callback route.
To handle the incoming callback, create a class implementing SusuTawar\Interfaces\DuitKuCallbackInterface
,
and register it in config/duitku.php
under routing.callback_handler
"routing" => [ // ... "callback_handler" => MyCallbackHandler::class, ]
If you prefer to manually set up the callback, disable this feature by updating the configuration:
"routing" => [ "enabled" => false, // ... ]
When this feature is disabled, you need to set the callback_url
or add DUITKU_CALLBACK_URL
to your .env
file. Otherwise, the callback_url
will default to home
or /
.