PayPal Single Payout to an Email

I am trying to integrate PayPal's Payout option to be able to pay users directly by providing their email.

I am calling the controller function through a form with POST method, after I call the simplePay function, all I get is a blank page, I would like to know if what I'm doing is correct, incomplete (and what its lacking), or wrong (and what am I doing wrong).

I've based my research on code samples provided by Paypal Documentation. https://github.com/paypal/PayPal-PHP-SDK/blob/master/sample/payouts/CreateBatchPayout.php

Route:

Route::post('/simplePay', 'PaypalController@simplePay');

Controller:

    <?php
    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use PayPal\Rest\ApiContext;
    use PayPal\Api\Amount;
    use PayPal\Api\Details;
    use PayPal\Api\Item;
    use PayPal\Api\ItemList;
    use PayPal\Api\Payer;
    use PayPal\Api\Payment;
    use PayPal\Api\Transaction;
    use PayPal\Api\RedirectUrls;
    use PayPal\Api\ExecutePayment;
    use PayPal\Api\PaymentExecution;
    use PayPal\Auth\OAuthTokenCredential;

    class PaypalController extends Controller
    {

      private $_api_context;
        public function __construct()
        {
            // setup PayPal api context
            $paypal_conf = \Config::get('paypal');
            $this->_api_context = new ApiContext(new OAuthTokenCredential($paypal_conf['client_id'], $paypal_conf['secret']));
            $this->_api_context->setConfig($paypal_conf['settings']);
        }
        public function simplePay()
        {
            $payouts = new \PayPal\Api\Payout();
            $senderBatchHeader = new \PayPal\Api\PayoutSenderBatchHeader();
            $senderBatchHeader->setSenderBatchId(uniqid())->setEmailSubject("You have a Payout!");
            $senderItem = new \PayPal\Api\PayoutItem();
            $senderItem->setRecipientType('Email')->setNote('Thanks for your patronage!')->setReceiver('[email protected]')->setSenderItemId("001")->setAmount(new \PayPal\Api\Currency('{
                                    "value":"1.00",
                                    "currency":"MXN"
                                }'));
            $payouts->setSenderBatchHeader($senderBatchHeader)->addItem($senderItem);
            $request = clone $payouts;
            try {
                $output = $payouts->createSynchronous($this->_api_context);
            } catch (\Exception $ex) {
                //  \ResultPrinter::printError("Created Single Synchronous Payout", "Payout", null, $request, $ex);
                exit(1);
            }
            // \ResultPrinter::printResult("Created Single Synchronous Payout", "Payout", $output->getBatchHeader()->getPayoutBatchId(), $request, $output);
            return $output;
        }
    }

Call to controller:

                    <div class="row">
                        <form class="form-horizontal" role="form" enctype="multipart/form-data" method="POST" action="{{ url('/simplePay') }}">
                        {{csrf_field()}}

                        <input type="submit" name="submit" value="Pay">
                        </form>

                    </div>

paypal.php - app/config file - Just in Case:

<?php
return array(
    // set your paypal credential
    'client_id' => 'AXs...',
    'secret' => 'AHE...',

    /**
     * SDK configuration
     */
    'settings' => array(
        /**
         * Available option 'sandbox' or 'live'
         */
        'mode' => 'sandbox',

        /**
         * Specify the max request time in seconds
         */
        'http.ConnectionTimeOut' => 12000,

        /**
         * Whether want to log to a file
         */
        'log.LogEnabled' => true,

        /**
         * Specify the file that want to write on
         */
        'log.FileName' => storage_path() . '/logs/paypal.log',

        /**
         * Available option 'FINE', 'INFO', 'WARN' or 'ERROR'
         *
         * Logging is most verbose in the 'FINE' level and decreases as you
         * proceed towards ERROR
         */
        'log.LogLevel' => 'FINE'
    ),
);

Upvotes: 2

Views: 2206

Answers (1)

Gramatton
Gramatton

Reputation: 385

PayPal sync mode is not available for new integrations. Existing integrations will continue to function if your passing "sync_mode = true".

To solve the issue, you need to pass "sync_mode = false" so instead of

$output = $payouts->createSynchronous($this->_api_context);

use the following:

$output = $payouts->create(array('sync_mode' => 'false'), $this->_api_context);

Upvotes: 3

Related Questions