I've spent hours trying to research and solve this but am still struggling unfortunately.
I have created a custom 'courses' post type in Wordpress that involves using embedded Calendly event registrations. I am using the Calendly embed API to notify the parent window when an event registration takes place. The notification payload provides the URI of the event, which I then want to look up using the Calendly API and return the name of the event. I am struggling with hiding the API key in the header:
"Content-Type": "application/json",
"Authorization": "Bearer MY_API_KEY"
}
I've tried to add a line in wpconfig to define the key:
define( 'CALENDLY_KEY', '**key**' );
But I don't know how to then use this in my function without exposing it via 'echo'.
Any advice would be much appreciated.
Extended code below:
<!-- Calendly widget script -->
<script type="text/javascript" src="https://assets.calendly.com/assets/external/widget.js" async></script>
<script>function isCalendlyEvent(e) {
return e.data.event &&
e.data.event.indexOf('calendly') === 0;
};
window.addEventListener(
'message',
function(e) {
if (isCalendlyEvent(e)) {
if (e.data.event == "calendly.event_scheduled") {
console.log("event scheduled!");
let event_uri = e.data.payload.event.uri;
console.log(event_uri);
fetch(event_uri, {
"method": "GET",
"headers": {
"Content-Type": "application/json",
"Authorization": "Bearer MY_API_KEY"
}
})
.then(response => response.json())
.then((json) => {
console.log(json);
let ordered_course = json.resource.name;
console.log(ordered_course);
})
}
console.log(e.data);
}
}
);</script>