Integrating Midtrans Payments in Laravel

Rizky Purnawan Dwi Putra
3 min readApr 19, 2024

--

Photo by Tech Daily on Unsplash

In the rapidly evolving world of web development, integrating a robust payment gateway is crucial for any e-commerce or online transaction-based application. For developers using Laravel, one of the most popular PHP frameworks, integrating with Midtrans, a leading payment solution in Indonesia, offers a seamless way to handle transactions. This article provides a comprehensive guide on how to integrate Midtrans with a Laravel application, covering the key steps and best practices.

What is Midtrans?

Midtrans is a payment gateway service that offers a wide range of payment methods including credit cards, bank transfers, and e-wallets, making it a versatile choice for businesses targeting the Indonesian market. It provides a secure, reliable, and easy-to-use platform for processing payments.

Why Use Laravel for Payment Integration?

Laravel is known for its elegant syntax, robust features, and a strong ecosystem, making it an ideal choice for building complex applications. Its built-in security features and extensive community support make it particularly suited for projects that involve sensitive data like payments.

Prerequisites

Before diving into the integration, ensure you have the following:
- A Laravel application setup.
- Composer installed on your system.
- Midtrans account and access to the API credentials (server key and client key).

Step 1: Install Midtrans Package

To start, you need to include the Midtrans SDK in your Laravel project. You can do this via Composer:

composer require midtrans/midtrans-php

This command installs the Midtrans PHP SDK, which provides a convenient way to interact with the Midtrans API.

Step 2: Configure Midtrans in Laravel

After installation, set up your Midtrans configuration. Create a config file in `config/midtrans.php` and add your server key and client key:

return [
'server_key' => env('MIDTRANS_SERVER_KEY', 'your-server-key-here'),
'client_key' => env('MIDTRANS_CLIENT_KEY', 'your-client-key-here'),
'is_production' => false,
'is_sanitized' => true,
'is_3ds' => true,
];

Add these keys to your `.env` file to keep sensitive data secure.

Step 3: Create a Payment Controller

Create a controller that will handle the payment logic:

php artisan make:controller PaymentController

In `PaymentController`, set up methods to handle payment transactions. For example, creating a charge:

use Midtrans\Config;
use Midtrans\Snap;

class PaymentController extends Controller
{
public function __construct()
{
Config::$serverKey = config('midtrans.server_key');
Config::$isProduction = config('midtrans.is_production');
Config::$isSanitized = config('midtrans.is_sanitized');
Config::$is3ds = config('midtrans.is_3ds');
}

public function createCharge(Request $request)
{
$params = [
'transaction_details' => [
'order_id' => rand(),
'gross_amount' => $request->amount,
],
'credit_card' => [
'secure' => true
],
'customer_details' => [
'first_name' => $request->first_name,
'last_name' => $request->last_name,
'email' => $request->email,
'phone' => $request->phone,
],
];

$snapToken = Snap::getSnapToken($params);
return response()->json($snapToken);
}
}

Step 4: Handling Payments on the Frontend

On the frontend, use the snap.js library provided by Midtrans to handle the payment form and submission:

<button id="pay-button">Pay with Midtrans</button>
<script src="https://app.midtrans.com/snap/snap.js" data-client-key="{{ config('midtrans.client_key') }}"></script>
<script>
document.getElementById('pay-button').onclick = function(){
snap.pay('{{$snapToken}}', {
onSuccess: function(result){ /* Handle success */ },
onPending: function(result){ /* Handle pending */ },
onError: function(result){ /* Handle error */ }
});
};
</script>

Conclusion

Integrating Midtrans with Laravel simplifies the process of adding payment functionalities to your application. By following the steps outlined above, developers can leverage Midtrans’s powerful features to offer a smooth payment experience. Remember to test thoroughly in a development environment before going live to ensure everything works as expected. Happy coding!

--

--