In this article we are going to learn integrateing making payment via khalti's
latest e-Payment Checkout platform. There are few steps to consider when using
the khalti payment gateway integration which we will demistify in this article.
Let's start with the practical example,
Live_demo and
source_code,
and docs are available on
khalti developer.
First you need to get the secret key form the khalti test merchant panel,
here, after you successfully
signup and when you are logging in make sure that otp won't show in your actual
mail but it is 987654.
now your secret key is,
Khalti Test Credential
Setting up Backend
If you also want to implement the esewa integration you can check my previous
article.
Here is the step by step how to integrate khalti payment, we will start via
importing the required dependencies.
I also have create some interface for you if you are a typescript lover, make
sure you validate them before you use, it can be updated via khalti team in
future.
Import your actual merchant secret key!
I have initiated the payment before user clicks the pay button, you can do the
opposite as well!
here, we have create the main route which will render the payment form, and also
fetching the single product data from the server and passing it to client, but
for in your real world scenario you can fetch the actual order data from your
database.
Now, we have imported all the required components.
This is the webhook route where you can verify the payment, or do bunch of
others thing that you like.
After the user completes the payment, the success response is obtained in the
return URL specified during payment initiate. Sample of success response return
URL.
pidx - The initial payment identifier
status - Status of the transaction
Completed - Transaction is success
Pending - Transaction is in pending state, request for lookup API.
User canceled - Transaction has been canceled by user.
transaction_id - The transaction identifier at Khalti
tidx - Same value as transaction id
amount - Amount paid in paisa
mobile - Payer KHALTI ID
purchase_order_id - The initial purchase_order_id provided during payment
initiate
purchase_order_name - The initial purchase_order_name provided during payment
initiate
total_amount - Same value as amount
After a callback is received, You can use the pidx provided earlier, to lookup
and reassure the payment status.
How Payment Verification Works
To verify a payment after receiving a callback, you can use the Payment Lookup
API by passing the pidx received during the payment initiation.
Payment Verification (Lookup)
Endpoint: POST /epayment/lookup/
You must pass the Authorization header with your Live Secret Key.
Request Payload
Response
failure
Client-Side Implementation
Frontend side, We’ll render the data from the initiated payment and just have
the button to redirect user to payment_url and displaying some minor
information.
Conclusion
This blog has demonstrated how to integrate the Khalti Webcheckout with a modern
stack.