PayPal单一付款到电子邮件

时间:2018-02-16 19:57:00

标签: php laravel paypal paypal-rest-sdk

我正在尝试整合PayPal的付款选项,以便能够通过提供电子邮件直接向用户付款。

我通过POST方法调用控制器函数,在调用simplePay函数后,我得到的只是一个空白页面,我想知道我所做的是否正确,不完整(和它缺少什么,或者错误(以及我做错了什么)。

我的研究基于Paypal文档提供的代码示例。 https://github.com/paypal/PayPal-PHP-SDK/blob/master/sample/payouts/CreateBatchPayout.php

路线:

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

控制器:

    <?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@gmail.com')->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;
        }
    }

致电控制器:

                    <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 - 以防万一:

<?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'
    ),
);

1 个答案:

答案 0 :(得分:0)

PayPal同步模式不适用于新集成。如果您通过"sync_mode = true",则现有的集成功能将继续起作用。

要解决此问题,您需要通过"sync_mode = false",而不是

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

使用以下内容:

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