Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Id cannot be null'

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Id cannot be null'

我很困惑为什么会收到此错误。该页面的目的是使用存储在我的数据库中的产品信息进行贝宝付款。

这是完整的错误:

( ! ) Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Id cannot be null' in /paypal/rest-api-sdk-php/lib/PayPal/Validation/ArgumentValidator.php on line 25
( ! ) InvalidArgumentException: Id cannot be null in /paypal/rest-api-sdk-php/lib/PayPal/Validation/ArgumentValidator.php on line 25
Call Stack
#   Time    Memory  Function    Location
1   0.0018  238920  {main}( )   ../makepurchase.php:0
2   0.0138  394792  PayPal\Api\Payment->execute( )  ../makepurchase.php:73
3   0.0141  396480  PayPal\Validation\ArgumentValidator::validate( )    ../Payment.php:368

*第 25 行是 PDO SELECT 语句。

这是我的 makepurchase.php 代码:

try {
      $dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_USERNAME, DB_USERNAME, DB_PASSWORD);
} catch   (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

error_reporting(E_ERROR | E_WARNING | E_PARSE);
  $book_id =$_GET["book_id"];
  $user =$_GET["user"];


$sth = $dbh->prepare("SELECT title, price FROM books2 WHERE b_id= :book_id");
$sth->bindValue(':book_id', $book_id);
$sth->execute();
$results = $sth->fetch(PDO::FETCH_ASSOC);
if(!empty($results)) {
    $title = $results['title'];
    $price = $results['price'];
}

echo '<pre />';
print_r($_GET);


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


$item1 = new Item();
$item1->setName($title)
    ->setCurrency('USD')
    ->setQuantity(1)
    ->setPrice($price);



 $details = new Details();
 $details->setShipping(1.2)
     ->setTax(1.3)
     ->setSubtotal(17.50);



$transaction = new Transaction();
$transaction->setAmount($price)
    ->setItemList($item1)
    ->setDescription("Payment description")
    ->setInvoiceNumber(uniqid());



$payment = new Payment();
$payment->setIntent("sale")
    ->setPayer($user)
    ->setRedirectUrls($redirectUrls)
    ->setTransactions(array($transaction));

    $execution = new PaymentExecution();
$result = $payment->execute($execution, $apiContext);


$request = clone $payment;


try {
    $payment->create($apiContext);
} catch (Exception $ex) {
    ResultPrinter::printError("Created Payment Using PayPal. Please visit the URL to Approve.", "Payment", null, $request, $ex);
    exit(1);
}


$approvalUrl = $payment->getApprovalLink();

ResultPrinter::printResult("Setting up payment using Paypal. Please visit the URL to Approve.", "Payment", "<a href='$approvalUrl' >$approvalUrl</a>", $request, $payment);

return $payment;

这是 paypal\validation\ArgumentValidator 的第 20-30 行:

  public static function validate($argument, $argumentName = null) { 
if ($argument === null) { // Error if Object Null throw new \InvalidArgumentException("$argumentName cannot be null"); } 
else if (gettype($argument) == 'string' && trim($argument) == ''){ 
// Error if String Empty throw new \InvalidArgumentException("$argumentName string cannot be empty"); } return true; }

我认为您误解了创建付款的步骤。通过查看代码,它表明您正在尝试 execute 付款,甚至在创建付款之前。

我建议您试试 the sample first。看看这两个步骤是怎样的。

不过,我会尽量把步骤弄到这里:

如果您的付款方式是 PAYPAL

  1. 创建付款,如下所示:http://paypal.github.io/PayPal-PHP-SDK/sample/doc/payments/CreatePaymentUsingPayPal.html

  2. 这将为您提供 approval_url,您可以使用 $approvalUrl = $payment->getApprovalLink(); 代码进行检索。

  3. 将您的浏览器重定向到 URL,要求买家通过登录帐户并批准付款来批准付款。

  4. PayPal 将根据用户的选择将浏览器重定向回 return_urlcancel_url

  5. 您将按照 http://paypal.github.io/PayPal-PHP-SDK/sample/doc/payments/ExecutePayment.html 中显示的步骤检索由 paypal 在 URL 中传递的 PayerIDpaymentId

  6. 如上所示执行支付

  7. 您的帐户中现在有钱了。

如果您的付款方式是信用卡

  1. 如果直接提供信用卡号进行支付,只需一步,如下所示:http://paypal.github.io/PayPal-PHP-SDK/sample/doc/payments/CreatePayment.html

  2. 您不需要execute付款,因为它不需要 paypal 发送用户登录 paypal(因为您在这里直接提供信用卡)。

如果这有助于您理解,请告诉我。

我强烈建议通过将示例托管在您的本地计算机上并 运行 来试用这些示例,并了解流程。我们在 http://paypal.github.io/PayPal-PHP-SDK/

向我们的 SDK 添加了大量文档

如果您发现任何错误,请随时创建一个 Github 问题。

顺便说一句,这里提供了支付 API 的官方文档:https://developer.paypal.com/docs/integration/web/accept-paypal-payment/

这对我有用:

$payment = new \PayPal\Api\Payment();
$payment->setId($paymentId);
$payment->get($paymentId, $apiContext);