这是我与数据库的连接以及$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,
)
);
这是从沙箱创建批量支付和测试的方式。
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);
}
这是错误代码加上例外
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}
这是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) "jill@gmail.com"
["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) "me@gmail.com"
["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) "me@gmail.com"
["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) "tex@gmail.com"
["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"
}
}
}
帮我弄清楚为什么我的PayPal Payouts API会出现这个错误。
答案 0 :(得分:2)
你不应该形成自己的JSON字符串并传递它。
相反,使用stdClass
创建一个对象,并为其指定属性和值。然后,使用json_encode
传递给PayPal。
例如(摘录,未经测试):
// 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']);
我认为问题来自您何时调用addItem
方法。这可以解决您的问题(使用整个循环)。这也使用了上面的修改:
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);
};
请注意,我删除了$i
,因为在这种情况下它没用。您在每次迭代中追加该项目,因此计算该项目并不重要。
我可以在这里进行的最后一次编辑...您可以使用stdClass
类在Paypal SDK中使用的方法,而不是创建json_encode
和Currency
:
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);
};