尝试在 Google 脚本中创建新的 Stripe 客户时出现无效的 customer.address 对象错误

Invalid customer.address object error when trying to create new Stripe customers in Google Script

我正在尝试使用 Google 脚本在我的 stripe 帐户上自动创建和更新客户,但是在传递客户对象时,即使它似乎与 structure laid out in the docs 保持一致我从 Stripe API 收到 400 错误响应,指出 address 对象无效。

当前函数:(用于将客户对象发布到 Stripe API)

function SendCustomer(details) {
    // determine endpoint based on if sheet row has stripe_customer_id value
    var endpoint;
    if (details.stripe_customer_id && details.stripe_customer_id.length() > 0) {
      // We're updating an existing customer
      endpoint = CUSTOMERS_ENDPOINT + '/' + details.stripe_customer_id;
    } else {
      // We're creating a new customer
      endpoint = CUSTOMERS_ENDPOINT;
    }

    var cust = {
        'address': JSON.stringify({
            'line1': details.street,
            'line2': '',
            'city': details.city,
            'state': details.state,
            // Google Sheets holds a Number, so convert to String as required by Stripe API
            'postal_code': details.postal_code ? details.postal_code.toString() : details.postal_code,
            'country': 'US'
        }),
        'email': details.email,
        'phone': details.phone_cell,
        'name': details.fname + ' ' + details.lname
    }
    Logger.log(cust);
    var options = {
        'method' : 'post',
        'headers': {
            'Authorization': 'Bearer ' + API_KEY
        },
        'payload' : cust
    };
    var cust_response = UrlFetchApp.fetch(endpoint, options)
    var json_response = JSON.parse(cust_response.getContentText());
    return json_response.id
}

运行 上述函数时的错误信息:

"error": {
    "message": "Invalid object",
    "param": "address",
    "type": "invalid_request_error"
  }

我做错了什么?据我所知,我遵循 API 文档中 customer.address 对象的定义结构。会不会是 UrlFetchApp.fetch() 发送有效负载的方式搞砸了?如果是这样,我该如何解决?

在尝试了很多不同的方法之后,我终于弄明白了如何正确构造任何嵌套对象(例如本例中的 address 对象)以通过 Google 应用程序脚本。在 UrlFetchApp documentation 中捕捉到这一行让我想到了如何处理 customer.address 对象的构造,以便它在 Stripe 的末端被正确解析。

payload - Description: "...A JavaScript object is interpreted as a map of form field names to values, where the values can be either strings or blobs."

这让我想起了在 Chrome 开发者控制台中查看通过各种服务发布的其他表单数据时的情形。因此,使用该线索,我能够构建正确的结构:

var cust = {
      'address[line1]': details.street,
      'address[line2]': '',
      'address[city]': details.city,
      'address[state]': details.state,
      'address[postal_code]': details.postal_code ? details.postal_code.toString() : details.postal_code,
      'address[country]': 'US',
      'email': details.email,
      'phone': details.phone_cell,
      'name': details.fname + ' ' + details.lname
    }
    Logger.log(cust);
    var options = {
        'method' : 'POST',
        'content-type': 'application/x-www-form-urlencoded',
        'headers': {
            'Authorization': 'Bearer ' + API_KEY,
        },
        'payload' : cust
    };
    var cust_response = UrlFetchApp.fetch(endpoint, options)
    var json_response = JSON.parse(cust_response.getContentText());