#bengtnamada_api
1 messages ¡ Page 1 of 1 (latest)
đ 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.
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);
}
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
Maybe there's another Invoice that this Invoice Item was attached to instead?
I can delete all invoices and invoice items and run through the process again to show you
Maybe you could use a fresh Customer object
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?
Why are you deleting the customer? And how is API key related to this?
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
You can just create a new Customer without touching the old ones. Unless we are talking about different things.
What API request have you made for this?
I know, but I wanted to use the same email lmao
Ok wait its not working at all now :/
You can use the same email for multiple Customer object, it's not a unique key.
Oh interesting
What's not working exactly? Could you please share the error messages and Request IDs?
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
I suggest you to use stripe-node library, instead of calling the API directly.
I need to do this in AppsScripts
Can you check if secretApiKey is not empty?
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
You can as well specify the Invoice ID when creating an Invoice Item: https://docs.stripe.com/api/invoiceitems/create#create_invoiceitem-invoice
It's better since it's more explicit.
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
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.
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
Let me check...
Oh, you're probably missing this: https://docs.stripe.com/api/invoices/create#create_invoice-pending_invoice_items_behavior
I thought it's set by default to true, sorry.
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
I would suggest testing with your own email address first.
Probably a good idea
Since it's Live mode, we will actually send emails.
It's a friend but he's asleep right now haha
I'll send one to myself one sec
Thanks! Everything works
Happy to help.