beaconho
beaconho

Reputation: 27

How to get "balance transactions" from Stripe API via UrlFetchApp

I am trying to get the transaction details from Stripe API "The balance transaction object" with a Google Apps Script app. I have studied another question to get my code:

How to integrate Stripe payments with Google Apps Script

And I have read the following Strip documentation:

https://stripe.com/docs/api/balance_transactions

My code is:

function callStripe() {
  var url = "https://api.stripe.com/v1/balance_transactions";

  var params = {
    method: "GET",
    headers: {Authorization: "Basic " + Utilities.base64Encode("sk_test_XXX:")},
    payload: {id: "txn_1GryACFHk3f0sLjYo9FDDSM1", source: "ch_1GryABFHk3f0sLjY88wmWPFy"}
  };
  var res = UrlFetchApp.fetch(url, params);
  Logger.log(res.getContentText())
}

When I ran the function, I got the following error:

Exception: Request failed for https://api.stripe.com returned code 404. Truncated server response: { "error": { "message": "Unrecognized request URL (POST: /v1/balance_transactions). Please see https://stripe.com/docs or we can help at http... (use muteHttpExceptions option to examine full response) (line 59, file "Stripe")

Line 59 is referring to the var res = UrlFetchApp.fetch(url, params);

Upvotes: 0

Views: 513

Answers (1)

0Valt
0Valt

Reputation: 10355

Problem

Stripe API request returns the following error message and 404 HTTP code:

"Unrecognized request URL (POST: /v1/balance_transactions)"

Solution

Remove the payload parameter from advanced fetch() method options. GET requests cannot have a body (which is what payload parameter represents), but UrlFetchApp assumes that if the payload is provided then the developer actually attempts to make a POST request and changes the method accordingly.

Reference

  1. Stripe API reference
  2. GET method reference
  3. POST method reference
  4. fetch() method reference

Upvotes: 1

Related Questions