Paypal PHP SDK批量支付

时间:2015-08-23 21:48:17

标签: php laravel paypal

我正在尝试测试Paypals批量付款:

我的代码:

<div style="display:inline-block">
    <p>
      <em>
          <img src="images/quotes-1.jpg" width="45" height="35" style="float:left" alt="quotations"/>  
          Everything seems perfect from far away
          <img src="images/quotes-2.jpg" width="45" height="35" style="float:right" alt="quotations"/>
      </em>
   </p>
</div>

但我一直得到400。

我怀疑它是因为它在循环中:

// ...

public function sendBatchPayment(array $payoutItems, $senderBatchId, $type= 'Email',  $currency = 'CAD', $note = '') {

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

        $senderBatchHeader = new \PayPal\Api\PayoutSenderBatchHeader();

        $senderBatchHeader->setSenderBatchId($senderBatchId)
                            ->setEmailSubject("You have a Payout!");



        foreach ($payoutItems as $payoutItem) {

            $emailExists = (isset($payoutItem['email']) && !empty($payoutItem['email']));

            $amountExists = (isset($payoutItem['amount']) && !empty($payoutItem['amount']));

            $senderItemIdExists = (isset($payoutItem['sender_item_id']) && !empty($payoutItem['sender_item_id']));

            if(!$emailExists || !$amountExists || !$senderItemIdExists) continue;

            $receiver = $payoutItem['email'];

            $item_id = $payoutItem['sender_item_id'];

            $value = (float) $payoutItem['amount'];

            $senderItem = new \PayPal\Api\PayoutItem();

            $senderItem->setRecipientType('Email')
                        ->setNote('Your Payment')
                        ->setReceiver($receiver)
                        ->setSenderItemId($item_id)
                        ->setAmount(new \PayPal\Api\Currency('{
                                            "value":$value,
                                            "currency":"CAD"
                                        }'));


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


        } // EO foreach

        $request = clone $payouts;

        try {

            $output = $payouts->create(null, $this->apiContext);

        } catch (Exception $e) {

            return $e->getMessage;
        }

        return $output;


    }

当Paypals示例如下:

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

如何在循环中添加项目?

编辑: 错误:

$payouts->setSenderBatchHeader($senderBatchHeader)
                        ->addItem($senderItem1)
                        ->addItem($senderItem2);

;

三江源。

1 个答案:

答案 0 :(得分:1)

我设法通过将$ senderBatchId值更改为:

来解决此问题
uniqid()

而不是:

uniqid() . microtime(true)

Paypal REST API出于某种原因不喜欢这种方式。如果一个比我更好的程序员可以解释这个,我会标记他/她的答案是正确的。