Hi Will
Good day!
Thanks for the reply. No this happens only when using the plugin. But still
This is the plugin we are using : Official page
Following are the main snippets that involve in generating registrants. The plugin developer was of no help so I took it upon myself to resolve this.
What happens, succinctly, in this plugin is once a customer pays (using woocommerce) for the webinar,
function woocommerce_to_zoom_add_registrant() gets called. This function in turn calls the function woocommerce_to_zoom_get_access_token() to get the access_token. There in lies the problem. This function sometimes fails to retrieve the access _token due to the timeout.
function woocommerce_to_zoom_add_registrant( $order_id ) {
//start WooCommerce logging
$logger = wc_get_logger();
$context = array( 'source' => 'woocommerce-to-zoom' );
$logger->info( 'STARTING PROCESSING ORDER: '.$order_id, $context );
//get order object inc ase we need it
$order = new WC_Order( $order_id );
//get the metadata for the order
$all_post_meta = get_post_meta($order_id);
foreach($all_post_meta as $meta_key => $meta_value){
//only proceed for keys which contain zoom_webinar
if (strpos($meta_key, 'zoom_webinar') !== false) {
//key looks like: zoom_webinar-217072930-1
$key_exploded = explode('-',$meta_key);
$webinar_id = $key_exploded[1];
$webinar_id = woocommerce_to_zoom_sanitize_webinar_id($webinar_id);
$meta_value = get_post_meta($order_id,$meta_key,true);
$body_data_to_json = json_encode($meta_value);
//register the person for the webinar
$url = woocommerce_to_zoom_get_api_base().'webinars/'.$webinar_id.'/registrants';
$access_token = woocommerce_to_zoom_get_access_token();
$response = wp_remote_post( $url, array(
'headers' => array(
'Authorization' => 'Bearer '.$access_token,
'Content-Type' => 'application/json; charset=utf-8',
),
'body' => $body_data_to_json,
));
$status = wp_remote_retrieve_response_code( $response );
if($status == 201){
//do something
$decodedBody = json_decode(preg_replace('/("\w+"):(\d+(\.\d+)?)/', '\\1:"\\2"', $response['body']), true);
$registrant_id = $decodedBody['registrant_id'];
$join_url = $decodedBody['join_url'];
//lets add meta to the order id
if(get_post_meta($order_id,'zoom_registrant_ids',false)){
$existing_registrants = get_post_meta($order_id,'zoom_registrant_ids',true);
$existing_registrants[$registrant_id] = array(
'first_name' => $meta_value['first_name'],
'last_name' => $meta_value['last_name'],
'email' => $meta_value['email'],
'join_url' => $join_url,
'webinar_id' => $webinar_id,
);
update_post_meta( $order_id, 'zoom_registrant_ids', $existing_registrants );
} else {
update_post_meta( $order_id, 'zoom_registrant_ids', array($registrant_id => array(
'first_name' => $meta_value['first_name'],
'last_name' => $meta_value['last_name'],
'email' => $meta_value['email'],
'join_url' => $join_url,
'webinar_id' => $webinar_id,
)));
}
//we will also log the success
$logger->info('USER ADDED TO ZOOM WEBINAR ('.$webinar_id.'): '.$meta_value['first_name'].' '.$meta_value['last_name'],$context);
} else {
//something bad happened
$logger->info('URL: '.$url,$context);
$logger->info('BODY: '.$body_data_to_json,$context);
$logger->info('ACCESS TOKEN: '.$access_token,$context);
$logger->info('STATUS: '.$status,$context);
//if in error, $response would be an object of WP_Error not an array
//check this
if(is_array($response)){
$logger->info('RETURN BODY: '.$response['body'],$context);
}
else if(is_wp_error( $response )){
$error_message = $response->get_error_message();
error_log('wc2zoom wp add registrant error '.$error_message);
}
}
}
}
}
function woocommerce_to_zoom_get_access_token() {
$refresh_token = get_option('wc_settings_zoom_refresh_token');
//debug
$start = microtime(true);
error_log('wc2zoom get access token process started at '.$start );
$response = wp_remote_post( 'https://zoom.us/oauth/token', array(
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
'Authorization' => 'Basic '.woocommerce_to_zoom_get_authorisation(),
'Cache-Control' => 'public,max-age=3300',
),
'body' => array(
'refresh_token' => $refresh_token,
'grant_type' => 'refresh_token',
),
));
$status = wp_remote_retrieve_response_code( $response );
//debug
$end = microtime(true);
error_log('wc2zoom token status ' .$status );
error_log('wc2zoom get access token process ended after ' ($end - $start) );
//debug
if ( is_wp_error( $response ) ) {
error_log('wc2zoom wp remote response error in woocommerce_to_zoom_get_access_token '. print_r($response,true));
}
if($status == 200){
$decodedBody = json_decode(preg_replace('/("\w+"):(\d+(\.\d+)?)/', '\\1:"\\2"', $response['body']), true);
$refresh_token = $decodedBody['refresh_token'];
$access_token = $decodedBody['access_token'];
//update the options with the result
update_option('wc_settings_zoom_refresh_token', $refresh_token);
//debug
error_log('wc2zoom token updated ref token' .$refresh_token. ' access token '.$access_token );
return $access_token;
} else {
//debug
error_log('wc2zoom refresh token error returned' );
//recursively try to get the access token
woocommerce_to_zoom_get_access_token();
//return 'ERROR';
}
}
Please give a look see and let me know
Best Regards