Angad Dubey
Angad Dubey

Reputation: 5452

Paypal PHP SDK bulk payout

I am trying to test Paypals batch payout:

My code:

// ...

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;


    }

But I keep getting a 400.

I suspect its due to this being inside the loop:

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

when Paypals examples are like this:

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

;

How do I add items inside the loop?

EDIT: Error:

PayPalConnectionException in PayPalHttpConnection.php line 180:
Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payouts?.

Thankyou.

Upvotes: 1

Views: 590

Answers (1)

Angad Dubey
Angad Dubey

Reputation: 5452

I managed to fix the issue by changing the $senderBatchId value to just:

uniqid()

instead of:

uniqid() . microtime(true)

Paypal REST API does not like it that way for some reason. If a better programmer than I can explain this I will mark his/her answer are correct.

Upvotes: 1

Related Questions