khidirdotid / midtrans-laravel
A Midtrans Wrapper for Laravel
Fund package maintenance!
Saweria
v1.0.1
2024-03-13 04:27 UTC
Requires
- php: ^8.1
- illuminate/support: ^10.0|^11.0
- midtrans/midtrans-php: ^2.5
Requires (Dev)
- nunomaduro/collision: ^7.0
- phpunit/phpunit: ^10.0
README
A Midtrans Wrapper for Laravel
Installation
-
Install the package
composer require khidirdotid/midtrans-laravel
-
Publish the config file
php artisan vendor:publish --provider="KhidirDotID\Midtrans\Providers\MidtransServiceProvider"
-
Add the Facade to your
config/app.php
intoaliases
section'Midtrans' => KhidirDotID\Midtrans\Facades\Midtrans::class,
-
Add ENV data
MIDTRANS_PRODUCTION_SERVER_KEY=Mid-server- MIDTRANS_PRODUCTION_CLIENT_KEY=Mid-client- MIDTRANS_SANDBOX_SERVER_KEY=SB-Mid-server- MIDTRANS_SANDBOX_CLIENT_KEY=SB-Mid-client- MIDTRANS_ENVIRONMENT=sandbox MIDTRANS_3DS=false MIDTRANS_APPEND_NOTIF_URL= MIDTRANS_OVERRIDE_NOTIF_URL=
or you can set it through the controller
\Midtrans::setServerKey($serverKey); \Midtrans::setClientKey($clientKey); \Midtrans::setProduction(true); \Midtrans::set3ds(true); \Midtrans::setAppendNotifUrl(route('midtrans.ipn')); \Midtrans::setOverrideNotifUrl(route('midtrans.ipn'));
Usage
Snap
- Get Snap Token
$params = [ 'transaction_details' => [ 'order_id' => rand(), 'gross_amount' => 10000 ] ]; $snapToken = \Midtrans::getSnapToken($params);
- Initialize Snap JS when customer click pay button
<button id="pay-button">Pay!</button> <pre><div id="result-json">JSON result will appear here after payment:<br></div></pre> <!-- TODO: Remove ".sandbox" from script src URL for production environment. Also input your client key in "data-client-key" --> <script src="https://app.sandbox.midtrans.com/snap/snap.js" data-client-key="{{ env('MIDTRANS_SANDBOX_CLIENT_KEY') }}"></script> <script type="text/javascript"> document.getElementById('pay-button').onclick = function() { // SnapToken acquired from previous step snap.pay('{{ $snapToken }}', { // Optional onSuccess: function(result) { /* You may add your own js here, this is just example */ document.getElementById('result-json').innerHTML += JSON.stringify(result, null, 2); }, // Optional onPending: function(result) { /* You may add your own js here, this is just example */ document.getElementById('result-json').innerHTML += JSON.stringify(result, null, 2); }, // Optional onError: function(result) { /* You may add your own js here, this is just example */ document.getElementById('result-json').innerHTML += JSON.stringify(result, null, 2); } }); }; </script>
Snap Redirect
- Get Redirection URL of a Payment Page
$params = [ 'transaction_details' => [ 'order_id' => rand(), 'gross_amount' => 10000 ] ]; try { // Get Snap Payment Page URL $paymentUrl = \Midtrans::createTransaction($params)->redirect_url; // Redirect to Snap Payment Page return redirect()->away($paymentUrl); } catch (\Throwable $th) { throw $th; }
Core API
- Create Transaction details
$transaction_details = [ 'order_id' => time(), 'gross_amount' => 200000 ];
- Create Item Details, Billing Address, Shipping Address, and Customer Details (Optional)
// Populate items $items = [ [ 'id' => 'item1', 'price' => 100000, 'quantity' => 1, 'name' => 'Adidas f50' ], [ 'id' => 'item2', 'price' => 50000, 'quantity' => 2, 'name' => 'Nike N90' ] ]; // Populate customer's billing address $billing_address = [ 'first_name' => "Andri", 'last_name' => "Setiawan", 'address' => "Karet Belakang 15A, Setiabudi.", 'city' => "Jakarta", 'postal_code' => "51161", 'phone' => "081322311801", 'country_code' => 'IDN' ]; // Populate customer's shipping address $shipping_address = [ 'first_name' => "John", 'last_name' => "Watson", 'address' => "Bakerstreet 221B.", 'city' => "Jakarta", 'postal_code' => "51162", 'phone' => "081322311801", 'country_code' => 'IDN' ]; // Populate customer's info $customer_details = [ 'first_name' => "Andri", 'last_name' => "Setiawan", 'email' => "test@test.com", 'phone' => "081322311801", 'billing_address' => $billing_address, 'shipping_address' => $shipping_address ];
- Create Transaction Data
// Transaction data to be sent $transaction_data = [ 'payment_type' => 'bank_transfer', 'bank_transfer' => [ 'bank' => 'bca' ], 'transaction_details' => $transaction_details, 'item_details' => $items, 'customer_details' => $customer_details ];
- Charge
$response = \Midtrans::charge($transaction_data);
Handle HTTP Notification
- Create route to handle notifications
Route::match(['GET', 'POST'], 'midtrans.ipn', [PaymentController::class, 'midtransIpn'])->name('midtrans.ipn');
- Create method in controller
public function midtransIpn(Request $request) { try { $response = \Midtrans::status($request->transaction_id); if (in_array($response->transaction_status, ['settlement', 'capture']) && $response->fraud_status === 'accept') { // TODO: Set payment status in merchant's database to 'success' } } catch (\Throwable $th) { throw $th; } }
- Except verify CSRF token in
app/Http/Middleware/VerifyCsrfToken.php
protected $except = [ 'midtrans/ipn' ];