jack
jack

Reputation: 348

create a stripe token in google apps script

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)

Upvotes: 1

Views: 366

Answers (1)

Tanaike
Tanaike

Reputation: 201553

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:

Upvotes: 2

Related Questions