PhonePe Payment Gateway Integration Using NodeJs | ExpressJs | PhonePe API Integration | English

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends welcome to the standup coder I am viic Tucker and in this session we will explore how to integrate the phone pay payment Gateway using nodejs and expressjs so uh first let's begin by referring to the phone pay API documentation so inside the documentation uh they have given the following steps to integrate the checkout page on the Merchant's website so the first step is initiating payment request the second step is redirecting user to complete the payment and the third step is reacting user to the merchant web page again and fetch the status so I've made a flow diagram here as you can see from the merchant website we will initiate a payment request in the response we will get the checkout URL to which we will redirect which will open a checkout page of the phone pay where the user user can complete its payment and then once the payment is completed uh it will re to the merchants red URL and on this page we will uh like fetch the status of the payment and show the message whether it's success or in pending state or in fail State whatever status the payment is in okay so let's start by uh in initializing a Express app node application so to start with first let's create an Express server so we will do npm in it for the express we will need a dependency of Express so let's install Express and here there should be starting points we will create index.js file and inside package let's write a script to start be node index.js let's include the dependency here and text Press app will be running on some Port so let's define a port here I won't be going into much detail as setting up an Express server is quite easy so I guess you already familiar with how the setup works as you can see our app is up and running at Port 3002 okay and we can test that by going to uh the SL route th000 SL yeah phone pay so Express Ser is up and running now okay now let's refer the documentation as which API should be integrate to initiate a payment request UA testing okay uh so for testing purpose they have provided one URL on which we can test our phone pay integration so let's copy this host URL here and paste it in a code in as a constant phone P host URL is equal the next thing is the merchant ID uh for production we need to uh contact with the phone pay integration team to get the merchant ID but for testing purpose we can use this merch ID so let's add it in the code code and regarding the salt Keys also uh for the production access we will have to contact phone pay integration team but for testing purpose we can use this for salt key index and actual salt key salt index here the salt index is one [Music] and Sol key is is this key okay so these are the four variables which are required by phone pay for testing purpose testing purpose okay now the apis which we need to integrate are I think that this will be present in the integration steps API integration here so the steps to integrate will be first we need to initiate a payment request so for that the merchant does a Ser to call to initiate a payment request so our server will be calling the phone pay server to initiate a request and the response URL for that particular transaction return for the Forman payment Gateway as we saw here when we initiate a payment uh URL will will be received in the response of that request and that URL can be used to redirect the user to the checkout page so let's integrate this API first into our Express server and for that API we need to refer here so this is the API which we need to integrate uh for the uat host we already added the host URL here on which we need to call that okay so let's start by making an API so we will uh create a get request itself because for testing purpose we can create a gr request so that it can be called directly from the browser window so let's create an API p with the name pay to initiate a request then point of this URL is p slv1 SLP do this let's add this phone penpoint and this is a post request so we'll be making a post request to this URL and let's check out what all details it will need so for the request headers we will need a content type as application Json X verify and also there's a sample request they have provided so we can copy the whole request con xus okay let's copy it uh they have installed a dependence of exos to make the API calls we can install [Music] xos and the options the me for URL you already have the URL added in the constant so we can replace it here with the constant and the remaining part we havea so we can replace that variable okay accept will be application Json we will be expecting a Json format as they mentioned here okay I think that's it next will be uh these are the parameters request parameters which we will need to make the request uh it contains merchant ID merchant ID we have already specified here for testing it will be PG test pay U Merchant transaction ID it's uh generated by generated by the merchant it means we will generate a merchant ID that will be passed here the amount will be in passive Merchant user ID will be the user ID uh the user who is initiating the payment from the Merchant's website redirect URL the URL the user will of the transaction completion so the redirect URL will be our own end point which the user will be redirected to after they completed completed the payment so here once the payment is completed it will be redirected to a particular redirect URL so we will have to specify our own URL here and all those things so this is a simple uh request they have provided us so let's copy it and we can paste it here const payload is equal to the merchant ID is this so we can replace this variable Merchant transaction ID will be a unique ID created by Merchant's website for each transaction okay so to do that let's create a unique ID every time this API is called so we can uh use a library uh to create unique ID every time so it's Unique ID uh let's import that here okay con Merchant transaction ID is equal to U ID uh every time a request is called this will create a unique ID Merchant transaction ID and we will pass your merch user ID for for testing purpose let's keep the user ID as 1 2 3 we will pass this user ID here this is the amount in pass suppose you want to make a payment of 300 rupees then we'll multiply it by 100 to convert into P redirect URL is the URL which will be called once the transaction is completed so let's give it as Local Host 3002 as our server is running on 3002 redirect URL SL let's add Merchant transaction ID as well because that is a unique identifier for each transaction and since uh the user will be redirected to this URL we need a transaction ID to get the status of that particular payment so let's pass this ID here as a path param okay okay uh call back we don't need call back URL here phone number can be any any number of the user which who is like initiating a payment and payment Ty is pay page okay so our payload is ready okay so this is done now let's Che the documentation so what all things do we need here uh X verify so in X verify we will need this particular thing so let's copy it const X verify is equal to we will knit this particular thing to be passed in X verify headers so first like we need to convert this payload into base 64 format and then do s 256 encoding on all the three pay and go to payload the endpoint and the salt key and then add triple HH plus Sol index to make the actual X verify key so let's do it so first we need to create a buffer object okay so the formula for buffer object is buffer Dot from and let's convert our payload into stringified format comma qdf it okay now converting into B 64 encoded string the formula is converting the buer object to base 64 so this we have base 64 encoded payload we can name it as payload also payload okay now the X verify will be sh a 256 to use this we need to install the dependency of ss 256 and import it in a code let's do this okay s24 and inside that we need a piced payload which is this plus we need the URL here which we have stored in this variable P end point plus we need a salt key so for testing purpose the salt key is this so we will pass it here and then we need to add triple hash then we need salt index which is here for testing purpose and here we go the X verify is created here okay and we need to pass this x verify in this variable X verifier so let's pass it in the headers XY okay and so now we need to pass the whole payload into the body and convert the J B 64 ined payload the B request payLo should be converted to B 64 inquired per then request should be sent in the below format okay so we need to pass uh our P 64 enquired payload as the value of the request key so let's pass it here in the data you need to pass request key with the value as base 64 encoded payload so this is the basics to ened payload and we'll pass it here so guess this code should work and it should return the response with the data in this format you haven't send the response to this API so let's change the go to response. send response. data now let's start the code and run it again yeah so here we got the response from the initiate payment request let's check yeah so in the response we have got Success Through payment initiated in the data we have received a merchant ID Merchant transaction ID and type in R direct info URL this is the URL uh to which we need to Reed the user to to make the payment so to do that let's do it here const URL is equal to response. dat do data do instrument response. redir info do redirect info do URL let's print the URL like what is the URL we are getting okay let's do it again so this is the URL we are getting if we visit this URL we will get the phone pay checkout page where the user can enter details and complete the payment process so instead of going it like using the URL manually we can directly redirect the user to this URL using our API itself so we can do it response. redirect URL okay now restart the server so as soon as you as the user will go to this API a phone pay checkout page will open and user can pay through this check out page now there are some testing credentials which they have provided which we can use here so in the U testing these are some testing credentials which you can use to do the payment in the testing environment so let's copy this credit card uh card credit card let's give it my name uh 06 2027 06 2027 CVB 508 pay and the OTP will be 1 2 3 4 5 6 5 6 pay so once the payment is completed uh the checkout page redirected uh the user to this particular URL so this is the same URL which we have mentioned here which you mentioned here as a redirect URL so this Z Z1 ah20 this code is the merchant transaction ID which we have created randomly for that particular request so this API got called and this give the error cannot get as we have not defined this route in our server so let's create this particular API in a server so that we can get the status of that particular payment using this Merchant transaction ID okay so let's create one API here API do get redirect URL now to get the value of merchant transaction ID we can get it from request. parents so cons transaction ID if mer transaction ID and we can response on send transaction ID else let's try this uh if you're getting the merchant transaction ID or not so let's initiate the payment again loog L 2000 P okay uh so for testing purpose we can add any of the uh any value of month and year the only criteria is the year should be greater than the current year or the date should be greater than the today's State CVV anything you can put password anything P so once the payment got completed it got redacted to this URL and our API is working fine we got the merchant transaction ID now let's get the status of the payment we have done so we need to refer the API docs again to get which API we need to call so in the API integration uh there's an API to check the status uh okay so this is the API so here we already have a sample request you provided by phone pay so we can copy this and we can put it inside our a API we already have the import statement at the top phone pay [Music] URL we can do this merchant ID merchant ID we have it in the constant so we can replace it here and Merchant transaction ID we got it from the request parms so we can copy that here M this is the API this is ready and we need to change the accept AS application Json as we are passing in the Json this is a get request and what else do we need here in the content tab we need application Jon we have passed application Jon okay in the X verifi we need the same thing thing again but with a different URL I guess okay and next merchant ID using the merchant by phone pay okay so let's add this x merchant ID in our headers X merchant ID will be the merchant ID assigned to the merchant by phone pay so this is the merchant transaction ID and we also need X verify in the headers this x verify and const X verify is equal to what is the formula for sh 256 this is the formula to Cate X verify we need sh 256 sh 256 uh okay we need this URL to be appended here okay merchant ID Merchant transaction ID okay plus the Sol key plus triple HH plus solt index we got X verify and we will be passing it in the ADD and let's respon let's send the response as response. data okay so this I guess will return the response as similar to this object with a payment success or payment failure or payment error whatsoever the payment status is so let's try it out let's run the server again and let's go to let's call the P API here payment got initiated we will add a c details to it 36 1 2 3 1 2 3 4 5 6 P so the payment flow is completed and this will DCT us to redact URL and here we are getting Merchant transaction ID [Music] okay okay so we have commented this that's why we got the same response back okay let's start the server and call the P API to initiate the payment payment got initiated we entered the card details 1 2 3 4 5 6 our payment flow got completed now it will redirect us to this and we got our response as this object we can check a response here so a payment is successful your payment is successful data we got all the details the amount the state Response Code and the payment details through which the payment was done now we can add conditions here for uh for example uh if uh response data. code which is this is payment success is equal to equal to payment success then you can redirect the user to uh re the user to front end success page else else you can check the documentation uh if the payment is error if uh payment. error then you can redirect the user to frontend error page else you can uh show the pending page like the pending is in process and in the background you can make an API calls every 1 minute or 2 minute and get the status of the payment whether it is completed whether it is success or error so this is the whole flow of uh uh the phone pay payment Gateway so a quick summary so uh as soon as uh a user wants to make a payment so it will initiate a payment using the using this API 3,002 pay so this will uh so this will uh the initiating payment URL will return a checkout URL uh we will redict the user to this checkout URL which will open a checkout page where the user can uh make make the payment and once the payment is completed the phone pay will redirect the user to the merchants redirect URL which in turn will get the status of the payment done and if the status is Success we will internally react the user to a success page of the front end and if it's an error stage we can redirect the user to the front end error stage so that's the whole process of the payment Gateway integration so that's it for this video you can find the code on GitHub and detailed insights in the medium article both the links I've mentioned in the description below your feedback is crucial so drop your thoughts in the comments don't forget to like subscribe and hit the bell for more content happy cing and I will see you in the next one thank you
Info
Channel: The Standup Coder
Views: 3,735
Rating: undefined out of 5
Keywords: phonepe payment gateway, phonepe payment gateway nodejs, phonepe api integration, phonepe api integration nodejs, phonepe integration nodejs, phonepe integration, payment gateway integration nodejs, payment gateway integration, phonepe checkout, phonepe nodejs, phonepe expressjs, phonepe nodejs integration, phonepe javascript
Id: Jcfo-j5y0rM
Channel Id: undefined
Length: 26min 57sec (1617 seconds)
Published: Mon Feb 26 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.