Search code examples
phplaravelpaypal

Laravel Paypal - Let vendors link paypal account in settings page


I have a laravel application where people act as buyers and sellers. Before a seller can sell his product, he should link his paypal account over a button. If they click the button, they should get the paypal-login screen and after login they get send back to my page where i can fetch the email from the paypal account and save it to my database. I will act as escrow and hold the money from the buyers until the buyer received his good from the seller and after that i can send the money to the seller (check linked email adress when the seller linked his paypal profile) and send it there.

Now the problem is, that im not able to get the email adress. I dont see any examples helping me. I dont want to make any payments, its just about linking paypal account to my user account before the seller is able to create products to sell. How do I only receive the paypal email so I can save it in my database to the user?

I tried it like this but its depriciated it seems

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Srmklive\PayPal\Services\PayPal as PayPalClient;
use Illuminate\Support\Facades\Redirect;

class PayPalController extends Controller
{
    // Step 1: Redirect to PayPal OAuth page
    public function connect(Request $request)
    {
        $provider = new PayPalClient;
        $provider->setApiCredentials(config('paypal'));

        // Access Token holen
        $accessToken = $provider->getAccessToken();

        // OAuth-URL generieren und den Benutzer dorthin weiterleiten
        return redirect()->away("https://www.sandbox.paypal.com/connect?flowEntry=static&client_id=" . config('paypal.sandbox.client_id') . "&scope=openid&redirect_uri=" . route('paypal.callback'));

    }

    public function handlePayPalCallback(Request $request)
    {
        $provider = new PayPalClient;
        $provider->setApiCredentials(config('paypal'));

        // Hier kommt der Authorization Code aus dem Request
        $authCode = $request->input('code');

        // Tausche den Authorization Code gegen ein Access Token
        $tokenData = $provider->getAccessToken([
            'grant_type' => 'authorization_code',
            'code'       => $authCode,
        ]);

        // Abrufen der Benutzerinformationen
        $userInfo = $provider->getUserInfo($tokenData['access_token']);

        // E-Mail-Adresse des Benutzers anzeigen
        dd($userInfo['email']);
    }


}

Solution

  • You can obtain the seller's PayPal email by integrating PayPal with Laravel Socialite. Here's how to do it:

    composer require laravel/socialite
    composer require socialiteproviders/paypal
    

    Add the PayPal provider to your config/services.php:

    'paypal' => [
        'client_id'     => env('PAYPAL_CLIENT_ID'),
        'client_secret' => env('PAYPAL_CLIENT_SECRET'),
        'redirect'      => env('PAYPAL_REDIRECT_URI'),
    ],
    

    Set your PayPal credentials in the .env file:

    PAYPAL_CLIENT_ID=your_paypal_client_id
    PAYPAL_CLIENT_SECRET=your_paypal_client_secret
    PAYPAL_REDIRECT_URI=https://yourdomain.com/paypal/callback
    

    Update your routes in routes/web.php:

    use App\Http\Controllers\PayPalController;
    
    Route::get('/paypal/connect', [PayPalController::class, 'redirectToProvider'])->name('paypal.connect');
    Route::get('/paypal/callback', [PayPalController::class, 'handleProviderCallback'])->name('paypal.callback');
    

    Modify your PayPalController:

    namespace App\Http\Controllers;
    
    use Laravel\Socialite\Facades\Socialite;
    
    class PayPalController extends Controller
    {
        public function redirectToProvider()
        {
            return Socialite::driver('paypal')->redirect();
        }
    
        public function handleProviderCallback()
        {
            $paypalUser = Socialite::driver('paypal')->user();
    
            // Save PayPal email to the seller's profile
            $user = auth()->user();
            $user->paypal_email = $paypalUser->getEmail();
            $user->save();
    
            return redirect()->route('dashboard')->with('success', 'PayPal account linked successfully.');
        }
    }
    

    Ensure the provider is loaded by adding the event listener in app/Providers/EventServiceProvider.php:

    protected $listen = [
        \SocialiteProviders\Manager\SocialiteWasCalled::class => [
            'SocialiteProviders\\PayPal\\PayPalExtendSocialite@handle',
        ],
    ];