Stripe API trial_end 10 seconds from now, environment independent

1.1k Views Asked by At

I'm testing failing recurrent payments with Stripe Api.

To make recurrent payment fail soon after subscription creation, subscription itself should have trial period and trial_end on customer creation should be some time soon or string 'now'.

It I pass string 'now', card is charged right on Stripe.customers.create(), making it fail that isn't my objective.

Instead, I tried to pass current unix timestamp +10 seconds, but it seems that machine clock is set earlier on couple minutes that Stripe clock.

As a result, I get trial_end 1454753552 and trial_start 1454753109 that isn't 10 but 443 seconds.

Is there any other way to make test environment independent than changing system clock?

I'm using moment().unix() + 10 to get current unix time + 10 secs and nodejs.

2

There are 2 best solutions below

0
On BEST ANSWER

For anyone in the future. Stripe has now introduced "Test Clocks" to help with this kind of situation. You can set a simulation environment at a specific time while continuously advancing the clock to create scenarios. You can learn more about it here

0
On

I'm not sure there's a clean way to do this here. What I'd do is create the customer before that call and look at the created property on the Customer object. This tells you the clock that Stripe's servers are using. You can then quickly calculate the offset with your own clock and guess the correct timestamp to put in trial_end to create a trial period that ends in 10 seconds.

I'd also recommend against the 10 seconds part because even with the clocks in perfect sync, if your call is a bit too slow for that specific request the trial would end immediately and prevent you from testing what you're looking for. I personally use a 60 seconds delay to decrease the risk.