PHP, MySQL & Stripe API Payment App
Lesson 3 of 3 · PHP, MySQL & Stripe API Payment App – Part 3 · Programming
PHP, MySQL & Stripe API Payment App – Part 3
alright guys welcome to part 3 of the pay page application with PHP MySQL and the stripe API so up to this point we're able to make a payment and it goes
through our stripe dashboard it also gets saved in our database we have a customers table and a transactions table now what I want to do is create two pages one for viewing the customers one
for viewing the transactions so in my in the root of our application here I'm going to create a new file called customers dot PHP and from here let's copy what we have in the success dot PHP
not that we don't actually need the PHP code but just the HTML we need bootstrap and all that so I'm just going to grab that and put that in here let's change the title to view customers and let's
see we're gonna have we're gonna have a table let's get rid of everything in the container and then I'll put let's see let's put in h2 and we'll say customers and then we're gonna have a table
and I'm actually going to give this it's this table a class or a couple classes one of table one of table – striped which are bootstrap classes there's only one peon striped and let's do a heading
so a tea head area with a table row and then some table headings okay so we're going to have the customer ID we're gonna have the name the email and let's do the date and then we'll have the tea
body under the tea head and then we're gonna have to loop through our customers here so I'm gonna stop with the HTML and then we're gonna go up top and we need to do a couple things with PHP so we're
going to bring in C we're gonna first of all bring in our database config which has the parameters so config slash DB PHP then we want to bring in our library database library which is Lib / PDO
underscore DB and then we also want to bring in our customer model so that's in models slash customer alright so let's go ahead and instantiate a customer by creating a variable called customer
and setting it to new customer and then we want to get a customer now we haven't created this method yet but we will and it's really easy so we're gonna say customer is equals customer and then
we're gonna have a method called get customers like that so let's go into our model now customer model and let's go under the add customer which ends right here and let's say public function get
customers all right and then for this this is gonna be pretty easy this library I really like this PDO library I've been using it for years I think it's it's simple but it does everything
you need it to so we're gonna say this DB so we're gonna use that library that class and we're gonna call query and inside here let's say select all from customers and we want to order them by
the created at ok and we want to order that we want it to be descending so that's our statement and then all we have to do is say results equals and remember in our in our library here in
our database library we have this result set this is what we use when we're returning more than one results okay if we were fetching a single object or a single record we would use single but
all we have to do is say results equals this DB result set actually I think it's all lowercase this DB results set and then we just return that and that should give it to
us so if I go to customers PHP and just to test it out let's do a print R which prints out an array and we'll pass in customers and let's see what we get so if we go to our application slash
customers dot PHP and there we go it's printing out Tom which is the only customer we have in our database so now we want to get Tom down here into the table so all we have to do is go down to
our body the body of our table and let's do a for each so I'm gonna use a shorthand by saying for each colon like that and then we can say PHP and for each like that okay and then what we
want to loop through we want to say for customers as C or whatever I'm just using C and then we can put our table rows so for each customer a table row is going to be created and then for the
columns let's do the ID so we'll say PHP echo C ID all right and then we'll just copy this down let's see need for all together then we have the name so we're gonna do the first name and then let's
copy this and then we'll do the last name so it'll be first name space last name then we want the email and then we want the created at all right so let's save
that let's go back and then we see let me just get rid of this print R and save and there we go so now we have our customers and I'm just going to put a link under the table that goes back to
the pay page if we want so I'll just put a line break and let's put an a tag going to index dot PHP all right and one other thing I want to do is just have a button group at the top so we can switch
to the transactions page if we want which we haven't actually created yet but we'll go ahead and create that that button group so right under the container div let's do a div with the
class of BTN group and we're gonna say role equals group and inside here we're gonna use links so we're gonna have an a tag with the class of BTN and let's do BTN primary since we're on the customers
page and this will go to customers dot PHP customers alright and then we'll copy that down this one will go to transactions dot PHP we're gonna change
the class from primary to secondary like that and let's see what that looks like let's put an HR underneath the button group div and there we go now transactions isn't going to work because
we haven't created it yet so let's do that so I'm gonna copy everything in the customers dot PHP that's in the root create a new file called transactions dot PHP and paste that in and this time
we want to bring in our transactions model we want to instantiate a transaction customer let's see it's to change all these so that'll be transaction that should be
lowercase T alright so transaction equals new transaction and then transactions equals transaction get transactions which we have to create and then down here let's change the title to
transactions and for the button group here all I'm going to do is change the classes I want transactions to be primary so that we know which one we're on and this will be secondary all right
we'll change this and then as far as the fields we have a couple more than we do for customers so we will have a transaction ID we're going to have a customer that'll be the customer ID
we're gonna have the product we're gonna have we will have a date so what I'll do is just bring in the amount like that alright and then we're going to loop through our transactions it'll say as T
okay so this first one will be the transaction ID so T ID second one is going to be the customer ID so it's gonna be t customer ID we don't need this
and then let's see we're gonna have the amount right is that the next one no the product okay but that should be T naught C then we have the amount so T amount and then we have the date right yeah all
right now if we save that and go to transactions which we have a link here it's not going to work because we haven't created yeah it's this should be transaction the models are singular but
it's still not going to work because we don't have the get transactions method so what I'm going to do is go to the customer model and copy the get customers method and then paste that
into the transit transaction model right here okay but we're going to change it to get transactions okay and then all we want to do is change the table name order by creative
that everything else can be the same that's good let's save let's reload and there we go so there's our transaction now for the amount that it looks like five thousand so what I'm going to do is
just use a little bit of PHP and go let's say we want to go to transactions PHP and where we have the T amount right here we can actually change this – we can get a decimal point in here by using
the S print F method which is a replacement method and we want to pass in here we want to pass in a % dot 2f for a first parameter and then pass in as a second parameter the amount which
is T amount and we want to divide that by 100 so if I save that and reload now we have 50 we have the dot the decimal places and let's just put the currency in here as well so after that we'll put
a space and then we'll do actually want to go after the PHP tag and then open another one and say echo T currency so 50 USD let's make that all caps so we'll just wrap the cut T currency in
str2 upper which just makes it all uppercase oops what I do and there we go so customers and transactions so once and for all let's test let's delete I'm gonna delete everything from our
database and from stripe so that we can just do one file test all right and then we'll go to stripe we'll go to our business settings data delete all test data
okay so we have nothing in stripe we have nothing in our database tables so let's go to our pay page and let's do John Doe submit all right let's do one more say Maggie Smith all right so two
people have bought in the react course now let's go to transactions dot PHP and there's our two transactions and you can see the customer ID is different if we go to customers we have our two client
our two customers okay and if we go to our dash stripe dashboard and reload we should have two payments okay there's our two payments and we should have two customers Maggie and John so there you
have it we have a pay page we can check out our customers and transactions like I said I might continue on with the authentication because obviously you don't want these pages open to the
public you'd want to have some kind of login system if you watched my you to me PHP course we implement a log in with this PDO class and you could easily add that
and even if you wanted to do that and make a pull request to the git repository feel free to do so I'll check it out and approve it if it looks good and yeah I might do a video on on
authentication but that's gonna be it for now guys hopefully you enjoyed the series I know a lot of you guys have been asking me for PHP videos even though the majority of my audience are
more towards JavaScript there are a lot of PHP developers so I don't want to leave you guys hanging so hopefully you enjoyed it and thanks for watching hey guys so as I mentioned a few times
in the series I do have a udemy course called object-oriented PHP and MVC where we actually build a complete model-view-controller framework from scratch and then we build an application
with that framework so this course uses a lot of the same concepts like I said it uses the exact same PDO class it has authentication and login so if you like what you just watched in this series and
you want to you know take your knowledge further then check out this course I'll put a $10 link in the description below
In part 3 we will build our customers.php and transactions.php pages so that we can view our stored data in MySQL. We will use our PDO library and models for this
SPONSOR: Eduonix Machine Learning Course
https://goo.gl/jo6vNd
CODE: Github Repo
https://github.com/bradtraversy/php_stripe_paypage
Stripe API Docs: Card Element Quickstart
https://stripe.com/docs/stripe-js/elements/quickstart
PDO Class Gist:
https://gist.github.com/bradtraversy/a77931605ba9b7cf3326644e75530464
PHP OOP Udemy Course:
https://www.udemy.com/object-oriented-php-mvc/?couponCode=TRAVERSY010
💖 BECOME A PATRON: Show support & get perks!
http://www.patreon.com/traversymedia
Website & Udemy Courses
http://www.traversymedia.com
Follow Traversy Media:
http://www.facebook.com/traversymedia
Posts by traversymedia
http://www.instagram.com/traversymedia
The Developer Hangout: Public Discord Server
https://discord.gg/developers