Stripe Recurring Payments With Stripe API & PHP [Part 2]

rootF IMG 604b884c228c7

Welcome to the part two of the three part
video series where I will show you how to create stripe recurring payments. In this
video I'm going to show how to redirect person to the Thank You page, how to verify
all the informations, how to send an email with login details, how to create
an account and store the data inside the database and a lot more so in the first part of this video
tutorial just as a quick reminder we have created small pricing page with
three different packages okay so this price okay some features and then
there is buy now button, so now when someone click here and we verify his credit card
details we will redirect person to the paymentProcess.php file. So
now in this file what we are going to create is we will first verify all the
details so we need to make sure that they are correct then if they are not
correct we will redirect okay person back to
this pricing page after that we are going to create a new user and store his
login details to the database and then we are going to send an email with all
the login details and then we are going to redirect him to the Thank You page
so now if everything here is correct after that the person will be able to
login and then access all the information that he purchased so let's
now write a code so now that you understand what you are going to create
in this tutorial I'm going to show you what are the basics that you need to
have for this one so as you can see here I have created three different products
and plans the first one I have been showing how to create in the previous
video tutorial and now I have just added two more on stripe so now the first
thing that we need to verify is do we have those three informations that we
actually need in order to create subscription and really make a payment
so first thing that I'm going to create is I will say one associative array and
then in that array I'm going just to put all the product IDs that we have so it
will be product ID one and here you can put whatever you have it can be
something more advanced that not as simple one two three it's all on you and
then also I'm going to create one small array that will contain all those keys
so I will say here one two three okay so the first one is obviously 27 the second
one is 67 and the third one is 97 okay now I can delete this and now we are
going to verify that all the information that we are getting are correct so first
I want to verify that if there isn't product ID we will reddirect person to
the index.php of course if it is which is our pricing page okay and then if product ID
doesn't exist in this array here we will also reddirect person so I'll say if or
not in array and I'm talking about get Product ID and we will check him inside
products and then product IDs okay and then also I'm going to check if not
isset post stripe token or not isset post stripe email okay so I'm not going to
spend a lot of the time explaining the basic things that we have here and for
those of you that don't understand or that maybe just want to improve your
coding skills make sure that you check my the latest course that I have created
which is I have get I'm getting amazing response so far okay so now let's test
this out so let's say if we put here six you can see I have been resurrected to
this index dot PHP file and if I try even to put the project ID one which is
correct still I will be redirected because I
don't have a stripe token or stripe email which is great okay so now if that
is correct we can include this we can create we can get those details and it's
all good now we just need to subscribe the person to
the correct plan because at the moment by default is 27 but here I'm going to
change it a say we want products and then the product ID which means if ID is
one we will get this plan if product IDs two will get this one and so on it is
very simple so let's test that so I will try to purchase 67 now I will say test I want to fill manually okay okay 67 cool ok it all worked well let's check our stripe
preload okay there is no customer we will open it subscribed to the monthly
and down below we can see that he's already charged 67 and that there is
active subscription for 67 bucks each month which is exactly what we wanted to
create okay so now the next thing that we will need to do is to actually create
an account for this customer based on the plan that he purchased and then
later we will send the login details redirect to Thank You page and so on so
let's do it one by one so first thing that I want to do is I will go to my PHP
my admin and then here I will just create a new database so let's say
stripe reccuring whatever I'm usually setting at utf-8 okay create
now the first table I will say users number of columns I usually leave as is
and then add I need so the first one will be ID integer Auto increment okay
next one I will say it will be name no we don't name
I will just say email varchar and then let's say 50 whatever and then let's say
registration form applicants the plan that he subscribed to and since I have
only one character I would just say one but you can set whatever you want it can
be 10 20 50 all depends on the name of your plan then here I'll say password
that we are going to create now for this one I will say 255 okay and then I will
add one more column and I will say registration day and it will be date
time okay like this and that's it I will hit save and here we go okay now here
once everything is correct what I will do I will just create a new connection
to our database so I will say localhost my user name is root password is empty
and the database name is stripe recurring I can see I'm not wrong so let
me just do it like this so you can see just verify it is stripe reccuring okay
and then here now first thing that I want to do is to escape the email so
say escape straight email okay now the next thing that we need to create is
to actually create random string and this is something that I have been shown
in my other videos but maybe let me quickly show again you can skip few
minutes ahead if you already know this so what I will say I will have one
random string with all the characters that I want to have so let's say I will
just go through my keyboard and put all the characters that I want to have and
then also I will include all the numbers that I want to have and you can
also include some other characters whatever you like but I don't want to
then the next thing that I want to do is I want just to mix this drink so let's
say echo string just you can see what has happened so as you can see now this
is looking different and time when i refresh it will look
different okay and then I will just say that our
string is maybe we can use substring and get first 10 characters from the string so no I have forgot it like this yeah so
as you can see here it will each time be the different 10 characters pretty much
that's it okay so I will just grab this and take it here okay
and then what I like to do I will just change the string to the password okay and we can add string to uppercase so it
will all have uppercase characters and now we will also encrypt the password so
I'm going to use password hash function and for those of you that don't know
this is the latest way on how to encrypt securely the password and I have video
on my channel just search for it so password hash here we will put our
string which is password and then we need to choose the encryption type or
algorithm type that we are going to use and for this tutorial I will use be
decrypt and he his length is 60 characters and I know that in my
database I have 255 it's just in case that some of you use the different
algorithm okay now after this all I want to do is to actually create a user
account with this details so I'll say connection
query and I will say in certain users I will say email password registration
date and I will say values email we have email then password is encrypted
password and registration date is now and we have forgot to add the plan
so I'll just say plan here and I will put product ID and that's it so let's
see what will happen now I will go here trust refresh so I do not need to fill
yeah we can't use it again so we need to do it manually okay let's see there isn't any error so it should mean
that we did everything correctly and yes we are so as you can see here now the
new customer has been created there is an email there's a plan ID there's a
passport and the registration date which is which means that everything is
correctly and lasting I'll just verify on stripe yeah there is another customer
and everything is good okay so let's maybe now try with
different one or we can maybe do it once we implement sending the email so now
once we have this let's just send an email with all the login details and
after that we will just redirect person to the Thank You page ok so in order for
me to send an email I am going to get the PHP mailer that's something that I'm
using all the time and it's something that I suggest you to use and also I
have really amazing tutorial on my channel about PHP mailer which I think
you should check if you don't know how to send an email okay so I'll just
extract here the lead archive okay I will rename this to the PHP mailer
and I'm going to delete everything except the source and from source I will
just get all the files here so I just wanted to simplify things for me okay
and now here what I'm going to do is at the top I would I just love to do it at
the top I will just include PHP mailer namespace and then here I will just
include the libraries from the PHP mailer
PHP mail and then they are different that we are going to need so one of is
SMTP then we will need also exception class and of course before this one we
are going to need the PHP mailer one and that's it okay
so I'm just going to say okay it will create new variable new object you PHP
mailer now we need to set the host so I will say that my host will be now we need to turn on his SMTP yes and I will show the
different cases when this one of the code should be removed and the other one
it needs to be there okay then equal true then male user name is I will
just use my email let's see I will need to get
my password too little bit later then we need to set add address is where we are
sending an email okay then we need to set who is sending an email so I would
here say okay I will use again the same email okay like this and I will set a
name let's say it's me okay cool and now the last thing that we need to
do is set is this going to be HTML is HTML I will say true and we need to set
the subject so all the basic info that we need to set for one email and then we
need to set the body does the last one so here I will say hey and then I'll
make two breaks and we'll say thank you for the purchase you are included
okay and here's a username it's an email then there is the password which is
password okay so we need break here we need to break here we need both here
it's just a little bit to create this alpha bit nice and nothing special and then here I can say thanks okay and
this is break okay and the last thing is we are just going to send an email so we
can see here if mail sent we can maybe set some variable so it's if mail sent
you'll see error equal zero else error equal one and we will use that
later on out Thank You page and you can see here echo error okay and maybe maybe
maybe I will see he just so I can show you guys the different reasons
while email won't be sent so I will say male error info so let's let's not try
this so I can show you so now if I don't have this line of the code here when we
refresh sorry let's try to purchase this one okay okay now if we don't have that line it
says cannot instantiate male function now in the some servers it will show
this and in some it will be required to remove this line so for example at the
moment on my computer or my local host it is required that I have this line of
the code so I will just remove it and then everything will be okay so that's
the one thing that you need to understand now the second thing is let's
try to reload okay now yeah now the problem is with strap talk okay we need
to go again to this that's a little bit boring okay now it says SMTP Connect field now does
the second error and basically that means usually when you get this error it
can be that your port is not open it so you need if you are using the shared
hosting you need to contact your support just to make sure that the ports that
you need are opened then the next thing is that you need for your gmail account
just go here and as you can see this is my URL and usually if you have only one
email but I have many it will be user zero less secure apps and then turn this
on okay so those are the reasons why it can happen that email is not sending for
you now also I know that I am missing the password so I'm just going to grab
it from here and I would like to change it of course so this will be password so
as you can see I have dollar sign here so maybe I can just change to single
quotes okay then we will need to set the port so I will just use or if you are
using TLS it will be for Excel and then mail equal
I will say as a cell but if you are using TLS it will be I was just say here
TLS you will do is far exam okay so let's see what will happen now okay you
don't need to do everything again okay okay so now there isn't any error which
means that the email should be sent and that all our settings are correct
so let's check our email INBOX okay so sometimes it will take few seconds but
here we go set your login details and as you can
see here it says hey thank you for the purchase you along with this blah blah
username password it's all good it looks that we need one more break
here we need to include include the login URL and when let's check our
database so yeah there is an account in our database okay so we need to add one
more break here and then here we can say maybe age press click here to load and
that's it okay and then here we will need to specify the URL where the person
will be able to log in okay I will leave it empty at the moment but later we will
add one okay and now we are going just to redirect person to our Thank You
page okay so you're safe here location let's say thank you dot PHP and
then there we are going to pass this error so I will say error then I will
also pass the password just in case and what else do we need I think everything
other than that is okay okay and I'm passing the password just in case that
if the email is not said I will be able on the thing
page to also show the password okay so here I will just say this is error okay
password is password and then maybe we can also pass the product ID so I will
see here product I equal ID okay and let's see what will happen
so we need to do it one more time yeah it's boring I know okay yeah it's redirected we are on the
Thank You page there isn't any error the password is
here and the product ID is – which is all good let's just check our email
INBOX yeah we just received an email it's here
now it looks good and that's it okay cool now let's quickly just create
our thank-you page so I will just go on far I will say thank you okay is
okay and then here let's create normal HTML document okay here I'll say thank you let's include the boost of library and I will just delete things that I
don't mean okay so here you have want to deal with that is container then one
that is raw and col md 12 okay and can maybe say here also will be text
in the center and here I can say h2 for example thank you for purchase okay and
then here let's have one paragraph and here I will check if you get error equal
one that means that we need to show the password and the login URL for this
person so I'll say like this okay and maybe we can just close it and open here
so it's easier and here I will say your login details are included below okay maybe we
can just copy from here this part here okay maybe this here so I will just say
like this so email we don't have so we can just say your email we could also
pass the email here maybe let's let's do it
so let's say okay it's an email so here I will say like email
okay user email so I will displayed here so I'll say echo get user email that's
what is echo get password okay and click here to login we will
just add the link here and otherwise I will say something very very simple like
echo please check your inbox spam folder and that's it so let's see now
when we refresh this page you can see it says thank you for purchase okay and
index error is undefined let me check change this okay thank you for purchase
please check your inbox spam folder which is okay and if we said that there
was an error we couldn't send an email we can say here that user email was
hello you can see what will happen it will say your login details are included
below user name password and click here to log in which is exactly what we
wanted to create in this part and in the part 3 we are going to create the
loading page and also we are going to create the dashboard where we will
display different things based on what customer did purchase so I will see you in
the next one 🙂

pexels photo 5849548

As found on YouTube

You May Also Like