Changing second user role while updating/downgrading membershiplevel

Hi, here is the thing that i want to achive:

s2_member_level0 has the second role “customer” - when upgrading the account via shortcode provided by s2member the account should upgrade to s2member_level1 and change it’s second role to “vendor”

and

s2_member_level1 has the second role “vendor” - when downgrading the account via shortcode provided by s2member the account should downgrade itself to s2member_level0 and change it’s second role back to “customer”

i’m setting the roles for the different accounts while they are activated with this function in my child theme:

add_action( 'bp_core_activated_user', 'add_secondary_role_new', 10, 1 );

function add_secondary_role_new( $user_id ) {
   global $members_template;
   $user = get_user_by('id', $user_id);
     	if (user_can($members_template->member->id,'s2member_level1')){
	     $user->add_role('vendor');
        }
        else{
	    $user->add_role('customer');
      }
 }

and i want to change the user roles with these function which basically update the userdata in the database:

function change_role_while_upgrade_account(){
  global $members_template;
  $user = get_user_by('id', $user_id);
  $user = wp_update_user(array('ID'=>$user,'role'=>'vendor'));
}

function change_role_while_downgrade_account(){
   global $members_template;
   $user = get_user_by('id', $user_id);
   $user = wp_update_user(array('ID'=>$user,'role'=>'vendor'));
}

So the question is: how can i use this functions (if they are working) with the shortcodes that s2 member generates?
Does anyone have a clue and can helpme figure it out?

my shortcodes for upgrading:

[s2Member-PayPal-Button modify="1" level="1" ccaps="" desc="Upgrading Account to Premium" ps="paypal" lc="" cc="EUR" dg="0" ns="1" custom="www.netz-mitteldeutschland.de" ta="0" tp="0" tt="Y" ra="2799.00" rp="1" rt="Y" rr="1" rrt="" rra="1" image="default" output="button" /]

shortcode for unsubscriping:

[s2Member-PayPal-Button cancel="1" image="default" output="anchor" /]

Thanks for your responses

There is two ways I can see:

  1. Find a good hook to run these functions

  2. [My favourite] Use “s2Member -> API / Notifications” to run these functions. Works for all the payment processors, while hooks depends or each processor.

Hope that helps…

Thanks for your quick answer.

  1. What could be an possible hook that is triggered when pressing one of these generated buttons?
  2. will only send out an notification and not trigger a custom written function if i read it right. so thats probably not going to work for me.
  1. Can’t say by memory, must search in s2M code. But you can too…

  2. Will send notification to a specified URL, where you can set your function, so it will be executed when that URL is loaded. These is an explanation of how to build that, in the notification’s pages.