Cara menggunakan is php becoming obsolete?

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.

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

require_once dirname(__FILE__) . '/pathofproject/Midtrans.php';

// my code goes here
3 file:

{
    "require": {
        "midtrans/midtrans-php": "2.*"
    }
}

and run

require_once dirname(__FILE__) . '/pathofproject/Midtrans.php';

// my code goes here
4 on your terminal.

Note: If you are using Laravel framework, in case you also need to run

require_once dirname(__FILE__) . '/pathofproject/Midtrans.php';

// my code goes here
5

require_once dirname(__FILE__) . '/pathofproject/Midtrans.php';

// my code goes here
6 will 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

require_once dirname(__FILE__) . '/pathofproject/Midtrans.php';

// my code goes here
7 file on your code.

require_once dirname(__FILE__) . '/pathofproject/Midtrans.php';

// my code goes here

2. How to Use

2.1 General Settings

// Set your Merchant Server Key
\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.

// Add new notification url(s) alongside the settings on Midtrans Dashboard Portal (MAP)
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

require_once dirname(__FILE__) . '/pathofproject/Midtrans.php';

// my code goes here
8 and
require_once dirname(__FILE__) . '/pathofproject/Midtrans.php';

// my code goes here
9 are used together then only
require_once dirname(__FILE__) . '/pathofproject/Midtrans.php';

// my code goes here
9 will 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.

Config::$paymentIdempotencyKey = "Unique-ID";

2.2 Choose Product/Method

We have 3 different products of payment that you can use:

  • - Customizable payment popup will appear on your web/app (no redirection). doc ref
  • - Customer need to be redirected to payment url hosted by midtrans. doc ref
  • - 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

$params = array(
    'transaction_details' => array(
        'order_id' => rand(),
        'gross_amount' => 10000,
    )
);

$snapToken = \Midtrans\Snap::getSnapToken($params);

Initialize Snap JS when customer click pay button

<html>
  <body>
    <button id="pay-button">Pay!button>
    <pre><div id="result-json">JSON result will appear here after payment:<br>div>pre> 


    <script src="https://app.sandbox.midtrans.com/snap/snap.js" data-client-key="">script>
    <script type="text/javascript">
      document.getElementById('pay-button').onclick = function(){
        // SnapToken acquired from previous step
        snap.pay('', {
          // Optional
          onSuccess: function(result){
            /* You may add your own js here, this is just example */ document.getElementById('result-json').innerHTML += JSON.stringify(result, null, 2);
          },
          // Optional
          onPending: function(result){
            /* You may add your own js here, this is just example */ document.getElementById('result-json').innerHTML += JSON.stringify(result, null, 2);
          },
          // Optional
          onError: function(result){
            /* You may add your own js here, this is just example */ document.getElementById('result-json').innerHTML += JSON.stringify(result, null, 2);
          }
        });
      };
    script>
  body>
html>

Implement Notification Handler

2.2.b Snap Redirect

You can see some Snap Redirect examples here.

Get Redirection URL of a Payment Page

$params = array(
    'transaction_details' => array(
        'order_id' => rand(),
        'gross_amount' => 10000,
    )
);

try {
  // Get Snap Payment Page URL
  $paymentUrl = \Midtrans\Snap::createTransaction($params)->redirect_url;
  
  // Redirect to Snap Payment Page
  header('Location: ' . $paymentUrl);
}
catch (Exception $e) {
  echo $e->getMessage();
}

Implement Notification Handler

2.2.c Core API (VT-Direct)

You can see some Core API examples here.

Set Client Key

MidtransNew3ds.clientKey = "";

Checkout Page

Please refer to this file

Checkout Process

1. Create Transaction Details

{
    "require": {
        "midtrans/midtrans-php": "2.*"
    }
}
0

2. Create Item Details, Billing Address, Shipping Address, and Customer Details (Optional)

{
    "require": {
        "midtrans/midtrans-php": "2.*"
    }
}
1

3. Get Token ID from Checkout Page

{
    "require": {
        "midtrans/midtrans-php": "2.*"
    }
}
2

4. Create Transaction Data

{
    "require": {
        "midtrans/midtrans-php": "2.*"
    }
}
3

5. Charge

{
    "require": {
        "midtrans/midtrans-php": "2.*"
    }
}
4

6. Credit Card 3DS Authentication

The credit card charge result may contains

// Set your Merchant Server Key
\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;
1 for 3DS authentication. 3DS Authentication should be handled on Frontend please refer to

For full example on Credit Card 3DS transaction refer to:

  • Core API examples
7. Handle Transaction Status

{
    "require": {
        "midtrans/midtrans-php": "2.*"
    }
}
5

8. Implement Notification Handler

2.3 Handle HTTP Notification

Create separated web endpoint (notification url) to receive HTTP POST notification callback/webhook. HTTP notification will be sent whenever transaction status is changed. Example also available here

{
    "require": {
        "midtrans/midtrans-php": "2.*"
    }
}
6

2.4 Process Transaction

Get Transaction Status

{
    "require": {
        "midtrans/midtrans-php": "2.*"
    }
}
7

Approve Transaction

If transaction fraud_status == CHALLENGE, you can approve the transaction from Merchant Dashboard, or API :

{
    "require": {
        "midtrans/midtrans-php": "2.*"
    }
}
8

Cancel Transaction

You can Cancel transaction with

// Set your Merchant Server Key
\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;
2, or credit card transaction with
// Set your Merchant Server Key
\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;
3 (before it become SETTLEMENT)

{
    "require": {
        "midtrans/midtrans-php": "2.*"
    }
}
9

Expire Transaction

You can Expire transaction with

// Set your Merchant Server Key
\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;
4 (before it become SETTLEMENT or EXPIRE)

{
    "require": {
        "midtrans/midtrans-php": "2.*"
    }
}
9

Refund Transaction

Refund a transaction (not all payment channel allow refund via API) You can Refund transaction with

// Set your Merchant Server Key
\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;
5

require_once dirname(__FILE__) . '/pathofproject/Midtrans.php';

// my code goes here
1

Direct Refund Transaction

Refund a transaction via Direct Refund API You can Refund transaction with

// Set your Merchant Server Key
\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;
5

require_once dirname(__FILE__) . '/pathofproject/Midtrans.php';

// my code goes here
2

Unit Test

Integration Test (sandbox real transactions)

Please change server key and client key on

// Set your Merchant Server Key
\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;
7 to your own.

All Test

// Set your Merchant Server Key
\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;
8

Specific Test

// Set your Merchant Server Key
\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;
9

Contributing

Developing e-commerce plug-ins

There are several guides that must be taken care of when you develop new plugins.

  1. Handling currency other than IDR. Midtrans

    // Add new notification url(s) alongside the settings on Midtrans Dashboard Portal (MAP)
    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";
    0 and
    // Add new notification url(s) alongside the settings on Midtrans Dashboard Portal (MAP)
    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";
    1 currently accepts payments in Indonesian Rupiah only. As a corrolary, there is a validation on the server to check whether the item prices are in integer or not. As much as you are tempted to round-off the price, DO NOT do that! Always prepare when your system uses currencies other than IDR, convert them to IDR accordingly, and only round the price AFTER that.