Small Legend
Small Legend

Reputation: 688

IPN how to add the users username to the Paypal transaction (PHP)

So I've added this untested code to my IPN

if (strcmp ($res, "VERIFIED") == 0) {
    // check whether the payment_status is Completed
    // check that txn_id has not been previously processed
    // check that receiver_email is your PayPal email
    // check that payment_amount/payment_currency are correct
    // process payment and mark item as paid.

    // assign posted variables to local variables
    $item_name = $_POST['item_name'];
    $item_number = $_POST['item_number'];
    $payment_status = $_POST['payment_status'];
    $payment_amount = $_POST['mc_gross'];
    $payment_currency = $_POST['mc_currency'];
    $txn_id = $_POST['txn_id'];
    $receiver_email = $_POST['receiver_email'];
    $payer_email = $_POST['payer_email'];

    include 'connect.php';
    if($payment_status == "completed")
    {
        mysql_query('UPDATE `users` SET `is_member`= 1 WHERE `username` == $username')
    }
    else
    {
       mysql_query('UPDATE `users` SET `is_member`= 0 WHERE `username` == $username')
    }

essentially altering the data dependent on the payment_status. The problem I have though is that paypal have no idea what username is. How would I give paypal this information? do I do it through the subscribe button or change the html code?

Upvotes: 0

Views: 819

Answers (1)

pp_pduan
pp_pduan

Reputation: 3402

Couple of options here to complete your user/membership reconciliation with IPN,

  1. Store "username" / "email" into your database during the checkout, and when payment is completed, read the $payer_email from IPN POST-back and make the data matching with your database entries (order details)

Though this requires the least change on your code, it would fail to work when user uses a different email account (PayPal account) to pay

  1. (Recommended) Use the "invoice" parameter to uniquely identify an order for reconciliation. Generate an unique invoice ID and store it along with username / email to your database at the checkout; Pass that "invoice" tag to PayPal, and IPN will return the same when the payment is completed; grab the $invoice value and map it with your order data entry.

Some references here for further details:

IPN Variables

Check here for how to add the "invoice" field:

For Standard Payment buttons, see HTML Variables for PayPal Payments Standard

For Express Checkout APIs, see SetExpressCheckout API Operation (NVP)

Upvotes: 1

Related Questions