Paypal新商品 - > setQuantity错误

时间:2016-10-30 14:50:21

标签: php paypal paypal-rest-sdk

我遇到了Paypal-PHP-SDK的问题。

我有这段代码:

<?php

use PayPal\Api\Payer;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Details;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Payment;
// 1. Autoload the SDK Package. This will include all the files and classes to your autoloader
// Used for composer based installation
require __DIR__  . '/app/first.php';
require __DIR__  . '/../ajaxscript.php';
// Use below for direct download installation
// require __DIR__  . '/PayPal-PHP-SDK/autoload.php';

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

$Bestellungsanzahl = 1;
$product = "Mousepad";
$price = 20;
$total = $price;


$item = new Item();
$item ->setName($product)
    ->setCurrency('EUR')
    ->setQuantity("4")
    ->setPrice($price);

$itemList = new ItemList();
$itemList ->setItems([$item]);

$details = new Details();
$details->setSubtotal($total);

$amount = new Amount();
$amount->setCurrency('EUR')
      ->setTotal($total)
       ->setDetails($details);

$transaction = new Transaction();
$transaction->setAmount($amount)
      ->setItemList($itemList)
      ->setDescription('KSTM Mousepads')
      ->setInvoiceNumber(uniqid());

$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl('http://localhost' . '/pay.php?success=true')
      ->setCancelUrl('http://localhost' . '/pay.php?success=false');

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

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

$approvalUrl = $payment->getApprovalLink();

header("Location: {$approvalUrl}");

问题是如果我将数量设置为1就可以了。 因此,如果 - > gt; setQuantity(1)或 - &gt; setQuantity(&#34; 1&#34;)则可以正常工作。

但是,如果数量大于1( - > setQuantity(&#34; 4&#34;)或 - > gt; setQuantity(4)),则会出现此错误:

exception 'PayPal\Exception\PayPalConnectionException' with message 'Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment.' in C:\xampp\htdocs\paypal\vendor\paypal\rest-api-sdk-php\lib\PayPal\Core\PayPalHttpConnection.php:183 Stack trace: #0 C:\xampp\htdocs\paypal\vendor\paypal\rest-api-sdk-php\lib\PayPal\Transport\PayPalRestCall.php(73): PayPal\Core\PayPalHttpConnection->execute('{"intent":"sale...') #1 C:\xampp\htdocs\paypal\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:\xampp\htdocs\paypal\vendor\paypal\rest-api-sdk-php\lib\PayPal\Api\Payment.php(579): PayPal\Common\PayPalResourceModel::executeCall('/v1/payments/pa...', 'POST', '{"intent":"sale...', NULL, Object(PayPal\Rest\ApiContext), NULL) #3 C:\xampp\htdocs\paypal\checkout.php(61): PayPal\Api\Payment->create(Object(PayPal\Rest\ApiContext)) #4 {main}

有人有想法吗? 感谢

1 个答案:

答案 0 :(得分:0)

2017 :c, total = quantity *price