Wednesday, March 18, 2020

Laravel 6/7 Paypal Integration

Laravel 6/7 Paypal Integration


Hello Buddies

In this tutorial I would like to take you step by step through the example of the PayPal integration of Laravel 6 and the PayPal integration of Laravel 7. We can easily integrate the PayPal payment gateway into Laravel 7/6. I wrote the PayPal Laravel 7/6 integration so that your user can easily pay for the PayPal account and credit card information.

We use the integrated Paypal API with the srmklive Laravel Paypal package. The package srmklive / laravel-paypal offers API methods for PayPal code. We will use the express payment method in the Laravel 6/7 application.

As we know, the Paypal payment gateway is a popular gateway in web development. Almost the customer or customers prefer to use the PayPal payment gateway to transfer money to their website. Paypal is an easy to use gateway to transfer words from all over the world.

In this tutorial, we are using the srmklive package for Laravel Paypal Integra in Laravel 6. You only need to take a few steps to complete payment integration in PHP Laravel 6.


Step 1: Install Laravel 6/7


We go from scratch. If you don't have Laravel installed on your system, you can run the following command and get a new project from Laravel.

composer create-project --prefer-dist laravel/laravel blog


Step 2: Install Composer Package


Now we need to install the srmklive / paypal package for PayPal integration so we can use its method. Then open your terminal and run the following command.

composer require srmklive/paypal

Now open the file config / app.php and add the service provider and the alias.

config/app.php
'providers' => [

....

Srmklive\PayPal\Providers\PayPalServiceProvider::class

]

....


We can also adjust the changes in the srmklive / paypal package. If you also want to make changes, you can activate the following command and call up the configuration file in config / paypal.php.

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

You can see the paypal.php file as follows:

config/paypal.php
<?php

/**

* PayPal Setting & API Credentials

* Created by Raza Mehdi .

*/

return [

'mode'    => env('PAYPAL_MODE', 'sandbox')

'sandbox' => [

'username'    => env('PAYPAL_SANDBOX_API_USERNAME', ''),

'password'    => env('PAYPAL_SANDBOX_API_PASSWORD', ''),

'secret'      => env('PAYPAL_SANDBOX_API_SECRET', ''),

'certificate' => env('PAYPAL_SANDBOX_API_CERTIFICATE', ''),

'app_id'      => 'APP-80W284485P519543T',

],

'live' => [

'username'    => env('PAYPAL_LIVE_API_USERNAME', ''),

'password'    => env('PAYPAL_LIVE_API_PASSWORD', ''),

'secret'      => env('PAYPAL_LIVE_API_SECRET', ''),

'certificate' => env('PAYPAL_LIVE_API_CERTIFICATE', ''),

'app_id'      => '',

],

'payment_action' => 'Sale',

'currency'       => env('PAYPAL_CURRENCY', 'USD'),

'billing_type'   => 'MerchantInitiatedBilling',

'notify_url'     => '',

'locale'         => '',

'validate_ssl'   => false,

];


Step 3: Add Routes


Here we need to add a resource path for the PayPal payment gateway. Open your "route / web.php" file and add the following route.

routes/web.php
Route::get('payment', 'PayPalController@payment')->name('payment');

Route::get('cancel', 'PayPalController@cancel')->name('payment.cancel');

Route::get('payment/success', 'PayPalController@success')->name('payment.success');


Step 4: Create Controller


In this step, we should now create a new controller like PayPalController. Then run the following command and create a new controller. to create under the controller using some methods.

php artisan make:controller PayPalController

After the following command you will find a new file in this path "app / Http / Controllers / PayPalController.php".

app/Http/Controllers/PayPalController.php
<?php

  

namespace App\Http\Controllers;

  

use Illuminate\Http\Request;

use Srmklive\PayPal\Services\ExpressCheckout;

   

class PayPalController extends Controller

{

    /**

     * Responds with a welcome message with instructions

     *

     * @return \Illuminate\Http\Response

     */

    public function payment()

    {

        $data = [];

        $data['items'] = [

            [

                'name' => 'ItSolutionStuff.com',

                'price' => 100,

                'desc'  => 'Description for ItSolutionStuff.com',

                'qty' => 1

            ]

        ];

  

        $data['invoice_id'] = 1;

        $data['invoice_description'] = "Order #{$data['invoice_id']} Invoice";

        $data['return_url'] = route('payment.success');

        $data['cancel_url'] = route('payment.cancel');

        $data['total'] = 100;

  

        $provider = new ExpressCheckout;

  

        $response = $provider->setExpressCheckout($data);

  

        $response = $provider->setExpressCheckout($data, true);

  

        return redirect($response['paypal_link']);

    }

   

    /**

     * Responds with a welcome message with instructions

     *

     * @return \Illuminate\Http\Response

     */

    public function cancel()

    {

        dd('Your payment is canceled. You can create cancel page here.');

    }

  

    /**

     * Responds with a welcome message with instructions

     *

     * @return \Illuminate\Http\Response

     */

    public function success(Request $request)

    {

        $response = $provider->getExpressCheckoutDetails($request->token);

  

        if (in_array(strtoupper($response['ACK']), ['SUCCESS', 'SUCCESSWITHWARNING'])) {

            dd('Your payment was successfully. You can create success page here.');

        }

  

        dd('Something is wrong.');

    }

}


Step 5: Create View File


In this step we need to update the welcome.blade.php file. A button for the PayPal payment gateway is inserted in this file. So let's enter the following code:

resources/views/products/welcome.blade.php
<!doctype html>

<html>

    <head>

        <meta charset="utf-8">

        <meta name="viewport" content="width=device-width, initial-scale=1">

  

        <title>Laravel 6 PayPal Integration Tutorial - ItSolutionStuff.com</title>

  

        <!-- Fonts -->

        <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">

        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha256-YLGeXaapI0/5IgZopewRJcFXomhRMlYYjugPLSyNjTY=" crossorigin="anonymous" />

  

        <!-- Styles -->

        <style>

            html, body {

                background-color: #fff;

                color: #636b6f;

                font-family: 'Nunito', sans-serif;

                font-weight: 200;

                height: 100vh;

                margin: 0;

            }

            .content {

                margin-top: 100px;

                text-align: center;

            }

        </style>

    </head>

    <body>

        <div class="flex-center position-ref full-height">

  

            <div class="content">

                <h1>Laravel 6 PayPal Integration Tutorial - ItSolutionStuff.com</h1>

                  

                <table border="0" cellpadding="10" cellspacing="0" align="center"><tr><td align="center"></td></tr><tr><td align="center"><a href="https://www.paypal.com/in/webapps/mpp/paypal-popup" title="How PayPal Works" onclick="javascript:window.open('https://www.paypal.com/in/webapps/mpp/paypal-popup','WIPaypal','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=1060, height=700'); return false;"><img src="https://www.paypalobjects.com/webstatic/mktg/Logo/pp-logo-200px.png" border="0" alt="PayPal Logo"></a></td></tr></table>

  

                <a href="{{ route('payment') }}" class="btn btn-success">Pay $100 from Paypal</a>

  

            </div>

        </div>

    </body>

</html>


Step 6: Add Configuration


In this step, we set the configuration value such as PayPal username, secret and certificate key in the ENV file.

.env
PAYPAL_MODE=sandbox

PAYPAL_SANDBOX_API_USERNAME=sb-e2n47..

PAYPAL_SANDBOX_API_PASSWORD=XKCGW...

PAYPAL_SANDBOX_API_SECRET=A0EXIz....

PAYPAL_CURRENCY=INR

PAYPAL_SANDBOX_API_CERTIFICATE=


We are now ready to run our example of this application with Laravel 6. Therefore, run the following command to speed up execution:

php artisan serve
You can now open the following URL in your browser:

http://localhost:8000/


I hope this laravel 6/7 paypal integration tutorial could help you .

Thaaaanks….




0 comments:

Post a Comment

Please don't enter any spam link in the comment box.