【发布时间】:2016-01-05 10:07:09
【问题描述】:
我正在学习 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());
但我不知何故得到了这个错误:
致命错误:未捕获异常 '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->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 [第 180 行 C:\dev\htdocs\Shop\vendor\paypal\rest-api-sdk-php\lib\PayPal\Core\PayPalHttpConnection.php 中的内部函数
你可以看看 $items 数组
【问题讨论】:
-
所以运输有问题。如果运费等于 0,它可以工作。