-1

I have written a library program where a user can search for books. I am now onto the part where a user can select an item by it's ID and their own username. This then sets a a payment on PayPal for the user to approve. Now I've made a start but I'm quite lost. I get the book by its ID and fill use its details stored in the database - price and title to set up the payment.

The first issue I'm having is I get the following error:

Undefined index: book_id 

I get this error too for price, user and title.

Here is the code for the payment:

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;

Can anyone tell why why I'm getting these errors so I can make this code work?

stark
  • 287
  • 4
  • 9
  • 20
  • 2
    `Fatal error: Class 'Item' not found ` said that you missed this class, did you include this class to your file? – mcklayin Apr 29 '15 at 14:21
  • God, actually that's one problem sorted, i left out the USE clause, i now have a new error, ill edit. Thanks. – stark Apr 29 '15 at 14:51
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – andrewsi Apr 30 '15 at 01:00

1 Answers1

1

Undefined index means the parameter you're expecting was not available. So apparently your form is not including a book_id field.

What you can do to help troubleshoot is add the following to the page that receives the POST data to see exactly what it is receiving.

echo '<pre />';
print_r($_POST);
Drew Angell
  • 25,968
  • 5
  • 32
  • 51
  • I did this and it returned nothing, So I went to the form, silly mistake - the form was sent using GET method not POST as i'd assumed. – stark Apr 30 '15 at 09:35