#nadiya_code
1 messages ยท Page 1 of 1 (latest)
๐ Welcome to your new thread!
โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always 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/1399316878513406084
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
Hi there!
Can you share the ID (req_xxx) of the failing API request?
https://support.stripe.com/questions/finding-the-id-for-an-api-request
yes a minute
You should be able to add a discount in each phase of the Subscription schedule
https://docs.stripe.com/api/subscription_schedules/update#update_subscription_schedule-phases-discounts
req_cwBM1nhLkVjcg2
i think i wrote similar code.
subscription schedule is already created.
now, i need to attach specific coupon to an existing subscription (for all its phases)
also, i'm retrieving the schedule from stripe, and using its phase
const schedule = await stripe.subscriptionSchedules.retrieve(subscription.schedule);
You should remove application_fee_percent from the phase
Or set a valid decimal, you are sending empty value
i'm not manually adding it.
Ah your backend is sending it according to the request Id ๐ค
if (subscription.schedule) {
const schedule = await stripe.subscriptionSchedules.retrieve(subscription.schedule);
const updatedPhases = schedule.phases.map((phase) => {
// Destructure all keys except `coupon`
const { coupon, ...rest } = phase;
// Return the phase with discounts applied
return {
...rest,
discounts: [{ coupon: couponId }],
};
});
// Step 3: Apply coupon to all phases
const updatedSchedule = await stripe.subscriptionSchedules.update(schedule.id, {
phases: updatedPhases,
});
console.log(`โ
Applied coupon ${couponId} to ALL phases of scheduled subscription ${subscriptionId}`);
return updatedSchedule;
}
do i need to santize all the input that have blank or null values from the stripe?
is it there better or simpler soultion to just update coupon to all the phases of subscription schedule?
You need to omit for empty fields here:
// Destructure all keys except `coupon` const { coupon, ...rest } = phase; // Return the phase with discounts applied return { ...rest, discounts: [{ coupon: couponId }], };
is it there better or simpler soultion to just update coupon to all the phases of subscription schedule?
No, You need to fetch all the Subscription schedule phases and update them one by one.
You should update your code, and remove blank fields
i see, okay
another question: do we need to pass entire 'not null' existing phases data attributes while updating?
or can we just update it with the key that we want (like a PATCH update)
Yeah it's like a patch update, you pass null attribute only when you want to omit/delete a value of an attribute
otherwise, you can send only the fields that you want to update
Ah sorry, I knew about passing all phases, but not all attributes too.
Then yeah you should pass all phases and all attributes, but remove the blank fields
hmm okay, let me try that then
๐ taking over for my colleague. Let me know if there's any follow-up Qs I can answer!