Build a Paypal Payment Gateway in Node.js & Express to Sell Digital Goods as Attachment in Browser
Jan 9, 2025
Buy the full source code of application here:
https://procodestore.com/index.php/product/build-a-paypal-payment-gateway-in-node-js-express-to-sell-digital-goods-as-attachment-in-browser/
Show More Show Less View Video Transcript
0:00
uh hello guys welcome to this video so in this video we will try to integrate Paypal payment Gateway inside your
0:08
nodejs and express application right in the browser itself so we will try to sell digital Goods after the user has
0:15
made the payment and they will be automatically be redirected to the download page where you can download any
0:21
digital good let's suppose you're are downloading a PDF file image file any source code that you are selling so it's
0:27
still a complete tutorial so let me just start from scratch and if I open this inside incognito window and if I open
0:35
the homepage of the application Local Host
0:41
8,000 so now you will actually see the homepage of PayPal and here you will be
0:47
allow to the user to let me just open the zoomit application so that I can
0:52
show you so now you can see we have used tailin CSS this is our pay pay with
0:59
PayPal button and this is actually the homepage so as you click this button you
1:04
will be redirected to PayPal so here you need to enter the email address and
1:09
password so I'm here inside sandbox mode we are in testing mode so PayPal does
1:15
provide you with the test email accounts so one such email account you can create
1:20
inside ppal dash dashboard so I've already created this let me show
1:25
you how to pay so I basically providing a PayPal sandbox
1:31
account and then next and here you need to provide the password so I will simply
1:37
paste the password right here and log in so now it will have your virtual wallet
1:44
and you will basically see $1 the payment amount is that and it will translate this to your local currency
1:52
and it will simply continue to review order so once you make the payment you will be
1:58
redirected to the download page so here you will see we have this download page
2:04
and now the user can simply download their file so this is actually the PDF file that we are selling so once they do
2:11
this they can view the digital product which is actually a PDF file
2:17
so let's suppose the user
2:25
actually so let me show you the second scenario let's suppose the user pays and
2:33
uh once they make the payment let me just show you with the same email
2:41
address so once they make the payment they will be redirected to the download
2:46
page but before that if you want to directly go to that route if I go to this route I can't access it
2:54
download you can just see it will redirect me to the pay payment page I can't access without payment I can't go
3:02
to slash download that route is protected so once you make the payment
3:07
after that automatically you will be redirected to the download page where you can simply download your digital
3:13
product so now you can just see that now we are successfully redirected to the download page so here we can simply
3:21
download the file so let's suppose if you close the tab for some reason let's suppose accidentally the user closed
3:27
this tab you can even go to the same page if you open the homepage right here
3:32
it will detect that the user has made the payment it will store that information in the session variable and
3:37
you will be redirected to the login uh download page automatically so the application will automatically
3:42
redirected to so so this information is saved in the session variable so again
3:49
if you close it if you try to access it you will be redirected to the download page so this is really useful because we
3:58
don't want the user to access accidentally if they close the tab still
4:03
if they visit the website they will be redirected the application will detect that the user has made the payment and
4:10
that's why it will be showing the download page and I I'm just selling
4:17
this PDF file you can just sell anything you just need to move this in the public
4:22
folder so instead of this you can sell any digital good so now let me show you
4:28
the this one PayPal account from which we are actually buying it so this is actually a Sandbox account so you can
4:34
just see it actually tells you from which store you are buying so John do test store and $1 you can just click on
4:42
this transaction to get more additional information about this transaction such as uh which Visa card that you are using
4:49
which uh this is a seller information email address purchase details so everything is there and also if you want
4:56
to view the business account from which the merchant account you are selling it this is actually the sandbox account for
5:03
the merchant so here once you get that uh payment you will get a notification
5:08
that you got the payment from this person which is of $1 so let's suppose if I change the code here let me change
5:16
the price let me change the person name to show you so here you can customize
5:21
everything you can change the item name price as well let's suppose instead of $1 let me make it $100 and you can even
5:29
change a currency as well so PayPal supports I think 15 to 20 currencies around the world you
5:35
can uh do this change the currency as well and uh you can change the
5:42
description about the product you can change the let's suppose if I show
5:47
source code I change the name
5:58
here so so if I make this change right here and once again try to purchase it
6:04
and if I open the same route Local Host [Music] 8,000 click on this
6:20
button some error is there uh invalid
6:26
request okay whenever you are providing your or I think
6:35
this okay this needs to be item this is actually a name
6:42
property so whatever you are selling you need to change this
6:50
information I have just changed the price here from $1 to
6:57
$100 so here we need to go to the index. HTML file so whatever you are selling
7:04
here you need to change
7:09
it uh I think let me make this change right here
7:15
and refresh
7:30
uh guys I figured out the error the error was pretty simple so right here if you are changing the price let's suppose
7:37
I changed it from 1 to 100 so this will be the name here you can just change it
7:43
so source code of app so right here inside the amount object
7:51
here also this Total Property needs to be changed so this is actually the price that you have said 100 you also need to
7:58
change it here also from 1 to 100 and right at the bottom side you have the
8:04
success call back here also the amount is there you need to change the total property so whatever the price that you
8:11
are setting you need to set the total property to be like this so now if you make this change and again if you try
8:19
to buy this if I open
8:24
this so click on that and here you again need to log in with your details so let
8:30
me copy the email address paste it and then if I paste the
8:36
password as well so now you will see instead of $1
8:44
we will be charging the user $100 you will see that price is changed and click on continue so once
8:52
you continue you will see you will be redirected to the download page and now
8:57
you can download your digital good save it so if you now see the
9:05
account right here if I show
9:12
you we again need to there is a personal account this is a business account so
9:18
you need to go to this URL sandbox
9:27
paypal.com so instead of for $1 it will be
9:35
charging you can log in with this details so
9:46
here so these are the different accounts are there let me log in with the business
9:53
account so after some time you will be log out automatically so you again need
9:59
to login with your details so this is actually the business account
10:05
here and uh I just need to paste the password right
10:19
here so you can just see you are currently in sandbox
10:28
mode
10:58
for e
11:31
so now if you see guys you will see you have been charged this is the business
11:37
account you have been getting this payment here of $100 from this user
11:45
joho so now if you see the price has been changed same goes with the personal
11:52
account as well if you log in with these details if you go to this uh URL
12:05
login with this email
12:12
address and this is actually the
12:20
password so now if you see $100 has been cut
12:25
so if you want to see more additional information about it is transaction so you will actually see the price you will
12:33
see the title of the app is product is been changed from source
12:39
code of app and we are charging 100 down so all this is customizable in the code
12:44
and I will be showing you in this video step by step on how to integrate PayPal inside your app to actually sell this
12:51
digital good so all the source code is given in the description of this video
12:56
you can go to my website Pro Cod store.com and directly purchase the full source code of this application so a zip
13:03
file will be there after you make the payment you will be getting the zip file from Google Drive so this will actually
13:09
have the nodejs express code and also the HTML code as well so full directory
13:14
structure will be available the link is given in the description so now let's get started by building this application
13:21
uh now to get started guys we are starting it from scratch if you see no file is present so first of all we do
13:28
need to initialize the the package.json file by npm n-y so simply execute this and you will
13:36
actually see a package.json file will be created with the default options now we
13:41
need to install some modules so first module we need to install is
13:47
Express which will be the backend server which we will be writing the application
13:52
Express and second we will need to install payal rest sdga so we are
13:59
integrating PayPal right here so this actually has PayPal functionality so just install
14:07
this module as well payal rest SDK and then we also need to
14:13
install Express session so Express session will handle
14:19
the session details for us so once the user makes the payment we will actually store all that information in the
14:25
session so that the user can download the digital product after that so this is required
14:33
that's all that is required for this application these three dependencies so simply install them so now we will
14:41
actually create a index.js file so this will actually be the
14:48
starting point of the application so we need to require the express you need to make a simple
14:55
Express app and start that application on port
15:01
number
15:08
5,000 server is listing on Port
15:14
5,000 now we also need to make a public directory where we will be storing all
15:19
the static assets so just make a index. HTML file right here which will be the
15:25
file that we will show to the user so also include the path module as well
15:31
which is a built-in module and now whenever the user goes to the homepage we will simply
15:39
say we will show the index. HTML file so you'll simply say response. send file
15:47
path.
15:53
join index. HTML so whenever users goes to the homepage we are showing this
15:59
index. HTML file so here we can simply sh payal login
16:11
page so if you need to start this application simply nodeb index.js it
16:18
will start this on Local Host 5000 if you try to go to it to look
16:25
something no such file or directory okay it is present in the public
16:30
directory so we do need to write here public and then index.
16:39
HTML in single codes because it is present in the public directory so we do
16:44
need to do this now you can see PayPal is showing so we are on the
16:51
homepage so now we need to construct this homepage and for this we will be
16:56
making use of Tailwind CSS so this will be really
17:05
simple instead of this H1 tag just delete this
17:13
and we will be including the CDN link for Tailwind CSS so we have
17:20
included this so after this we need to Simply say class Flex items Center
17:27
justify Center minimum height
17:33
screen and background color of gray
17:39
100 so inside the div we will have a background of
17:50
white padding will be six and it will be rounded it will have a shadow of medium
17:57
and it will be placed in the center position and we will have an H1 tag right
18:06
here text 2 XEL font bold margin bottom
18:12
four you will say that Pap payments page so if you refresh now it will look
18:19
something like this right in the center position so after this we will have a
18:26
simple paragraph
18:32
we'll simply say that click the button below to make a payment with
18:47
payal so after this we will have a simple anchor tag which will redirect
18:52
the user to the HTTP Local Host 5,000 SL
18:58
payment route and it will have a simple class of
19:04
BG blue 500 text color will be white padding X
19:11
in the four padding in the y direction to rounded
19:17
Shadow so when we hover onto this the color will change BG blue 600
19:29
so we do need to actually add a label to it to this anchor tag we will simply say
19:36
pay with
19:42
payal so you can just see this is actually the homepage is ready now if I click this button I will be redirected
19:48
to this payment route so we do need to create this route so go to index.js so right here
19:59
we need to create this payment route but before creating it we do need to configure
20:04
payal so for configuring payal we do need to include this
20:10
dependency we need to require it which is payal rest s and uh this module expects a client ID
20:20
and a client secret right here we can simply say paypal. configure
20:29
and this takes an object first property is takes as a mode so if you are in live
20:34
mode of sandbox so we are on sandbox mode so you'll provide sandbox and then it takes the client
20:42
ID and the client secret so these two information you need to get this from
20:47
your payer dashboard I will show you how to get this it's very simple so now you
20:54
need to go to this address which is papal developer dashboard so just write
21:00
this on Google and just go to it and you will be redirected to so you
21:07
will be in sandbox mode by default so then you need to go to this option which
21:12
is apps and CR credentials and here you need to create a brand new app by
21:18
default this default application will be created so you can create a brand new app by clicking this button create app
21:26
so now you can create app so you can give a app name as well
21:34
but before creating an app you need to create a Sandbox account so right
21:39
here there is an option of testing tools right here and you will see this sandbox
21:45
accounts you do need a business account and a personal account here so go to the sandbox accounts and uh by default two
21:55
accounts will be created by default so if you want to create a brand new
22:01
Sandbox account click on this create account option and here you need to select either it can be personal or a business
22:08
account merchant account or buyer account so we need to create a business account so here you can select your
22:14
country as well so I will select the country and click
22:21
on create account so now it will create a business sandbox account you will see
22:26
it has been created successfully so this is my account right here
22:33
SP similarly we can create a personal account as
22:42
well just need to refresh
22:50
it now the personal account is selected
23:03
so now if you see uh a personal account is also been created so we have created
23:09
this now at time of creating an application you can give this anything let's suppose testing
23:16
app so by default this needs to be selected with is which is Merchant because you will be selling the digital
23:23
goods and now you can select the your
23:30
business account that you have created this is sp
23:37
spgw you need to refresh it for that to show
23:45
up so now if you see our account which we have created is showing up so you
23:51
need to select that business account and then give it a name testing app
23:59
click on create app and now it will give you that client ID and client
24:05
secret so make sure that you select the same business account that you have created in which you will be accepting
24:12
payments now it will give you this client ID and client secret this will be different for you so don't copy my
24:20
information so paste similarly this client secret as
24:26
well so after getting both this information now we can
24:31
actually write the code for the payment and also we do need to initialize the
24:37
session as well session if you want to store the
24:43
information of the payment so this package express session that we
24:48
installed so now to set up this it takes some options uh you need to pass it as a
24:54
middleware so once we initialize the express app you can after this we can use it as a middleware app.use function
25:03
so you can say that uh session you can see session variable
25:08
you're passing it and it takes some options first option is take is the secret key this can be anything any
25:15
value let me just type secret key the second option is take a resave this is a
25:21
Boolean parameter set it to false save uninitialized it's also a Boolean
25:27
parameter the default value is true and the cookie so this is actually how much seconds you
25:35
need to store it so secure fall simply means that
25:42
store this information unlimited number of time so this is actually the session is
25:49
initialized with these default options now we can actually write that uh
25:55
request SL payment so once the user clicks that button this
26:02
request will be executed it will be a get request SL
26:10
payment uh this will actually be a Asing function request
26:17
response and here we need to create a Json payment object so inside this
26:25
object we will have different details about the product such as the information the details so intent
26:32
property is actually sale because we do need to make a sale put a comma and then
26:38
we need to provide the information about the pairer so what payment method the user
26:44
will be selecting so we will simply be selecting the Paypal payment method the
26:50
third property will be the redirect URLs property so this property here you
26:57
need to set the two properties here return URL so this means that if the
27:04
payment is successful then payal redirect to which page so you can create
27:10
a success page so we can create a success route right here similarly if
27:16
the payment is for some reason is not completed in that case this will be
27:22
redirected to we can create a failed route
27:29
so this is really simple information so if the payment is successful it will be redirected to this route if the payment
27:34
is not successful this will be redirected to the failed route the next property is the information related to
27:41
transaction transaction object this needs to be in double quotes
27:47
because it's a Json object transactions this will be a array of
27:55
object this is also in double quotes just make sure it's a Json not
28:01
JavaScript so inside this you need to provide whichever item that you're
28:06
selling so item list object is
28:12
there and it will contain a items array each item has its own property
28:21
such as name quantity how much quantity that you're selling so I will simply say this is source code of app
28:29
and then we'll say SQ which is item and here is the price how much
28:39
price that you're selling so let me make it uh $500 US so here you need to provide the
28:46
currency as well USD and then the quantity so if you're selling more quantities you can just
28:52
select but we are selling only one quantity so quantity will be one
28:59
so after you do this put a
29:06
comma so this is the item list is complete and then it takes the amount
29:14
object so here also it basically have two properties first is
29:20
the currency property which will be USD same currency that you selected and a
29:25
total property so this total property will be 500 because we are only selecting one quantity right here let's
29:32
suppose if you have selling two quantities so the total will be $1,000 so that's why you just need to
29:40
make sure how much quantity that you're selling based upon that you need to select the total property
29:46
so last property is the description of short little description about the
29:51
product that you're selling so in double quotes so you can say say
29:58
this is the source code just make sure you put the double
30:05
codes right here also so this is actually the overall
30:10
object which is the in create payment Json now we can actually pass this
30:20
object to a method payle do payment.
30:25
create so this is a create method and here you can pass this uh Json preate
30:32
payment Json and this actually have a callback
30:44
function so this actually will be a call back
30:53
function the second property will be payment if the payment will be successful
30:58
so here you need to Simply have a if condition if any sort
31:05
of error take place then in that case we will console log
31:11
it and also we'll be redirecting the user to the failed route so if the payment is
31:19
not failed if the payment is successful in that case we will
31:25
use a simple for Loop
31:31
payment. links. length so there will be this property
31:37
payment. links. length which will be coming in this object so we just need to
31:45
redirect here we need to Simply compare Loop through all the links and
31:51
there is a property called as relative we need to only get this approval URL and approval uncore
32:01
URL if this URL is equal to that then we need to Simply redirect the user to this
32:09
uh like this so all these links will be relate
32:14
uh returning but here we are simply using a simple for Loop and a condition to Simply
32:21
only get the approval URL and redirect the user to that approval URL
32:31
so if you try to now execute this application at this moment of time if I go to Local Host 5,000 click this
32:40
button so now it will show me uh Local Host SL approval
32:48
URL so now we do need to make this because if you see we are having this slash approval URL we do need to make
32:56
this uh we are successfully getting
33:08
this app.get
33:18
payment uh guys I figured out the error this is showing because we actually
33:24
inside uh links. I we need to actually get the HF
33:29
property not real property just make this change this was the problem just we
33:35
need to get the HF property so now if you again go to Local Host 5,000 click on this button you will be
33:43
actually be redirected to the Paypal payment page so here you can see we are
33:48
having this we are on the sandbox environment so that's why sandbox. do.com we redirected to so here we do
33:56
need to go to we need to select our any personal
34:02
account that we created so let me select this where you need to
34:08
paste the email
34:16
address and then we need to paste the password as well so now it will actually go to your
34:25
wallet and you will charge 500 dollar you can see we are sorry this seller doesn't accept payment in your
34:35
currency you will see all these links will be coming this is actually the payment link right
34:43
here so this error which is uh this seller doesn't accept payments in your
34:56
currency so just make sure that you have selected a business account which uh
35:03
have uh United State USD dollar selected so if you see we have selected
35:10
this business account if you
35:23
see let me go to the apps right here uh
35:34
refresh let me select this app in which the account is
35:41
connected let me paste the client ID right here uh so just make sure the
35:47
currency that you are selecting is uh USD because pay uh PayPal only accepts
35:55
USD not your native currency
36:02
so just copy
36:11
this so let me again uh
36:43
so now you can see it is actually cutting $500 that we mentioned click on
36:49
continue so now it will have this error uh because we haven't written the route for the success you will see it will
36:55
have this success route the pay payment ID will be returned here all this information this is the actual token
37:01
which we will need all this information the payer ID so now we need to actually write this success URL that we actually
37:09
configured which is when the payment is successful this route will be hit so now we need to make this
37:16
route so when the payment is successful we now need to allow the user to
37:22
download the digital good so right here app do
37:30
get/ success so you'll be having the payer ID
37:38
which we can get request. cury [Music]
37:43
do the property payer ID the second property we do need to get
37:49
is the payment ID which we'll get request. query. payment ID these two
37:54
parameters now we need to Simply make a Json object which will have these
38:01
details this will be a simple object and here is the first property
38:08
pair underscore ID is equal to the pair ID that we got
38:15
the second property is the transactions object this will be a array
38:22
of object same thing we need to set the amount current see and the total
38:31
500 because the total if you say we have 500 right here so after you set this now
38:39
you need to execute PayPal do payment execute this is actual execute method
38:46
which will complete the payment with this payment ID and this execute payment Json and
38:55
this function will be returned error of payment this is actually call back
39:00
function if any sort of error take place we can again console
39:07
log we can set a session variable of error and we will simply say error.
39:14
response and then we can simply return back to the failed
39:20
route if the payment is successful in the else block we will simply say a
39:26
request or session do payment is equal to the payment object that we got
39:33
request. session do we will basically make this variable allow download to be
39:39
true so this variable will keep track of whether the user has made the payment or not based upon that we will set it to
39:45
true or false so now after the payment is successful we will set it to true now we'll redirect the user to the
39:53
homepage like this now we do need to make change in this uh homepage we just
40:00
need to make a simple middleware so that this middle Weare
40:06
function will actually check that if the user can download the file or not check
40:12
download access so request next this will be a simple function in
40:19
this function we'll simply check that if that session variable is set or not allow download if this is true in that
40:26
case we will actually be redirecting the user to the download page SL download
40:32
route else we'll simply say next now we need to pass this middleware
40:39
just pass to the Home Route like this now this is actually a protected route
40:45
because we will be checking this session variable if this if this is true then only go to the download route if this is
40:52
not true then simply show the login page
40:57
and now we need to actually write this uh download
41:07
route so inside this route we just need to have a if
41:13
condition that if that session variable of allow download is true in that case
41:19
only we need to show
41:25
the download page so inside the download page it will be very simple download
41:33
page it will be present in the public
41:39
directory download. HTML so we do need to create inside
41:46
public folder just create download. HTML you can just customize accordingly
41:53
download page so inside this
42:01
uh you will get all the source code in the description so for saving the time I'm just pasting it so right here it has
42:09
using Tailwind CSS to actually have a download button and if you click this download button you will be redirected
42:15
to this route which is download Dash file and we are simply storing it inside
42:20
local storage local storage. set item allow download variable to be true
42:30
now we need to make this route which is download
42:36
file so now to download the file slash download
42:52
file so here we again need to check if that session variable
42:57
is set to true then only we need to download
43:04
the file so now to actually download the file it's very
43:10
simple file
43:18
path so it is actually present will be present in the public folder and uh I
43:24
think I named it as sample.pdf
43:30
and then response. download of Express function to actually download the
43:35
file like this so just move your digital good whichever thing that you are
43:41
selling so I'm just selling this simple PDF file so I will simply paste it and
43:48
rename this to sample. PDF you can even sell a zip file as well
43:56
which will contain multiple files we are set setting the simple sample.pdf file
44:02
so here we are providing this path and now if you refresh here uh Local
44:18
Host you can just see the inside your download file uh just
44:26
change this to file 5,000 because we are running this application of 5,000 port
44:35
number click the
44:45
button so now you it 500 if I
44:52
click so now if you see after the payment it is redirecting me to this
44:58
page Local Host 8,000 uh I think let me open
45:17
this uh Local Host oh sorry you can just see we have put 8,000 here needs to be
45:25
5,000 because the app is running on Local Host
45:37
5,000 so now if you see uh it will if you click the payment button and you
45:43
will see failed route is showing us what is
45:48
the error let me check request Source ID was not found
45:59
invalid resource
46:09
ID uh let me see guys what is the in the success call
46:22
back what I will do uh this is actually the route here which
46:29
is a
46:35
success I think I made some kind of typo mistake right here so what I will do I will copy this and don't waste the
46:46
time this is the thing let me paste
46:54
it if I based once
47:14
again if you are stuck at a problem guys you can make use of chat GPD just tell
47:20
the problem invalid resource ID is the code correct
47:29
so in this way you can ask questions to chat GPT so it will tell you the problem the error message invalid resource ID
47:36
typically indicates an issue with the payment
47:46
ID make sure the total amount in that's so it is giving you the correct
47:51
code by correcting it
48:02
the total amount in the execute payment Json does not match the amount specified
48:07
in the payment creation if the price is 500 the total should also be
48:13
5,000 uh let me see if the okay you will see that guys the total is not
48:20
matching it needs to be 5,000 so we do need to change it to 5,000
48:41
now if you see uh the it is successfully completed now it has redirected to me
48:46
this download page now you can simply download your digital
48:53
good you will see the file has been successfully download loed if you open it this is the actual file you can view
49:02
it so the thing is that uh if you let's suppose if you close this for some
49:08
reason and Vis again visit it let's suppose Local Host 5,000 it will
49:14
redirect you to the download page if you see so once you make the payment this
49:21
route will be accessible to you so now let's suppose if I restart
49:27
the application now if you try to go to it this
49:32
route SL download not before not ping
49:38
it so if you see you can't go to this route SL
49:53
download so you can't go to this route directly you do need to make the
49:59
payment so you can't go directly SL download if you see you
50:06
can't go to it so you do need to make the payment
50:12
first of all so this is actually the system so after you make the payment you
50:18
will be redirected to the
50:32
this is actually the tutorial guys if you are interested the link is given in the
50:38
description you will get all this source
50:52
code and the link is given you can directly purchase it from
50:58
my website Pro Cod store.com so I will be seeing you guys in the next one
#Mobile Payments & Digital Wallets
