#bengtnamada_api

1 messages ¡ Page 1 of 1 (latest)

turbid pastureBOT
#

👋 Welcome to your new thread!

⏲️ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

⏱️ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

🔗 This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1228281617337024542

📝 Have more to share? Add more details, code, screenshots, videos, etc. below.

peak grove
#

I've attached the relevant code below:

  var invoiceItemPayload = {
    'customer': customerId,
    'amount': amount,
    'currency': currency,
    'description': description
  };

  var invoiceItemOptions = {
    'method': 'post',
    'headers': {
      'Authorization': 'Bearer ' + secretApiKey,
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    'payload': Object.keys(invoiceItemPayload).map(function(key) {
      return encodeURIComponent(key) + '=' + encodeURIComponent(invoiceItemPayload[key]);
    }).join('&'),
    'muteHttpExceptions': true
  };

  var invoiceItemResponse = UrlFetchApp.fetch('https://api.stripe.com/v1/invoiceitems', invoiceItemOptions);
  var invoiceItemJson = JSON.parse(invoiceItemResponse.getContentText());

  if (invoiceItemResponse.getResponseCode() == 200) {
    Logger.log("Invoice item created successfully");
  } else {
    Logger.log("Failed to create invoice item: " + invoiceItemJson.error.message);
    return;
  }

  var invoiceItemId = invoiceItemJson.id

  var daysUntilDue = 30;
  var invoiceOptions = {
        'method': 'post',
        'headers': {
            'Authorization': 'Bearer ' + secretApiKey,
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        // Add the collection_method here in the payload for creating the invoice
        'payload': "customer=" + encodeURIComponent(customerId) + "&collection_method=send_invoice" + "&days_until_due=" + daysUntilDue + "&auto_advance=true",
        'muteHttpExceptions': true
  };

  var response = UrlFetchApp.fetch('https://api.stripe.com/v1/invoices', invoiceOptions);
  var jsonResponse = JSON.parse(response.getContentText());

  if (response.getResponseCode() == 200) {
      Logger.log("Invoice created successfully: " + jsonResponse.id);
      Logger.log(jsonResponse);
  } else {
      Logger.log("Failed to create invoice: " + jsonResponse.error.message);
  }
sweet ledgeBOT
rancid spire
#

Hi, let me help you with this.

#

Could you please share the Invoice ID?

peak grove
#

Hey!

#

Let me run the flow and send you one sec

#

The invoice id is in_1P4gwx1SJWYUlloEOfiMNRFP

#

And the corresponding invoice_item id I would like to attach is ii_1P4gwx1SJWYUlloECq2r6pw3, although all of the pending invoices would be what I would expect at this point

rancid spire
#

Maybe there's another Invoice that this Invoice Item was attached to instead?

peak grove
#

I can delete all invoices and invoice items and run through the process again to show you

rancid spire
#

Maybe you could use a fresh Customer object

peak grove
#

Wait are you sure? I feel like I'm not using the endpoints correctly or something

#

Just created this invoice id in_1P4h3y1SJWYUlloEFVyIscXV

#

After deleting my customer, now my api key is invalid?

rancid spire
#

Why are you deleting the customer? And how is API key related to this?

peak grove
#

I have no idea

#

I deleted it in order to have a new customer i can create

#

And now it says my api key is invalid when i run the same function :S

rancid spire
peak grove
#

I know, but I wanted to use the same email lmao

#

Ok wait its not working at all now :/

rancid spire
#

You can use the same email for multiple Customer object, it's not a unique key.

peak grove
#

Oh interesting

rancid spire
peak grove
#

function createStripeCustomer(secretApiKey, email, name) {
var customerData = {
"email": email, // Replace with your customer's email
"name": name, // Replace with your customer's name
};

var options = {
'method' : 'post',
'headers' : {
'Authorization': 'Bearer ' + secretApiKey,
'Content-Type': 'application/x-www-form-urlencoded'
},
// Convert customerData to URL-encoded string for the request payload
'payload' : Object.keys(customerData).map(function(key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(customerData[key]);
}).join('&'),
'muteHttpExceptions': true // Handle errors gracefully
};

try {
var response = UrlFetchApp.fetch('https://api.stripe.com/v1/customers', options);
var jsonResponse = JSON.parse(response.getContentText());
if (response.getResponseCode() == 200) {
Logger.log("Customer created successfully: " + jsonResponse.id);
} else {
// Handle API error
Logger.log("Error creating customer: " + jsonResponse.error.message);
}
} catch (e) {
Logger.log("Exception occurred: " + e.toString());
}
}

#

Thats my code

#

Error is Error creating customer: Invalid API Key provided: undefined

#

But im clearly passing my new api key

#

I can log it as well

rancid spire
#

I suggest you to use stripe-node library, instead of calling the API directly.

peak grove
#

I need to do this in AppsScripts

rancid spire
#

Can you check if secretApiKey is not empty?

peak grove
#

Sorry it was i was running the wrong function fml

#

Ok I created a new customer

#

This is my new invoice id in_1P4hGM1SJWYUlloEDLuooX9x

#

Once again, it has 0.00 amount attached

#

My logs are:

POST /v1/invoices
200 OK
12/04/2024, 10:23:18
POST /v1/invoiceitems
200 OK
12/04/2024, 10:23:18
POST /v1/customers
200 OK
12/04/2024, 10:22:59
rancid spire
peak grove
#

Ok I can try that, so I create the invoice first, and then attach the invoice item to it

#

But then again, in the docs it claims that it will be added to the next invoice created, though this seems to be false

rancid spire
#

It is supposed to work this way, unless there's some edge-cases. But it's always better and safer to set the Invoice ID explicitly, as long as your use case permits.

peak grove
#

Yeah, I assume it will be fine. I dont understand what the edge case is though

#

Should I submit an issue?

#

It works btw if i create invoice first and then attach the invoice item to it afterwards

rancid spire
#

I thought it's set by default to true, sorry.

peak grove
#

Oh same!

#

Nice catch

#

Can you check if this email was sent out btw? in_1P4hRZ1SJWYUlloEtbmY80tM
I'm not sure if it's sufficient to finalise or whether I need to call the send/ endpoint to have the email sent

rancid spire
peak grove
#

Probably a good idea

rancid spire
#

Since it's Live mode, we will actually send emails.

peak grove
#

It's a friend but he's asleep right now haha

#

I'll send one to myself one sec

#

Thanks! Everything works

rancid spire
#

Happy to help.