Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to integrate paypal standard with my Wordpress site and all was going well until I wanted to do one simple thing:

  • The user submits a paypal payment
  • Paypal posts the IPN to my server
  • Post IPN Hook adds a user_meta
  • If user reloads current page, it checks to see if previous user_meta is set to 1, if it is, displays "you have already purchased your ticket"

I thought this would be pretty straight forward, but if I add this to my functions.php for the post IPN Hook :

add_filter("gform_paypal_post_ipn", "update_order_status", 10, 4);
function update_order_status($ipn_post, $entry, $config, $cancel){

    // if the IPN was canceled, don't process
    if($cancel)
        return;

    global $current_user;
    get_currentuserinfo();
    $currUID = $current_user->ID;
    update_user_meta($currUID, 'eventRegistration', 1);

    return; 
}

Absolutely nothing happens. My thought is that since the IPN is bouncing from off-site, maybe the user ID isn't available? Or am I wrong here?

Figuring that I'm correct about the user ID not being avail, I was thinking of another way to grab the user ID. Since I'm using Gravity Forms with the PayPal addon, all forms submitted are stored in a database tables wp_rg_lead & wp_rg_paypal_transaction. Since every IPN posts a transaction id, I figured I could do a JOIN between the two tables using the transaction ID sent by paypal.

so I tried:

$txn_id = 'aRandomStringOfNumbersAndLetters'; // The transaction ID that paypal POSTs

$uid = $wpdb->get_col( $wpdb->prepare( 
"
SELECT wp_rg_lead.created_by
FROM wp_rg_lead, wp_rg_paypal_transaction
WHERE wp_rg_lead.id = wp_rg_paypal_transaction.entry_id AND wp_rg_paypal_transaction.transaction_id = %s
",
$txn_id 

) );

which returned the user ID associated with the transaction ID. I thought to myself, great! This will work!

However, if I try to combine all this in to my functions.php nothing seems to work in its entirety.

This:

add_filter("gform_paypal_post_ipn", "update_order_status", 10, 4);
function update_order_status($ipn_post, $entry, $config, $cancel){

    // if the IPN was canceled, don't process
    if($cancel)
        return;

    $txn_id = $ipn_post["txn_id"]; // transaction ID POSTed by PayPal

    $uid = $wpdb->get_col( $wpdb->prepare( 
    "
    SELECT wp_rg_lead.created_by
    FROM wp_rg_lead, wp_rg_paypal_transaction
    WHERE wp_rg_lead.id = wp_rg_paypal_transaction.entry_id AND wp_rg_paypal_transaction.transaction_id = %s
    ",
    $txn_id 
    ) ); 

    update_user_meta($uid[0], 'eventRegistration', 1)


    return; 
}

function update_order_status($ipn_post, $entry, $config, $cancel){

    $txn_id = $ipn_post["txn_id"]; // transaction ID POSTed by PayPal

    $uid = $wpdb->get_col( $wpdb->prepare( 
    "
    SELECT wp_rg_lead.created_by
    FROM wp_rg_lead, wp_rg_paypal_transaction
    WHERE wp_rg_lead.id = wp_rg_paypal_transaction.entry_id AND wp_rg_paypal_transaction.transaction_id = %s
    ",
    $txn_id 
    ) ); 

    update_user_meta($uid[0], 'eventRegistration', 1)
}

Produced nothing useful.

If I remove the SQL query and say change the update_user_meta to 1, 'eventRegistration', $txn_id and submit a paypal request, the meta updates fine.

I'm kind of at a loss, I must be missing something.

Any help would be much appreciated!

Thanks!

Tre

share|improve this question

1 Answer 1

up vote 0 down vote accepted

I ended up using kind of a hack. I made a hidden field that was populated with the user ID. I then used this to update metas and what not on a successful IPN and or successful payment. Ie:

add_filter("gform_paypal_post_ipn", "update_order_status", 10, 4);
function update_order_status($ipn_post, $entry, $config, $cancel){

    // if the IPN was canceled, don't process
    if($cancel)
        return;

    $currUID = $entry["8"] // The Input # of your hidden ID field

    update_user_meta($currUID, 'eventRegistration', 1);

    return; 
}

The Database checking I handled by on the template level by having it check the wp_rg_lead table to see if the current user has an entry for the appropriate form id with a payment status of 'Pending' or 'Approved'. If so, display that there has been a purchase already associated with the account and to contact support if there has been any error. The SQL looks something like this (not on the same computer, so going off the top of my head) :

 $trans_type = $wpdb->get_col( $wpdb->prepare( 
     "
     SELECT wp_rg_lead.transaction_status
     FROM wp_rg_lead
     WHERE wp_rg_lead.form_id = 1 AND wp_rg_lead.created_by = %s
     ",
     $txn_id 
     ) ); 

Then my IF/THEN was simply something like:

if ( $trans_type == 'Pending' || $trans_type == 'Approved' ) {
    // If the user has a transaction pending or accepted do this
} else {
    // If not, do this
}

I hope this helps someone out in a similar situation!

share|improve this answer
    
Alternatively, since each form is pre-populated with the user ID of who created it, you could also do an SQL query on the wp_rg_lead table based on the transaction ID returned by paypal and match that up in the wp_rg_paypal_transaction table to then update the meta. This may be a more fool-proof plan than using a hidden field. Can anyone confirm this? –  tr3online Apr 3 '12 at 17:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.