#hurin_webhooks

1 messages ¡ Page 1 of 1 (latest)

arctic mirageBOT
#

👋 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/1298811019715543072

📝 Have more to share? Add more details, code, screenshots, videos, etc. below.

thorny wedge
#

Hi @keen saddle , to give you more background info,

I am implementing a referral system and the referrer only get the credit granted when the the person they referred has bought a subscription and they have surpassed the trial period and have been charged for a month.

In my current logic, I require the status of the subscription to be active (and do not accept weekly subscriptions):

// Check if this is a payment for a monthly or quarterly plan
const subscription = await stripe.subscriptions.retrieve(subscriptionId);
const interval = subscription.items.data[0].price.recurring?.interval;
console.log('Interval:', interval);
console.log('Subscription status:', subscription.status);
if (subscription.status !== 'active' || interval === 'week') {
console.log('Not eligible for referral credit (weekly plan or not active)');
return; // Not eligible for referral credit (weekly plan or not active)
}

// Check if this is the first billing cycle
// We'll use the created timestamp and current_period_start to determine this
const isFirstBillingCycle = subscription.created === subscription.current_period_start;
console.log('Is first billing cycle:', isFirstBillingCycle);
if (!isFirstBillingCycle) {
console.log('Not the first payment');
return; // Not the first payment
}

I am executing this in a function when I get the invoice.payment_suceeded event, so the question is whether I will get a second event when the user has been charged after the trial period - it has to be the invoice.payment_suceeded.

#

My AI suggested I tweak my code to this to account for trial periods by replacing the above with this:

// Check if this is the first paid invoice
const invoices = await stripe.invoices.list({
customer: customerId,
subscription: subscriptionId,
limit: 2,
status: 'paid'
});

if (invoices.data.length !== 1) {
console.log('Not the first paid invoice');
return; // Not the first payment
}


keen saddle
#

Hi @lavish crystal is this a question about whether you'll receive a invoice.payment_succeeded event for the first $0 invoice?

thorny wedge
#

yes, sir and if I will receive a second one after the trial period

keen saddle
thorny wedge
#

Thanks for this, I saw these clocks, very comprehensive docs but I am afraid I am not as smart as the people who wrote them. I shall test my clocks all al naturale... I have just gone through the entire process and I have just updated my trial period to 1 day instead of 2 days and for some reason, Stripe doesn't recognise 1 day of a trial - immediately goes on to charge the users - is this by design? 2 days of trial shows up on the check out - try out for 2 days for free, while 1 day doesn't - why is that?

keen saddle
#

what's the subscrpition ID?

thorny wedge
#

sub_1QDFtmCszBkpi7B0YDjtMyBW

keen saddle
thorny wedge
#

Hmm, good spot, there seems to be a unique function which calcultes when the trial is supposed to finish and it had null, undefined, <2 days - when I changed it to <1 day and left the 1 day in my sql table I got the following error from Stripe:

StripeInvalidRequestError: The trial_end date has to be at least 2 days in the future.
at generateV1Error

keen saddle
thorny wedge
#

oh I see, by one of the strings it is by design 2 days free as a minimum, and by the other when you specify the actual days not the end date - it's 1 day