PHPackages                             khidirdotid/midtrans-laravel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Payment Processing](/categories/payments)
4. /
5. khidirdotid/midtrans-laravel

ActiveLibrary[Payment Processing](/categories/payments)

khidirdotid/midtrans-laravel
============================

A Midtrans Wrapper for Laravel

v1.0.2(1y ago)033MITPHPPHP ^8.1

Since May 2Pushed 1y ago1 watchersCompare

[ Source](https://github.com/KhidirDotID/midtrans-laravel)[ Packagist](https://packagist.org/packages/khidirdotid/midtrans-laravel)[ Fund](https://saweria.co/khidirdotid)[ RSS](/packages/khidirdotid-midtrans-laravel/feed)WikiDiscussions main Synced today

READMEChangelog (3)Dependencies (4)Versions (4)Used By (0)

midtrans-laravel
================

[](#midtrans-laravel)

A Midtrans Wrapper for Laravel

Installation
------------

[](#installation)

1. Install the package

    ```
    composer require khidirdotid/midtrans-laravel
    ```
2. Publish the config file

    ```
    php artisan vendor:publish --provider="KhidirDotID\Midtrans\Providers\MidtransServiceProvider"
    ```
3. Add the Facade to your `config/app.php` into `aliases` section

    ```
    'Midtrans' => KhidirDotID\Midtrans\Facades\Midtrans::class,
    ```
4. Add ENV data

    ```
    MIDTRANS_PRODUCTION_SERVER_KEY=Mid-server-
    MIDTRANS_PRODUCTION_CLIENT_KEY=Mid-client-
    MIDTRANS_SANDBOX_SERVER_KEY=SB-Mid-server-
    MIDTRANS_SANDBOX_CLIENT_KEY=SB-Mid-client-
    MIDTRANS_ENVIRONMENT=sandbox
    MIDTRANS_3DS=false
    MIDTRANS_APPEND_NOTIF_URL=
    MIDTRANS_OVERRIDE_NOTIF_URL=
    ```

    or you can set it through the controller

    ```
    \Midtrans::setServerKey($serverKey);
    \Midtrans::setClientKey($clientKey);
    \Midtrans::setProduction(true);
    \Midtrans::set3ds(true);
    \Midtrans::setAppendNotifUrl(route('midtrans.ipn'));
    \Midtrans::setOverrideNotifUrl(route('midtrans.ipn'));
    ```

Usage
-----

[](#usage)

### Snap

[](#snap)

1. Get Snap Token ```
    $params = [
        'transaction_details' => [
            'order_id' => rand(),
            'gross_amount' => 10000
        ]
    ];

    $snapToken = \Midtrans::getSnapToken($params);
    ```
2. Initialize Snap JS when customer click pay button ```
    Pay!
    JSON result will appear here after payment:

        document.getElementById('pay-button').onclick = function() {
            // SnapToken acquired from previous step
            snap.pay('{{ $snapToken }}', {
                // 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);
                }
            });
        };

    ```

### Snap Redirect

[](#snap-redirect)

1. Get Redirection URL of a Payment Page ```
    $params = [
        'transaction_details' => [
            'order_id' => rand(),
            'gross_amount' => 10000
        ]
    ];

    try {
        // Get Snap Payment Page URL
        $paymentUrl = \Midtrans::createTransaction($params)->redirect_url;

        // Redirect to Snap Payment Page
        return redirect()->away($paymentUrl);
    } catch (\Throwable $th) {
        throw $th;
    }
    ```

### Core API

[](#core-api)

1. Create Transaction details ```
    $transaction_details = [
        'order_id' => time(),
        'gross_amount' => 200000
    ];
    ```
2. Create Item Details, Billing Address, Shipping Address, and Customer Details (Optional) ```
    // Populate items
    $items = [
        [
            'id' => 'item1',
            'price' => 100000,
            'quantity' => 1,
            'name' => 'Adidas f50'
        ],
        [
            'id'       => 'item2',
            'price'    => 50000,
            'quantity' => 2,
            'name'     => 'Nike N90'
        ]
    ];

    // Populate customer's billing address
    $billing_address = [
        'first_name' => "Andri",
        'last_name' => "Setiawan",
        'address' => "Karet Belakang 15A, Setiabudi.",
        'city' => "Jakarta",
        'postal_code' => "51161",
        'phone' => "081322311801",
        'country_code' => 'IDN'
    ];

    // Populate customer's shipping address
    $shipping_address = [
        'first_name' => "John",
        'last_name' => "Watson",
        'address' => "Bakerstreet 221B.",
        'city' => "Jakarta",
        'postal_code' => "51162",
        'phone' => "081322311801",
        'country_code' => 'IDN'
    ];

    // Populate customer's info
    $customer_details = [
        'first_name' => "Andri",
        'last_name' => "Setiawan",
        'email' => "test@test.com",
        'phone' => "081322311801",
        'billing_address' => $billing_address,
        'shipping_address' => $shipping_address
    ];
    ```
3. Create Transaction Data ```
    // Transaction data to be sent
    $transaction_data = [
        'payment_type' => 'bank_transfer',
        'bank_transfer' => [
            'bank' => 'bca'
        ],
        'transaction_details' => $transaction_details,
        'item_details' => $items,
        'customer_details' => $customer_details
    ];
    ```
4. Charge ```
    $response = \Midtrans::charge($transaction_data);
    ```

### Handle HTTP Notification

[](#handle-http-notification)

1. Create route to handle notifications ```
    Route::match(['GET', 'POST'], 'midtrans.ipn', [PaymentController::class, 'midtransIpn'])->name('midtrans.ipn');
    ```
2. Create method in controller ```
    public function midtransIpn(Request $request)
    {
        try {
            $response = \Midtrans::status($request->transaction_id);

            if (in_array($response->transaction_status, ['settlement', 'capture']) && $response->fraud_status === 'accept') {
                // 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 = [
        'midtrans/ipn'
    ];
    ```

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance42

Moderate activity, may be stable

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~343 days

Total

3

Last Release

473d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/20750742?v=4)[Khidir Zahid](/maintainers/KhidirDotID)[@KhidirDotID](https://github.com/KhidirDotID)

---

Top Contributors

[![KhidirDotID](https://avatars.githubusercontent.com/u/20750742?v=4)](https://github.com/KhidirDotID "KhidirDotID (14 commits)")

---

Tags

laravelmidtrans

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/khidirdotid-midtrans-laravel/health.svg)

```
[![Health](https://phpackages.com/badges/khidirdotid-midtrans-laravel/health.svg)](https://phpackages.com/packages/khidirdotid-midtrans-laravel)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[mollie/laravel-cashier-mollie

Laravel Cashier provides an expressive, fluent interface to Mollie's subscription billing services.

178204.3k1](/packages/mollie-laravel-cashier-mollie)[linkxtr/laravel-qrcode

A clean, modern, and easy-to-use QR code generator for Laravel

3720.4k](/packages/linkxtr-laravel-qrcode)[sebdesign/laravel-viva-payments

A Laravel package for integrating the Viva Payments gateway

4851.0k](/packages/sebdesign-laravel-viva-payments)[api-platform/laravel

API Platform support for Laravel

58171.5k14](/packages/api-platform-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
