PHP code example of gusmanwidodo / laravel-billing-midtrans
1. Go to this page and download the library: Download gusmanwidodo/laravel-billing-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/ */
gusmanwidodo / laravel-billing-midtrans example snippets
use Gusmanwidodo\Billing\Facades\Billing;
// 1. Charge an invoice via Midtrans -> pending intent with the Snap redirect URL.
$intent = Billing::charge($invoice, provider: 'midtrans');
$snapUrl = $intent->meta['redirect_url']; // send the customer here
$snapToken = $intent->meta['snap_token']; // or use with Snap.js
// 2. In your Midtrans webhook route, hand the raw body + headers to billing.
// The signature is verified before anything is recorded.
Route::post('/webhooks/midtrans', function (Illuminate\Http\Request $request) {
try {
Billing::handleWebhook('midtrans', $request->getContent(), $request->headers->all());
} catch (\RuntimeException $e) {
abort(403); // signature verification failed
}
return response()->json(['ok' => true]);
});
// 3. Refund a succeeded intent.
Billing::refund($intent);