#nerder
1 messages ยท Page 1 of 1 (latest)
๐ happy to help
Hello
so it seems to me that is not possible to setup a default payment method when the payment intent confirmed is coming from an invoice
i'll try to explain the code so that you can try to reproduce it
are we talking about a one-off invoices or a subscription's invoice?
what are you setting up as a default_payment_method to?
this is how i'm creating the invoice and finalize it
let invoice = await this.stripe.invoices.create(
{
customer: customerId,
application_fee_amount: fee.amount,
},
{
stripeAccount: stripeAccountId,
},
);
await this.stripe.invoiceItems.create(
{
invoice: invoice.id,
customer: customerId,
price: dropIn.code,
},
{
stripeAccount: stripeAccountId,
},
);
invoice = await this.stripe.invoices.finalizeInvoice(
invoice.id,
{ expand: ['payment_intent'] },
{
stripeAccount: stripeAccountId,
},
);
const clientSecret = (invoice.payment_intent as Stripe.PaymentIntent).client_secret;
do you mean you want the PM to become the Customer's invoice_settings.default_payment_method?
so the payment intent is created here
the PI that comes our from finalizeInvoice has setup_future_usage set as null
than in my frontend, using the client_secret i go ahead and confirm that PI
await _stripe.confirmPayment(
clientSecret,
PaymentMethodParams.card(
paymentMethodData: PaymentMethodData(
billingDetails: BillingDetails(
email: customerEmail,
name: customerName,
),
),
options: PaymentMethodOptions(
setupFutureUsage: PaymentIntentsFutureUsage.OffSession,
),
),
);
NOTE: i'm using flutter_stripe to confirm the PI
now what I expected to happen (as for subscriptions) is that the payment method used to confirm this payment intent is attached to the customer and saved as default
as it happens automagically for subscriptions
it actually doesn't really happen automatically for subscriptions either
what you need to do is listen to the invoice.paid event, and update the Customer to add the invoice_settings.default_payment_method property
ok, here comes the issue
https://stripe.com/docs/api/customers/update#update_customer-invoice_settings-default_payment_method
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
i was doing something similar last night
but listening to payment_intent.succeeded
and when trying to attach the payment method to a customer i get an error
it is already attached to the Customer
you just need to update the invoice_settings.default_payment_method
nope is not, if I try to updete the customer it asks me to attach it first
and if I try to attach it I can't
this is the limitation/bug here
This PaymentMethod was previously used without being attached to a Customer or was detached from a Customer, and may not be used again.
ok, let's try this
before sending the client_secret to the front-end
update the PI's s_f_u https://stripe.com/docs/api/payment_intents/update#update_payment_intent-setup_future_usage and then send the client_secret to the front-end
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
I like this!
good idea
let me try this one
very weird, doesn't find that PI but in the dashboard exists: No such payment_intent: 'pi_3M7drELAVB3C1lDI1V2mwTvT'
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
oh shit my bad sorry
forgot the connected account ID
sorry
no worries
and after that you'd have to listen invoice.paid
took me like 2 days to make it work
It's always better to use invoice.paid than payment_intent.succeeded for invoices
yes yes, i'm using that infact
since it will get you the Invoice instead of the PaymentIntent object in the webhook request body
i'll get that and set it up as default
now that the payment method is attached
sweet
actually I can even do that in payment_method.attached
what do you mean?
my understanding now is that using setup_future_usage: 'off_session' the payment method is attached to the customer but is not the default right?
so what i should do now is to listen for a webhook event either payment_intent.succeeded, invoice.paid or payment_method.attached to set it as a default on the customer
correct
but anyway no worries, now I can do it by myself. I know how to handle it from here to fit in my usecase
invoice.paid
no the other ones ๐
as you should have ๐
you too ๐ let me know if you need any more help
yeah sure thing
the API for one-off invoices is a bit weird
how so?
there are couple of strange things imo
hit me up
all of them are on finalizeInvoice
for instance
invoice = await this.stripe.invoices.finalizeInvoice(
invoice.id,
{ expand: ['payment_intent'] },
{
stripeAccount: stripeAccountId,
},
);
initially I didn't know that you can add expand to it, because the doc let you think that finalize doesn't accept params
but only the ID
infact i was wronlgy expading the payment intent in the invoice.create method
then this little workaround of updating the PI i think it should be a parameters on finalizeInvoice, like "what do you want to do with the payment method when the invoice is paid"?
instead of default it to null and let me update it afterward as we did here
like if finalizeInvoice is creating the PI for me (on top of other things), than it should let me create it as a i like it
instead of forcing me to update it manually later, which feels like a workaround
I wouldn't expand on payment_intent at all, especially now that you're doing an update on that PI
ok i see what you mean actually
i retreive it back from the update directly
btw maybe i'm doing it wrong or misunderstanding the API
anyway was a great learning
thank you so much for your help again
sorry I haven't read all of your messages, I will comment one by one
take your time
but just thought that this would make your integration faster
since we don't have to expand the PI which would take extra time
is good advice actually yes
this is a known limitation and we're working on improving this I hope soon enough
ok so the rest falls under the same statement as my previous one ๐