PayPal-PHP-SDK 无法使用列表中的单个项目
PayPal-PHP-SDK not working using single item in list
我正在关注 this example 并在 Laravel 应用程序中使用它。在测试期间,我注意到当我将仅包含单个项目的数组传递给
$itemList->setItems($items);
我收到以下错误:Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment
这是我正在使用的代码片段:
$card = new CreditCard();
$card->setType("visa")
->setNumber("4669424246660779")
->setExpireMonth("11")
->setExpireYear("2019")
->setCvv2("012")
->setFirstName("Joe")
->setLastName("Shopper");
$fi = new FundingInstrument();
$fi->setCreditCard($card);
$payer = new Payer();
$payer->setPaymentMethod("credit_card")
->setFundingInstruments(array($fi));
$items[0] = new Item();
$items[0]->setName('Ground Coffee 40 oz')
->setDescription('Ground Coffee 40 oz')
->setCurrency('USD')
->setQuantity(1)
->setTax(0.3)
->setPrice(7.50);
/*
Removing this comment makes this code work
$items[1] = new Item();
$items[1]->setName('Granola bars')
->setDescription('Granola Bars with Peanuts')
->setCurrency('USD')
->setQuantity(5)
->setTax(0.2)
->setPrice(2); */
$itemList = new ItemList();
$itemList->setItems($items);
$details = new Details();
$details->setShipping(1.2)
->setTax(1.3)
->setSubtotal(17.5);
$amount = new Amount();
$amount->setCurrency("USD")
->setTotal(20)
->setDetails($details);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription("Payment description")
->setInvoiceNumber(uniqid());
$payment = new Payment();
$payment->setIntent("sale")
->setPayer($payer)
->setTransactions(array($transaction));
$request = clone $payment;
try {
$payment->create($apiContext);
} catch (Exception $ex) {
ResultPrinter::printError('Create Payment Using Credit Card. If 500 Exception, try creating a new Credit Card using <a href="https://ppmts.custhelp.com/app/answers/detail/a_id/750">Step 4, on this link</a>, and using it.', 'Payment', null, $request, $ex);
exit(1);
}
//ResultPrinter::printResult('Create Payment Using Credit Card', 'Payment', $payment->getId(), $request, $payment);
return $payment;
在哪里,如果我取消注释 $items[1]
部分,它工作正常。为什么会这样?
它抛出 400 异常,因为总数不匹配。您可以使用正确的消息捕获正确的异常,如下所示:
https://github.com/paypal/PayPal-PHP-SDK/wiki/exception-%27PayPal%5CException%5CPayPalConnectionException%27-with-message-%27Got-Http-response-code-400-when-accessing
try {
$payment->create($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);
}
在您的情况下,因为您删除了第二项,所以总数不匹配。用适当的值替换下面的代码,它应该可以工作。
$details->setShipping(1.2)
->setTax(1.3)
->setSubtotal(7.8);
$amount = new Amount();
$amount->setCurrency("USD")
->setTotal(10.3)
我正在关注 this example 并在 Laravel 应用程序中使用它。在测试期间,我注意到当我将仅包含单个项目的数组传递给
$itemList->setItems($items);
我收到以下错误:Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment
这是我正在使用的代码片段:
$card = new CreditCard();
$card->setType("visa")
->setNumber("4669424246660779")
->setExpireMonth("11")
->setExpireYear("2019")
->setCvv2("012")
->setFirstName("Joe")
->setLastName("Shopper");
$fi = new FundingInstrument();
$fi->setCreditCard($card);
$payer = new Payer();
$payer->setPaymentMethod("credit_card")
->setFundingInstruments(array($fi));
$items[0] = new Item();
$items[0]->setName('Ground Coffee 40 oz')
->setDescription('Ground Coffee 40 oz')
->setCurrency('USD')
->setQuantity(1)
->setTax(0.3)
->setPrice(7.50);
/*
Removing this comment makes this code work
$items[1] = new Item();
$items[1]->setName('Granola bars')
->setDescription('Granola Bars with Peanuts')
->setCurrency('USD')
->setQuantity(5)
->setTax(0.2)
->setPrice(2); */
$itemList = new ItemList();
$itemList->setItems($items);
$details = new Details();
$details->setShipping(1.2)
->setTax(1.3)
->setSubtotal(17.5);
$amount = new Amount();
$amount->setCurrency("USD")
->setTotal(20)
->setDetails($details);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription("Payment description")
->setInvoiceNumber(uniqid());
$payment = new Payment();
$payment->setIntent("sale")
->setPayer($payer)
->setTransactions(array($transaction));
$request = clone $payment;
try {
$payment->create($apiContext);
} catch (Exception $ex) {
ResultPrinter::printError('Create Payment Using Credit Card. If 500 Exception, try creating a new Credit Card using <a href="https://ppmts.custhelp.com/app/answers/detail/a_id/750">Step 4, on this link</a>, and using it.', 'Payment', null, $request, $ex);
exit(1);
}
//ResultPrinter::printResult('Create Payment Using Credit Card', 'Payment', $payment->getId(), $request, $payment);
return $payment;
在哪里,如果我取消注释 $items[1]
部分,它工作正常。为什么会这样?
它抛出 400 异常,因为总数不匹配。您可以使用正确的消息捕获正确的异常,如下所示: https://github.com/paypal/PayPal-PHP-SDK/wiki/exception-%27PayPal%5CException%5CPayPalConnectionException%27-with-message-%27Got-Http-response-code-400-when-accessing
try {
$payment->create($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);
}
在您的情况下,因为您删除了第二项,所以总数不匹配。用适当的值替换下面的代码,它应该可以工作。
$details->setShipping(1.2)
->setTax(1.3)
->setSubtotal(7.8);
$amount = new Amount();
$amount->setCurrency("USD")
->setTotal(10.3)