gonon/laravel-midtrans
Composer 安装命令:
composer require gonon/laravel-midtrans
包简介
Gonon Midtrans Payment Gateway Integration for Laravel.
README 文档
README
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.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-07-10