Making a Payment Using Paypal Payout

What you need

First and foremost make sure you have the following

1. Login to Developer Dashboard: https://developer.paypal.com and create a PayPal Sandbox account and add funds. If you want to pay others from your business account then Create a ‘BUSINESS’ type user. Otherwise, if you want to purchase as a shopper you should create ‘PERSONAL’ type user.

2019-09-23 21_22_05-Activity - PayPal
2019-09-23 20_53_10-Sandbox accounts _ PayPal Developer

2. Create an app for the Sandbox Account. You should get CLIENT_ID and SECRET for this app which will be required for authenticating API calls later in your application. Make sure the app has the necessary permission.

2019-09-23 21_02_37-Summary - PayPal

3. Log-in to Sandbox https://www.sandbox.paypal.com and make sure the user/account has sufficient fund in the currencies you want to do the transaction. Note – If you need multiple currencies then you must have sufficient fund for in all the currencies.

2019-09-23 21_19_51-Edit Applications - PayPal Developer

Now you have everything that you need to move on to the coding part. Here is the list of things that you should have by now.

  1. Developer dashboard login
  2. Sandbox account
  3. Sandbox App CLIENT_ID and SECRET

Code

"paypal/rest-api-sdk-php": "*"

2. Update config/paypal.php and include the Sandbox App CLIENT_ID and SECRET. A smart approach is to create a couple of  .env vars

PAYPAL_CLIENT_ID='AVeNF_OsmXeg1TNxoUgL4BXDYZrploB0-G_NinSxvIY4Oi6SHOaJHYtok7n3kx_0twJBuz'
PAYPAL_SECRET='EEvSwYJQaDKH1cy6DOuhSw4hWwxZZCZKKhtwdL0BCCEBCucYtyzxS3h2x5foa9PJMdmoav'
<?php

namespace App\Classes;

use App\Invoice;
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;

class Paypal
{
    private $_api_context;

    public function __construct()
    {
        // setup PayPal api context
        $this->_api_context = new ApiContext(new OAuthTokenCredential(conf('paypal.client_id'), conf('paypal.secret')));
        $this->_api_context->setConfig(conf('paypal.settings'));

    }

    public function pay(Invoice $invoice)
    {


        $payouts = new \PayPal\Api\Payout();

        /*
         * {
                    "sender_batch_header":{
                        "sender_batch_id":"2014021801",
                        "email_subject":"You have a Payout!"
                    },
                    "items":[
                        {
                            "recipient_type":"EMAIL",
                            "amount":{
                                "value":"1.0",
                                "currency":"USD"
                            },
                            "note":"Thanks for your patronage!",
                            "sender_item_id":"2014031400023",
                            "receiver":"shirt-supplier-one@mail.com"
                        }
                    ]
                }
         */

        $senderBatchHeader = new \PayPal\Api\PayoutSenderBatchHeader();
        $senderBatchHeader->setSenderBatchId($invoice->code)
            ->setEmailSubject("You have a Payout!");

        $senderItem = new \PayPal\Api\PayoutItem();
        $senderItem->setRecipientType('Email')
            ->setNote('Thanks for your patronage!')
            ->setReceiver('shirt-supplier-one@gmail.com')
            ->setSenderItemId("2014031400023")
            ->setAmount(new \PayPal\Api\Currency('{
                        "value":"1.0",
                        "currency":"USD"
                    }'));

        $payouts->setSenderBatchHeader($senderBatchHeader)
            ->addItem($senderItem);

        // $request = clone $payouts;

        try {
            // $output = $payouts->createSynchronous($this->_api_context);
            $output = $payouts->create(array('sync_mode' => 'false'), $this->_api_context);
        } catch (\Exception $e) {
            return setError($e->getMessage());
        }
        return $output;
    }
}

Additional resources

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.