1)问题是$ paypal-> create($ api)返回这样的错误
Exception: Got Http response code 400 when accessing
https://api.sandbox.paypal.com/v1/payments/payment. string(271) "
{"name":"VALIDATION_ERROR","details":
[{"field":"transactions.amount","issue":"Currency should be a valid ISO
currency code"}],"message":"Invalid request - see
details","information_link":"https://developer.paypal.com/docs/api/
payments/#errors","debug_id":"cea4b8e54646c"}"
2)这是我的代码
$paypal = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
'XXX',
'YYY'
)
);
$paypal->setConfig([
'mode' => 'sandbox',
'http.ConnectionTimeOut' => 30,
'log.LogEnabled' => false,
'log.fileName' => '',
'log.LogLevel' => 'FINE',
'validation.level' => 'log'
]);
$payer = new Payer();
$details = new Details();
$amount = new Amount();
$payment = new Payment();
$transaction = new Transaction();
$redirectUrls = new RedirectUrls();
//Payer
$payer->setPaymentMethod('paypal');
//Details
$details->setShipping('2.00')
->setTax('0.00')
->setSubtotal('20.00');
//Amount
$amount->setCurrency('USA')
->setTotal('20.00')
->setDetails($details);
//Transactions
$transaction->setAmount($amount)
->setDescription('Add balance');
$redirectUrls->setReturnUrl('http://thechaller.com/paypal/pay')
->setCancelUrl('http://thechaller.com/paypal/paypalCancel');
$payment->setIntent('sale')
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions([$transaction]);
try {
$payment->create($paypal);
} catch (\PayPal\Exception\PayPalConnectionException $e) {
echo "Exception: " . $e->getMessage() . PHP_EOL;
var_dump($e->getData());
exit(1);
} catch (Exception $e) {
echo $e->getMessage();
exit(1);
}
3)客户端ID和秘密从这里获取
答案 0 :(得分:3)
你有$amount->setCurrency('USA')
。它应该是$amount->setCurrency('USD')
。它在错误消息中解释:
货币应为有效的ISO货币代码
美国的有效ISO货币代码是USD。