Laravel 8: add PayPal payments to your app in less than 5 mins

Fast, free of bullshit and junior friendly tutorial to make PayPal simple charges with Laravel.

Install the package

  1. Run this command in your project route:

    composer require srmklive/paypal:~3.0
    
  2. Publish PayPal configuration

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

    A config/paypal.php file will be created.

  3. Add this lines to your .env file in your root folder.

PAYPAL_MODE=sandbox
PAYPAL_SANDBOX_CLIENT_ID=YOUR_CLIENT_ID
PAYPAL_SANDBOX_CLIENT_SECRET=YOUR_CLIENT_SECRET

PAYPAL_LIVE_CLIENT_ID=
PAYPAL_LIVE_CLIENT_SECRET=

Where the fuck I'm gettin my Paypal credentials

  1. Follow this link developer.paypal.com/developer/applications
  2. Log in with your paypal account.
  3. On the sidebar click "My Apps and Credentials"
  4. Click default application
  5. Copy your CLIENT_ID and click "show" in order to see your SECRET token

Ok, now what?

  1. Make a controller named PaypalController

    php artisan make:controller PaypalController
    
  2. Create routes for the controller in your routes/web.php file

use App\Http\Controllers\PaypalController;

Route::get('/paypal', [PaypalController::class, 'payment']);
Route::get('/paypal/capture', [PaypalController::class, 'capture'])->name('paypal.capture');

Copy and paste this shit into your PaypalController.php file

namespace  App\Http\Controllers;
use Illuminate\Http\Request;

use  PayPal;

class  PaypalController  extends  Controller
{

  private  $paypal;

  public  function  __construct()
  {
    $provider = PayPal::setProvider();

    // Get configuration from config/paypal.php
    $provider->setApiCredentials(config('paypal'));
    // Required access token for the Api
    $provider->getAccessToken();
    // Set your currency (uppercase)
    $provider->setCurrency('USD');
    // Save into private $paypal class variable
    $this->paypal = $provider;
  }

  public function payment()
  {
      $result = $this->paypal->createOrder([
        "application_context" => [
          // Redirect buyer to the route we defined in web.php
          "return_url" => route('paypal.capture')
        ],
        "intent"=> "CAPTURE",
        "purchase_units"=> [
            [
              "description" => "My product in MXN currency",
              "amount"=> [
                "value"=> "179.00",
                "currency_code"=> "MXN",
              ]
            ]
        ]
      ]);

      return $result;
  }

  public function capture(Request $request)
  {
   // Paypal will redirect with [token] and [PayerId] GET URL parameters.
   // Capture the order to get the founds into your account
   return $this->paypal->capturePaymentOrder($request->get('token'));
  }

}

Copied, whats next?

Open your browser and go to /paypal url if everything is working ok you should be redirected to paypal.com to complete the checkout

How can I pay the order without a bank account?

Sign in with fake credentials and complete a test order?

  1. Go to this link => developer.paypal.com/developer/accounts
  2. Login if needed
  3. A list of fake accounts will be displayed
  4. Click View/Edit account under "Manage Accounts" column
  5. Copy email and password

What parameters should I send to paypal->createOrder method? developer.paypal.com/docs/api/orders/v2/#or..

How can I set Taxes, a list of items, etc? developer.paypal.com/docs/api/orders/v2/#de..

Want to know how this works?

Basically we connect to paypal with your CLIENT_ID and SECRET We are in sandbox mode by default (testing the software)

Put special attention on return_url parameter inside payment method in PaypalController. this will be the url your customer will be redirected once he completes the order.

I used the route laravel function to redirect them to /paypal/capture

Ok once your customer lands into /paypal/capture with an URL parameter provided by paypal (/paypal/capture?token=XDXDXDXDXD)

We ask Paypal Api to capture the payment and send the money to our account.

Yep.

So.

Thats it.

I spent a whole day trying to figure it out how this fucking package works.

I even opened this blog because I want you to stop loosing time

Get this api working in a record time

and start developing the real app youre making

im sure it will be amazing

so thanks for read

this is my twitter in case you want to check it out

I like videogames

twitter.com/fernandomunoz02