PHP Paypal支付请求中的错误

时间:2015-04-29 14:19:30

标签: php pdo paypal paypal-sandbox paypal-rest-sdk

我编写了一个图书馆程序,用户可以在其中搜索图书。我现在处于用户可以通过它的ID和他们自己的用户名选择项目的部分。然后,这将在PayPal上设置付款以供用户批准。现在我已经开始了,但我很失落。我通过其ID获取该书,并使用其存储在数据库中的详细信息 - 价格和标题来设置付款。

我遇到的第一个问题是我收到以下错误:

Undefined index: book_id 

我也因价格,用户和标题而收到此错误。

以下是付款代码:

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


  $book =$_POST["book_id"];
  $user =$_POST["user"];
  $price =$_GET["price"];
  $title =$_GET["title"];


$sth = $dbh->prepare("SELECT title, price FROM books2 WHERE b_id=$book");
$sth->execute();


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


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



 $details = new Details();
 $details->setShipping(1.5)
     ->setTax(1.7);


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


$baseUrl = getBaseUrl();
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl("$baseUrl/review.php?success=true")
    ->setCancelUrl("$baseUrl/payment.php?success=false");

$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;

任何人都可以说明为什么我会收到这些错误,以便我可以使这段代码有效吗?

1 个答案:

答案 0 :(得分:1)

未定义的索引表示您所期望的参数不可用。显然你的表格不包括book_id字段。

您可以采取的措施来帮助排除故障,将以下内容添加到接收POST数据的页面中,以确切了解其接收的内容。

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