PayPal IPN Account Tracking, How is it accomplished? - php

I'm trying to write an IPN that, upon successful transaction, modifies a database entry (like as to increment a currency (like Envato does for their prepaid deposits)).
I've looked over the IPN variables a few times and I can't figure out how to track a user transaction, except for the payer's email (but this relies on the fact that the registered account's email is the same as the payer email (which is not always true).
I'd like to track it using an account ID, but while the IPN will reply with a custom variable, I can't transmit a dynamic variable (using the Buy Now button) for it to send back (like an account ID).
My question is, how can I send a dynamic variable with these buttons or track it an already established way?

You can transmit custom data via Paypal. Look at the custom variable.
So the scenario is this:
user logs in;
in the page which is before payment page, you either have hidden input having custom field set to user id or form it dynamically with Javascript;
custom field is not even shown to the payer when he/she confirms the payment;
when transaction happens, custom field is stored (so you can always look it up in Paypal website);
IPN notification sends you the custom field value (amongst other ones) which you can use for identifying the user.
I have got similar scenario working on a production system.

Related

attach website username to paypal IPN product_id

i have IPN data sending back the correct info, i have the product id and i also have a text box for user's to write in their in-game-name in on my website.
im stuggling to attach the username input with the IPN product_id. what i want it to do is when the user clicks the paypal button, it process's the transaction and if approved the username entered from the website and product_id get placed into a array or something that i can later put into my database
im quite new to php and html please bare with me
When you create a PayPal transaction, pass a variable named custom with the values you want to store as part of the transaction.
For old-style HTML integrations that use no API or JS, this is documented here: https://developer.paypal.com/docs/paypal-payments-standard/integration-guide/Appx-websitestandard-htmlvariables/#payment-transaction-variables
IPN is very old and clunky. Crrent PayPal integrations don't use IPN at all, but rather two routes on your server that call the PayPal API -- one for 'Create Order' and one for 'Capture Order', documented here: https://developer.paypal.com/docs/business/checkout/server-side-api-calls/#server-side-api-calls
When you do that capture call you get an immediate response of success/failure and can update your database/system accordingly. Thus there is no need to wait around for PayPal to send you an IPN notification.
Those two routes on your server should return JSON data (and only JSON data) when called. The approval flow to pair with them is https://developer.paypal.com/demo/checkout/#/pattern/server
You can add POST data values to your fetch request.

Paypal IPN and PHP: handle the order before and after Paypal operations

I have already done a small e-commerce with Paypal and it works, but I feel like it is done wrongly.
Because the page that handles the IPN response from Paypal can't read $_SESSION variables, before the user submits the form and is redirected to Paypal, I get all the cart items and store them in a table in the database and also I register a new order with the user information but with the status 0 ( not completed ).
Then, after the user pays and I get the IPN response, I check if the status is completed, check if the amount paid is correct, currency and the usual checks to make sure the payment is successful.
The problem with this approach is that if the user once redirected to Paypal, decide to leave/close the browser/Paypal tab, I will have a failed order registered in the database and I can't delete it but manually.
How can I handle this kind of situation, and is it correct to register the order/cart before the user pays ?
Thank you and sorry for the long post.
If you want to check if the transaction occurs rely on the IPN messages. Depending on which api you are using you have several way to customize the IPN message with useful information for you, ie:
using paypal custom field
define dynamically the IPN url with extra parameter ie www.yoursite.com/ipn?myvar=myval
That way you can have a hook between the payment and the actual user in your application.
Obiusly the php session attributes of the user is not available when the http post comes from paypal.

Session being cleared during paypal Express checkout

I am currently developing an E-Comm site that uses Paypals express check out system. The express checkout system works fine, I send the payments amount, the user logs in to confirm the shipping details, and returns back to my page where I confirm it and the payment is completed. The main issue I am having is that I have the username (email) saved in the session. Once the user clicks checkout with paypal and confirms his/her paypal account, my session is cleared. So I cannot keep a record of who purchased what item (unless I use the users paypals email address which could be different from the one the user used on my site).
So my question is, is there any way that I can preserve the session state throughout this confirmation action? Or is there another way to keep the users information? is this a documented problem (I have not seen it anywhere)?
If any more detail is required let me know.
Thanks for your time.
No, once the user leaves your domain the session will terminate.
Instead you can store the email address in a table before user leaves your site and pass the ID of the inserted record to paypal. As this ID is a custom field it will be returned in the call back url once the user completes the payment. You can get the ID from URL and retrieve the corresponding email from database.
Even if your PHP session didn't terminate (there is likely a different reason for it closing), it still isn't a good idea to depend on the session being unmodified between the time the user leaves for PayPal and returns. Consider a user with multiple tabs that starts poking around your website in another tab before finishing Express Checkout.
Instead, store everything that is important to your checkout completion in a database, and pass a unique identifier through the Express Checkout process via the CUSTOM or INVNUM parameters. Those will be juggled through the checkout process. Once the callback returns to your site, you can very easily identify which "incomplete" order in your database it is referring to and mark it "paid."

Capturing custom PayPal data using PHP

I have a custom store where I'm selling a single product (as a gift), I need to be able to store the recipients address (different from paypal users), a message for the gift and the name they want their gift to be from (if any).
I'd really like to capture the data on this page:
http://sendvalentinesflowers.co.uk/responsive-buy-rose.html
It appears that the standard buttons don't allow this much data to be passed/stored along side a transaction. I'm just wondering how this HAS to be done with IPN? I'm looking for the simplest way to do it.
I would save all of the information in your local database as "pending" prior to sending the user over to PayPal for payment. You can include the invoice parameter in your PayPal code and set the value to the record ID of your local record.
This invoice value will be returned in IPN so you can pull the data back out and process it as necessary, and also update the existing record's payment status according to the current IPN.

PHP/MySQL PayPal IPN with multiple custom variables

Am I correct in my understanding that I can only pass-through one custom variable from a form I create to PayPal using IPN?
I have my IPN script up and running with no issues. However, upon payment notification I need to store information input by the user from a form on my site into a MySQL database.
In this specific case its for a sports league registration form. The user fills out info regarding their team (i.e. Team name, League, ect..) then pays the league fee via PayPal. Upon verification I'd like to store their team name, the league they're signing up for and other info I need to collect in my database. But since I can only pass-through one custom variable from my form through the PayPal IPN process, I'm a bit stumped.
I've read other posts about storing the info immediately in the database, then marking it 'paid' upon verification. However, how could I accomplish this since when the user clicks Pay Now, the form action performed is the PayPal payment process (as opposed to an insert statement I create). Where would I perform a database INSERT with the posted form information?
Tips or advice would be appreciated!
However, how could I accomplish this since when the user clicks Pay Now, the form action performed is the PayPal payment process (as opposed to an insert statement I create). Where would I perform a database INSERT with the posted form information?
Perform the INSERT before sending the customer to PayPal. Before that, define a unique random key for the transaction. Put that key
into the database record that's being inserted and
pass it to PayPal's IPN.
Once notification comes back from PayPal, you can use the key to identify the record (with all the info already in it) and mark it paid.

Categories