#digdug
1 messages · Page 1 of 1 (latest)
Hello there
You want to use collection_method: 'charge_automatically' in this case
If you use send_invoice then you have to have an email associated to the Customer
we are not using send_invoice we are creating invoice and using /pay
Can you provide me an example Invoice that hit this error?
invoice does not create
just the error
here is the function async function createCreditCardInvoice(stripeCustomerId, amount, description, cardId, projectId, paymentType, submittedBy) {
let invoice;
try {
const customer = await stripe.customers.retrieve(stripeCustomerId);
const defaultPaymentMethod = customer.invoice_settings.default_payment_method;
await stripe.customers.update(stripeCustomerId, {
invoice_settings: {
default_payment_method: cardId,
},
});
invoice = await stripe.invoices.create({
customer: stripeCustomerId,
collection_method: 'send_invoice',
days_until_due: 0,
auto_advance: true,
description: "Pay by Card",
metadata: {
projectId: projectId,
paymentType: paymentType,
submittedBy: submittedBy,
},
});
const invoiceItem = await stripe.invoiceItems.create({
customer: stripeCustomerId,
amount: amount,
currency: 'usd',
description: description,
invoice: invoice.id,
});
const finalizedInvoice = await stripe.invoices.finalizeInvoice(invoice.id);
const paidInvoice = await stripe.invoices.pay(finalizedInvoice.id);
await stripe.customers.update(stripeCustomerId, {
invoice_settings: {
default_payment_method: defaultPaymentMethod,
},
});
return paidInvoice;
} catch (error) {
console.error('Error creating invoice:', error);
if (invoice) {
return { error:error, invoice:invoice };
} else {
throw new Error(error.message); // if the invoice doesn't exist, just propagate the error
}
}
}
Yeah okay can you give me a request ID for the error?
See: https://support.stripe.com/questions/finding-the-id-for-an-api-request for how to grab one
Err
You can see in your code above
collection_method: 'send_invoice',
Which is what I stated
You need to change that
but will that charge automatically the default payment method?
no we are specifying the card
Okay then you can just create the Invoice and then call /pay and specify the payment_method (https://stripe.com/docs/api/invoices/pay#pay_invoice-payment_method) and that will finalize and pay it
You don't actually need to have a separate request to finalize prior to calling /pay
You can also create the Invoice with auto_advance: false as I mentioned so that it doesn't charge without you taking explicit action
Okay. Awesome. Thank you kindly
Sure
is an invoice also finalized when it is sent?
we do this for ach wondering if it is redundant as well const finalizedInvoice = await stripe.invoices.finalizeInvoice(invoice.id);
const sentInvoice = await stripe.invoices.sendInvoice(finalizedInvoice.id);
Yep
good deal. One more question. I am specifying payment method here but it is still charging the default card on file const paidInvoice = await stripe.invoices.pay(invoice.id, {
payment_method: cardId,
});
Hmm can you give me the request ID for that?
req_CsnX3gL0bbdw3J
we have the declined card set as default but specify another cardid here const paidInvoice = await stripe.invoices.pay(invoice.id, {
payment_method: cardId,
});
Thanks looking
Not seeing that you passed the payment_method on that request: https://dashboard.stripe.com/test/logs/req_CsnX3gL0bbdw3J
I am passing it here const paidInvoice = await stripe.invoices.pay(invoice.id, {
payment_method: cardId,
});
Yeah but if you look at the request it isn't present
So either your code is cached
Or you didn't save
Or something
You can see the POST parameters via looking at the request in your Dashboard
is payment_method: id of credit card acceptable or is that wrong
Yep that works fine
interesting
then this should be correct given that cardid is the card specified? async function createCreditCardInvoice(stripeCustomerId, amount, description, cardId, projectId, paymentType, submittedBy) {
let invoice;
try {
const customer = await stripe.customers.retrieve(stripeCustomerId);
const defaultPaymentMethod = customer.invoice_settings.default_payment_method;
invoice = await stripe.invoices.create({
customer: stripeCustomerId,
collection_method: 'charge_automatically',
auto_advance: false,
description: "Pay by Card",
metadata: {
projectId: projectId,
paymentType: paymentType,
submittedBy: submittedBy,
},
});
const invoiceItem = await stripe.invoiceItems.create({
customer: stripeCustomerId,
amount: amount,
currency: 'usd',
description: description,
invoice: invoice.id,
});
const paidInvoice = await stripe.invoices.pay(invoice.id, {
payment_method: cardId,
});
return paidInvoice;
} catch (error) {
console.error('Error creating invoice:', error);
if (invoice) {
return { error:error, invoice:invoice };
} else {
throw new Error(error.message); // if the invoice doesn't exist, just propagate the error
}
}
}
Yep the code looks fine