我有一个访问 Stripe API 并从 Stripe 发票中检索数据的 Google Apps 脚本 reference并将数据添加到 Google 表格中。
Stripe 提到我可以展开对象以获得更多信息 expanding an object reference .但我对使用 API 还很陌生,所以我不确定该怎么做。
我认为我被卡住的原因是因为我从 URL(即 https://api.stripe.com/v1/invoices/xxxxxxxxxxxxxxxxxxxxxxxxxx)中提取发票数据。我不确定如何修改 URL 以展开对象(或者我应该在下面的代码中执行此操作)。
例如,我想扩展“Charge”对象以包含有关付款的更多详细信息。我想从收费对象中获取的数据是卡类型(例如: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]);
最佳答案
这里有两个选择:
当您获取发票时,Stripe API 会返回一个发票对象 1 ,它又包含一个包含费用 ID 的
charge
字段。然后,您可以单独调用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 的交互变得更加容易
关于javascript - 从他们的 API 检索 Stripe 发票时,如何扩展对象以检索信用卡详细信息和其他信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55857450/