我有一个Google Apps脚本,可以访问Stripe API,并从Stripe发票参考中检索数据,并将数据添加到Google表格中。Stripe提到我可以扩展对象以获得扩展对象引用的更多信息。但是我对使用API还是很陌生,所以我不确定该怎么做。我认为卡住的原因是因为我是从URL(即https://api.stripe.com/v1/invoices/xxxxxxxxxxxxxxxxxxxxxxxxxxxxx)中提取发票数据。我不确定如何修改URL来扩展对象(或者在下面的代码中应该在哪里执行)。例如,我要扩展“收费”对象以包括有关付款的更多详细信息。我想从收费对象中获取的数据将是卡类型(例如:Visa),有效期,最后四位数字。我可能会对Customer对象做同样的事情,以访问有关发票的客户详细信息。这是我目前正在使用的Google Apps脚本:function onOpen() { var ui = SpreadsheetApp.getUi(); ui.createMenu('Stripe') .addItem('Retrieve Invoice Data','getInvoiceObj') .addToUi();}function getInvoiceObj() { var apiKey, content, options, response, secret, url; //API Key secret = "rk_live_xxxxxxxxxxxxxxxxxxxxxxxxxx"; apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxx"; //Stripe API invoice URL url = "https://api.stripe.com/v1/invoices/xxxxxxxxxxxxxxxxxxxxxxxxxx"; options = { "method" : "GET", "headers": { "Authorization": "Bearer " + secret }, "muteHttpExceptions":true }; response = UrlFetchApp.fetch(url, options); //Push data to Sheet from invoice. **Writes over existing Sheet data** content = JSON.parse(response.getContentText()); var sheet = SpreadsheetApp.getActiveSheet(); //Retrieves currency type and adds to Sheet sheet.getRange(5,2).setValue([content.currency.toUpperCase()]); //Invoice amount due sheet.getRange(3,2).setValue([content.amount_due]); //Invoice date sheet.getRange(1,2).setValue([content.date]); //Invoice period begin sheet.getRange(6,2).setValue([content.lines.data[0].period.start]); //Invoice period end sheet.getRange(7,2).setValue([content.lines.data[0].period.end]); //Number of licenses sheet.getRange(10,2).setValue([content.lines.data[0].quantity]); //Invoice number sheet.getRange(13,2).setValue([content.number]);
1 回答

阿晨1998
TA贡献2037条经验 获得超6个赞
您在这里有两个选择:
提取发票时,Stripe API返回一个发票对象1,该对象又具有一个
charge
包含费用ID的字段。然后,您可以单独致电v1/charges/{charge_id_goes_here}
以获取费用明细。charge
提取发票时,展开。我不熟悉UrlFetchApp
,但是您必须在请求中指定要扩展的charge
对象。您将发送一个GET请求,其中包含您想扩展为的对象application/x-www-form-urlencoded
。看看如何在cURL中完成操作,这可能对您有帮助:https ://stripe.com/docs/api/expanding_objects?lang =curl
另外,如果可以的话,我建议您使用官方的stripe-node库,这将使与Stripe API的交互更加容易
添加回答
举报
0/150
提交
取消