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

问题描述 投票:1回答:1

我试图用谷歌脚本在我的stripe账户上自动创建和更新客户,但当传递客户对象时,尽管它似乎与我的客户对象一致。文档中列出的结构 我收到来自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"
  }

我到底做错了什么?据我所知,我是按照定义的结构来执行的。customer.address 对象的方式。会不会是 UrlFetchApp.fetch() 是发送的有效载荷把它搞乱了?如果是这样,我该如何解决?

google-apps-script stripe-payments urlfetch
1个回答
0
投票

在尝试了许多不同的方法后,我终于知道如何正确地构建任何嵌套对象(如 address 对象),以便通过Google Apps Scripts正确发送到Stripe API。抓住这一行,在 UrlFetchApp文档,给了我如何处理建设的想法。customer.address 对象,以便在Stripe端正确解析。

payload - 描述。"...一个JavaScript对象被解释为表单字段名到值的映射,其中值可以是字符串或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());
© www.soinside.com 2019 - 2024. All rights reserved.