Reputation: 3
This is my connection to the database plus $apiContext
// Bootstrap and Configure
define('DB_HOST', '*');
define('DB_USERNAME', '*');
define('DB_PASSWORD', '');
define('DB_NAME', '*');
//get connection
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
include __DIR__ . '/autoload.php';
error_reporting(E_ALL);
ini_set('display_errors', '1');
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
// Replace these values by entering your own ClientId and Secret by visiting https://developer.paypal.com/webapps/developer/applications/myapps
$clientId = '*';
$clientSecret = '*';
$apiContext = new ApiContext(
new OAuthTokenCredential(
$clientId,
$clientSecret
)
);
$apiContext->setConfig(
array(
'mode' => 'sandbox',
'log.LogEnabled' == true,
'log.FileName' == 'PayPal.log',
'log.LogLevel' == 'DEBUG', // PLEASE USE `INFO` LEVEL FOR LOGGING IN LIVE ENVIRONMENTS
'cache.enabled' == true,
)
);
This is how am creating batch payouts and testing from the sandbox.
if(!$mysqli){
die("Connection failed: " . $mysqli->error);
}
$query = "SELECT * FROM payments WHERE Channel ='paypal' AND status='2' ORDER BY created_at desc LIMIT 500";
//execute query
$result = $mysqli->query($query);
// # Create Bulk Payout
use PayPal\Api\Payout;
use PayPal\Api\Currency;
use PayPal\Api\PayoutItem;
use PayPal\Api\PayoutSenderBatchHeader;
// Create a new instance of Payout object
$payouts = new \PayPal\Api\Payout();
$senderBatchHeader = new \PayPal\Api\PayoutSenderBatchHeader();
$senderBatchHeader->setSenderBatchId(uniqid())
->setEmailSubject("You Have a new Payment");
$payouts->setSenderBatchHeader($senderBatchHeader);
// #### Sender Item
$i = 0;
while($row = mysqli_fetch_assoc($result)){
$senderItem[$i] = new \PayPal\Api\PayoutItem();
$senderItem[$i]->setRecipientType('Email')
->setReceiver($row['them_email'])
->setAmount(new \PayPal\Api\Currency("{ \"value\":\"{$row['tasker_amount']}\", \"currency\":\"USD\" }"))
->setSenderItemId($row['created_at'])
->setNote($row['pay_type']);
$i++;
$payouts->addItem($senderItem);
};
var_dump($senderItem);
// ### Create Payout
try {
$payouts->create(null, $apiContext);
} catch (PayPal\Exception\PayPalConnectionException $ex) {
echo $ex->getCode(); // Prints the Error Code
echo $ex->getData(); // Prints the detailed error message
die($ex);
} catch (Exception $ex) {
die($ex);
}
This is the error code plus the exceptions
400{"name":"MALFORMED_REQUEST_ERROR","message":"Json request malformed.","debug_id":"15e4b40545d51","information_link":"https://developer.paypal.com/docs/api/payments.payouts-batch/#errors"}
PayPal\Exception\PayPalConnectionException: Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payouts?. in /Applications/XAMPP/xamppfiles/htdocs/Paypal-PHP-SDK/paypal/rest-api-sdk-php/lib/PayPal/Core/PayPalHttpConnection.php:202 Stack trace:
#0 /Applications/XAMPP/xamppfiles/htdocs/Paypal-PHP-SDK/paypal/rest-api-sdk-php/lib/PayPal/Transport/PayPalRestCall.php(73): PayPal\Core\PayPalHttpConnection->execute('{"sender_batch_...')
#1 /Applications/XAMPP/xamppfiles/htdocs/Paypal-PHP-SDK/paypal/rest-api-sdk-php/lib/PayPal/Common/PayPalResourceModel.php(104): PayPal\Transport\PayPalRestCall->execute(Array, '/v1/payments/pa...', 'POST', '{"sender_batch_...', Array) #2 /Applications/XAMPP/xamppfiles/htdocs/Paypal-PHP-SDK/paypal/rest-api-sdk-php/lib/PayPal/Api/Payout.php(121): PayPal\Common\PayPalResourceModel::executeCall('/v1/payments/pa...', 'POST', '{"sender_batch_...', NULL, Object(PayPal\Rest\ApiContext), Object(PayPal\Transport\PayPalRestCall))
#3 /Applications/XAMPP/xamppfiles/htdocs/Paypal-PHP-SDK/wunipayout.php(45): PayPal\Api\Payout->create(Array, Object(PayPal\Rest\ApiContext)) #4 {main}
This is the output from var_dump($senderItem)
array(43)
{
[0]=> object(PayPal\Api\PayoutItem)#10 (1)
{
["_propMap":"PayPal\Common\PayPalModel":private]=> array(5)
{
["recipient_type"]=> string(5) "Email"
["receiver"]=> string(14) "[email protected]"
["amount"]=> object(PayPal\Api\Currency)#11 (1)
{
["_propMap":"PayPal\Common\PayPalModel":private]=> array(2)
{
["value"]=> string(5) "12580"
["currency"]=> string(3) "USD"
}
}
["sender_item_id"]=> string(19) "2017-03-11 20:02:40"
["note"]=> string(7) "TASK_ME"
}
}
[1]=> object(PayPal\Api\PayoutItem)#12 (1)
{
["_propMap":"PayPal\Common\PayPalModel":private]=> array(5)
{
["recipient_type"]=> string(5) "Email"
["receiver"]=> string(12) "[email protected]"
["amount"]=> object(PayPal\Api\Currency)#13 (1)
{
["_propMap":"PayPal\Common\PayPalModel":private]=> array(2)
{
["value"]=> string(4) "1000"
["currency"]=> string(3) "USD"
}
}
["sender_item_id"]=> string(19) "2017-03-09 21:41:49"
["note"]=> string(4) "TASK"
}
}
[2]=> object(PayPal\Api\PayoutItem)#14 (1)
{
["_propMap":"PayPal\Common\PayPalModel":private]=> array(5)
{
["recipient_type"]=> string(5) "Email"
["receiver"]=> string(12) "[email protected]"
["amount"]=> object(PayPal\Api\Currency)#15 (1)
{
["_propMap":"PayPal\Common\PayPalModel":private]=> array(2)
{
["value"]=> string(4) "1288"
["currency"]=> string(3) "USD"
}
}
["sender_item_id"]=> string(19) "2017-03-06 18:04:47"
["note"]=> string(4) "TASK"
}
}
[3]=> object(PayPal\Api\PayoutItem)#16 (1)
{
["_propMap":"PayPal\Common\PayPalModel":private]=> array(5)
{
["recipient_type"]=> string(5) "Email"
["receiver"]=> string(13) "[email protected]"
["amount"]=> object(PayPal\Api\Currency)#17 (1)
{
["_propMap":"PayPal\Common\PayPalModel":private]=> array(2)
{
["value"]=> string(1) "3"
["currency"]=> string(3) "USD"
}
}
["sender_item_id"]=> string(19) "2017-03-05 22:41:56"
["note"]=> string(7) "PROJECT"
}
}
}
Help me figure out why am getting this error am new to the PayPal Payouts API.
Upvotes: 0
Views: 229
Reputation: 9142
You should not be forming your own JSON string and passing it.
Instead, create an object using stdClass
and assign properties and values to it. Then, use json_encode
to pass to PayPal.
For example (excerpt, untested):
// Create empty object
$payAmount = new stdClass;
// Assign object attributes
$payAmount->value = $row['tasker_amount'];
$payAmount->currency = "USD";
// Create currency object
$currency = new \PayPal\Api\Currency(
json_encode($payAmount)
);
$senderItem[$i]->setRecipientType('Email')
->setReceiver($row['email'])
->setAmount($currency)
->setSenderItemId($row['created_at'])
->setNote($row['pay_type']);
I believe the issue comes from when you're calling the addItem
method. This may solve your problem (use this entire loop). This also uses the modifications above:
while($row = mysqli_fetch_assoc($result)) {
// Create empty object
$payAmount = new stdClass;
// Assign object attributes
$payAmount->value = $row['tasker_amount'];
$payAmount->currency = "USD";
// Create currency object
$currency = new \PayPal\Api\Currency(
json_encode($payAmount)
);
$senderItem = new \PayPal\Api\PayoutItem();
$senderItem->setRecipientType('Email')
->setReceiver($row['them_email'])
->setAmount($currency)
->setSenderItemId($row['created_at'])
->setNote($row['pay_type']);
$payouts->addItem($senderItem);
};
Notice that I dropped $i
, as it's useless in this case. You are appending the item in each iteration, so counting the item doesn't matter.
One last edit I could throw in here... Instead of creating a stdClass
and json_encode
ing it, you could use the methods that the Currency
class has in the Paypal SDK:
while($row = mysqli_fetch_assoc($result)) {
$amount = new \PayPal\Api\Currency();
$amount->setCurrency("USD");
$amount->setValue((float)$row['tasker_amount']); // float values only
$senderItem = new \PayPal\Api\PayoutItem();
$senderItem->setRecipientType('Email')
->setReceiver($row['them_email'])
->setAmount($amount)
->setSenderItemId($row['created_at'])
->setNote($row['pay_type']);
$payouts->addItem($senderItem);
};
Upvotes: 2