我有这个代码用于paypal单一支付电话,并且想要添加一个php变量,其价格为':':"。$ planPrice。" 如果不是$ planPrice,我会输入任何数字,例如:" 1000"它没有问题,我收到通知,但当我添加php变量时,它说:
致命错误:未捕获的异常' InvalidArgumentException'同 消息'无效的JSON字符串'在 C:\ XAMPP \ htdocs中\网站\ paypal_payouts \贝宝PHP-SDK \贝宝\ REST的API-SDK-PHP \ LIB \贝宝\确认\ JsonValidator.php:29 堆栈跟踪:#0 C:\ XAMPP \ htdocs中\网站\ paypal_payouts \贝宝PHP-SDK \贝宝\ REST的API-SDK-PHP \ LIB \贝宝\ COMMON \ PayPalModel.php(50): PayPal \ Validation \ JsonValidator :: validate(' {\ r \ n ...')#1 C:\ XAMPP \ htdocs中\网站\ paypal_payouts \ CreateSinglePayout.php(67): PayPal \ Common \ PayPalModel-> __ construct(' {\ r \ n ...')#2 C:\ XAMPP \ htdocs中\网站\ ADMIN \报告\ paynow.php(19): include(' C:\ xampp \ htdocs ...')#3 {main}引入 C:\ XAMPP \ htdocs中\网站\ paypal_payouts \贝宝PHP-SDK \贝宝\ REST的API-SDK-PHP \ LIB \贝宝\确认\ JsonValidator.php 第29行
这是php中的代码:
$senderItem = new \PayPal\Api\PayoutItem();
$senderItem->setRecipientType('Email')
->setNote('Thanks for your patronage!')
->setReceiver($coachUsername)
->setSenderItemId("2014031400023")
->setAmount(new \PayPal\Api\Currency("{
'value':".$planPrice.",
'currency':'USD'
}"));
答案 0 :(得分:2)
You don't need to pass a JSON string to Paypal\Api\Currency
,使用数组会更简单:
$senderItem = new \PayPal\Api\PayoutItem();
$senderItem->setRecipientType('Email')
->setNote('Thanks for your patronage!')
->setReceiver($coachUsername)
->setSenderItemId("2014031400023")
->setAmount(new \PayPal\Api\Currency([
'value' => $planPrice,
'currency' => 'USD'
]));
但是如果必须使用JSON,它必须是有效的。 JSON只允许双引用字符串和键名:
$senderItem = new \PayPal\Api\PayoutItem();
$senderItem->setRecipientType('Email')
->setNote('Thanks for your patronage!')
->setReceiver($coachUsername)
->setSenderItemId("2014031400023")
->setAmount(new \PayPal\Api\Currency('{
"value":'.$planPrice.',
"currency":"USD"
}'));
(我假设"value"
应设置为JSON号码。如果它应该是JSON字符串,请用{{1替换倒数第三行相反。)
答案 1 :(得分:2)
您应该永远手动生成JSON。
使用PHP函数json_encode()
。它处理引号,逗号,匹配的括号和所有内容:
$senderItem = new \PayPal\Api\PayoutItem();
$senderItem->setRecipientType('Email')
->setNote('Thanks for your patronage!')
->setReceiver($coachUsername)
->setSenderItemId("2014031400023")
->setAmount(
new \PayPal\Api\Currency(
json_encode(array(
'value' => $planPrice,
'currency' => 'USD',
))
)
);
更重要的是,您甚至不必生成JSON
。创建一个新的PayPal\Api\Currency
对象,并使用其方法setValue()
和setCurrency()
来设置其属性。
$amount = new \PayPal\Api\Currency();
$amount->setValue($planPrice);
$amount->setCurrency('USD');
$senderItem = new \PayPal\Api\PayoutItem();
$senderItem->setRecipientType('Email')
->setNote('Thanks for your patronage!')
->setReceiver($coachUsername)
->setSenderItemId("2014031400023")
->setAmount($amount)
;
我从未使用过PayPal API,但我想接受一块JSON的构造函数用于使用从服务器收到的响应来构造对象。在构建请求时,您应始终构造空对象并设置其属性(与$senderItem
已经采用的方式相同)。