Midtrans-PHP
Midtrans PHP!
This is the Official PHP wrapper/library for Midtrans Payment API, that is compatible with Composer. Visit https://midtrans.com for more information about the product and see documentation at http://docs.midtrans.com for more technical details. Starting version 2.6, this library now supports Snap-bi. You can go to this docs to learn more about Snap-bi.
1. Installation
1.a Composer Installation
If you are using Composer, you can install via composer CLI:
composer require midtrans/midtrans-php
or
add this require line to your composer.json file:
"require": {
"midtrans/midtrans-php": "2.*"
}
}
and run composer install on your terminal.
Note: If you are using Laravel framework, in some case you also need to run
composer dumpautoload
/Midtranswill then be available (auto loaded) as Object in your Laravel project.
1.b Manual Instalation
If you are not using Composer, you can clone or download this repository.
Then you should require/autoload Midtrans.php file on your code.
// my code goes here
2. How to Use
2.1 General Settings
\Midtrans\Config::$serverKey = '
// Set to Development/Sandbox Environment (default). Set to true for Production Environment (accept real transaction).
\Midtrans\Config::$isProduction = false;
// Set sanitization on (default)
\Midtrans\Config::$isSanitized = true;
// Set 3DS transaction for credit card to true
\Midtrans\Config::$is3ds = true;
Override Notification URL
You can opt to change or add custom notification urls on every transaction. It can be achieved by adding additional HTTP headers into charge request.
Config::$appendNotifUrl = "https://example.com/test1,https://example.com/test2";
// Use new notification url(s) disregarding the settings on Midtrans Dashboard Portal (MAP)
Config::$overrideNotifUrl = "https://example.com/test1";
Note: When both
appendNotifUrlandoverrideNotifUrlare used together then onlyoverrideNotifUrlwill be used.
Both header can only receive up to maximum of 3 urls.
Idempotency-Key
You can opt to add idempotency key on charge transaction. It can be achieved by adding additional HTTP headers into charge request. Is a unique value that is put on header on API request. Midtrans API accept Idempotency-Key on header to safely handle retry request without performing the same operation twice. This is helpful for cases where merchant didn't receive the response because of network issue or other unexpected error.
2.2 Choose Product/Method
We have 3 different products of payment that you can use:
- Snap - Customizable payment popup will appear on your web/app (no redirection). doc ref
- Snap Redirect - Customer need to be redirected to payment url hosted by midtrans. doc ref
- Core API (VT-Direct) - Basic backend implementation, you can customize the frontend embedded on your web/app as you like (no redirection). doc ref
Choose one that you think best for your unique needs.
2.2.a Snap
You can see Snap example here.
Get Snap Token
'transaction_details' => array(
'order_id' => rand(),
'gross_amount' => 10000,
)
);
$snapToken = \Midtrans\Snap::getSnapToken($params);