简体   繁体   中英

When retrieving a Stripe invoice from their API, how do I expand an object to retrieve credit card details and other info?

I have a Google Apps Script that accesses the Stripe API and retrieves data from a Stripe invoice reference and adds the data to a Google Sheet.

Stripe mentioned that I can expand an object to get more info expanding an object reference . But I'm fairly new to using APIs so I'm not sure how to do this.

I think the reason I'm stuck is because I'm pulling the invoice data from a URL (ie https://api.stripe.com/v1/invoices/xxxxxxxxxxxxxxxxxxxxxxxxxx ). I'm not sure how to modify the URL to expand an object (or where I should do this in the code below).

For example, I want to expand the "Charge" object to include more details about the payment. Data I'd want to get from the charge object would be the card type (ex: Visa), expiry, last four digits. I would likely do the same thing with the Customer object, to access customer details regarding the invoice.

Here's the Google Apps Script I'm using right now:

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]);

You have two options here:

  1. When you fetch your invoice, the Stripe API returns an Invoice object 1 , which in turn has a charge field which contains the Charge ID. You could then make a separate call to v1/charges/{charge_id_goes_here} to get the charge details.

  2. Expand the charge when fetching your invoice. I'm unfamiliar with UrlFetchApp , but you would have to specify in your request that you wish to expand the charge object. You'd send a GET request with the object you'd want to expand with type application/x-www-form-urlencoded . Have a look how it's done in cURL, that might help you out: https://stripe.com/docs/api/expanding_objects?lang=curl

Separately, if you at all can I'd advise using the official stripe-node library, which will make interacting with the Stripe API much easier

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM