致命错误:未捕获的异常'InvalidArgumentException',消息'Id不能为null'

问题描述 投票:2回答:2

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

这是完整的错误:

( ! ) 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; }
php pdo paypal-rest-sdk
2个回答
0
投票

我认为您误解了创建付款的步骤。从查看代码,它表明你甚至在创建它之前尝试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. 您将按照PayerID中显示的步骤检索由paypal传递的URL中的paymentIdhttp://paypal.github.io/PayPal-PHP-SDK/sample/doc/payments/ExecutePayment.html
  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问题。

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


1
投票

这对我有用:

$payment = new \PayPal\Api\Payment();
$payment->setId($paymentId);
$payment->get($paymentId, $apiContext);
© www.soinside.com 2019 - 2024. All rights reserved.