#bill92_webhooks

1 messages ยท Page 1 of 1 (latest)

copper stagBOT
#

๐Ÿ‘‹ 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/1308589359532085350

๐Ÿ“ Have more to share? Add more details, code, screenshots, videos, etc. below.

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.

rugged moat
#

Hi, let me help you with this.

upper depot
#
case 'customer.subscription.trial_will_end': {
  // Send email to the customer
  await postmark.sendEmailWithTemplate({
    From,
    To,
    TemplateAlias: 'template-alias',
    TemplateModel: {},
  });
  break;
}
rugged moat
#

Why do you want to prevent it from firing?

upper depot
#

I want to send an email to the user only when their trial is exactly 3 days away from ending, not just when the trial_will_end event is triggered. (which happens when I update a subscription on the customer portal)

#

I need to make the distinction

rugged moat
#

I want to send an email to the user only when their trial is exactly 3 days away from ending, not just when the trial_will_end event is triggered. (which happens when I update a subscription on the customer portal)
I don't think I understand it from your message. According to the docs this happens in 2 cases:
(1) 3 days before trial ends
(2) when you set trial_end=now

Do you want to do it just in case (1), or both (1) and (2)?

upper depot
#

these are the order of the events

rugged moat
#

In any case, it's best to check the value of the trial_end property when you receive the webhook and make the decision in the code.

upper depot
#

here is my flow:

1 - user signs up to my platform
2 - they select a product and a price id
3 - a subscription is created with a trial of 7 days with trial_period_days
4- if the user goes to the customer portal and updates his subscription to a paid one (lets say he pays the annual fee)
5- the trial_will_end event is triggered

#

isn't there a way to prevent this behavior? like a checkbox on settings? ๐Ÿค”

rugged moat
#

No. You need to make the decision in your own code.

upper depot
#

due to the nature of webhooks, the decision can not always be precise

#

but I'll give it a shot

rugged moat
copper stagBOT
upper depot
#

the slight offset between when the stripe event is fired and when I compute the current time (now)

const trialEndDate = stripeEvent.data.object.trial_end;
        const now = Date.now() / 1000;

        if (trialEndDate < now) {
          console.log('no email sending since the trial will not end now');
          break;
        }
#

but, then again.. its not the end of the day

arctic swan
#

๐Ÿ‘‹ Taking over this thread, catching up now

#

Your code looks fine to me

upper depot
#

doing some testing real quick

#

have you had other users talk about this to you river?

arctic swan
#

This is expected behaviour

#

Your code should handle accordingly. There can be latency due to network and other factors that cause the slight offset

upper depot
#

weird, on both cases my email is being sent ๐Ÿค”

arctic swan
#

I'd recommend logging the timestamp in your code and different code block to identify why the email was sent

upper depot
#

stripe trial_end is in seconds and not in miliseconds, right?

arctic swan
#

trial_end is UNIX timestamp in seconds in UTC

#

I'd recommend ensuring your current timestamp is in UTC as well

upper depot
#

I'll need to add the three days from now, otherwise the email will always be sent

#

๐Ÿค”

arctic swan
#

Please also ensure your โ€œnow()โ€ timestamp is in UTC, same timezone as the trial_end

upper depot
#

let me try that, one sec

upper depot
#
// Convert to timestamp in seconds
        const trialEndDate = Number(stripeEvent.data.object.trial_end); // seconds
        const threeDaysBeforeTheTrialPeriodEnds = Math.floor(trialEndDate - 60 * 60 * 24 * 3); // seconds
        const nowDate = Math.floor(Date.now() / 1000); // seconds

        if (nowDate > threeDaysBeforeTheTrialPeriodEnds) {
          console.log('skipping sending trial_will_end email');
          break;
        }
#

looks like its not working, maybe its due that my server is in the present but stripe's simulations are in the future? ๐Ÿค”

arctic swan
#

If you use test clock, then that is likely the case

upper depot
#

yes, using test clocks