Skip to main content
danyaalk74530408
Known Participant
February 14, 2017
Question

PayPal Mysql PHP

  • February 14, 2017
  • 2 replies
  • 3114 views

Hi,

I'm not sure if this is the right area to post this, but I was wondering if anyone could help.

For my coursework I am making an e-commerce website with Paypal integration.

When I complete the PayPal process and I am taken to my success.php file I am given the error:

Notice: Undefined index: tx in /storage/h3/495/770495/public_html/success.php on line 12

Notice: Undefined index: amt in /storage/h3/495/770495/public_html/success.php on line 13

Notice: Undefined index: cc in /storage/h3/495/770495/public_html/success.php on line 14

Notice: Undefined index: st in /storage/h3/495/770495/public_html/success.php on line 15

My success.php file:

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PayPal Transaction Success</title>
</head>
<body>
<?php
include
'dbConfig.php';

//Get payment information from PayPal
$txn_id
= $_GET['tx'];
$payment_gross
= $_GET['amt'];
$currency_code
= $_GET['cc'];
$payment_status
= $_GET['st'];

if(!empty($txn_id)){
//Check if payment data exists with the same TXN ID.
  $paymentResult
= $db->query("SELECT * FROM payments WHERE txn_id = '".$txn_id."'");
  
if($paymentResult->num_rows > 0){
//payment information
  $paymentRow
= $paymentResult->fetch_assoc();
  $payment_id
= $paymentRow['payment_id'];

//order items details
$orderItemResult
= $db->query("SELECT p.name, i.quantity, i.gross_amount FROM order_items as i LEFT JOIN products as p ON p.id = i.item_number WHERE payment_id = '".$payment_id."'");
?>
<h1>Your payment has been successful.</h1>
  
<h2>Payment ID: <?php echo $payment_id; ?></h2>
  
<h2>Payment Gross: <?php echo '$'.$paymentRow['payment_gross'].' '.$paymentRow['currency_code']; ?></h2>
  
<?php if($orderItemResult->num_rows > 0){ ?>
  
<h3>Order Items</h3>
  
<table style="width:50%">
  
<tr>
 
<th>#</th>
  
<th>Product Name</th>
  
<th>Quantity</th>
  
<th>Gross Amount</th>
  
</tr>
  
<?php $i=0; while($item = $orderItemResult->fetch_assoc()){ $i++; ?>
  
<tr>
 
<td align="center"><?php echo $i; ?></td>
  
<td align="center"><?php echo $item['name']; ?></td>
  
<td align="center"><?php echo $item['quantity']; ?></td>
  
<td align="center"><?php echo '$'.$item['gross_amount'].' '.$paymentRow['currency_code']; ?></td>
  
</tr>
  
<?php } ?>
  
</table>
  
<?php } ?>
  
<?php }else{ ?>
<h1>Your payment has been successful.</h1>
  
<h2>TXN ID: <?php echo $txn_id; ?></h2>
  
<h2>Payment Gross: <?php echo '$'.$payment_gross.' '.$currency_code; ?></h2>
<?php } }else{ ?>
<h1>Your payment has failed.</h1>
<?php } ?>
<a href="products.php">Back to products</a>
</body>
</html>

Could anyone help me fix this issue?

Thanks a million,

Danyaal.

    This topic has been closed for replies.

    2 replies

    danyaalk74530408
    Known Participant
    February 15, 2017

    Hi,

    I have added a bracket under the code on my success.php file.

    if($paymentResult->num_rows > 0){
              //payment information
            $paymentRow = $paymentResult->fetch_assoc();
            $payment_id = $paymentRow['payment_id'];

    }

    On my success page I now get this message:

    Parse error: syntax error, unexpected 'else' (T_ELSE) in /storage/h3/495/770495/public_html/success.php on line 53

    Have I put too many brackets.

    Thanks,

    Danyaal.

    Legend
    February 15, 2017

    danyaalk74530408  wrote

    Hi,

    I have added a bracket under the code on my success.php file.

    if($paymentResult->num_rows > 0){
              //payment information
            $paymentRow = $paymentResult->fetch_assoc();
            $payment_id = $paymentRow['payment_id'];

    }

    On my success page I now get this message:

    Parse error: syntax error, unexpected 'else' (T_ELSE) in /storage/h3/495/770495/public_html/success.php on line 53

    Why are you adding a bracket there?

    danyaalk74530408
    Known Participant
    February 15, 2017

    the bracket was missing.

    Legend
    February 14, 2017

    How does the script GET tx, amt, cc, st? From the script it looks like they are trying to be sent via a url parameter.

    The script is saying we cant find this information so effectively we cant do anything.

    Normally one would communicate with Paypal using IPN instant payment notification which is a listener script you house on your server which listens for any completed payment. This script retrieves all the various Paypal variables which you use in a form to send to Paypal when a buyer purchases something from your site.

    Once you have all those variables passed back to you from the Paypal server you can do someting with the values like update your database and sent confirmation emails of the purchase to the buyer etc

    Does that sound like a workflow that you have used?

    danyaalk74530408
    Known Participant
    February 14, 2017

    Hi,

    Thanks for your reply.

    Yes, that is the workflow I am using.

    I have the ipn.php file on my server and have set up IPN on my PayPal account to the file on my server.

    Danyaal.

    Legend
    February 14, 2017

    Hi,

    I do have <input type='hidden' name='notify_url' value='http://www.xxxxxxxxx.xxxx/ipn/ipn.php'> in my payment form.

    Unfortunately, my teachers have been unable to help me with this so far. I was hoping for someone to help online.

    Thanks for the reply though,

    Danyaal.


    I'm not sure how you are even testing this?

    Are you using a live link to the Paypal gateway?

    What I usually do is avoid the Paypal sandbox testing area because its always not working or corrupt in some way. I specify a low nominal amount like a penny to the items and then use the live environment. This way you can have 100 goes for a britsh pound to get things right.

    .

    I first check to see if everything is functioning by using the redirect function between the else { } block of code in the IPN script:

    else {

    header("Location: http://example.com/success.php");

    }

    If you get redirected to the success.php page you know something is happening. You can then connect to your database and do what you need to do within the IPN scripts else { } block.