/** * ZoomWebinar * @param {number} webinarID - The webinar ID in "long" format(represented as int64 data type in JSON). * @param {object} options * @param {string} token - Authorization token for oAuth2 */ function ZoomWebinar(webinarId, options /*, token*/) { // this.oauthAccesToken = token || ''; this.webinarId = webinarId; this.options = options || {}; this.apiBaseUrl = 'https://unite.jolipage.co/api/v3/thirds/zoom/'; // 'https://api.zoom.us/v2/'; } ZoomWebinar.prototype.sendRequest = function(options) { return new Promise((resolve, reject) => { let xhr = new XMLHttpRequest(); xhr.open(options.method || 'GET', options.url); if (options.headers) { Object.keys(options.headers).forEach(key => { xhr.setRequestHeader(key, options.headers[key]); }); } xhr.onload = () => { let response; try { const obj = JSON.parse(xhr.response); response = obj; } catch(error) { response = xhr.response } if (xhr.status >= 200 && xhr.status < 300) { resolve(response); } else { reject({ response, status: xhr.status, statusText: xhr.statusText, }); } } xhr.onerror = () => reject(xhr); xhr.send(options.body); }); // return fetch( // options.url, // { // mode: 'no-cors', // method: options.method || 'GET', // body: options.body, // headers: options.headers, // } // ); } /** * @param {object} options * @returns {promise} */ ZoomWebinar.prototype.sendPostRequest = function(options) { return this.sendRequest({ method: 'POST', url: `${this.apiBaseUrl}${options.uri}`, headers: { 'Content-Type': 'application/json', 'X-Version': '2018_R2', ...(options.headers || {}), }, body: options.body, }); } /** * https://marketplace.zoom.us/docs/api-reference/zoom-api/webinars/webinarregistrantcreate * @param {Element} form - Form with elements which use specific "name" attributes. There is a list of available fields with reserved names, 2 fields are required (email and first_name) and there is field for custom data (field key "custom_questions" {title:string, value:string}[]). * @param {string} [occurrence_ids] - Occurrence ID. Get this value from the webinar get API. Multiple values separated by a comma. */ ZoomWebinar.prototype.register = function(form, occurrence_ids) { let uri = `webinar/${this.webinarId}/registrants`; const formData = new FormData(form); const formDataObj = {}; formData.forEach((value, key) => { const isCustomQuestion = (new RegExp('^custom_questions')).test(key); if (isCustomQuestion) { formDataObj.custom_questions = formDataObj.custom_questions || []; formDataObj.custom_questions.push({ title: key.replace(/^custom_questions\[(.+)\]/, '$1'), value }); } else { formDataObj[key] = value; } }); if (occurrence_ids) { uri += `?occurrence_ids=${occurrence_ids}`; } const headers = {}; if (this.options.companyToken) { headers['X-Company-Token'] = this.options.companyToken; } if (this.options.projectToken) { headers['X-Project-Token'] = this.options.projectToken; } return this.sendPostRequest({ uri, body: JSON.stringify(formDataObj), headers, }); } window.ZoomWebinar = window.ZoomWebinar || ZoomWebinar;