PayPal Payments: Accept Instant Payments With PayPal

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to learn how we can allow our users to make payments using PayPal so what we're doing here is you can see just in front of you this form which is going to allow us to enter a product a price and then click pay this doesn't need to be how you set up your application this is just an example form that's going to pass through these details and go ahead and then take the user off to PayPal allow them to pay with either at their PayPal balance or go ahead and use a stored card that they have then they're going to come back and and the payment is going to complete and we'll give them a message so before we start doing this we need to just take a look at a couple of things that we need in order to do this now the first thing is that you need a PayPal account and once you do have a PayPal account or if you already do have a PayPal account you need to come over to developer that PayPal com sign in with your PayPal account and head over to your dashboard and this is where we can create an app which will give us our API keys in order to actually process a payment the second thing you're going to need is composer which is a dependency manager for PHP and what this will allow us to do is pull down PayPal's PHP SDK this is just a library essentially that allows us to connect to PayPal in a really easy to understand way very clean code in some respects and just go ahead and you know sort everything out for us we don't need to make calls to PayPal on our own so it's just a helper really so once you have a PayPal account or you've signed into develop at PayPal calm if you really do have an account and you have composer downloaded we can go ahead and start with this and obviously this whole process can be integrated into anything within your application you can define multiple products you can set tax rates you can set shipping and you can set your currency as well so you can go and take this and just use it however you need to so let's get started from pretty much from scratch and start to build in this functionality so we'll first take a quick look at this form that we're using at to send data through to our checkout page and we can see here we got a basic page basic markup layout we have a style sheet LinkedIn which is obviously what makes this page look like this and then we have this form so this goes through to check out PHP which will eventually be creating we're sending this through with a post method so we can pick this up using PHP post superglobal and we have two inputs here one called product and one called price and then we have a submit button to allow the user to make a payment so the next step is going to be downloading the paypal PHP SDK that we saw earlier so once you have composer installed we can go ahead and pull this down into our application and start to use it straightaway and if you go ahead and search for packages under PayPal's at REST API we can go ahead and pull in a version so in this case I'm putting in version 1.3 point two so I'm going to copy this and I'm going to head over to my application and I'm going to create a composer JSON file which will allow us to pull this down and in here we can define what we want to require what we need to require into our application as a dependency so let's just copy this and paste that into there perfect and we can obviously change the version we could do one dot three dot star if we wanted we'll just leave it at this for now so once we've done that we need to head over to our terminal to actually install this so if we just pull this up I'm currently in the PayPal directory so we have about index file our CSS directory and I'll compose JSON file and we will just run compose it install that's going to download that dependency and then we can just include it straight into our project so there we go it's already done mine loaded from cache but yours might take a little bit longer if you've not downloaded this before and you'll have to download from PAC just but now we have this vendor folder with PayPal here and our auto loader which we can include into our project so what we want to do now is set up our application to actually be able to use this but before that we're going to create a paypal application so once you are signed in your over in your dashboard you want to hit create app just up here that's going to go ahead and allow you to create an app and we can give our app a name so in this case I'm just going to call it pay for something but you can call that whatever you want so we hit create app and once that is done we now see a little bit of information about this app so it has a unique client ID and secret so we use these to be able to actually connect to PayPal to to process our payments and it's important to note that we have a sandbox in a live version so doing this within the sandbox will not matter at all it won't take any money from you or anyone else it's only when you switch over to live that you will actually be able to take you know payments from real people so we're going to stick with our sandbox for now we've got our client ID and secret so inside of our application let's go ahead and pull in the SDK and set everything up so I'm going to create a app directory within my root PayPal account directory and inside of here we're going to create a new file I'm going to call this start dot PHP so although this isn't structured like a real application we're going to pretend this is some kind of container maybe that we can actually just you know create a new PayPal instance from so inside of here then we obviously need to require in vendor autoload PHP that's going to go ahead and pull in the SDK ready to use and I'm going to create just a variable here called paypal and everything under the PayPal SDK is namespaced so in this case we're going to be pulling in instantiating an API context class which is namespaced under paypal rest then we have an OAuth token credential class which is names based on the paypal or if you've not worked with namespaces before don't worry too much I'm sure you'll understand by the end of this video because everything is namespaced and we can import namespaces at the top in here we're just going to create inline so I'm going to say new PayPal REST API context and inside of here we need to pass in our credentials so to do this we pass in a new instance of PayPal or I or token credential and we have two arguments here the first is the client ID the second is the secret so we already have these here we can just copy these and paste them in and just by doing this this is going to allow us to just do anything really make card payments make any other payments set up anything within the open API so let's copy over our secret as well I've just put these on different lines just it's a little bit neater and we should be good to go the last thing I'm going to do inside of Start dot PHP is to find the overall site URL and the reason I'm doing this is because when we actually set everything up we need to tell PayPal where to redirect the user after they've made a successful payment or they've cancelled or something's gone wrong so I'm just going to do this as a global variable but obviously this would be in some kind of configuration within your application and the URL is just going to be this one here so let's just paste this in and I'll get rid of the trailing forward slash so we're now done in start dot PHP we can close this off and we'll now go ahead and create this check out dot PHP file so we can actually start to set everything up and then send the user off to PayPal to make a payment so let's create a new file in here and we'll call this check out dot PHP and inside of here then what we need to do first of all is pull in app start because that's where we're defining our PayPal connection and just for the sake of this video remember we have this product and price I'm going to check if these are actually set before I do anything obviously you would have some kind of validation here but in this case I'm just going to for this example check if they're not set so we're checking post product and we're checking pup post price if they're not set we'll just kill the page so now we want to put in our product price we want to define the shipping rate we don't have to do this but I'm going to in this example and then we want to calculate our total amount so we're going to say product that's going to be post product we have our price which is exactly the same but with pricing we have our shipping rate I'm going to set this to two in whatever currency we define you could set your currency up here as well or set your currency in some kind of configuration and now I want to work out the total which is just the price plus the shipping and you could obviously go ahead and cast this to a float if you wanted to it's entirely up to you how you structure this and do everything so the next step then is to define the payment method how is the user paying they're paying with their PayPal account so this is pretty straightforward so I'm going to create a variable called payer and this is a new instance of payer which is part of the PayPal rest SDK and then we're going to say payer set payment method and we're just going to choose PayPal simple as that so let's go ahead and just check out what we get here this will result in an error just to pre warn you and I'll tell you why in just a moment so we hit pay and we get class payer not found so remember I said everything's name spaced everything that we're going to using be using here in terms of these classes are name spaced under PayPal API so at the top we can import these namespaces so we can say use PayPal API payer and then when we go and refresh again send that later through again we see nothing so we've got no errors and we're good to carry on with the rest of this so now we're getting to defining the items that we want to be in the users basket when they go over to PayPal this will list them to them calculate it all over and then we'll give them the total they need to pay so we're going to define the first item here if you want to find multiple items you can so for example if you're building some kind of shopping cart you can do that this is a new item let's just import this at the top so it's API item and with this item we're going to go ahead and set the name of this item we already have the name of the item it's the product that the user typed in so we're going to say product and we're also going to set the currency you could have the user define this and you could check it against the currencies that you support we're going to set the quantity in this case it's just going to be 1 and we're going to set the price in this case it's just going to be price so all this is is a list of methods or a chain of methods from this item here and you would do this for every item that you wanted to add to your basket or to the users pipe our list of things they need to pay for so now we create a new item list object so we say item list that's a new item list and again we need to import this at the top so it's just PayPal API item list and now with item list we're going to set the items so the items is an array of items you've provided so I'm going to create an array in here and I'm just going to pass one in if you add multiple items you can go ahead and do this and that would just calculate everything up for you within PayPal so let's get rid of these two so now what we want to do is set some details about this payment so we create a new details object we import it at the top and with this what we want to do is set the shipping and the subtotal you can also set tax here as well so we're going to say set shipping and we have a shipping rate that we defined in a variable just here again come from your config and another method we're training on methods again setting the subtotal and that's the total that we can related just here so that is the price plus the shipping so now that we've done that let's just give this a refresh just to make sure we didn't misspell anything perfect so it looks good now we want to create a new amount object set the currency the total and set the details so we're going to say amount you amount and as you probably guessed need to import this at the top and from the amount we want to go ahead and set the currency again this is going to be GBP matches our items currency we want to set the overall total so remember this is subtotal but in our case the total and the subtotal are the same and we want to set the details of this payment and we already have the details in here so what we do is we just pass in the details object so now we're getting to the transaction which is going to allow us to set the transaction amount which is the amount we created here so we're just parting that into it we're going to set the item list which we have here and we're going to set the description and the invoice number as well that can be anything you want so let's go ahead and say transaction is a new transaction just capitalize that T and again we need to import this at the top so transaction and with the transaction then we want to set the amount this isn't like the actual value amount this is this amount here so amount we're passing that object in we also want to set the item list so the item list is here so we just pass in item list and then we want to set the description this is just a string of anything you want it to be so I'm going to say pay for something which are imagining imaginary app payment and then we want to set our invoice number this can be anything you can generate whatever you want in this case just as an example but a unique ID generated in there that's it so now we want to set our redirect URLs this is where the user is going to be returned returned on success or on failure so depending on what happens on PayPal side so we're going to set redirect URLs to a new redirect URLs object again we need to import these up here that and what we do here then is we say redirect URLs set return URL that's going to be our return URL and we set our cancel URL so our return URL we're going to utilize the global site URL that we created up here so remember that's tutorials dev port 8000 PayPal this will just be whatever domain you're working on and we are going to put this into here I'm going to concatenate on forward slash pay dot PHP which we haven't created yet and we're going to be creating in a moment and we're going to define here success true so that's it and would be pretty much the same thing for this our if the user cancels it we want success to be false and we can use this within PHP to check if it was successful or not successful then we can give errors redirect log something whatever we need to do so now we're going to create a and this is the final object that we're going to instantiate which is the payment object so we're going to say payment new payment and within this we want to set the intent which is just going to be a sale we're just selling something straight off we're not delaying anything we want to set the payer redirect URLs and the transactions we created so there's nothing really new here so we're just passing these objects into this payment so we're going to say payment set intent this is a sale we are going to set the payer which we've created here so we just pass that in pea we're going to set the redirect URLs which we've created here so we just pass them in and we're going to set the transactions which we created here this is an array of any additional transaction so we just have one in this case so again let's just refresh just make sure we didn't do anything wrong so yet we forgot to import this final class here so we're just going to say payment when we refresh there we go so we're not actually sending anything to PayPal yot yet and we're not actually doing anything in terms of redirecting the user but what we need to do is from this payment object use the create method passing in our PayPal connection what that's going to do is it's going to send these details to PayPal and verify that everything's ok then what it's going to do is it's going to give us back a load of data which we can then do things like get the approval link which is essentially just the link that we want to send the user to to approve the payment so we're going to wrap this in a try-catch block because we can actually catch exceptions here it's not really important at this point it's more important at the point that the user returns so for example if something went wrong we could return the user catch the error and then go ahead and output it out paired to them so this will typically throw an exception we can just go ahead and kill this an output II and this is great for debugging or logging if anything goes wrong so you can just see the result of that so the line of code that we're trying for is using payment that remember we created here and we're using the create method this will actually create a payment in terms of sending all the details you've already defined over to PayPal it will give us the approval link and we can go ahead and redirect the user to finish their payment this won't actually charge the user at this point so we're passing in PayPal which is obviously this in here and what we can then do is go ahead and grab the approval URL so we're going to set that to payment get approval link so just for the sake of it at the moment let's just echo this out just to see what we get and we'll head back over to this page here we'll type in a product we'll type in a price hit pay and it looks like we did in fact get an error so what we're going to have to do is go back and check everything here so we can see we've got an HTTP response code of 400 when accessing sandbox that paypal.com and this would pass in all of the data that we were already sent and it's gone ahead and sent it over to the API so there was obviously a problem somewhere so let's go back and just have a look at what might have gone wrong so if we just head over to our text editor let's just take a look here so it probably has something to do with the price here that we've set these are coming through properly and if we just check our item price and oh okay so our subtotal is obviously minus our shipping and tax so in this case we're setting the subtotal to total when this should be the price of the item so let's go ahead and just give this another go so we'll go back to our page here let's go ahead and type in coffee again ten pounds hit pay and this should go ahead and give us that URL we want it and there it is so we obviously don't want to display to the user this URL we want to actually redirect them so at the bottom here instead of echoing this out we'll go ahead here and just redirect the user so we'll say header choose the location here we're just sending a HTTP header here and this is just going to be approval URL so that's pretty much it let's now go back again go ahead and type in an item in the price and hit pay and now what will happen is we'll be forwarded to PayPal to see them details so as you can see basically been sent over we've got the price of the item here and the quantity which remember we set and you can see that we've got postage and packaging here set to two pounds as well and this just gives us the overall price so how do we go about testing this I'm not going to login with my PayPal account what we actually want to do here is go ahead and hit this accounts area just here and once we're over here you can see that we've got a couple of accounts just here we can set up other accounts you can create another account if you want but in this case we've got Alex - buyer this is just added by default so what we can do is we can head over to profile and we can check this out and this allow us to do things like change any settings to live the user so payment reviews etc etc we can change the password as well so you can go ahead and set a password using this and then you can actually log in with this so what going to do then is we're going to take this email address here and over on this screen here we're going to paste that in we're going to type in the password that we set in the screen we just saw a minute ago and I'm going to hit log in so that's going to lock me in it's going to give me a fake address just here it's going to show me that the transaction is going to come or the amount is going to come from my PayPal balance and I can go ahead and click continue so what this is going to do then is send us back to the file that we specified earlier so remember we set the return URL to the site URL and then this paid up PHP file success equals true and this is what's happened here although this far can't be found so you're just getting this error here so we need to create pay dot PHP then we need to check if success was true or false then do something depending on that if it is true we can use the payment ID and we can use the payer ID to actually charge the user at the moment if you head under transactions we won't actually see a transaction for what we've just done now because it hasn't actually charged the user we only charge the user when they come back to us we need to send another API call to go ahead and actually process that payment so here are all of the transactions I've tested this one here does say 12-pound but it isn't for this you can take the times a little bit different so we need to actually go ahead and sort out this paid up here's P file so again under our root directory let's create that paid-up PHP file and we'll go ahead and starts write everything out we need to do in here this is a significantly less work so we're going to require in an app start up PHP as usual and now what we're going to do is check if success ie the success flag that we defined within now check out so we're going to check if that set first of all we're going to check that the payment ID is set which is just here and we're going to check that the payer ID is set this payment ID basically just corresponds to the ID that's been created on PayPal side the payer idea identifies my account so we'll go ahead and check this so again you might want to structure this in a slightly different way depending on your application but we're going to check yet success remember this is stalling yet superglobals now because it's in the query string of the URL we're going to check the payment I D and we're going to check the payer ID now pay special attention to the capitalization here in fact that's a capital P so we've got payment ID with lowercase P and ID and then pair ID it's essentially exactly what how it appears in the URL so if you get stuck you can just refer to that so if these aren't set then we just want to kill the page there but what you could do is just redirect the user somewhere and say something they're wrong somewhere and now what we want to do is check if I'm going to cast get success to a boolean we're going to check if that's false so that if that is false it means something went wrong along the way the user may be cancelled the payment etc so we can just kill the page but in reality and here what you want to do is redirect them off somewhere and give them a message or you might want to log this somewhere really just depends on what you want to do so this less important this would just be part of your control of how the application flows this would be more like an actual problem they've cancelled the check out so now we want to local East or the payment ID in payer ID so let's go ahead and create payment ID variable and set that to payment ID remember uppercase I lowercase D and then payer ID as well which again remember comes from I get super global is payer with a capital P ID capital I and D and now what we want to do is go ahead and grab the payment so we grab the payment using the payment ID so it's create variable here called payment and this is payment so it's a class we have a static method yet and in here we pass two arguments the first is the payment ID the second is our paypal set up for our SDK so what we defined in start of PHP so wherever you're storing that but again this is namespace payment is namespace so we can go ahead and use PayPal API payment so that's grabbed the payment now we want to execute the payment so we're going to create a variable called execute or execution whatever we want to do I'm going to say new payment execution I'm going to need to do anything directly within the constructor of this what we need to do is set the payer ID so who's paying for this so you say execute set payer ID and then we pass in the payer ID that we've stored just here so now that we've done that we're going to create another try-catch block and this is significantly more relevant to actually catch any errors here because we can grab the data and then grab a particular message if something did go wrong so we'll test that out in just a moment let's create our try-catch block here let's grab any exceptions that were thrown and the line that we want to try is setting a result here to payment execute remember payment here is this original class we used when we used our static get method we want to pass in the execute object here and again we want to pass in our connection to out the API so at this point let's just do a die on II and we'll test this out and see how we can dive into any messages in here just a moment but otherwise we're going to say echo payment made thanks now ideally what you'd want to do is at this point you'd want to redirect the user because otherwise they're going to be left with this in the URL and you don't really want that so what you want to do is redirect them off somewhere do what you've got to do upgrade the membership if you're charging for membership store some kind of transaction store the fact that those page so you can ship items to them whatever you're doing here so this would ideally be a redirect so we can test this all over again so let's go ahead and just go back to the previous page and that is just here and let's type this in in again and type in a price then hit pay we forwarded through to PayPal as we've already seen and again we can go ahead and log in with this test account that we have as part of our app hit login go ahead and hit continue now we're going to be sent back and all of that that we've just written is actually going to process the payment and then we should see the success message okay of course so again this is namespace so really important to know so we've got a payment execution just here so we'll just say payment execution and we can just refresh this page because the payment ID and the payer ID will still be valid so if you just do a refresh on this page there we go so payment made thanks so what we can now do is head over to our transactions and you'll see another 12 pounds be placed into that transaction list and there we go so we've got the sale here state is completed got an amount of 12 GBP so now let's finally just wrap up by looking at what happens if we get some kind of problem here with processing the payment if in fact we refresh this page again we will get an error so the reason for this is obviously the payment has already happened for this particular payment ID so we get a 400 error back that's not really that useful we wouldn't want to output that to the user at all what you might want to do is just go ahead and say data and what we can do is from our exception we can use the get data method and that's going to go ahead and get the data that we need what we want to do is because it's returned in JSON we want to JSON decode it all that means is it's going to turn it into an object for us so now we can do a var dump or echo or store in your database somewhere data message in fact if we just go ahead and do a dump on data and just kill the page there you'll see this so let me just give it a refresh again and there you can see our objects we've got a payment state invalid this is the message that you could either output to the user or just store somewhere probably best not to output these messages because you never know what they're going to contain so it's probably best to just go ahead and maybe look up the name of this determine what kind of problem it was and then show your own message to the user but anyway you can grab the message from this if you really wanted to as you could go ahead and echo data message if you really wanted to refresh the page and then you can see that error just there so that is pretty much it we have gone from start to finish and pulling the paypal PHP SDK in we've set everything up in mind our application structure isn't probably as you would create it you wouldn't have these files floating around in your root directory but so that's pretty much how we process a PayPal payment and it's pretty straightforward we've done everything we need to do and you can go ahead and incorporate your own logic into this to basically do whatever you need to do in your application once a user has made a payment
Info
Channel: Codecourse
Views: 159,868
Rating: undefined out of 5
Keywords: paypal, learn php, PayPal (Venture Funded Company), php, codecourse, paypal sdk, money, tutorials, card payments, paypal payments, phpacademy, web development
Id: BD1dOWIABe0
Channel Id: undefined
Length: 32min 50sec (1970 seconds)
Published: Thu Apr 30 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.