PayPal server side integration Laravel & VueJS [ Easiest Way ][ Updated 2019 ]

PayPal-server-side-integration-Laravel-VueJS-Easiest-Way-Updated-2019

PayPal server side integration Laravel & VueJS [ Easiest Way ][ Updated 2019 ]



So today we are going to integrate Paypal into our laravel-Vuejs project. Here is the demo





[ads]



PayPal server side integration Laravel & VueJS [ Easiest Way ][ Updated 2019 ]





So to install Paypal SDK in laravel you can use Check



[ads]





Below given command will work, Make sure you have composer and nodejs installed before running this command.



     composer require paypal/rest-api-sdk-php:*

You can include a link in your layout or where you include scripts





  

Make sure put a link above Paypal code that we are going to add in a moment.



[ads]





Here are some steps to follow



1.Setup Sandbox Account

Login into Paypal developer website





Once Logged in go under SANDBOX / Accounts



[ads]





PayPal server side integration Laravel & VueJS [ Easiest Way ][ Updated 2019 ]



Here you should see two email id and on clicking on them you can change the password for each of the sandbox emails.Using these emails you can log in www.sandbox.paypal.com





[ads]
PayPal-server-side-integration-Laravel-VueJS-Easiest-Way-Updated-2019



Buyer account is a demo buyer we some balance in it. So you can make and test purchases before putting integration on the live website.





facilitator account is for the seller, there you can check if payment received and order placed.



Working With REST API

At this point in time, we need to create a Rest API App.





[ads]



Under MY APPS / CREDENTIALS
PayPal server side integration Laravel & VueJS [ Easiest Way ][ Updated 2019 ]





It Should Look Like This And You Should have your client ID and secret once the app is ready.



To create it Just click “Create” Button and provide “App NAME”. and that’s it.





[ads]



PayPal-server-side-integration-Laravel-VueJS-Easiest-Way-Updated-2019





Now We can proceed to code.
You can get Paypal untouched code from here



[ads]





Code that we use in our project is given below



Add button using below div, Where ever you want to display PayPal button.





[ads]
 

Button will be visible once rendered so you need to add below given code



              // Render the PayPal button into #paypal-button-container
        paypal.Buttons({
            
            // Set up the transaction
            createOrder: function(data, actions) {
                var payableAmount = $("#finalAmountTotalOrders").attr("amountData");  
                return actions.order.create({
                    purchase_units: [{
                        amount: {
                            value: payableAmount
                        }
                    }]
                });
            },

            // Finalize the transaction
            onApprove: function(data, actions) {
                return actions.order.capture().then(function(details) {
                    //will redirect user to custom page change values as desired
                    window.location.replace("/purchase/complete"); 
                    // Call your server to save the transaction
                    return fetch('/paypal/purchase/complete', {
                    method: 'post',
                    headers: {
                        'content-type': 'application/json',
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    body: JSON.stringify({
                        orderID: data.orderID,
                        details:details

                    })
                   
                    });
                   
                });
                }


        }).render('#paypal-button-container');

Replace “payableAmount” with the purchasing total amount. Then calling to server you need to create a route. I defined route “/paypal/purchase/complete” Like this in code above.





     //paypal route
     Route::post('/paypal/purchase/complete',[
          'uses'=>'PaypalController@getOrder'
     ]);

Here route is using “PaypalController And Method getOrder”.Lets see the code in controller



     all();
        return $data;
        //using $data save all details to database.
        //check if $data is empty or not by dd($data) or print_r($data)
   }
}

Using print_r() i am getting this result





     Array
(
    [orderID] => 727019011T0564344
    [payerID] => 9ED9387K2DJ68
    [details] => Array
        (
            [create_time] => 2019-07-27T02:29:47Z
            [update_time] => 2019-07-27T02:30:02Z
            [id] => 727019011T0564344
            [intent] => CAPTURE
            [status] => COMPLETED
            [payer] => Array
                (
                    [email_address] => uk-buyer@gmail.com
                    [payer_id] => 9ED9387K2DJ68
                    [address] => Array
                        (
                            [country_code] => AU
                        )

                    [name] => Array
                        (
                            [given_name] => test
                            [surname] => buyer
                        )

                )

            [purchase_units] => Array
                (
                    [0] => Array
                        (
                            [reference_id] => default
                            [amount] => Array
                                (
                                    [value] => 1.00
                                    [currency_code] => USD
                                )

                            [payee] => Array
                                (
                                    [email_address] => uk-facilitator@gmail.com
                                    [merchant_id] => FS5H99JM89XQU
                                )

                            [shipping] => Array
                                (
                                    [name] => Array
                                        (
                                            [full_name] => test buyer
                                        )

                                    [address] => Array
                                        (
                                            [address_line_1] => 1 Cheeseman Ave Brighton East
                                            [admin_area_2] => Melbourne
                                            [admin_area_1] => Victoria
                                            [postal_code] => 3001
                                            [country_code] => AU
                                        )

                                )

                            [payments] => Array
                                (
                                    [captures] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [status] => COMPLETED
                                                    [id] => 7NF09541FU9824623
                                                    [final_capture] => 1
                                                    [create_time] => 2019-07-27T02:30:02Z
                                                    [update_time] => 2019-07-27T02:30:02Z
                                                    [amount] => Array
                                                        (
                                                            [value] => 1.00
                                                            [currency_code] => USD
                                                        )

                                                    [seller_protection] => Array
                                                        (
                                                            [status] => ELIGIBLE
                                                            [dispute_categories] => Array
                                                                (
                                                                    [0] => ITEM_NOT_RECEIVED
                                                                    [1] => UNAUTHORIZED_TRANSACTION
                                                                )

                                                        )

                                                    [links] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [href] => https://api.sandbox.paypal.com/v2/payments/captures/7NF09541FU9824623
                                                                    [rel] => self
                                                                    [method] => GET
                                                                    [title] => GET
                                                                )

                                                            [1] => Array
                                                                (
                                                                    [href] => https://api.sandbox.paypal.com/v2/payments/captures/7NF09541FU9824623/refund
                                                                    [rel] => refund
                                                                    [method] => POST
                                                                    [title] => POST
                                                                )

                                                            [2] => Array
                                                                (
                                                                    [href] => https://api.sandbox.paypal.com/v2/checkout/orders/727019011T0564344
                                                                    [rel] => up
                                                                    [method] => GET
                                                                    [title] => GET
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

            [links] => Array
                (
                    [0] => Array
                        (
                            [href] => https://api.sandbox.paypal.com/v2/checkout/orders/727019011T0564344
                            [rel] => self
                            [method] => GET
                            [title] => GET
                        )

                )

        )

)

[ads]



That’s it you have Working PayPal payment system in your website.





[ads]



If everything goes fine you should see transactions in PayPal sandbox buyer account as well.
Something Like This





[ads]
PayPal server side integration Laravel & VueJS [ Easiest Way ][ Updated 2019 ]



[ads]