PHP code example of gonon / laravel-midtrans

1. Go to this page and download the library: Download gonon/laravel-midtrans library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gonon / laravel-midtrans example snippets


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;

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;

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);

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);
    }
}
bash
php artisan vendor:publish --tag="midtrans-config"
bash
php artisan vendor:publish --provider="Gonon\Midtrans\Laravel\MidtransServiceProvider"