S2Memeber is a great free WordPress Memebership plugin. We use it on our Club Orbisius site for our memberhship area.
We finally had chance to implement the automatic account upgrade and here is a little function that we've compiled to upgrade or downgrade a user account programmatically via php.

Upgrade user ID 123 to a plan 2

orb_club_change_user_plan(123, 2);

Downgrade user ID 123 to a free user

orb_club_change_user_plan(123, 0);

 

/**
 * Adds or removes S2Member levels and resets the role.
 * @param int $user_id
 * @param int $level 0-4
 * @return boolean
 * @see http://s2member.com/kb-article/rolescapabilities-via-php/
 * @author Slavi Marinov | https://orbisius.com
 */
function orb_club_change_user_plan( $user_id = 0, $level = 0 ) {
    $user = new WP_User( $user_id );
    $s2_cap = 'access_s2member_level' . $level;
    $s2_level = 's2member_level' . $level;

    if ( ! empty( $user ) ) {
        if ( ! $user->has_cap( $s2_level ) ) {
            $user->add_cap( $s2_cap );
        }

        if ( empty( $level ) ) {
            $user->set_role( 'subscriber' );

            foreach ( array( 1, 2, 3, 4, ) as $l ) {
                $s2_custom_cap = 'access_s2member_level' . $l;
                $user->remove_cap( $s2_custom_cap );
            }
        } elseif ( ! in_array( $s2_level , array_values( $user->roles ) ) ) {
            $user->set_role( $s2_level );
        }
        
        return true;
    }
    
    return false;
}

Is there a better way? Did you find a bug in the code? Let us know in the comments below.