PHP code example of blissjaspis / laravel-midtrans
1. Go to this page and download the library: Download blissjaspis/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/ */
blissjaspis / laravel-midtrans example snippets
use BlissJaspis\Midtrans\Exceptions\MidtransApiException;
use BlissJaspis\Midtrans\Facades\Midtrans;
class YourController
{
// ...
public function chargeBankTransfer()
{
try {
return Midtrans::bankTransfer()->charge([
'transaction_details' => [
'order_id' => 'order-123',
'gross_amount' => 10000,
],
'bank_transfer' => [
'bank' => 'bca',
],
]);
} catch (MidtransApiException $e) {
// $e->statusCode(), $e->validationMessages(), $e->responseBody()
report($e);
abort(502, $e->getMessage());
}
}
public function chargeQRIS()
{
return Midtrans::qris()->charge([
'transaction_details' => [
'order_id' => '1234567890',
'gross_amount' => 10000,
],
'item_details' => [
[
'id' => '1234567890',
'price' => 10000,
'quantity' => 1,
'name' => 'Product',
],
],
'customer_details' => [
'first_name' => 'John',
'last_name' => 'Doe',
'email' => '[email protected]',
'phone' => '081234567890',
],
'qris' => [
'acquirer' => 'gopay',
],
]);
}
public function handleNotification()
{
$payload = request()->all();
if (! Midtrans::isValidNotificationSignature($payload)) {
abort(403, 'Invalid Midtrans signature.');
}
// Handle notification...
}
public function refundTransaction()
{
Midtrans::gopay()->refundTransaction('order-id-123', [
'amount' => 50000,
'refund_key' => '1234567890',
'reason' => 'Item out of stock',
]);
// Or refund without knowing the payment type:
Midtrans::refundTransaction('order-id-123', [
'refund_key' => 'my-refund-key',
'amount' => 50000,
'reason' => 'Item out of stock',
]);
}
public function cancelTransaction()
{
Midtrans::creditCard()->cancelTransaction('order-id-123');
Midtrans::cancelTransaction('order-id-456');
}
public function translateTransactionStatus()
{
return Midtrans::translateTransactionStatus('capture');
}
public function translateFraudStatus()
{
return Midtrans::translateFraudStatus('accept');
}
}