midtrans/midtrans-php
Official Midtrans PHP wrapper for Core API and Snap (including Snap-bi). Composer-ready library to create transactions, get Snap tokens, handle notifications, and process payments in sandbox or production. Configure via Midtrans\Config and start integrating quickly.
Illuminate\Events\Dispatcher). The package’s notification handler can trigger Laravel events (e.g., PaymentSucceeded, PaymentFailed) for downstream processing (e.g., order fulfillment, inventory updates).serverKey, isProduction) can be injected via Laravel’s config files or environment variables.Http facade or Guzzle (via Laravel HTTP client) can consume. Webhook handling can leverage Laravel’s route model binding or middleware (e.g., VerifyMidtransSignature).Payment, Order) for transaction tracking.signature_key). Laravel’s middleware can enforce this, but misconfiguration risks replay attacks. Mitigation: Use Laravel’s VerifyCsrfToken or custom middleware.order_id). Risk: Duplicate charges if not handled.dispatch(new ProcessPayment($payload)))?.env (e.g., MIDTRANS_SANDBOX=true)?payment_logs) for auditing?fraud_status=challenge)?Laravel Ecosystem:
composer require midtrans/midtrans-php.$this->app->singleton(Midtrans\Config::class, function ($app) {
$config = new Midtrans\Config();
$config->serverKey = config('services.midtrans.server_key');
$config->isProduction = config('services.midtrans.production');
return $config;
});
Http facade or Guzzle for API calls (e.g., Transaction::status()).POST /midtrans/webhook) with middleware for signature validation.PaymentProcessed, PaymentFailed).Frontend Integration:
token_id submission.Phase 1: Configuration
config/services.php:
'midtrans' => [
'server_key' => env('MIDTRANS_SERVER_KEY'),
'client_key' => env('MIDTRANS_CLIENT_KEY'),
'production' => env('MIDTRANS_PRODUCTION', false),
'sandbox' => env('MIDTRANS_SANDBOX', true),
],
php artisan vendor:publish --tag=midtrans-config
Phase 2: Core API Integration
namespace App\Services;
use Midtrans\CoreApi;
class PaymentService {
public function charge(array $params) {
return CoreApi::charge($params);
}
}
public function checkout(PaymentService $paymentService) {
$response = $paymentService->charge($transactionData);
// Handle response...
}
Phase 3: Webhook Handling
Route::post('/midtrans/webhook', [MidtransController::class, 'handleWebhook'])
->middleware('verify.midtrans.signature');
public function handle($request, Closure $next) {
$signature = $request->header('X-Midtrans-Signature');
$isValid = \Midtrans\Config::validateSignature($request->getContent(), $signature);
if (!$isValid) abort(403);
return $next($request);
}
public function handleWebhook(Request $request) {
$notif = new \Midtrans\Notification($request->all());
event(new PaymentWebhookReceived($notif));
}
Phase 4: Frontend Integration
<script src="https://app.sandbox.midtrans.com/snap/snap.js"
data-client-key="{{ config('services.midtrans.client_key') }}"></script>
token_id to Laravel’s backend.Phase 5: Testing
$this->post('/midtrans/webhook', [
'transaction_status' => 'capture',
'fraud_status' => 'accept',
])->assertOk();
php-curl and php-openssl (for HTTPS and signature validation).payments table for tracking:
Schema::create('payments', function (Blueprint $table) {
$table->id();
$table->string('order_id');
$table->string('transaction_id')->nullable();
$table->string('status');
$table->string('fraud_status')->nullable();
$table->json('metadata');
$table->timestamps();
});
| Step | Task | Dependencies | Owner |
|---|---|---|---|
| 1 | Add Midtrans config to .env |
- | DevOps |
| 2 | Publish config file | Step 1 | TPM |
| 3 | Create PaymentService facade |
- | Backend |
| 4 | Implement webhook route/middleware | Step |
How can I help you explore Laravel packages today?