I have successfully set up the Paypal button and can receive email from my IPN code. However, I am not sure how to know which user actually made the payment. My doubt is that The email address used by user may not be the same as the one used to signup from my service. I tried to grab username from $_SESSION
but it returns empty string. Here is my code:
if (strcmp ($res, "VERIFIED") == 0) {
$txn_id = $_POST['txn_id'];
$payment_amount = $_POST['mc_gross'];
$payment_status = $_POST['payment_status'];
$payment_time = date("Y-m-d H:i:s");
$username = $_SESSION['user'];
$body = $txn_id.' '.$payment_amount.' '.$payment_status.' '.$payment_time.' '.$username;
mail("Email Address", 'Subject', $body, '');
$connection = new mysqli('localhost', 'user', 'password', 'db');
$query = $connection->query("UPDATE members SET TRANSID = '$txn_id', PAYAMOUNT = '$payment_amount', PAYSTATUS = '$payment_status', PAYTIME = '$payment_time' WHERE USERNAME ='$username'");
mysqli_close($connection);
I have also added session_start();
at the beginning of ipn.php
. What should I do to update data for correct user.
UPDATE
I am using Paypal IPN system. A user can pay with Paypal on the upgrade page. Then Paypal sends me transaction details on ipn.php. Now, I am writing code in ipn.php to update the MySQL table.
UPDATE TWO
After suggestion from steammike this is what I have.
Now my code looks like this:
$txn_id = $_POST['txn_id'];
$payment_amount = $_POST['mc_gross'];
$payment_status = $_POST['payment_status'];
$payment_time = date("Y-m-d H:i:s");
$username = $_POST['fuser'];
and my button like this:
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="ATXN4EP7FQXR8">
<input type="hidden" name="fuser" value="<?php echo $_SESSION['user'];?>">
<input type="submit" name="submit" value="Pay With Paypal" class="btn btn-primary">
</form>
The $username
variable is still blank though.
UPDATE 3
I am still unable to update the Table after changing fuser
to custom
in button like this:
<input type="hidden" name="custom" value="<?php echo $_SESSION['user'];?>">
and form like this:
$username = $_POST["custom"];
The only thing that gets updated is 'PAYTIME'. Although in the email I receive all the data. This is the structure of table:
TRANSID varchar(20)
PAYAMOUNT decimal(7,2)
PAYSTATUS varchar(25)
PAYTIME datetime