Directory Plugin (please test)

So, I've hacked together a simple addon for directory. I'm having trouble with it accounting for the actual purchase and I'm not sure why, because according to https://premium.wpmudev.org/blog/integrating-the-affiliate-plugin-part-one/ I have all of the parts covered.

As you'll see, I use the existence of the role in the user_id as proof that it is a paid user, as it's not set until the paypal transaction is completed.

This gets the role:

$user = new WP_User( $user_id );

	if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
	foreach ( $user->roles as $role )
		$rolecheck = $role;
	}	

	if($rolecheck != 'directory_member_paid') $rolevalid = false; //check the role, if not paid then mark rolevalid as false

And then when setting affiliate status I check to be sure that rolevalid isn't false.

if(function_exists('get_user_meta') && $rolevalid != false) {
		$aff = get_user_meta($user_id, 'affiliate_referrer', true);
		$paid = get_user_meta($user_id, 'affiliate_paid', true);
	} else {
		$aff = get_usermeta($user_id, 'affiliate_referrer');
		$paid = get_usermeta($user_id, 'affiliate_paid');
	}

I hooked in to wpmu_new_user so they both run on user creation. Maybe someone can clue me in?

It's tracking unique clicks but not tracking signups or paid subscriptions. It succesfully enables inside of the affiliate plugin settings and does not get in the way of account creation.

<?php
/*
Plugin Name: Directory add-on
Description: Affiliate system plugin for the WordPress Supporter plugin
Author: Kruzen
Author URI: http://nwgnetworks.com
*/

add_action('wpmu_new_user', 'affiliate_new_user', 10, 2);
add_action( 'wpmu_new_user', 'affiliate_new_paiduser', 10, 4 );

function affiliate_new_user( $user_id ) {

	// Call the affiliate action
	do_action( 'affiliate_signup' );

	if(defined( 'AFFILIATEID' )) {
		// We found an affiliate that referred this new user
		if(function_exists('update_user_meta')) {
			update_user_meta($user_id, 'affiliate_referrer', AFFILIATEID);
		} else {
			update_usermeta($user_id, 'affiliate_referrer', AFFILIATEID);
		}

	}

}

function affiliate_new_paiduser( $user_id ) {

	//Get User's Role
	$user = new WP_User( $user_id );

	if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
	foreach ( $user->roles as $role )
		$rolecheck = $role;
	}	

	if($rolecheck != 'directory_member_paid') $rolevalid = false; //check the role, if not paid then mark rolevalid as false

	if(function_exists('get_user_meta') && $rolevalid != false) {
		$aff = get_user_meta($user_id, 'affiliate_referrer', true);
		$paid = get_user_meta($user_id, 'affiliate_paid', true);
	} else {
		$aff = get_usermeta($user_id, 'affiliate_referrer');
		$paid = get_usermeta($user_id, 'affiliate_paid');
	}

	if(empty($aff)) $aff = false;

	if($aff && $paid != 'yes') {

		$amount = 1; //set affiliate amount

		do_action('affiliate_purchase', $aff, $amount);

		if(defined('AFFILIATE_PAYONCE') && AFFILIATE_PAYONCE == 'yes') {

			if(function_exists('update_user_meta')) {
				update_user_meta($user_id, 'affiliate_paid', 'yes');
			} else {
				update_usermeta($user_id, 'affiliate_paid', 'yes');
			}

		}

	}

}

?>
  • Kruzen

    I also noticed that a transaction_id is stored for users too with paypal

    // If we have a valid transaction ID lets add it to our DB entries
            if ( !empty( $transaction_id ) ) {
                array_push( $user_payment_details['paypal']['transactions'], $transaction_id );
                update_user_meta( $user_id, $this->options_name, $user_payment_details );
            }

    which might be a better way to check for a valid user. Since that is how supporter does it.

    function affiliate_supporter_paid($bid, $amount, $supporterperiod) {
    
    	if(function_exists('get_site_option')) {
    		$getoption = 'get_site_option';
    	} else {
    		$getoption = 'get_option';
    	}
    
    	// Check if the blog is from an affiliate
    	if(function_exists('get_blog_option')) {
    		$aff = get_blog_option( $bid, 'affiliate_referrer', false );
    		$paid = get_blog_option( $bid, 'affiliate_paid', 'no' );
    	} else {
    		$aff = false;
    	}

    I haven't tested it yet though since i'm not sure if that data is a good verification point or not.

  • Kruzen

    Small update. I was concerned I wasn't getting the right input so I added a test case page to see.

    I made my code a little more clear to make sure I was getting my expected output instead of doing any shorthand.

    function affiliate_new_paiduser( $user_id ) {
    
    	//Get User's Role
    	$user = new WP_User( $user_id );
    
    	if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
    	foreach ( $user->roles as $role )
    		$rolecheck = $role;
    	}
    	//check the role, if not paid then mark rolevalid as false
    	if($rolecheck != 'directory_member_paid') {
    	$rolevalid = false;
    	} else {
    	$rolevalid = true;
    	}
    	//only counts if rolevalid is true is true
    	if(function_exists('get_user_meta') && $rolevalid = true) {
    		$aff = get_user_meta($user_id, 'affiliate_referrer', true);
    		$paid = get_user_meta($user_id, 'affiliate_paid', true);
    	} else {
    		$aff = get_usermeta($user_id, 'affiliate_referrer');
    		$paid = get_usermeta($user_id, 'affiliate_paid');
    	}
    
    	if(empty($aff)) $aff = false;
    
    	if($aff && $paid != 'yes') {
    
    		$amount = 1; //Set the amount to pay affiliates
    
    		do_action('affiliate_purchase', $aff, $amount);
    
    		if(defined('AFFILIATE_PAYONCE') && AFFILIATE_PAYONCE == 'yes') {
    
    			if(function_exists('update_user_meta')) {
    				update_user_meta($user_id, 'affiliate_paid', 'yes');
    			} else {
    				update_usermeta($user_id, 'affiliate_paid', 'yes');
    			}
    
    		}
    
    	}
    
    }

    This is my test output:

    <?php
      $user_id = 3;
      $key = 'wp_capabilities';
      $single = true;
      $user_last = get_user_meta( $user_id, $key, $single);
      echo '<p>The '. $key . ' value for user id ' . $user_id . ' is: ' . $user_last . '</p>';
    $user = new WP_User( $user_id );
    
    	if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
    	foreach ( $user->roles as $role )
    		$rolecheck = $role;
    	}	
    
    	if($rolecheck != 'directory_member_paid') {
    	$rolevalid = false;
    	} else {
    	$rolevalid = true;
    	}
    
    	echo '<p>rolecheck is ' . $rolecheck . '</p>';
    
    	if($rolevalid = true) {
    	echo '<p>rolevalid is true</p>';
    	} else {
    	echo '<p>rolevalid is not true</p>';
    	}
    
    	if($rolevalid = false) {
    	echo '<p>rolevalid is false</p>';
    	} else {
    	echo '<p>rolevalid is not false</p>';
    	}
    
    ?>

    This prints the following

    The wp_capabilities value for user id 3 is: Array

    rolecheck is directory_member_paid

    rolevalid is true

    rolevalid is not false

    Which confirms

    1) For any user id passed in, I'm getting the accurate role.
    2) In checking that role, I can confirm beyond a shadow of a doubt that rolevalid is in fact always true. so long as directory_member_paid is accurate.

    I am still only registering unique clicks, not signups or payments.

    Anyone with actual development experience that wants to 'top this off' and make it work would be wonderful :slight_smile:

  • Barry

    Firstly, try hooking into the user_register action rather than wpmu_new_user, also you are telling the action that it should be passing 2 parameters but are only receiving one in your function.

    The do_action you hit in that new user function, (affiliate_signup) should record the signup in the stats, if you aren't seeing it then either that function isn't being fired, or it's not finding the cookie to identify the user - is the usermeta record being created correctly?

    When does the payment take place? You've hooked into new_user twice, so both the new_user and _new_paid_user will be fired as soon as a user is created - but if the payment is made after the user is created (which it would be) then the paid_user function is being called to soon and so will never find the role so won't record the payment..

    You need an action hook on receipt of correct payment - I haven't checked the directory plugin, but there should be one - I'll grab and see if I can find one.

    But all in all a good start - as I said in my last message, if you want to pop it through to me, I can have a play and see if I can enhance it.

  • Kruzen

    Hi Barry,

    First off, thanks VERY much for taking a look at this.

    I've fired off an email with a zip (in case php is filtered out by your server) of the file.

    The items you bring up make perfect sense, I'll be going back to take a look at my current implementation to see how I can make the appropriate adjustments.

    I spend a lot of time looking through both directory, affiliate, and the other affiliate plugins to try to learn how this all works, and admittedly I'm not too php saavy other then hacking apart wordpress stuff :slight_smile: So this has been a great experience for learning!

    RE: The passing two variables. I grabbed those from the existing plugins, so that's how that happened :slight_smile: It makes sense! Really each is only calling 1 var from what I can tell :slight_smile:

    In looking at the user_meta, there is no affiliate_referrer or affiliate_paid fields, which would make sense as to why it isn't being counted :wink:

    I'll see if I can find where to hook onto! Thanks for the tips, and let me know if you end up taking a looksie :slight_smile:

  • Kruzen

    Thanks Barry,

    I was just looking at payments.php for the new directory beta. (which i'm using)

    in update_user towards the end it sets the directory member role and then calls update_user_payment_Details

    if ( "recurring" == $billing_type )
                    $role = "directory_member_not_paid";
                else
                    $role = "directory_member_paid";
    
                $user_id = wp_insert_user( array(
                    'user_login'    => $user_login,
                    'user_pass'     => $user_pass,
                    'user_email'    => $user_email,
                    'display_name'  => $first_name . ' ' . $last_name,
                    'first_name'    => $first_name,
                    'last_name'     => $last_name,
                    'role'          => $role
                ));
    
                // If user account created successfully, proceed
                if ( !empty( $user_id ) ) {
    
                    // Set payment details ( transaction ID's and recurring payment profile ID )
                    $this->update_user_payment_details( $user_id, $billing_type, $transaction_details );
    
                    // $this->update_user_credits( $credits, $user_id );
                    wp_new_user_notification( $user_id, $user_pass );
                    $credentials = array( 'remember' => true, 'user_login' => $user_login, 'user_password' => $user_pass );
                    wp_signon( $credentials );
    
                    return $user_id;
                }

    Perhaps hooking into update_user_payment_details would work?

  • Kruzen

    I tested a bit more. Here's my updated code.

    I udpated the add_action's to only account for one passed in variable each since both just call for $user_id. Additionally I commented out the role check, since if i'm hooking in after payment to update_user_payment_details it should only be running after payment is accepted.

    Unfortunately, this was met with continued unsuccess.

    I checked the session cookie:

    Name: affiliate_6fa5e0ad9d1ec06ff6763d9d01e717ca
    Value: 6fa5e0ad9d1ec06ff6763d9d01e717ca
    Domain/Path: .mydomain.com/
    Expires: Sun 11 Sep 2011

    Great! It is setting a cookie with affiliate information

    Doing a little database digging..

    wp_affilliatereferrers does not have any primary keys yet, however my user I am using to refer is entered in wp_affiliatedata:

    user_id period uniques signups completes debits credits payments lastupdated
    3 201108 7 0 0 0.00 0.00 0.00 2011-08-11 14:32:31

    Additionally, the user which was referred, does not have any wp_usermeta for the affiliate related fields. (All user_meta for user_id 118)

    2267 118 primary_blog 1
    2263 118 jabber
    2264 118 wp_capabilities a:1:{s:21:"directory_member_paid";s:1:"1";}
    2265 118 wp_user_level 0
    2266 118 dr_options a:1:{s:6:"paypal";a:3:{s:12:"transactions";a:0:{}s:10:"profile_id";s:14:"I-KA4H5RULTH9T";s:3:"key";s:32:"4f262bcf2415fb985b8428e6fe5d9b90";}}
    2262 118 yim
    2261 118 aim
    2260 118 show_admin_bar_admin true
    2259 118 show_admin_bar_front true
    2257 118 admin_color fresh
    2258 118 use_ssl 0
    2255 118 rich_editing true
    2256 118 comment_shortcuts false
    2254 118 description
    2253 118 nickname afftest12
    2252 118 last_name
    2251 118 first_name afftest123

    So at this point, I think you are right that the affiliate action is not launching at all, and I'm not sure why! :slight_smile: I made sure to disable and re-enable the plugin each time I updated it. I would think that at the very least it would get the referring signups.

    <?php
    /*
    Plugin Name: Directory add-on
    Description: Affiliate system plugin for the WordPress Supporter plugin
    Author: Kruzen
    Author URI: http://nwgnetworks.com
    */
    
    add_action('wpmu_new_user', 'affiliate_new_user', 10, 1);
    add_action( 'update_user_payment_details', 'affiliate_new_paiduser', 10, 1 );
    
    function affiliate_new_user( $user_id ) {
    
    	// Call the affiliate action
    	do_action( 'affiliate_signup' );
    
    	if(defined( 'AFFILIATEID' )) {
    		// We found an affiliate that referred this new user
    		if(function_exists('update_user_meta')) {
    			update_user_meta($user_id, 'affiliate_referrer', AFFILIATEID);
    		} else {
    			update_usermeta($user_id, 'affiliate_referrer', AFFILIATEID);
    		}
    
    	}
    
    }
    
    function affiliate_new_paiduser( $user_id ) {
    /* role check not necessary if payment details is reached.
    	//Get User's Role
    	$user = new WP_User( $user_id );
    
    	if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
    	foreach ( $user->roles as $role )
    		$rolecheck = $role;
    	}
    	//check the role, if not paid then mark rolevalid as false
    	if($rolecheck != 'directory_member_paid') {
    	$rolevalid = false;
    	} else {
    	$rolevalid = true;
    	}*/
    	//only counts if rolevalid is true is true
    	if(function_exists('get_user_meta')) {
    		$aff = get_user_meta($user_id, 'affiliate_referrer', true);
    		$paid = get_user_meta($user_id, 'affiliate_paid', true);
    	} else {
    		$aff = get_usermeta($user_id, 'affiliate_referrer');
    		$paid = get_usermeta($user_id, 'affiliate_paid');
    	}
    
    	if(empty($aff)) $aff = false;
    
    	if($aff && $paid != 'yes') {
    
    		$amount = 1; //Set the amount to pay affiliates
    
    		do_action('affiliate_purchase', $aff, $amount);
    
    		if(defined('AFFILIATE_PAYONCE') && AFFILIATE_PAYONCE == 'yes') {
    
    			if(function_exists('update_user_meta')) {
    				update_user_meta($user_id, 'affiliate_paid', 'yes');
    			} else {
    				update_usermeta($user_id, 'affiliate_paid', 'yes');
    			}
    
    		}
    
    	}
    
    }
    
    ?>
  • Kruzen

    Woo! Moderate Success :slight_smile:

    After looking at membership and remembering what you said above, I hooked the signup process to user_register.

    The signup was accounted for, and the affiliate referrer was accuratly placed into the user's user_meta.
    2284 119 affiliate_referrer 3

    So if I can find a good place to hook into, hopefully the paid membership is soon to follow :slight_smile:

  • Barry
    add_action( "updated_user_meta", 'affiliate_payment_process', 10, 4);
    
    function affiliate_payment_process($meta_id, $object_id, $meta_key, $_meta_value ) {
    
    	if($meta_key == 'module_payments') {
    		// We are here because module_payments was updated
    		//Get User's Role
    		$user = new WP_User( $object_id );
    
    		//only counts if rolevalid is true is true
    		if(function_exists('get_user_meta')) {
    			$aff = get_user_meta($user_id, 'affiliate_referrer', true);
    			$paid = get_user_meta($user_id, 'affiliate_paid', true);
    		} else {
    			$aff = get_usermeta($user_id, 'affiliate_referrer');
    			$paid = get_usermeta($user_id, 'affiliate_paid');
    		}
    
    		if(empty($aff)) $aff = false;
    
    		if($aff && $paid != 'yes') {
    
    			$amount = 1; //Set the amount to pay affiliates
    
    			do_action('affiliate_purchase', $aff, $amount);
    
    			if(defined('AFFILIATE_PAYONCE') && AFFILIATE_PAYONCE == 'yes') {
    
    				if(function_exists('update_user_meta')) {
    					update_user_meta($user_id, 'affiliate_paid', 'yes');
    				} else {
    					update_usermeta($user_id, 'affiliate_paid', 'yes');
    				}
    			}
    		}
    	}
    }
  • Barry

    Use this instead -

    add_action( "updated_user_meta", 'affiliate_payment_process', 10, 4);
    
    function affiliate_payment_process($meta_id, $object_id, $meta_key, $_meta_value ) {
    
    	if($meta_key == 'module_payments') {
    		// We are here because module_payments was updated
    		//Get User's Role
    		$user = new WP_User( $object_id );
    
    		if(!$user) {
    			return;
    		}
    
    		//only counts if rolevalid is true is true
    		if(function_exists('get_user_meta')) {
    			$aff = get_user_meta($user->ID, 'affiliate_referrer', true);
    			$paid = get_user_meta($user->ID, 'affiliate_paid', true);
    		} else {
    			$aff = get_usermeta($user->ID, 'affiliate_referrer');
    			$paid = get_usermeta($user->ID, 'affiliate_paid');
    		}
    
    		if(empty($aff)) $aff = false;
    
    		if($aff && $paid != 'yes') {
    
    			$amount = 1; //Set the amount to pay affiliates
    
    			do_action('affiliate_purchase', $aff, $amount);
    
    			if(defined('AFFILIATE_PAYONCE') && AFFILIATE_PAYONCE == 'yes') {
    
    				if(function_exists('update_user_meta')) {
    					update_user_meta($user->ID, 'affiliate_paid', 'yes');
    				} else {
    					update_usermeta($user->ID, 'affiliate_paid', 'yes');
    				}
    			}
    		}
    	}
    }

    Leave the user creation in and then do a quick test on it just in case.

  • Kruzen

    Hey Barry,

    Tested it out. Here's what i'm working with .

    First -- It's still not tracking paid signups.

    I noticed a few things in your code that I'm curious about however.

    add_action( "updated_user_meta", 'affiliate_payment_process', 10, 4); had a typo in it "updated" so I changed to "update_user_meta"

    I'm a little confused by the new method because module_payments doesn't appear to be written to the user_meta at all when a paid user is made.

    directory uses dr_options to store payment information.

    dr_options a:1:{s:6:"paypal";a:3:{s:12:"transactions";a:0:{}s:10:"profile_id";s:14:"I-MG626AT106T4";s:3:"key";s:32:"4f262bcf2415fb985b8428e6fe5d9b90";}}

    Perhaps dr_options would be a better field to check for an update? However, I had an instance where a payment was cancelled and so the user did not get marked as a paid member, but dr_options was still updated.

    2135 111 wp_capabilities a:1:{s:25:"directory_member_not_paid";s:1:"1";}
    2136 111 wp_user_level 0
    2137 111 dr_options a:1:{s:6:"paypal";a:3:{s:12:"transactions";a:0:{}s:10:"profile_id";s:0:"";s:3:"key";s:32:"4f262bcf2415fb985b8428e6fe5d9b90";}}

    So that leads me to believe that dr_options is not a reliable place either and that ulatimately we must look at wp_capabilities or another source..

    Either way, it's not tracking payments yet :slight_smile: I think we're close though!

    P.S. Also a little confused as we are taking in a lot of variables for the function but only using meta_key and object_id. $meta_value should also be $meta_value too I think?

    <?php
    /*
    Plugin Name: Directory add-on
    Description: Affiliate system plugin for the WordPress Supporter plugin
    Author: Kruzen
    Author URI: http://nwgnetworks.com
    */
    
    add_action( 'user_register', 'affiliate_new_user' );
    add_action( "update_user_meta", 'affiliate_payment_process', 10, 4);
    
    function affiliate_new_user( $user_id ) {
    
    	// Call the affiliate action
    	do_action( 'affiliate_signup' );
    
    	if(defined( 'AFFILIATEID' )) {
    		// We found an affiliate that referred this new user
    		if(function_exists('update_user_meta')) {
    			update_user_meta($user_id, 'affiliate_referrer', AFFILIATEID);
    		} else {
    			update_usermeta($user_id, 'affiliate_referrer', AFFILIATEID);
    		}
    
    	}
    
    }
    
    function affiliate_payment_process($meta_id, $object_id, $meta_key, $meta_value ) {
    
    	if($meta_key == 'module_payments') {
    		// We are here because module_payments was updated
    		//Get User's Role
    		$user = new WP_User( $object_id );
    
    		if(!$user) {
    			return;
    		}
    
    		//only counts if rolevalid is true is true
    		if(function_exists('get_user_meta')) {
    			$aff = get_user_meta($user->ID, 'affiliate_referrer', true);
    			$paid = get_user_meta($user->ID, 'affiliate_paid', true);
    		} else {
    			$aff = get_usermeta($user->ID, 'affiliate_referrer');
    			$paid = get_usermeta($user->ID, 'affiliate_paid');
    		}
    
    		if(empty($aff)) $aff = false;
    
    		if($aff && $paid != 'yes') {
    
    			$amount = 1; //Set the amount to pay affiliates
    
    			do_action('affiliate_purchase', $aff, $amount);
    
    			if(defined('AFFILIATE_PAYONCE') && AFFILIATE_PAYONCE == 'yes') {
    
    				if(function_exists('update_user_meta')) {
    					update_user_meta($user->ID, 'affiliate_paid', 'yes');
    				} else {
    					update_usermeta($user->ID, 'affiliate_paid', 'yes');
    				}
    			}
    		}
    	}
    }
    
    ?>
  • Kruzen

    Changed your code to look at dr_options instead.

    It now tracks paid users succesfully, but also tracks users who cancel the payment.

    I'm going to add back in my directory_member_paid check after dr_options and hopefully that will fix it :slight_smile:

    function affiliate_payment_process($meta_id, $object_id, $meta_key, $meta_value ) {
    
    	if($meta_key == 'dr_options') {
    		// We are here because dr_options was updated
    		//Get User's Role
    		$user = new WP_User( $object_id );
    
    		if(!$user) {
    			return;
    		}
    
    		//only counts if rolevalid is true is true
    		if(function_exists('get_user_meta')) {
    			$aff = get_user_meta($user->ID, 'affiliate_referrer', true);
    			$paid = get_user_meta($user->ID, 'affiliate_paid', true);
    		} else {
    			$aff = get_usermeta($user->ID, 'affiliate_referrer');
    			$paid = get_usermeta($user->ID, 'affiliate_paid');
    		}
    
    		if(empty($aff)) $aff = false;
    
    		if($aff && $paid != 'yes') {
    
    			$amount = 1; //Set the amount to pay affiliates
    
    			do_action('affiliate_purchase', $aff, $amount);
    
    			if(defined('AFFILIATE_PAYONCE') && AFFILIATE_PAYONCE == 'yes') {
    
    				if(function_exists('update_user_meta')) {
    					update_user_meta($user->ID, 'affiliate_paid', 'yes');
    				} else {
    					update_usermeta($user->ID, 'affiliate_paid', 'yes');
    				}
    			}
    		}
    	}
    }
  • Kruzen

    Well, color me stumped.

    I added a test case back in with a new method of detecting.

    I made a blank page to test this input. User 129 had directory_member_paid in it's DB for wp_capabilities. I changed it to not paid and it updated correspondingly along with the test case, and then changed it back which showed me that it was accuratly detecting it.

    <?php
    $ID="129";
    $user = new WP_User($ID);
    
    if ($user->wp_capabilities['directory_member_paid']==1) {
    echo 'directory_member_paid';
    } else { echo 'directory_member_not_paid'; }
    echo '<br><br>';
    print_r(array_keys($user->wp_capabilities,"1"));
    
    ?>

    This resulted in

    directory_member_paid

    Array ( [0] => directory_member_paid )

    I then implemented it into the plugin.

    <?php
    /*
    Plugin Name: Directory add-on
    Description: Affiliate system plugin for the WordPress Supporter plugin
    Author: Kruzen
    Author URI: http://nwgnetworks.com
    */
    
    add_action( 'user_register', 'affiliate_new_user' );
    add_action( 'update_user_meta', 'affiliate_payment_process', 10, 4);
    
    function affiliate_new_user( $user_id ) {
    
    	// Call the affiliate action
    	do_action( 'affiliate_signup' );
    
    	if(defined( 'AFFILIATEID' )) {
    		// We found an affiliate that referred this new user
    		if(function_exists('update_user_meta')) {
    			update_user_meta($user_id, 'affiliate_referrer', AFFILIATEID);
    		} else {
    			update_usermeta($user_id, 'affiliate_referrer', AFFILIATEID);
    		}
    
    	}
    
    }
    
    function affiliate_payment_process($meta_id, $object_id, $meta_key, $meta_value ) {
    
    	if($meta_key == 'dr_options') {
    		// We are here because dr_options was updated
    		//Get User's Role
    		$user = new WP_User( $object_id );
    
    		if(!$user) {
    			return;
    		}
    
    		//check for directory_member_paid
    		if ($user->wp_capabilities['directory_member_paid']==1) {
    		$rolevalid = true;
    		} else {
    		$rolevalid = false;
    		}		
    
    		//only continues if rolevalid is true
    		if($rolevalid = true) {
    			if(function_exists('get_user_meta')) {
    				$aff = get_user_meta($user->ID, 'affiliate_referrer', true);
    				$paid = get_user_meta($user->ID, 'affiliate_paid', true);
    			} else {
    				$aff = get_usermeta($user->ID, 'affiliate_referrer');
    				$paid = get_usermeta($user->ID, 'affiliate_paid');
    			}
    
    			if(empty($aff)) $aff = false;
    
    			if($aff && $paid != 'yes') {
    
    				$amount = 1; //Set the amount to pay affiliates
    
    				do_action('affiliate_purchase', $aff, $amount);
    
    				if(defined('AFFILIATE_PAYONCE') && AFFILIATE_PAYONCE == 'yes') {
    
    					if(function_exists('update_user_meta')) {
    						update_user_meta($user->ID, 'affiliate_paid', 'yes');
    					} else {
    						update_usermeta($user->ID, 'affiliate_paid', 'yes');
    					}
    				}
    			}
    		} else { update_user_meta($user->ID, 'affiliate_paid', 'noway');}
    	}
    }
    
    ?>

    I also added an alternative metaupdate in case it failed to see if I could get some measurable results in the DB.

    Unfortunately..

    It detects payments just fine.. the problem is it does it too early. If I register a new account, continue on to paypal, and then CANCEL payment, it still credits them as a paid user and I can't understand why.

    if ($user->wp_capabilities['directory_member_paid']==1) {
    		$rolevalid = true;
    		} else {
    		$rolevalid = false;
    		}

    This should only allow rolevalid to be true if directory_member_paid is the current object_id's role.

    //only continues if rolevalid is true
    		if($rolevalid = true) {

    This should only run the rest of the function if rolevalid is true.

    So I'm really not sure what the deal is, as I confirmed above that rolevalid is measured as true accurately in my manual test.

  • Kruzen

    Well, good news. I found a better place to hook into.

    Bad news, My attempts to hook into it were unsuccessful. I think it's because I don't have a good grasp on where it pulls data from.

    ajax_directory_ipn is located in admin.php. (line 258)

    /**
    	 * IPN script for change user role when Recurring Payment changed status
    	 *
    	 * @return void
    	 */
    	function ajax_directory_ipn() {

    The IPN! Perfect! This will tell us exactly when the user becomes paid status! Woohoo!

    //write subscr_id (profile_id) to user meta
                    $dr_options['paypal']['profile_id'] = $_POST['subscr_id'];
                    update_user_meta( $user_id, 'dr_options', $dr_options );
    
                    //set role with with access of directory plugin
                    wp_update_user( array(  'ID'    => $user_id,
                                            'role'  => "directory_member_paid" ) );

    Bingo.

    So I changed the hook to

    add_action( 'ajax_directory_ipn', 'affiliate_payment_process', 10, 4);

    Still didn't work..

    So I pruned out all my code and made it nice and simple.

    function affiliate_payment_process( $user_id ) {
    
    		if(function_exists('get_user_meta')) {
    			$aff = get_user_meta($user_id, 'affiliate_referrer', true);
    			$paid = get_user_meta($user_id, 'affiliate_paid', true);
    		} else {
    			$aff = get_usermeta($user_id, 'affiliate_referrer');
    			$paid = get_usermeta($user_id, 'affiliate_paid');
    		}
    
    		if(empty($aff)) $aff = false;
    
    		if($aff && $paid != 'yes') {
    
    			$amount = 1; //Set the amount to pay affiliates
    
    			do_action('affiliate_purchase', $aff, $amount);
    
    			if(defined('AFFILIATE_PAYONCE') && AFFILIATE_PAYONCE == 'yes') {
    
    				if(function_exists('update_user_meta')) {
    					update_user_meta($user_id, 'affiliate_paid', 'yes');
    				} else {
    					update_usermeta($user_id, 'affiliate_paid', 'yes');
    				}
    			}
    		}
    }

    Still no worky :slight_smile:

    But I think this is definitely the right hook, I'm just not sure how to give it the right data to do it's job now!

  • Kruzen

    Out of desperation, I tried adding my own meta data to check for an update in the IPN script.

    //set role with with access of directory plugin
                    wp_update_user( array(  'ID'    => $user_id,
                                            'role'  => "directory_member_paid" ) );
    		update_user_meta( $user_id, 'diraff_isvalid', 'yes' ); //added for affiliate plugin

    I then switched the hook back to update_user_meta and went back to the originally suggested code that worked before, only modifying dr_options to diraff_isvalid.. Confirmed it was added to the database when directory_member_paid was set.. Annnd... Still didn't work!

    So I'm at a loss :slight_smile: Hopefully one of you kind devs will take a look, lots of hard work into this one :slight_smile:

    function affiliate_payment_process($meta_id, $object_id, $meta_key, $meta_value ) {
    
    	if($meta_key == 'diraff_isvalid') {
    		// We are here because dr_options was updated
    		//Get User's Role
    		$user = new WP_User( $object_id );
    
    		if(!$user) {
    			return;
    		}
    
    		if(function_exists('get_user_meta')) {
    			$aff = get_user_meta($user->ID, 'affiliate_referrer', true);
    			$paid = get_user_meta($user->ID, 'affiliate_paid', true);
    		} else {
    			$aff = get_usermeta($user->ID, 'affiliate_referrer');
    			$paid = get_usermeta($user->ID, 'affiliate_paid');
    		}
    
    		if(empty($aff)) $aff = false;
    
    		if($aff && $paid != 'yes') {
    
    			$amount = 1; //Set the amount to pay affiliates
    
    			do_action('affiliate_purchase', $aff, $amount);
    
    			if(defined('AFFILIATE_PAYONCE') && AFFILIATE_PAYONCE == 'yes') {
    
    				if(function_exists('update_user_meta')) {
    					update_user_meta($user->ID, 'affiliate_paid', 'yes');
    				} else {
    					update_usermeta($user->ID, 'affiliate_paid', 'yes');
    				}
    			}
    		}
    	}
    }

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.