Laravel and Paypal Checkout API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys Andre here and today I'd like to show you how we can integrate PayPal check out in a laravel app PayPal is probably the most recognized online payment gateway and if you do any sort of online business it's probably a good idea to accept it as payment now unfortunately PayPal does not have the greatest reputation in the developer community it's known for confusing docks and buggy api's which I have actually experienced when I was trying to work with it nonetheless let's try to power through them and build an app so there are a few packages and tutorials out there but most of the ones that I found are still using PayPal Express which is PayPal previous version of its API which is now deprecated it still works but I wanted to do a tutorial on the latest API so I actually took a look at this one and I actually went through it and pretty much had the app done but I realized it was using the old version so if you take a look at the Express docks you can see right here it says it's deprecated out of this date so I scrapped everything and started to work on the new API which is called check out okay so let's get started first of all you have to have a PayPal account obviously then you have to log in to developer that PayPal comm and I believe you're going to have to have a PayPal business account to accept payments so you can just upgrade your current account it's free and you have the option to either combine your accounts or make a separate business account I personally just combined mine and after that you can log in let me log in here let's go login and next you're gonna want to make sure that you have sandbox accounts so if we go here to sandbox and if you don't have any make sure you make two you have to have one for personal and one for business and sandbox accounts are exactly as they sound they allow you to test your application before it is made available in production so you have an entire sandbox you can play with as if it were a real PayPal account so for example if I copy this this is the personal sandbox account if I go to sandbox PayPal calm and it looks just like PayPal so don't confuse them if you log in to paste that in then you can see all the test accounts that I have here and as we make payments we can see that transactions go here and you can see the balance go down as well and same deal for the seller account so make sure you have your sandbox accounts to set up and actually before that or after that you also want to make sure you have let's go into my apps I'll make sure you have a REST API and this is what we'll be using to interact with PayPal API and this will give you some secret and some client keys as well so we can use the API I think when you create this you get a sandbox account automatically but I don't remember what happened so just make sure you have those two things sandbox and the REST API so yeah the purpose of this video is just to give you a sense of how the off flow and how the API works I'm just going to dump the code in a routes file so we can play around so the code won't exactly be the cleanest in the next video we'll take a look at implementing the API in a more real-world application in my e-commerce series okay so let's get started let's go into the checkout Docs and see what's going on here okay so if you scroll down you'll see that the integration options there are two ways to interpret them or two ways we'll be looking at there is client integration which is the easiest way and this will embed a PayPal checkout button and take you to PayPal off the system and then we'll verify all the payments for you this like a naked states will never hit your web server it will only hit the client so for applications and use cases that don't require a web server this is great for example if you just want to accept donations you can just use this way and the other way we'll be looking at is server integration where your server sits in the middle and allows you to make calls to the PayPal API but the advantage of this is you can interact with your web server for whatever you want to do for example if you wanted to add it to an orders table or manipulate your database then this is the way to go okay so let's start with client integration and here is an example so that's why I was just trying to explain it just from your browser straight to the PayPal server and there's no your web server is not there hey so here let's just grab this and let's just paste this into our own laravel app and we have to replace this sandbox ID with our own ID so I have a laravel app here it's bare-bones I didn't do anything to it so they just used the welcome blade a PHP and let's just remove all of this stuff I mean this and let's remove this and let's just put everything in here so yeah let's just paste it in here and I'm gonna take this PayPal button that's where the PayPal buttons gonna go I'm gonna put it in here so it renders in the middle of the page because their CSS set up for that already and let's grab our client ID so we'll just be working with the sandbox obviously if you're making this live you would put this in production with the production key so back here here where you make your rest clients sorry here this is where you get your API explain to this one and we're gonna need a client ID but later on we need the secret ID too when we do the server integration this is fine for now they start in and that's literally it let's take a look fresh and there is that button I think there's an option to make the button bigger so right here I think it's large maybe okay so there you go and if we click this it takes us to PayPal in a new window so the old API would bring you to a brand new page so this is a bit of an improvement so the user doesn't get confused and this is our sandbox account since I'm already logged in let me just show you the log out just to show you okay let's stir from scratch let me close this open again and it should bring you to this login screen and I can login with my test buyer account okay so these are all my options to pay because this was what the sandbox account has it has normal PayPal balance as a visa and it has a bank account so I can just continue and we can continue again and that's it so as you can see there's a dirt here and it just go through the code so payment is the function that is run that takes you to PayPal site and grabs all the information from the user that logs in to their account and you also pass it the currency and a few other options like one size and this is all in talks that's but you are defined so after this completes the payment has not gone through yet this is the function that gets called and this is the function you have to call in order for the payment to come through and we'll take a look at this in more depth when we do this server integration so yeah if we take a look at our sign box and you can add some JavaScript to change the page to say that the payment went through but if we go here and i refresh the page actually it's not instant for whatever reason so this is kind of confusing in my experience it takes like five minutes for it to show up but it should go down by one cent because that's what we were charged this is instant but these are not instant things like five minutes but we'll see so yeah that hasn't gone through yet I'll give it a few minutes and see if it gone through just to prove to you that it worked a few moments later okay so it's been about two minutes and you can see the transaction went through right there and we charged one cent because that's what we specified in here so yeah that's basically the client-side integration there's options but we'll take a look at the options when we do the server-side integration which we will do now all right so let's go back to the docs and now let's take a look at the server-side integration so it's the same thing well not the same thing with it if you look at the diagram here you'll see that now our server sits in between here and our server is now responsible for calling the API and the API will return to our server which we can do whatever we want like add the order to the orders table or whatever our app needs so yeah the code looks similar and an example here is what we'll use so I'm gonna grab this this is to the client so it is still in our browser so in our case it's just gonna be in here in the Welcome page and it's gonna delete this don't need this anymore actually let me just comment it out so I can grab so I can grab the key actually we don't need the key because that's gonna go in our server so let me just have this code and space that in let's take a look at what's going on okay so now here is where we have to make a call to our own back end and this end point will be responsible for calling the paypal api so we have to make an end point here and here on authorize okay so when we call this it's going to return a token or a token ID or what they named it is a payment ID and what that payment ID has is all the information that's come back after the user logs in to their PayPal account and after we do that then we can make another call to our server at this endpoint and this will be responsible for executing the payment and we have to pass in the payment ID which we get from here and the payer ID which we also get from here so if we go back to the example they are using a node here so this is the client which we have already and here this is an example of the server-side code but they are using node it's pretty easy to understand actually they're just directly integrating the PayPal REST API if you see here they're posting to this endpoint which is the REST API and passing in the client and the secret and then here's the execution part where they actually execute the payment and again they're just making use of the REST API and we can totally do that as well if we wanted to we can just make use of a package like guzzle to make HTTP requests or you can use an SDK which is a wrapper around the API and that's what we'll do here so they have one for PHP it's not specific to laravel but we can just use it so let's go ahead and use this actually before I get to this let's make endpoints here for these two for creating payment and for executing the payment so I'm going to slash API and because we're gonna use the routes API gray payment and we don't industry like slash it's the same thing here and execute payment and let's go ahead and make these in our routes API you can put it in web too but it's better to put in here because they do not to deal with all the CSRF tokens so let's just make these two and they are post requests as you see here so let's do that and like I said I'm just gonna dump it in a closure but if this were really should put in a controller so route and it should be a post request and also called was called great payment and execute a mint I was making for create payment and it's the one for executed payment as well do that changes to execute and here we're gonna need the request because we're passing in data here you just do that and it's import that okay and it's just returned to make sure create payment working just make sure it works when we're calling it from our front end okay okay so let's see if this works so create payment execute payment alright open up Chrome it's going here here this here my network tab is opened and let's see if we get the proper response or create payment there we go create payment working and we can't execute it yet because that is called after this result so this is good let's make this larger so I'm just gonna paste in the setting that makes it bigger so let's change it to large okay so now we can install the PHP SDK so this is the the example page which will make yourself in a second I still have to install it so the instructions are here on the github page I think no they're not who are they installation ok poser okay so just composer require so let's do that okay so that's done let's go back to these samples and let's take a look at this there's two parts to it again it's for there's one part for creating the payment and there's one for part for executing the payment so this first one is for creating the payment so I'm just gonna grab all of this and make some slight modifications so it works in our app but I'll go through the modifications after I paste everything in a few moments later okay so I basically took all of this and just pasted it in my code but made some changes so the first thing as you can see they're requiring this and all this was doing is setting up the API credentials so that's the first thing I did in here so here's my client ID and here's my secret ID and then this is the same this is not necessary but I just left it in there since it's already in it in the example all this does is when once you go into PayPal once you click on this link and going to PayPal you'll see line items in there more specific line items too specific to your order so if you're doing this in your app you would get this information from whatever is currently in the cart but this is not necessary so it's setting two items so this item has one quantity of one and as a price this item has a quantity of five and there's a price so that's 1750 right now and there's the item list and then here again this is not necessary as well but it's adding more detailed payment information so this is adding shipping of a dollar twenty and tax up a dollar thirty so yeah so the two items are 1750 like I said but 1750 plus 250 is 20 so the total is being set down here and the amount is necessary and you have to make sure that the items and the totals add up if they don't then the API will throw in the error transaction is necessary it's setting the item list again you don't need this but it's in here you have to set the amount you don't have to set the description and the invoice number you can get this from your app as well but here just using a random ID you need the redirect URLs and in our case sorry this is wrong there should be a Arabelle paypal example and obviously in production you change this to your URL and you can change this to a specific one to go to a cancel page if you wanted to but I'm not doing that and here's where you're setting up the new payment object you're setting the payer you're setting the redirect URLs and you're setting the transactions and this is what creates a payment and if it's successful then it just returns it so we can see that in our network tab and also from our client I'm also console logging the response so we should see that in the console and the network tab if we did this correctly so back here that refresh something up our network tab doesn't matter it's just leaving the console see if we get anything and there's that response so that should have gone through should have gone through in the networks double-checking Network great payment so same thing cool okay now let's set up the off the execute callback so here in our client it's just calling this endpoint and it's passing in the payment ID and the payer ID so let's go ahead and do that and there's an example for that too and here and that is right here so here actually huge after success which is the next step and I'm going to do the same thing but remove what I don't need so I'll be back when this is updated in the code okay so I've gone through the code and I made some changes so it works with our code so here they're using get variables but in our case if you look at the client side we're posting to it so we have to make sure we use post variables so if you look here they're using get rebels so I'm just grabbing it from the request here right here instead of get variables and same for the payer ID and this I just commented out because what this is doing is its adding shipping shipping cost after the user puts in their information so the idea behind this is you have a price and then person logs in to their PayPal they enter their address and then you want to add another price on top of that because of their address and because that's the shipping cost so that's what this this does but I want to keep it simple and just charge him the original amount but you can do that if you like and here is where we're executing the payment passing through the execution object which is here and then passing the API contacts which we have appear again and if I did this correctly this should go through all the way and we should see our balance go down in our sandbox so let's try this Oh a lot more thing I also console logged the result here on the unauthorized method and I put an alert once it's gone through and I removed the console log from here so now if I did this correctly its PayPal checkout and I think I forgot to show you this earlier but this is the line items that we added from the other method from earlier and again this is not necessary but it's nice so the user knows what they're paying for so let's continue and it's put the order through and there's that alert there's the console.log so it looks like it went through and if you look at the state and it was approved it looks like it did go through it so again in my sandbox here and let's take a look if this goes down about $20 because this is in Canadian funds and it's $20 of American so there's a conversion so it should be somewhere around 674 ish so refresh that so there you go it went down 20 and this is not the one there should be another one on top of this in a few minutes this is just the one I tested right before so there should be another 20 here in a few minutes which I'll check in a second so if you noticed in the checkout flow there was an option it said ship now I'll show you and in our case or if you're just selling digital goods and there's no shipping then we don't want that right here we don't want to show this because if it's a digital good then we're not shipping anything so how do we remove that so to remove that go back into our code and let's go back into our API in the create payment method and right before we make a new payment and a paste in some code here and we have to make you have to do this call set no shipping on input fields and then after that we have to make a new web profile and right here I'm going to paste in another bit of code that adds that profile and allows you to remove that no shipping or allows you to remove that shipping label so yeah that should do it that you remove this ship to thing here so let's close this go refresh and try again and I'm there sorry I have to import input fields okay try it again another error web profile I have to import okay one more time another err web profile ID sorry I forgot to paste in another line here to be honest I totally know what this does I just found this off a Stack Overflow thread which just tried to remove the shipping okay they should do it okay and the ship too is gone awesome so let's see if that original payment went through there so there should be two 20s here on October 20th hopefully and there we go cool so yeah a few more things I want to show you or take note of there's this credit card generator tool for testing credit cards so there's also an option you can turn off if you want but it's on by default I mean just log out here actually some will log out here but there's an option to pay with debit or credit card without having a paypal account so if this is on then you can use this pedal cart generator and the flow will be the same so yeah you can make use of that if you want and the other thing I mention is negative testing so you should definitely test the negative cases as well so go through this documentation and make sure you test the negative cases to be honest I haven't looked into it too much because it wasn't the easiest to do for example in stripe it's really easy all you have to do is they give you a set of credit card numbers that will produce an error message and you can just test based on that for here you have to like set a header and I didn't find example code so I didn't look too deeply into it but definitely something you should look into if you're making a life for production but yeah I think I've stopped here I hope you got more insight on how to implement the PayPal checkout API with laravel like I said earlier we'll take a look at a more practical example in the next video where we'll use it on my laravel ecommerce application please like comment and subscribe if you have already done so thanks for watching guys see you in the next one kay thanks bye [Music]
Info
Channel: Andre Madarang
Views: 54,768
Rating: undefined out of 5
Keywords: laravel paypal, paypal laravel, laravel paypal checkout, laravel paypal integration, laravel paypal integration tutorial, paypal laravel tutorial, paypal checkout php, paypal checkout api, paypal checkout express, php paypal, php paypal api, paypal php, paypal php sdk, paypal php sdk tutorial, andre madarang, drehimself, laravel paypal php tutorial, paypal checkout
Id: _7YBIRQfSN0
Channel Id: undefined
Length: 28min 39sec (1719 seconds)
Published: Mon Oct 22 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.