php - PayPal SDK 产品数组返回错误

标签 php api paypal

我正在学习 PayPal SDK,当我第一次使用一种产品时,它运行得非常好。现在我必须处理一系列产品。这是代码

$paypal = new ApiContext(
        new OAuthTokenCredential(
            '...',
            '...'
        )
    );

    $payer = new Payer();
    $payer->setPaymentMethod('paypal');

    $shipping = 2;
    $totalPriceWithShipping = 0;
    $totalPriceWithoutShipping = 0;


    $items = [];

    foreach(Controller::getData('productsInCart') as $product)
    {
        $totalPriceWithShipping += ($product->price + $shipping);
        $totalPriceWithoutShipping += $product->price;

        $item = new Item();

        $item->setName($product->name)
            ->setPrice($product->price)
            ->setCurrency('USD')
            ->setQuantity(1);

        $items[] = $item;
    }

    $itemList = new ItemList();
    $itemList->setItems($items);

    $details = new Details();
    $details->setShipping($shipping)
            ->setSubtotal($totalPriceWithoutShipping);


    $amount = new Amount();
    $amount->setCurrency('USD')
        ->setTotal($totalPriceWithShipping)
        ->setDetails($details);



    $transaction = new Transaction();
    $transaction->setItemList($itemList)
                ->setAmount($amount)
                ->setDescription('Your Order from Online Shop')
                ->setInvoiceNumber(uniqid());

    $redirectUrls = new RedirectUrls();
    $redirectUrls->setReturnUrl('...')
                ->setCancelUrl('...');

    $payment = new Payment();
    $payment->setIntent('sale')
            ->setPayer($payer)
            ->setRedirectUrls($redirectUrls)
            ->setTransactions([$transaction]);

    try {
        $payment->create($paypal);
    }
    catch(Exception $e) {
        die($e->getTrace());
    }

   die($payment->getApprovalLink());

但我以某种方式收到此错误:

Fatal error: Uncaught exception 'PayPal\Exception\PayPalConnectionException' with message 'Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment.' in C:\dev\htdocs\Shop\vendor\paypal\rest-api-sdk-php\lib\PayPal\Core\PayPalHttpConnection.php:180 Stack trace: #0 C:\dev\htdocs\Shop\vendor\paypal\rest-api-sdk-php\lib\PayPal\Transport\PayPalRestCall.php(74): PayPal\Core\PayPalHttpConnection->execute('{"intent":"sale...') #1 C:\dev\htdocs\Shop\vendor\paypal\rest-api-sdk-php\lib\PayPal\Common\PayPalResourceModel.php(102): PayPal\Transport\PayPalRestCall->execute(Array, '/v1/payments/pa...', 'POST', '{"intent":"sale...', NULL) #2 C:\dev\htdocs\Shop\vendor\paypal\rest-api-sdk-php\lib\PayPal\Api\Payment.php(422): PayPal\Common\PayPalResourceModel::executeCall('/v1/payments/pa...', 'POST', '{"intent":"sale...', NULL, Object(PayPal\Rest\ApiContext), NULL) #3 C:\dev\htdocs\Shop\app\controllers\CartController.php(183): PayPal\Api\Payment->create(Object(PayPal\Rest\ApiContext)) #4 [internal functio in C:\dev\htdocs\Shop\vendor\paypal\rest-api-sdk-php\lib\PayPal\Core\PayPalHttpConnection.php on line 180

这样您就可以查看$items 数组

$items array

最佳答案

获得 400 异常意味着,paypal 正在返回但有一些异常。获取有关该异常的更多详细信息。您需要捕获未捕获的异常 PayPal\Exception\PayPalConnectionException 以获得更多详细信息。

This page should帮你出出主意。下面是捕获该异常的示例代码:

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);
}

关于php - PayPal SDK 产品数组返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33004710/

相关文章:

php - 查询显示冗余 ID

javascript - 如何从 jquery 中的文档调用 ajaxComplete 内的函数?

python - 在 Django 中构建 API 服务

paypal - 有没有实时paypal ipn服务

ios - 移动快速结帐 Paypal

尝试将存储过程结果插入表时 PHPmyadmin 出错

php - 删除具有特定条件的行内的div

android - 是否有适用于双 SIM 卡 Android 手机的 API?

javascript - 从 Kuler API 获取十六进制值

rest - 使用 REST API 对订阅进行 PayPal Sandbox 负面测试