1
votes

I'm trying to create a stripe token using GAS. my code is below

  function createToken(){
  var url = "https://api.stripe.com/v1/tokens";
  
  var payload = {
  "Card": { 
  "Number" : "4242424242424242",
  "Exp_month" : "10",
  "Exp_year": "2021",
  "Cvc": "314",
  }
  };
  
  var options = {
    "method" : "post",
    "payload" : payload
  };

  options.headers = {    
    "Authorization" : "Basic " + Utilities.base64Encode("sk_test_4eC***7dc:")
    };
    
    var res = UrlFetchApp.fetch(url, options);
    Logger.log(res.getContentText())
  }

But I keep getting the following error, what am I missing?

[20-10-22 12:39:19:452 EDT] Exception: Request failed for https://api.stripe.com returned code 400.

Truncated server response: { "error": { "code": "parameter_missing", "doc_url": "https://stripe.com/docs/error-codes/parameter-missing", "message": "You must su... (use muteHttpExceptions option to examine full response) at createToken(Code:43:27)

1
You're likely running into this error because the properties that you're passing as the request payload are capitalized. The should all be lowercase as shown in the examples here: stripe.com/docs/api/tokens/create_card. Bear in mind though that this approach for tokenizing credit card details is not very secure and opens you up to PCI regulations. I'd recommend reading this guide here for more on that: stripe.com/docs/security/guidettmarek
I changed that and now I'm getting the following error: Exception: Request failed for api.stripe.com returned code 400. Truncated server response: { "error": { "message": "You must pass full card details to create a token.", "type": "invalid_request_error" } } (use muteHttpExceptions option to examine full response) (line 43, file "Code")jack

1 Answers

2
votes

When I saw the official document, the sample curl command is as follows/

$ curl https://api.stripe.com/v1/tokens \
  -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
  -d "card[number]"=4242424242424242 \
  -d "card[exp_month]"=10 \
  -d "card[exp_year]"=2021 \
  -d "card[cvc]"=314

When above sample curl is converted to Google Apps Script, it becomes as follows.

Sample script (Google Apps Script):

var url = "https://api.stripe.com/v1/tokens";
var params = {
  method: "post",
  headers: {
    authorization: "Basic " + Utilities.base64Encode("sk_test_4eC39HqLyjWDarjtT1zdp7dc:")
  },
  payload: {
    "card[number]": "4242424242424242",
    "card[exp_month]": "10",
    "card[exp_year]": "2021",
    "card[cvc]": "314"
  }
};
var res = UrlFetchApp.fetch(url, params);
console.log(res.getContentText())

Note:

  • From OP's replying, each values are required to be the string type.

References: