user's email variable in PHP

Discussion in 'Installation' started by balancin, Jul 11, 2008.

  1. balancin

    balancin New Member

    Joined:
    Aug 29, 2006
    Messages:
    9
    Friends, I've looked at over a hundred posts now and at the manual... can't for the life of me figure out how to call user's email (that they entered when they signed up) from a PHP file. I am displaying their email to them using smarty variable $user.email in thanks.html upon successful transaction - that's no prob, but I have to submit user's email to another site's database using PHP. I've tried $member['email'], $user['email'], $_SESSION['_amember_user']['email'] and I am out of ideas...:confused:

    Thank you.
  2. jimjwright

    jimjwright New Member

    Joined:
    Sep 12, 2007
    Messages:
    162
    Hello,

    If you have some coding skills you can do it the following way.

    1. Rename the following file site-dist.inc.php to site.inc.php. This file can be found in amember folder.
    2. Add following plugin hook code into site.inc.php as follows:

    PHP:

    //
    // This function is called internally by aMember when payment completes.
    //

    function fwp($payment_id){    
        global 
    $db
        
    $payment $db->get_payment($payment_id); 
        
    $product $db->get_product($payment['product_id']); 
        
    $member $db->get_user($payment['member_id']); 

        
    // 
        // TODO - Add code to write member fields to secondary database.
        // You should be able to access email field as $member['email']
        // i.e. print "email is $member['email']";
        //
    }

    setup_plugin_hook('finish_waiting_payment''fwp');
    I know you said you tried $member but did you actually set $member? You should be able to verify by using code snippet above and in my todo section just do the following:

    PHP:
    print "email is $member['email']";
    The code above uses the passed in payment ID to demultiplex back to the payment record. It then uses the member_id field of the payment record to demultiplex back to the member record. Once you have the member record you should be able to do anything with the fields you want.

    If your trying to update your database from outside the context of aMember then you will have to access the aMember DB directly to pull out the member record. You wont be able to use global $db as in my example above.

    Jimmy
  3. balancin

    balancin New Member

    Joined:
    Aug 29, 2006
    Messages:
    9
    Thank you much, Jimmy... I will try to figure this out... However, my users supply their email in the process of signup, so it gets stored in my mySQL DB already... I just want to retrieve it. As I mentioned, I can call it using $user.email in thanks.html, but not from a .php page, that I have an include statement for in thanks.html.

    Also, to clarify: I have a partner supplied php URL I am using to post my user's email to their DB - I simply need to append it to the end of the supplied URL using a variable, then do fopen or curl on the completed URL.
  4. jimjwright

    jimjwright New Member

    Joined:
    Sep 12, 2007
    Messages:
    162
    Hello,

    Well this is how I think it works when a new users signs up and purchases a product.

    1. For a new user aMember will create a new user record that contains among other things the user's email address.
    2. aMember will create a new payment record that is marked as pending.
    3. aMember will send payment request to payment processor.
    4. payment processor will reply with approval or denial of payment.
    5. If approved aMember will update the payment record it created in step 2 as completed.
    6. aMember will call all plugins that have registered for the finish_waiting_payment callback with the completed payment id. This allows sites to post-process payment requests to do whatever they want.
    7. Finally aMember will display thanks.html to user. Please note aMember will not display a thanks.html for all payment processors this is payment processor dependent. For example you will not get a thanks.html displayed when using CCBill as payment processor.

    So using my previous sample this code will be called in step 6 right before step 7. You will be executing in context of .PHP file already and I showed how to gain access to member record so you could access email field. You would just need to add the curl request to the code I provided.

    If your dead set on doing it thru thanks.html then I don't really have any ideas because I didn't even think you could include .php file from a .html file. I would think that the PHP source code would just get dumped to the screen.

    Good luck

    Jimmy
  5. balancin

    balancin New Member

    Joined:
    Aug 29, 2006
    Messages:
    9
    Thank you again for taking the time, Jimmy. I was hoping for a simpler implementation, thinking in my apparent ignorance that there is a variable I can use in my .php file without having to write a function (again, what makes me think that, is fact that I am able to retrieve the email using $user.email in my thanks.html (same is used in the confirmation email sent to the new member.)

    btw, doing an include of a .php file in thanks.html does work fine - no different than placing php code in thanks.html - I am able to complete my post to my partner's DB if I use a hard-coded value for user's email... if I could only retrieve it from a variable...
  6. balancin

    balancin New Member

    Joined:
    Aug 29, 2006
    Messages:
    9
    Jimmy, I followed your suggestion above and after some trial and error, I got it to work! I really appreciated the tip!!! :D

    For others wondering about this: $member (not $member['email']) should be used.
  7. jimjwright

    jimjwright New Member

    Joined:
    Sep 12, 2007
    Messages:
    162
    Hello,

    Congrats good job. So do you figure out using the .html way or did you use the code snippet I provided? If you used the code snippet I provided just be aware that the plugin hook you set up fwp() is called for a completed payment and not just for a new user. So if a new user Bob purchases product A you will get a notification for product A. I would assume your code then would set the user information into remote database for Bob. If two days later he purchases product B then you will also get a notification for product B also. So at this point you would be setting user Bob again in your remote database. Just wanted you to be aware of this, this may or may not be the behaviour you expected. This notification hook is for completed payments and not for new users. I wouldn't think this would cause issues for you but it might.

    Jimmy
  8. balancin

    balancin New Member

    Joined:
    Aug 29, 2006
    Messages:
    9
    Jimmy, I used the code you provided...I saw it in the manual too - the manual helped me understand it better... $user.email must be what my payment gateway returns...

    As far as differentiating between product A, B - good point and I will have to worry about it when I add that product B. I have a product ID for each future product I and will be appending it to the string that i cURL to the remote DB (having only 1 product now, I have it hardcoded...) :eek:

    Thank you again!
  9. jimjwright

    jimjwright New Member

    Joined:
    Sep 12, 2007
    Messages:
    162
    Hello,

    You can get the aMember product_id from the payment record as follows so that you don't have to hardcode it:

    PHP:
    amember_product_id $payment[product_id];
    If the aMember product_id doesn't match the product_id you use in remote database then you will have to map the amember_product_id into your product_id.

    Anyway glad you got it working this is a very useful plugin hook for doing things exactly what you wanted to do and now that you know how it works it really is not that difficult or complicated code.

    Jimmy

Share This Page