ads

Laravel Paypal integration tutorial


 

hey guys, after a long time I thought to write a guide about setting up Paypal payments with Laravel application, so let's get started. I am referencing this documentation : documentation  and I have tested it. It works perfectly, and before I start I just want you to know that there are some googling so if you are a lazy people stop reading this guide and you are a disgrace to the industry too.

step 1 : start a new Laravel Project and create a component like "paypalComponent" and a two routes pointing to the methods called "paynow" and "return", you can name it as you like but I like it this way.

step 2 : run the command composer require srmklive/paypal:~1.0 and after the command runs successfully there will a .php file inside config folde called paypal.php.

Step 3 : get the paypal business account Sandbox API username,password,secret key and App ID. (if you don't know how to do this step just google it or stop your career)

Step 4 : in your config/paypal.php add the cridentials to the correct array or else it won't work. ( plz note that the credentials can be kept in the .env file and linked but to me it won't work so just hard coded the credentials inside the paypal.php file as below image shown)

 config/paypal.php example


Step5 : go to the config/app.php and add below line to the providers array.

     Srmklive\PayPal\Providers\PayPalServiceProvider::class

 

Step 6 : run the vendor publish command 

php artisan vendor:publish --provider "Srmklive\PayPal\Providers\PayPalServiceProvider"

 

  ------now the the setup part is over and let's proceed to do the coding----------

 

first I will show you the final code so that you get an general idea how the code should look like finally and what I am explaining below.

 


 

( I am sorry is the image is not clear but due to number of lines in the code this is how I was able to take a the screenshot)

Step 7 : now open your paypalComponent and create a import the
ExpressCheckout by adding the below line


    use Srmklive\PayPal\Services\ExpressCheckout

 

Step 8 : then inside the paynow method (or what ever you named it ) initialize an object from the express checkout class. then prepare an array called $data and add the relevant product details inside a item array push it into the $data['items'];

this is where you thinking power comes handy, you can fetch the billing item details and push inside the $data['items'] etc. for this example I have created a separate method called "makedata" which prepares the data array and returns as shown below 

 


please note the $data['return_url'] this is the url which user is redirected after user is authenticated from the paypal login page, this url should be the route which I mentioned in the beginning of the guide to make called "return" and point it to a method in the "paypalController" called "return".

you can either create a separate method called makedata() just like me or a write initialize and add data to $data array inside the paynow() method.

for all web application development services contact us : kriss sachintha



step 9 : inside a try catch block write the below code line,plz note that the $data array is been passed to the setexpresscheckout method.

 


 

 so your paynow() method should look like this

 

this setExpressCheckout method as name self explains it will setup the paypal transaction by logging in the user to make sure the payment is valid and the paypal api will setup an transaction with the given parameters inside the $data array and after the user is validated the user will be redirected to the return url which is mentioned earlier in the $data array.

(plz note that the transaction is incomplete and the amount is not charged from the user yet. this step is where the API setups the transaction and redirects user with a token)

 

Step 10 :   is the transaction if  setup is successfully in the API,the user will be redirected to our predefined return url which in this case "return" laravel route.this is the actual place where you write to charge the customer the relevant amount .


below shows the code you should write inside the return() method which is inside "paypalComponent" component in laravel.


so let me explain the code.

Step 11 : again expresscheckout object is initialized, the request (which is return redirect of the client) comes with a token property and a payerID property ( is you just dd($req); you will able to see the request which is return from the paypal API along with the user redirect.

Step 12 : get the token and payerid into variables

Step 13 :  prepare the $data array. ( i used a separate method because of code repetition, you can call the makedata() method just like me if you separated the $data array preparation.)

 next run the $checkout->doExpressCheckoutPayment()  method with the parameters as shown and get the response into a variable. 

 you can dd($response) to check whats the final result or just redirect the user into a view etc.


(at this point is the response is successful that means that the payment has been colleected from the customer and added to your business paypal account balance.)

 

hope you like the guide, feel free to express your opinion about my guide and plz become an awesome programmer :p 




2 comments:

Powered by Blogger.