PayPal Integration In Laravel - Step By Step Guide - #Laravel

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this video we are going to learn how we can integrate paypal payment gateway in our laravel application here you can see this in my visual studio code editor i have a laravel project in which i just have designed a simple product overview page and what i want to do i want to integrate paypal when i click this pay with paypal button and i want to pay this 200 dollar price with paypal when i click this pay with paypal button so in order to do this the first thing you need is obviously a paypal account so we are going to create a paypal account then we will integrate paypal with our application so to create paypal account you just need to go to the developer.paypal.com now in order to create your paypal developer account you just need to go to the login to dashboard button so click this login to dashboard button now if you have already a paypal account then you can log in directly from here but we are going to create a new account so just click sign up now the first thing you need choose your location then add your email a correct working email and then create a new password now it will ask you to pass a security challenge so select the correct images and then you are good to go to use your paypal account now this is the dashboard of our paypal account and from here we can generate our api keys and integrate our application to the paypal and here you can see there are two options here one is the sandbox and the other one is the live option so if you are working on a live server you can upgrade your account but we are working on a testing server so uh we are going to choose this sandbox option and then create a new app so click this create app button now in here you just need to fill up some details like your application name and your application type so let's say i am going to give e-commerce app because my application is not live then select platform your app type and then you can also select the country then select create app so here you can see our application has been created in our paypal account and we got our client id and secret key that we need to add in our laravel application.enb file now let's jump to the coding part of our application so let's go to the project your larval project and open your dot env file from your application from your project and in here in dot in file you have to add the paypal client id and the paypal secret id so let's add the paypal client id and the paypal secret id so the first one we need to add is the paypal client id now let's paste the client id from the paypal dashboard from your to your env file now let's add a paypal secret client id i would name it as paypal client secret you can name it as you want and then paste your client secret and then you can also add the paypal currency that you want to use so let's say i want to use usd so let's say let's enter paypal currency equals to usd now after adding this credential into your env file let's create a model and migration for payment table so let's open a new terminal and let's run php artisan make model payment and then dash m dash m will actually create the migration for payment model now let's go to the migration that we have created that is payment migration and let's add the columns that we need in our payments table so the first one is the id and then the payment id and then the second one is the payer id that who is going to pay and the third one is the payer email so and the fourth one is the amount that is the necessary part of our payments now we can also add the currency column in this table to store the currency of the amount that is paid and we can also add the column for payment status to store the payment status either the payment is paid or not now let's migrate this table into our database so first we need to create a database so let's go to the php my admin and create a new database for the application so you can create your database with the name you want i'm going to create my database with a name paypal app now let's go to your enb file and let's add the database name that you have created for your application and let's open a new terminal and let's migrate the migration with php artisan migrate now after migrating let's go and install the package that we are going to install into our application to integrate paypal with our laravel application so uh the package that we are going to use is the omni paylaravel package so let's go to your browser and search for omnipe laravel so this is the package that we are going to use in our application that is leak slash omnipay so let's install this package into our application now let's open your visual studio code and open our new terminal and let's install this package with composer composer require leak slash omnipe and then omnip slash paypal because we are going to integrate paypal into our application you can also integrate stripe with the help of this package now we have successfully installed this package into our application now let's make a new controller for handling the payment request or integrating the paypal into our application so let's make a new controller with php artisan make a controller and then payment controller you can name this controller as you want like paypal controller but i'm going to name it as payment controller now let's open that controller that we have created that is payment controller and in this controller we will write all the logic to integrate payment gateway so first of all let's make a variable with a name a gateway and then make a constructor and in this constructor we will assign this get wave variable uh omnip class so let's assign the omnipeak class and the function we will call from this omni omnipay class is the create function and in this create function we have to pass a argument that is paypal rest now we will assign the client id and the secret id to this omnipe class so to assign this client id we can use the set client id function from this omnibp class and in this function we can pass the client id that we have added into our dot env file so let's open your dot env file and here you can see we we named it as paypal client id so pass it in here and then set the secret client id and this will also as same as we have added in our dot env file and the function we will use is the set secret function and in this function we will pass the name of the secret id that we have added in our dot env file and then we can use another function that is set uh test mode so this function is used in a testing mode because we are in a testing mode so we will pass it as true now after setting up the credential uh into your constructor let's make another function with a name pay and this function will responsible to handle the uh payment request from your website to paypal so let's write the logic use try catch because it is a good approach and in here let's make a variable uh response and let's pass a function in this variable that is purchase function from the omnipeak class and in this function we need to pass an array the first uh argument of this array is the amount that we want to pay with the help of paypal and the second argument is the currency in which we want to pay this amount and we can get the currency from our dot env file so let's get the currency that we have added in our dot env file now the third argument is the return url so if the payment uh successful where you want to return this request so you want to return this back or you want to return this in to your your customer dashboard or home page so you can pass it in here and that fourth argument is the uh is the cancel url and this is the url that if the payment cancelled or if the payment cannot be paid then where you want to redirect the user so we need to pass these four argument into this function now let's write the if statement if this response is successful then we want to redirect our request to the paypal payment page and if it is not in the else statement we just want to get the or return the message the error message and in this catch statement or catch block we just want to return the message the error message that why what was wrong or why this request failed now we have successfully write all of the business logic into our controller so let's go to the front-end part of our application so what i want to do i want to integrate this paypal when i click this pay with paypal button so let's go to this index.blade.php file in which i have designed this product overview page now in here in this index.bladeword.php file we need to add a form that will handle the payment post request but before that we need to uh define the route that will handle the uh request the payment paypal payment request so let's go to the web.php and let's uh define your route that will be a post route and this will use the payment controller and the function is the pay function that we have right in our payment controller now we can use this route in this form so let's pass this route in this action of this form and the method will be post and don't forget to add the csrf token into your form then we need to add the input a hidden input to pass the amount as a request to the controller so you can see this is the amount that we are getting from the request from this form now we need to add this 200 value in this input so let's add the value if you are getting dynamic value from the database then you just have to add the dynamic value but i'm just adding the static value now let's include this button in this form and let's add the type of this button equals to submit now let's save this file and then let's refresh the application so we got an error route doesn't found so let's correct the route that is payment now let's refresh this and let's click this pay with paypal okay so we got an error and called undefined method is direct so this is our response and this is our is direct function so this is correct okay so we didn't add the send function with purchase function so let's add this and save this now let's again test this out so we got another error this that is authentication field due to the invalid credentials so let's go to the constructor of this payment controller and in here we have added the credentials so this is because i didn't use the env function in here because we are getting this paypal client id and the secret id from the env file so i have to use env function so let's test this out again okay so we have successfully integrated and implemented paypal with our laravel application here you can see now to go further and pay this uh 200 with paypal you can use your testing accounts go to your dashboard and go to accounts and in here you can find some default or some testing accounts that you can use so you can see these are three accounts so you can you can use any one of them so let's use this personal account so here you can see this is your testing email so copy it from here and let's paste it in here click next now for the password you can also get this from your dashboard so copy it from here and then paste it in here and click login now here you can see you can now pay with paypal and here you can see this is the 200 usd amount that is directly coming from your form coming from your website that you can pay with paypal so we have successfully integrate the paypal payment gateway in our larva application so click continue it will processing your amount now it will get 404 not found error this is because we didn't add the success uh function and the error function in this controller so let's add this function at the function success function and the uh error function in this controller so in this success function we have to first check either in the the payment id and the payer id is in the request so if both payer id and payment id is in the request then we can continue our transaction now let's make a variable transact transaction and this variable will call a function complete purchase from the omnipe class and in this function we have to pass an array and in this function the in this function the first argument will be the pair id that is coming from the request and the second argument is the transaction reference and it is also coming from the request as a name pay payment id so we have successfully processed the transaction now let's send this transaction with the help of send function you also need to store the transaction information in a response because we need to check uh this response if it if the response is successful if the transaction is successful or not so let's call a function is successful so if this response is successful that means transaction if transaction is successful then we will store all of the transaction information into the payment table so let's do this let's save all of the transaction information into the payment table now after storing or saving all of the transaction into the payment table let's return a simple message payment is successful your transaction id is and we can get the transaction id from the arr variable because it is getting all of the data from the response now let's save this and if response is not successful in the else part we can return that the error the response errors with the help of get message function now if the payment id and the pair id is not in the request then in the outer else part we can return that payment is declined now let's write another function that is error function and this function is actually responsible for handling the cancel url response okay so if user cancel the payment then we will just return a simple message that user declined the payment now let's test our application again okay so let's go back to the home page that is product overview screen but before that we need to add the success url and the success route and the error route so let's add the success route and the error routes now we have completely set up all of the project all of the payment integration work now let's test this application again so here you can see this is a paypal part now let's pay this by clicking continue and here you can see we got a successful message that payment is successfully paid your transaction id is this so we have successfully integrated payment paypal payment gateway into our laravel application and i hope that this video will help you in uh integrating paypal in your application you know laravel application if you face any problem or issue just mention your problem in the comment box of this video
Info
Channel: Takneeki Code
Views: 20,706
Rating: undefined out of 5
Keywords: Takneeki Code, takneeki code, code, programming, laravel, paypal, laravel paypal, paypal laravel, laravel paypal integration, paypal integration laravel, laravel 8 paypal inegration
Id: GVrbVXLfFzw
Channel Id: undefined
Length: 20min 34sec (1234 seconds)
Published: Sat Apr 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.