Getting Started with KarmaPay Integration
KarmaPay simplifies online payment integrations by providing a unified API to interact with multiple payment gateways such as Razorpay, Stripe, and PhonePe. Follow this guide to set up and start using the KarmaPay package in your Go project.Prerequisites
Before you begin, ensure you have the following:- Redis KarmaPay relies on Redis for managing order states temporarily. Ensure Redis is installed and accessible in your environment.
- Self-host KarmaPay PG Service Clone and self-host the KarmaPay PG Service. This service acts as the backend for managing payment orders. Configure it with proper Redis environment variables.
-
Environment Variables
Set up the required environment variables in your Go project. Here are the variables and their purposes:
Example
Environment Variable Description KARMAPAY_API_KEYAPI key encoded in Base64 (prefixed with kp_).KARMAPAY_WEBHOOKThe webhook endpoint URL that you configure for payment gateway callbacks. KARMAPAY_VERIFY_URLURL used to verify the payment after completion. KARMAPAY_PG_ENUMThe payment gateway you want to use ( RAZORPAY,STRIPE, orPHONEPE).KARMAPAY_IDENTIFIERYour project name for identification purposes. KARMAPAY_APP_DOMAINVerified domain for the payment gateway (Razorpay, Stripe, or PhonePe). WEBHOOK_SECRETSecret key for validating webhook calls. REDIS_URLRedis connection string for managing order states. .envfile:
Installation
Install the KarmaPay package in your Go project:Creating a Payment Order
To create a payment order, use thepayments.CreatePaymentOrder() function. This function generates a dynamic payment URL that redirects users to the payment gateway.
Parameters
Thepayments.CreatePaymentOrder() function accepts the following fields:
- OrderAmount (
int32): Total amount for the payment. - OrderCurrency (
string): Currency code (e.g.,INR). - OrderDescription (
string): Description of the order or purpose of payment. - OrderMode (
string): The payment gateway enum (RAZORPAY,STRIPE,PHONEPE) set in your environment variables. - WebhookURL (
string): The webhook URL to handle payment gateway callbacks. - RedirectURL (
string): A URL where users are redirected post-payment (optional). - Registration (
string): Set to"no"if registration is not required.
Example Code
Here’s how you can create a payment order in your Go application:Next Steps
-
Verify Payment
Once the payment is completed, use the
payments.VerifyPayment()function to validate the transaction. - Handle Webhooks Implement the webhook handler in your project to process real-time payment notifications from the payment gateway.
- Explore More Refer to the KarmaPay GitHub repository for advanced usage and additional configurations.
Troubleshooting
If you encounter any issues, check:- Redis connectivity (
REDIS_URL). - Proper configuration of environment variables.
- Correct hosting of the KarmaPay PG Service.
- Gateway-specific requirements (e.g., domain verification).