dsai1991 2016-07-07 18:05
浏览 56
已采纳

PayPal-PHP-SDK无法使用列表中的单个项目

I'm following this example and using it in a Laravel application. During tests I have noticed that when I pass an array of only single item to $itemList->setItems($items); I get the following error: Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment

Here's the piece of code I'm using:

    $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;

Where, if I uncomment the $items[1] part, it works fine. Why does this happen?

  • 写回答

1条回答 默认 最新

  • douou2026 2016-07-14 18:37
    关注

    It is throwing 400 exception because the totals are not matching. You can catch proper exception with proper message as shown here: 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);
    }
    

    In your case, because you deleted the second item, the total does not match up. Replace below code with proper values and it should work.

       $details->setShipping(1.2)
            ->setTax(1.3)
            ->setSubtotal(7.8);
        $amount = new Amount();
        $amount->setCurrency("USD")
            ->setTotal(10.3)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?