Search code examples
phpcodeigniterpaypalpayment-processingandroid-pay

How do I track what user made a purchase?


I am coding my website using Codeignitor and MySQL. Users need to create a new account to use my website. Every user has a unique ID along with other information saved in the database. I plan on using an online payment service such as PayPal or Google Wallet.

However, after a user makes a purchase, how do I match that purchase on the online payment service (ie. Paypal) to the user in my database, so I can render the appropriate service to that customer?

For example, if a user makes a purchase using the PayPal button, I'll see a record of that purchase on my PayPal account. But how do I know which user of mines in my database is the one who made that purchase listed on my PayPal merchant account? Do I just have to trust that user's information on PayPal (First name, last name, etc) is the same information he or she entered on my website?


Solution

  • With paypal IPN, paypal will automatically run a specified script once payment is made. In this script you can add info into your database, such as which user made the payment, txnid(transaction ID), payment amount, payment status. All things that may be useful in the future. Here are some useful links:
    IPN documentation.
    IPN setup walkthrough

    You will need to activate IPN in your paypal settings and input a script for paypal to run.
    You can also setup this script to "render the appropriate service to that customer" as you mentioned above.
    Once you have ipn and database insertion up and working you could add a php mail function to the script to inform yourself of which user has made a purchase:

    <?PHP
    
    //mail variables    
    $emailAddress = "[email protected]";  
    
    $subject = "User Purchase";    
    
    $message = "$user has made a purchase of $product for $productPrice";  
    
    $headers = "MIME-Version: 1.0" . "\r\n";    
    $headers.= "Content-type:text/html;charset=iso-8859-1" . "\r\n";    
    $headers.= 'From: [email protected]' . "\r\n" .      
            'Reply-To: [email protected]' . "\r\n" .        
            'X-Mailer: PHP/' . phpversion();
    
    //mail function    
    mail($emailAddress, $subject, $message, $headers);    
    
    ?>
    

    This mail function can also be used for debugging purposes. Just add information/variables you need to debug into the $message variable and you will receive an email everytime the script runs.