Search code examples
laravelpaypalpaypal-sandbox

How to pay users' withdrawal amounts through paypal using their details in Laravel?


I have a number of PayPal user accounts, and I wish to use those accounts to pay those users' withdrawal amounts through PayPal.

I have multiple user details. I have their paypal details, too.

By just using their paypal id, I want to transfer them their withdrawal amount.

Right now I have integrated PayPal in my Laravel application. But, their first step is "Checkout". I just want to enter their paypal ID and send their withdrawal amount.


Solution

  • Install the PayPal SDK:

    Install the PayPal SDK in your Laravel project using Composer: composer require paypal/rest-api-sdk-php. Configure PayPal settings:

    Add your PayPal credentials to the .env file of your Laravel project:

    PAYPAL_CLIENT_ID=your_paypal_client_id
    PAYPAL_SECRET=your_paypal_secret
    

    You can obtain the client ID and secret by creating a PayPal developer account and creating an application in the dashboard.

    Create a Payout Service: Generate a new service in Laravel using the following command: 'php artisan make:service PayoutService'. Implement Payout functionality: Open the app/Services/PayoutService.php file and update it with the following code:

    <?php
    
    namespace App\Services;
    
    use PayPal\Auth\OAuthTokenCredential;
    use PayPal\Rest\ApiContext;
    use PayPal\Api\Payout;
    use PayPal\Api\PayoutSenderBatchHeader;
    use PayPal\Api\Currency;
    
    class PayoutService
    {
        protected $apiContext;
    
        public function __construct()
        {
            $this->apiContext = new ApiContext(
                new OAuthTokenCredential(
                    config('paypal.client_id'),
                    config('paypal.secret')
                )
            );
        }
    
        public function payout($paypalEmail, $amount, $currency = 'USD')
        {
            $payouts = new Payout();
    
            $senderBatchHeader = new PayoutSenderBatchHeader();
    
            $senderBatchHeader->setSenderBatchId(uniqid())
                ->setEmailSubject('Withdrawal Amount')
                ->setRecipientType('Email');
    
            $item = new Currency();
            $item->setValue($amount)->setCurrency($currency);
    
            $payouts->setSenderBatchHeader($senderBatchHeader)
                ->addItem($item)
                ->setRecipientType('Email')
                ->setEmailSubject('Withdrawal Amount')
                ->setItems([
                    [
                        'recipient_type' => 'EMAIL',
                        'amount' => [
                            'value' => $amount,
                            'currency' => $currency
                        ],
                        'receiver' => $paypalEmail,
                        'note' => 'Withdrawal Amount'
                    ]
                ]);
    
            $payout = $payouts->create(null, $this->apiContext);
    
            return $payout;
        }
    
    }
    

    This code initializes the ApiContext with your PayPal credentials. The payout method creates a new payout object to send money to the specified PayPal email address with the defined amount.

    Use the Payout Service: In your controller or wherever you want to initiate the payout, import the PayoutService:

    use App\Services\PayoutService;
    

    Inject the PayoutService in your controller's method:

    public function payWithdrawal(PayoutService $payoutService)
    {
        $paypalEmail = 'recipient_email@example.com';
        $amount = '10.00'; // withdrawal amount
        $currency = 'USD';
    
        $payout = $payoutService->payout($paypalEmail, $amount, $currency);
    
        // Handle the payout response
        // You can check $payout->getBatchHeader()->getBatchStatus() to verify if the payout was successful
        // You can also obtain the payout details using $payout->getLinks()
    
        return response()->json(['message' => 'Payout initiated'], 200);
    }
    

    Provide the PayPal email address of the user and the withdrawal amount to the payout method in your controller. You can then handle the payout response accordingly.

    Routes and Views: Create a route to handle the payout initiation by adding the following to your routes file (routes/web.php or routes/api.php):

    use App\Http\Controllers\PaymentController;
    Route::post('/payout', [PaymentController::class, 'payWithdrawal']);
    

    You can create a form in your view to gather the withdrawal amount and PayPal email address. On submitting the form, make a POST request to the /payout endpoint. Make sure to handle any exceptions or errors that might occur during the process for a better user experience.