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.
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.
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.
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.
- Developer dashboard login
- Sandbox account
- 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
- Official PayPal PHP SDK : https://github.com/paypal/PayPal-PHP-SDK/blob/master/sample/payouts/CreateSinglePayout.php
- Paypal API Documentation: https://developer.paypal.com/docs/api/payments.payouts-batch/v1/#payouts_create
- Implementation guide – https://medium.com/@Ral15/paypal-sdk-with-laravel-5-1-9d77e3dc55ae
- Additional Laravel Packages
- https://github.com/srmklive/laravel-paypal (Did not try)