我正在学习PayPal SDK,当我第一次使用一种产品时,它工作得很好。现在我必须使用一系列产品。 Thay是代码
$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());
但我在某种程度上得到了这个错误:
致命错误:未捕获异常'PayPal \ Exception \ PayPalConnectionException',并在访问https://api.sandbox.paypal.com/v1/payments/payment时收到消息'获取Http响应代码400'。在C:\ dev \ htdocs \ Shop \ vendor \ paypal \ rest-api-sdk-php \ lib \ PayPal \ Core \ PayPalHttpConnection.php:180堆栈跟踪:#0 C:\ dev \ htdocs \ Shop \ vendor \ paypal \ rest-api-sdk-php \ lib \ PayPal \ Transport \ PayPalRestCall.php(74):PayPal \ Core \ PayPalHttpConnection->执行('{“意图”:“销售......”)#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,对象(PayPal \ Rest \ ApiContext),NULL)#3 C:\ dev \ htdocs \ Shop \ app \ controllers \ CartController.php(183):PayPal \ Api \ Payment-> create(Object(PayPal \ Rest \ ApiContext) ))#4 [内部功能在C:\ dev \ htdocs \ Shop \ vendor \ paypal \ rest-api-sdk-php \ lib \ PayPal \ Core \ PayPalHttpConnection.php 180行
这样你就可以看看 $ items 数组
了答案 0 :(得分:0)
获得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);
}