#jwerre_api
1 messages ¡ Page 1 of 1 (latest)
Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.
- jwerre_api, 17 hours ago, 14 messages
đ Welcome to your new thread!
â˛ď¸ We'll be here soon! We typically respond in a few minutes, but in some cases we might need a bit more time (e.g., server's busy, you've got a complex question, etc.).
âąď¸ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can 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/1250461543049592832
đ Have more to share? Add details, code, screenshots, videos, etc. below.
Is there a way to attach a payment method to a customer () once a subscription has been successfully paid for?
This should automatically happen. Do you have the ID of a subscription where the PaymentMethod did not get attached automatically?
Oh I was thinking it was attached to the subscription. Are you sure that's the case even with the payment_settings: { save_default_payment_method: 'on_subscription' }? Is there a query I can do to check if a payment method is attached to customer or sub?
If you retrieve the subscription, you will see this as default_payment_method on the subscription if the default is set specifically on the subscription
The PaymentMethod has to be attached to the customer to be used for recurring subscription payments. Now that I think of it, this may not apply to send_invoice subscriptions without that save_default_payment_method parameter. Checking in to that.
Are you using send_invoice or charge_automatically?
collection_method: 'charge_automatically',
Gotcha, so any which way this should work for your subscriptions. Are you seeing the card properly attached as you would expect?
Ya, it's working fine. I think I may be attaching additional (backup) cards to the subscription instead of the customer which is causing some issues.
Anyway, thanks for clearifying that for me.
Can you tell me more about that? In Stripe terms PaymentMethods are attached to the Customer, the subscription can designate one of those PMs as the default. But there shouldn't be PMs attached to the subscription itself and I don't think you can specify multiple defaults.
Happy to take a look if there are specific subscriptions that you have had this issue with.
Ya, let me see if I can dig up that function
/**
* @function pro.paymentMethod.setDefault
* @description Make a default payment method either from a payment intent
* which is created automatically with the subscription or with a setup
* intent which is created as backup payment methods.
* @async
* @param {String} subscriptionId The unique id of the subscription.
* @param {String} paymentId The unique id of the payment intent, setup intent or payment method.
* @return {Promise<Object>} The payment methods object. See: https://stripe.com/docs/api/payment_methods/object
*/
setDefault: async (subscriptionId, paymentId) => {
let promise, payment, sub;
if (paymentId.startsWith('pi_')) {
promise = stripe.paymentIntents.retrieve(paymentId);
} else if (paymentId.startsWith('seti_')) {
promise = stripe.setupIntents.retrieve(paymentId);
} else if (paymentId.startsWith('pm_')) {
promise = stripe.paymentMethods.retrieve(paymentId);
}
try {
payment = await promise;
} catch (err) {
throw SpError.parseStripeError(err);
}
const paymentMethodId = payment
? payment.payment_method || payment.id
: null;
sub = await stripe.subscriptions.update(subscriptionId, {
default_payment_method: paymentMethodId,
expand: ['default_payment_method'],
});
return sub.default_payment_method;
}
When a user has a list of cards on their account they can change their default card by running this function.
Gotcha, that would override the current default payment method on that subscription. Can you tell me more about the issues that that is causing for you?
Well, I don't think it's changing it because when I try to delete the old default it errors (my error not stripes) saying you cant' delete the default card. For example.
/**
* @function pro.paymentMethod.delete
* @description Remove a payment method from customer
* @async
* @param {String} id The unique id of the payment method.
* @return {Promise<Object>} The payment methods object. See: https://stripe.com/docs/api/payment_methods/object
*/
delete: async (customerId, paymentMethodId) => {
const cus = await stripe.customers.retrieve( customerId, {
expand: ['subscriptions'],
});
const currentDefaultId = get(cus,
['subscriptions', 'data', 0, 'default_payment_method']);
if (
currentDefaultId === paymentMethodId ||
cus.invoice_settings.default_payment_method === paymentMethodId ||
cus.default_source === paymentMethodId
) {
throw new SpError({
type: SpError.INVALID_ERR,
message: 'Cannot delete default payment method.',
});
}
let paymentMethods = await paymentMethod.list(customerId);
let pm = paymentMethods.find((pm) => pm.id === paymentMethodId);
await stripe.paymentMethods.detach(pm.id);
return pm;
}
Can you send me the request ID of a time that you saw that error? (req_123)
Well... it's erroring on my side before it runs on Stripe.
for example: throw new SpError({ type: SpError.INVALID_ERR, message: 'Cannot delete default payment method.', }); comes before await stripe.paymentMethods.detach(pm.id);
What line of code is that error coming from if not the detach line?
Here's a request id of the previous functin setDefault: req_yTNYIZr61AB1fd
It's coming from here:
if (
currentDefaultId === paymentMethodId ||
cus.invoice_settings.default_payment_method === paymentMethodId ||
cus.default_source === paymentMethodId
) {
throw new SpError({
type: SpError.INVALID_ERR,
message: 'Cannot delete default payment method.',
});
}
It's not a stripe error
Here's I'm just checking if the card that is trying to be delted is a default card.
Oh I apologize. I was switching between a couple of threads and looked over that in your code. So this is conflicting with your own logic on how to handle the default PM here?
No worries, I think what is happening is that when I set a new default payment method (card) I'm setting it on the subscription instead of the customer (where I should be doing it.) Then when I try to delete the old default it's still a default for the customer. So I essentially have two defaults one for the customer and one for the subscription. Does that sound right to you?
Yep, both objects can habe a default PM set. It can be set to the same PM, but they are separate settings
We use the default on the subscription if it exists, if not we use the one set on the customer
okay, good to know. Thank you.
BTW... you were extremely helpful @minor root. I was ready to end the conversation much earlier and you continued to walk me through it and now I fell much more confidient in what the problem is. I wish I always had this type of quality customer support.