#digdug

1 messages · Page 1 of 1 (latest)

lofty heathBOT
steady sedge
#

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

fast trench
#

we are not using send_invoice we are creating invoice and using /pay

steady sedge
#

Can you provide me an example Invoice that hit this error?

fast trench
#

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
}
}
}

steady sedge
#

Yeah okay can you give me a request ID for the error?

#

Err

#

You can see in your code above

#

collection_method: 'send_invoice',

#

Which is what I stated

#

You need to change that

fast trench
#

but will that charge automatically the default payment method?

steady sedge
#

Yes unless you set auto_advance: false

#

Do you not want to charge the default?

fast trench
#

no we are specifying the card

steady sedge
#

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

fast trench
#

Okay. Awesome. Thank you kindly

steady sedge
#

Sure

lofty heathBOT
fast trench
#

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

steady sedge
#

Yep

fast trench
#

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,
});

steady sedge
#

Hmm can you give me the request ID for that?

fast trench
#

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,
});

steady sedge
#

Thanks looking

fast trench
#

I am passing it here const paidInvoice = await stripe.invoices.pay(invoice.id, {
payment_method: cardId,
});

steady sedge
#

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

fast trench
#

is payment_method: id of credit card acceptable or is that wrong

steady sedge
#

Yep that works fine

fast trench
#

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
}
}
}

steady sedge
#

Yep the code looks fine

fast trench
#

well it doesnt work lol

#

Will go back to changing default payment then updating back

#

Appreciate your help with everything else.

steady sedge
#

I'd recommend double checking that everything is saved and restart your server and tryagain

#

And then check the specific /pay request

#

And look at the POST parameters

#

You last example did not include passing the payment_method parameter