Hi There;
Do you have any information about the pre-request script? The following questions and answers are passed between the developer, me and some bubble staff. Somehow the developer couldn’t solve the problem. Is there anyone who can help?
Dev.
I have tried several alternative to make the plugin in bubble but since Iyzico API, is having pre-request script, this script is executed before API call. But in bubble there is no support for a pre-request script.
Bubble
Thanks for reaching out. Plugins have an object called ‘context’ with some Bubble utilities. For example context.async – call context.async with a function that kicks off an asynchronous operation, taking a callback taking (err, res). Returns res or else throws error. Would that way of doing a callback work for your use case?
Dev
I am developing a bubble plugin for a payment gateway, but before running an API call there is a pre-request script that is executed , this script provides the Authorization header value.
But I am not understanding where should I put the script in the plugin editor and get value of the API Authorization header in the API section of the plugin editor.
Bubble
f your question is more around the plugin editor, you can add a server-side action (snapshot below) with the custom script to return a value then use that in the next step which is an API call defined in the API tab of the plugin editor as an action.
Dev
I need to add the following script -
"
var apiKey = “sandbox-pvY8iL8GbralWyf3rbzqqvP0SSouP6sQ”;
var secretKey = “sandbox-cVojNOhBjRqVjhhOThOFfv4cy35zKmC5”;
function nullClear(obj){
for (var member in obj) {
if(obj[member] === null) {
delete obj[member];
}
else if (typeof obj[member] === 'object'){
obj[member]=nullClear(obj[member]);
if(Object.keys(obj[member]).length===0){
delete obj[member];
}
}
}
return obj;
}
function jsonToObj(jsonString, obj) {
var parsedJsonString = JSON.parse(jsonString);
for(var key in parsedJsonString) {
if(parsedJsonString.hasOwnProperty(key)) {
if (typeof parsedJsonString[key] === ‘object’) {
if(Array.isArray(parsedJsonString[key])){
for(var i = 0; i < parsedJsonString[key].length; i++){
if(key ==“basketItems”){
obj[key].push(new BasketItem());
obj[key][i]=jsonToObj(JSON.stringify(parsedJsonString[key][i]), obj[key][i]);
}else {
obj[key][i] = parsedJsonString[key][i];
}
}
}else{
obj[key] = jsonToObj(JSON.stringify(parsedJsonString[key]), obj[key]);
}
}else{
obj[key] = parsedJsonString[key];
}
}
}
obj = nullClear(obj);
return obj;
}
function generateRequestString(obj) {
var isArray = Array.isArray(obj);
var requestString = '[';
for (var i in obj) {
var val = obj[i];
if (!isArray) {
requestString += i + '=';
}
if (typeof val === 'object') {
requestString += generateRequestString(val);
} else {
requestString += val;
}
requestString += isArray ? ', ' : ',';
}
requestString = requestString.slice(0, (isArray ? -2 : -1));
requestString += ']';
return requestString;
}
function generateAuthorizationString(obj) {
var requestString = generateRequestString(obj);
var hashSha1 = CryptoJS.SHA1(apiKey+request.headers[“x-iyzi-rnd”]+secretKey+requestString);
var hashInBase64 = CryptoJS.enc.Base64.stringify(hashSha1);
var authorization = “IYZWS”+" “+apiKey+”:"+hashInBase64;
postman.setGlobalVariable(“pkiString”, apiKey+request.headers[“x-iyzi-rnd”]+secretKey+requestString);
console.log(authorization,“authorization”);
return authorization;
}
var checkoutFormRetrieve = {
locale: null,
conversationId: null,
token: null
};
var requestModel = checkoutFormRetrieve;
requestModel = jsonToObj(request.data, requestModel);
var authorization = generateAuthorizationString(requestModel);
postman.setEnvironmentVariable(“authorization”, authorization);
"
This script generates the authorization header, that I want to use in API calls. But I dont know how to implement this.
Bubble
I checked with our engineering team and they recommended not using the API tab in the plugin editor for this and instead use the plugin actions tab with a server side action. This method will give you access to the request object.
Dev
I have analysed, the server side action in the plugin editor. And tried to apply the API. But I am not able to find out how to run the pre-request script in server side action, and after the script is applied then how use the authorization in the plugin API call. Since the pre-request script is executed prior to the API call is made and then the authorization value is returned to API call header, and then using the value the API call is made. But in bubble I am not able to understand, how to apply the pre-request script and get the authorization value and pass it to the API call header.
I’d be more than happy to help anyone with this information.
Please help
Eren