Below is an example of helper functions that can be used to call the Stripe API in Google Apps Script.
function getAuthHeader(){
var apiKey = "STRIPE_API_KEY__CONSIDER_TO_GENERATE_A_KEY_WITH_LIMITED_SCOPE";
var authHeader = 'Basic ' +Utilities.base64Encode(apiKey);
return {
headers: {Authorization: authHeader}
}
}
function goFetch(url){
var reponse;
try{
reponse = JSON.parse(UrlFetchApp.fetch(url,getAuthHeader()).getContentText());
}catch(err){
Logger.log(err);
}
return reponse;
}
Example usage, listing charges:
function listCharges(lim){
var url = 'https://api.stripe.com/v1/charges?'+limit=lim;
return goFetch(url);
}
Logger.log(listCharges(10));
In your example, you are making a post request with curl. From the curl manual:
-d, --data
(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit
button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded. Compare to -F, --form.
In the UrlFetchApp reference manual you find the following:
// Make a POST request with form data.
var resumeBlob = Utilities.newBlob('Hire me!', 'text/plain', 'resume.txt');
var formData = {
'name': 'Bob Smith',
'email': '[email protected]',
'resume': resumeBlob
};
// Because payload is a JavaScript object, it will be interpreted as
// as form data. (No need to specify contentType; it will automatically
// default to either 'application/x-www-form-urlencoded'
// or 'multipart/form-data')
var options = {
'method' : 'post',
'payload' : formData
};
UrlFetchApp.fetch('https://httpbin.org/post', options);
So a goPost function would be something like this:
function goPost(url,data){
var options = {
'method' : 'post',
'payload' : data,
'headers': getAuthHeader()['headers']
};
var response;
try{
response = JSON.parse(UrlFetchApp.fetch(url,options).getContentText());
}catch(err){
Logger.log(err);
}
return response;
}
Example usage:
var data = {
amount: 2000,
currency: 'usd',
source: 'tok_amex',
description: 'Charge for [email protected]'
}
var result = goPost('https://api.stripe.com/v1/charges',data);
Logger.log(result);
Optionsparameter inUrlFetchApp.fetch: You would put-uasauthorization header,-Has normally header pair and -d aspayload. - wsw