I have successfully added a card from google sheets to stripe through their API
function StripeAddCard(){
var cardno = "4242424242424242";
var cardexpmo = "42";
var cardexpyr = "42";
var url = "https://api.stripe.com/v1/customers/cus_M0hfHRz4zizqes/sources";
var params = {
// method: "get",
headers: {Authorization: "Basic " Utilities.base64Encode("sk_live_*************:")},
payload: {
"source[object]": "card",
"source[number]": cardno,
"source[exp_month]": cardexpmo,
"source[exp_year]": cardexpyr},
muteHttpExceptions: true
// limit: 2 //limit not working
}
var response = UrlFetchApp.fetch(url,params);
//const json = response.getContentText();
//const CustomerData = JSON.parse(json);
//console.log(CustomerData.name);
console.log(response.getContentText());
}
I am now attempting to example upon this to loop through a spreadsheet and gather all the appropriate values and then send them through the same API one at a time. Instead of a successful response, it returns this:
{ toString: [Function],
getResponseCode: [Function],
getContent: [Function],
getHeaders: [Function],
getContentText: [Function],
getAllHeaders: [Function],
getBlob: [Function],
getAs: [Function] }
If successful, the response is like this:
{
"id": "card_1LQElcG7pYQHkBLcGMk7wetP",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "MasterCard",
"country": "US",
"customer": "cus_M0hfHRz4zizqes",
"cvc_check": null,
"dynamic_last4": null,
"exp_month": 42,
"exp_year": 4242,
"fingerprint": "irG4XOVMCwurPGDJ",
"funding": "debit",
"last4": "4242",
"metadata": {
},
"name": null,
"tokenization_method": null
}
Here is the code that is not working:
function StripeAddMultiCard(){
var PaymentSSID = "********************";
var WorkingSS = SpreadsheetApp.openById(PaymentSSID);
var WorkingSheet = WorkingSS.getSheetByName("StripeToAdd");
var CardsToAdd = WorkingSheet.getRange(1, 1).getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow() - 1;
var DataSet = WorkingSheet.getRange(2,1,CardsToAdd,14).getValues;
var cardname = "Adam";//col2
var cardno = "06"; //col3
var cardexpmo = "12"; //col13
var cardexpyr = "12"; //col14
var cardcvc = "123"; //col5
var custID = "Temp"; //col12
var url = "Temp"; //"https://api.stripe.com/v1/customers/cus_M0hfHRz4zizqes/sources";
for (var i=0; i<3; i ){
cardname = WorkingSheet.getRange(i 2,2).getValues()[0][0];
cardno = WorkingSheet.getRange(i 2,3).getValues()[0][0];
cardexpmo = WorkingSheet.getRange(i 2,13).getValues()[0][0];
cardexpyr = WorkingSheet.getRange(i 2,14).getValues()[0][0];
cardcvc = WorkingSheet.getRange(i 2,5).getValues()[0][0];
custID = WorkingSheet.getRange(i 2,12).getValues()[0][0];
url= "https://api.stripe.com/v1/customers/" custID "/sources"
console.log("source[number]" cardno);
console.log("source[exp_month]" cardexpmo);
console.log("source[exp_year]" cardexpyr);
console.log("source[cvc]" cardcvc);
console.log("source[name]" cardname);
console.log("source[url]" url);
var params = {
// method: "get",
headers: {Authorization: "Basic " Utilities.base64Encode("sk_live_*********************:")},
payload: {
"source[object]": "card",
"source[number]": cardno,
"source[exp_month]": cardexpmo,
"source[exp_year]": cardexpyr},
muteHttpExceptions: true
// limit: 2 //limit not working
}
var response = UrlFetchApp.fetch(url,params);
console.log(response);
Utilities.sleep(51);
}
}
CodePudding user response:
It seems like this actually has started to work. I did change the console log at the end to get the expected response as the 2nd function had console.log(response) and not console.log(response.getContentText()).
CodePudding user response:
While you figured out that the log at the end was not working i made some improvements. Getting and setting values from and to the sheet is time consuming. Now we get all the data in memory with one call and from there make the requests.
There is one more optimalisation we could do and that is to use .fetchAll(). Google makes parallel requests and returns all the responses at once. This will improve your speed a lot when you make many requests, but for now:
function StripeAddMultiCard() {
var PaymentSSID = "********************";
var WorkingSS = SpreadsheetApp.openById(PaymentSSID);
var WorkingSheet = WorkingSS.getSheetByName("StripeToAdd");
var SheetValues = WorkingSheet.getRange(2, 1, WorkingSheet.getLastRow() - 1, 14).getValues();
SheetValues.forEach(row => {
var cardname = row[1] //Not used?
var cardno = row[2]
var cardexpmo = row[12]
var cardexpyr = row[13]
var cardcvc = row[4] //Not used?
var custID = row[11]
var url = "https://api.stripe.com/v1/customers/" custID "/sources"
var params = {
method: "post",
headers: { Authorization: "Basic " Utilities.base64Encode("sk_live_*********************:") },
payload: {
"source[object]": "card",
"source[number]": cardno,
"source[exp_month]": cardexpmo,
"source[exp_year]": cardexpyr
},
muteHttpExceptions: true
}
var response = UrlFetchApp.fetch(url, params);
console.log(response.getContentText());
})
}