#Mr Stinky Pants

1 messages · Page 1 of 1 (latest)

sudden treeBOT
flint portal
#

Hi there

#

There are a couple ways to do this

#

But I would recommend using a SetupIntent to ensure the card is set up and attached to the Customer

graceful pine
#

well ill tell you what ive done so far

#

customer = customerResult.Data[0];
subscription = customer.Subscriptions.First();

                InvoiceService invoiceService = new InvoiceService();
                invoice = invoiceService.Get(subscription.LatestInvoiceId);

                PaymentIntentService paymentIntentService = new PaymentIntentService();
                paymentIntent = paymentIntentService.Get(invoice.PaymentIntentId);
flint portal
#

Then you can update the Sub's or Customer's default with that Card

graceful pine
#

well this is if the card has failed

#

so i assume there will always be a payment to take anyway?

flint portal
#

Oh okay if the card fails then yes you want to grab the latest_invoice.payment_intent.client_secret and pass that to your frontend, and then confirm the PaymentIntent using the new card details.

graceful pine
#

but thinking about it you might be right

#

because a customer might want to update card with no due invoice

flint portal
#

Yeah you likely want a different flow for updating a card outside the billing cycle

graceful pine
#

well i did try to extend it actual but it said it wasnt valid

#

customerSearchOptions.AddExpand("data.subscriptions.data.paymentIntent");

#

whats up with that

#

oh because there latest invoice

#

yes i also tried this but that doesnt work

#

InvoiceGetOptions invoiceGetOptions = new InvoiceGetOptions();
invoiceGetOptions.AddExpand("paymentIntent");

                InvoiceService invoiceService = new InvoiceService();
                invoice = invoiceService.Get(subscription.LatestInvoiceId, invoiceGetOptions);
flint portal
#

Let's back up a second.

#

What are you using to determine a failed payment?

#

Are you using Webhooks?

#

Is this only for Sub-creation?

graceful pine
#

no i havent done that bit so far

flint portal
#

Or are you focused on handling renewals as well?

graceful pine
#

this is when card fails on the old global payments

#

to move to stripe

#

oh sorry i did that bit

#

this is just assuming i get an email from stripe a payment fails?

#

i would send a user to this page

#

i would link it with a webhook later

#

so what will it make with setup intent

#

a new payment method?

flint portal
#

Yes, but you don't really want to use a SetupIntent if you are going to charge that PaymentMethod immediately.

#

For the above, you look like you are attempting to list Invoices for a Sub.

graceful pine
#

no ill do what you say for now

flint portal
#

I wouldn't go that route

#

And then you can expand using subscription.LatestInvoiceId

graceful pine
#

ill just update the card and ill manually apply that in the dashboard for a start?

#

then i can take payment for any due invoices

#

in the dashboard

#

i can automate that later

#

or i guess i can apply the payment method to the sub in webhook

flint portal
#

I would not recommend using the Dashboard to update cards. This brings you into a higher PCI scope because you will have to handle card details.

#

Would really recommend just putting a full integration together here, or using the Customer Portal instead for this customer management

graceful pine
#

yes i dont mean update the card

#

i mean using elements

#

so is setup intent making a new payment method?

flint portal
#

Yes

#

Are you using Payment Element? Or Card Element?

graceful pine
#

payment

#

ive used that already for the setup

#

thats all working

flint portal
#

Gotcha, then yeah. You create a SetupIntent on your Server, pass the client-secret to frontend, render Payment Element, and use confirmSetup()

#

That will create a new PaymentMethod and attach it to the Customer (assuming you passed a Customer ID when creating the SetupIntent)

graceful pine
#

yes so then ill have 2 payment methods?

flint portal
#

Yep

graceful pine
#

i guess ill have to remove any others first?

flint portal
#

Up to you whether to remove or not. But you mostly want to just update the Customer's invoice_settings.default_payment_method with the new PaymentMethod

#

So that it is used for all of their Subs going forward

graceful pine
#

so what event would i listen for?

flint portal
#

In terms of...?

graceful pine
#

i guess i need a webhook to know when the setupintent is done

flint portal
#

Gotcha, yeah a Webhook is the safest here. You can also just use the callback from confirmSetup but it is always possible that the customer navigates away before that callback completes so we do recommend Webhooks. You would use setup_intent.succeeded in this case.

graceful pine
#

oh yeah cheers

#

will that fire with a new subscription as well though?

flint portal
#

That will fire anytime you confirm a SetupIntent successfully

graceful pine
#

its saying No such payment_intent: 'seti_1M46GsJKY8YXH2zhPjg0lv69'

flint portal
#

What is?

#

Are you using confirmSetup()?

#

And that isn't a client-secret, that is a SetupIntent ID.

graceful pine
#

well i got it from here
ClientSecret = service.Create(options).ClientSecret;

#

it was on confirmpayment though

#

thats updated default payment on customer in test mode cheers

#

i guess the next job is to check for any unpaid invoices and pay them?

flint portal
#

Yep

#

Though mostly I would expect this flow in between cycles when there wouldn't be any unpaid invoices

#

But you could also check for unpaid invoices and charge them with the new PaymentMethod if you want to do that