laravel-midtrans maintained by gonon
Gonon Laravel Midtrans
A Laravel package providing a seamless integration for the strictly-typed gonon/midtrans SDK.
Requirements
- PHP >= 8.2
- Laravel 11.x, 12.x, or 13.x
Installation
composer require gonon/laravel-midtrans
Configuration
Publish the configuration file to customize the SDK settings:
php artisan vendor:publish --tag="midtrans-config"
or
php artisan vendor:publish --provider="Gonon\Midtrans\Laravel\MidtransServiceProvider"
This will create a config/midtrans.php file in your application where you can manage your server key, client key, and environment configuration.
Usage (Facade)
You can use the strictly-typed Midtrans client directly through the provided Laravel Facade. The Facade provides access to all resources exposed by the gonon/midtrans SDK.
Snap API (Payment Links)
Generate payment links or Snap tokens to be used with the Midtrans frontend popup.
use Gonon\Midtrans\Laravel\Facades\Midtrans;
use Gonon\Midtrans\DTO\Snap\CreateSnapTransactionRequest;
use Gonon\Midtrans\DTO\Shared\TransactionDetails;
$request = new CreateSnapTransactionRequest(
transactionDetails: new TransactionDetails(
orderId: 'ORDER-' . time(),
grossAmount: 150000
)
);
// Create a Snap Redirect URL
$response = Midtrans::snap()->createRedirectUrl($request);
return redirect($response->redirectUrl);
// Or create a Snap Token for frontend popup
$tokenResponse = Midtrans::snap()->createToken($request);
$snapToken = $tokenResponse->token;
Core API (Direct Charges)
If you are building your own checkout page, you can charge customers directly using the Core API.
use Gonon\Midtrans\Laravel\Facades\Midtrans;
use Gonon\Midtrans\DTO\Core\CreateChargeRequest;
use Gonon\Midtrans\DTO\Core\BankTransferDetails;
use Gonon\Midtrans\DTO\Shared\TransactionDetails;
$request = new CreateChargeRequest(
paymentType: 'bank_transfer',
transactionDetails: new TransactionDetails('ORDER-CORE-123', 50000),
bankTransfer: new BankTransferDetails(bank: 'bca')
);
$response = Midtrans::charge()->charge($request);
echo $response->transactionId;
echo $response->transactionStatus;
Transaction Management
You can easily manage your existing transactions by retrieving their status, canceling them, or approving/denying them.
use Gonon\Midtrans\Laravel\Facades\Midtrans;
$orderId = 'ORDER-123';
// Check transaction status
$status = Midtrans::transactions()->status($orderId);
echo $status->transactionStatus;
// Cancel a transaction
Midtrans::transactions()->cancel($orderId);
// Approve a challenged transaction
Midtrans::transactions()->approve($orderId);
// Deny a challenged transaction
Midtrans::transactions()->deny($orderId);
// Force expire a pending transaction
Midtrans::transactions()->expire($orderId);
Notifications (Webhooks)
Midtrans will send asynchronous HTTP notifications to your server. The Facade provides a secure NotificationParser that automatically validates Midtrans SHA512 signatures using your configured Server Key.
use Gonon\Midtrans\Laravel\Facades\Midtrans;
use Gonon\Midtrans\Exceptions\NotificationException;
use Illuminate\Http\Request;
public function handleWebhook(Request $request)
{
try {
// Automatically parses and strictly validates the signature
$notification = Midtrans::notifications()->parse($request->getContent());
$orderId = $notification->orderId;
$status = $notification->transactionStatus;
if ($status === 'settlement' || $status === 'capture') {
// Safe to mark the order as paid in your database!
}
return response()->json(['message' => 'OK']);
} catch (NotificationException $e) {
// The signature was invalid or the JSON was malformed.
// Do NOT process the order.
return response()->json(['error' => 'Invalid Signature'], 403);
}
}
License
The MIT License (MIT). Please see LICENSE for more information.