Cara Menguji Webhook Midtrans Di Localhost Menggunakan Laragon Dan Ngrok

3 min read 5 hours ago
Published on Oct 31, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial provides a step-by-step guide on how to test webhooks for integrating with the Midtrans payment gateway using Laragon and Ngrok. By following this guide, you can effectively manage payment notifications in a local development environment using the Midtrans API sandbox.

Step 1: Set Up Laragon

  1. Download and Install Laragon

    • Visit the Laragon website and download the latest version.
    • Run the installer and follow the prompts to install Laragon on your system.
  2. Create a New Project

    • Open Laragon and click on the "Menu" button.
    • Select "Quick App" and choose a framework (e.g., Laravel).
    • Name your project (e.g., midtrans-webhook-test).
  3. Start the Laragon Server

    • Click the "Start All" button in Laragon to launch the local server.

Step 2: Configure Midtrans API

  1. Create a Midtrans Account

    • Go to the Midtrans website and sign up for an account.
    • Access the sandbox environment for testing by logging into the Midtrans dashboard.
  2. Generate API Keys

    • In the Midtrans dashboard, navigate to the "Settings" section and select "API Keys".
    • Copy the server key and client key for your project.
  3. Set Up Webhook URL

    • In the Midtrans dashboard, find the option for "Webhook" settings.
    • Enter the Ngrok URL (which you will create in the next step) as the webhook URL.

Step 3: Install and Configure Ngrok

  1. Download Ngrok

    • Visit the Ngrok website and download the application for your operating system.
  2. Install Ngrok

    • Extract the downloaded file and move the Ngrok executable to a folder accessible via the command line.
  3. Start Ngrok

    • Open your command prompt or terminal.
    • Navigate to the folder where Ngrok is located.
    • Run the following command to start Ngrok:
      ngrok http 80
      
    • Ngrok will provide you with a public URL that tunnels to your localhost.

Step 4: Develop and Test Your Webhook Handler

  1. Create a Webhook Endpoint

    • In your Laragon project, create a new route in routes/web.php:
      Route::post('/webhook', 'WebhookController@handle');
      
  2. Create the Webhook Controller

    • Create a new controller using Artisan:
      php artisan make:controller WebhookController
      
    • Implement the handle method to process incoming webhook requests:
      public function handle(Request $request) {
          // Process the webhook data
          $data = $request->all();
          // Log or handle the data as required
          Log::info('Webhook received:', $data);
      }
      
  3. Test the Webhook

    • Use the Midtrans simulator to send test notifications to your webhook URL (the public URL provided by Ngrok).
    • Check your Laragon log or database to confirm that the webhook was received and processed correctly.

Conclusion

In this tutorial, you learned how to set up Laragon and Ngrok to test Midtrans webhooks locally. You configured your Midtrans account, created a webhook handler in Laravel, and verified its operation using the Ngrok tunnel.

Next steps could include further testing with different payment scenarios and integrating the webhook responses into your application logic. Make sure to explore the Midtrans documentation for more advanced features and configurations.