javascript - 从他们的 API 检索 Stripe 发票时,如何扩展对象以检索信用卡详细信息和其他信息?

标签 javascript google-apps-script stripe-payments

我有一个访问 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]);

最佳答案

这里有两个选择:

  1. 当您获取发票时,Stripe API 会返回一个发票对象 1 ,它又包含一个包含费用 ID 的 charge 字段。然后,您可以单独调用 v1/charges/{charge_id_goes_here} 以获取收费详情。

  2. 在获取发票时展开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/

相关文章:

javascript - Google 电子表格 : conditional, 包含从不同电子表格导入的行

javascript - 如何将 Stripe 支付库与 HTML/JS 中单选按钮选择的多个按钮结合起来

node.js - 将 Firebase 函数中的 Stripe 响应返回到 Swift 应用程序

google-apps-script - 关于不存在的脚本的 Google 应用脚本错误通知

android - 模块是使用不兼容的 Kotlin 版本编译的。其元数据的二进制版本是 1.5.1,预期版本是 1.1.15

javascript - 制作在打开弹出窗口后重定向的javascript

javascript - React-native 0.49 CheckBox 如何更改 prop : value ?

javascript - 仅检测第一次碰撞(使用光线转换的碰撞检测)

javascript - 如何防止加载谷歌图表表格 css

javascript - 如何搜索产品 'item type' 的数组并将其发送到名称为 'Item Type' 的工作表?