Setting Up the Webhook Endpoint
The KarmaGo package provides a simple way to handle payment gateway webhooks securely and efficiently. Thepayments.KarmaPayWebhook() function integrates with GoFiber to validate incoming webhook requests and execute a custom action function when the webhook is verified.
Prerequisites
Before setting up the webhook endpoint, ensure:-
Environment Variables are Properly Configured
Set the
KARMAPAY_WEBHOOKvariable in your.envfile. For example: -
Webhook Secret
The
WEBHOOK_SECRETvariable must be configured in your environment for secure webhook validation. For example: -
Webhook URL Registered with Payment Gateway
Ensure that the
KARMAPAY_WEBHOOKURL is registered with the payment gateway you’re using (e.g., Razorpay, Stripe, etc.).
Creating the Webhook Endpoint
Thepayments.KarmaPayWebhook() function takes a custom action function as input. This action function is executed when the webhook is verified successfully.
Example Code
Here’s how to set up the webhook endpoint in your Go application:How It Works
-
Webhook Route Handling
- The
/webhookroute listens for POST requests from the payment gateway. - This should match the URL you set in the
KARMAPAY_WEBHOOKenvironment variable.
- The
-
Verification and Execution
- The
KarmaPayWebhook()function validates the incoming webhook request using theWEBHOOK_SECRET. - If the request is verified successfully, the custom action function (
WebhookHandlerin this example) is executed.
- The
-
Automatic Parameters
- The webhook payload automatically includes a
koidfield (the Order ID) added by the KarmaPay service. - You can use
koidto retrieve the relevant booking or order.
- The webhook payload automatically includes a
-
Custom Action Function
- The
WebhookHandlerfunction takes amap[string]stringcontaining the webhook data. - You can define custom logic, such as updating booking statuses or triggering other workflows, based on the webhook data.
- The
Example .env File
Here’s an example .env configuration for the webhook:
Testing the Webhook
-
Simulate a Payment
Use the
CreatePaymentOrderfunction to generate a payment URL and complete a transaction. -
Trigger the Webhook
The payment gateway sends a POST request to the
/webhookendpoint after the payment is completed. -
Verify the Results
Check that the
WebhookHandlerfunction executes correctly and performs the desired action (e.g., updating the booking status).
Notes for Beginners
- Webhook Verification: The
KarmaPayWebhookfunction automatically verifies the webhook signature usingWEBHOOK_SECRET. You don’t need to write custom verification logic. - Action Function Flexibility: The action function can perform any custom operation you define, as long as it matches the signature
func(data map[string]string) error.
Troubleshooting
- Webhook Not Triggering: Ensure the payment gateway has the correct URL and is configured to send webhook events.
- Signature Mismatch: Verify that
WEBHOOK_SECRETmatches the secret configured in the payment gateway. - Action Function Errors: Check the logs for errors in the custom action function.