4

I'm trying to create a stripe usage record for a customer on a metered plan.

When I'm using timestamp Date.now() in my request. The error I'm receiving is

"Cannot create the usage record with this timestamp because timestamps must be before the subscription's current period end time"

This seems self-explanatory. But given the subscription's current period end time isn't for another 14 days, how can Date.now() not be before this.

 await stripe.usageRecords.create(
 'si_EwzQ....',
 {
 quantity: 2,
 timestamp: Date.now(),
 action: 'set'
 }
 )

Is this because the current subscription period is a trial? Or have I misunderstood something here?

asked Apr 25, 2019 at 11:22

3 Answers 3

12

Stripe is using a slightly different timestamp here. It's the number of seconds. You must divide it by 1000.

  1. So Date.now() / 1000 shall work.
  2. However, Stripe APIs expects a whole number so Math.ceil(Date.now() / 1000) or (Date.now() / 1000).toFixed(0) should work. (@William's comment, verified)
ankitjaininfo
12.4k7 gold badges55 silver badges76 bronze badges
answered Apr 25, 2019 at 11:28
Sign up to request clarification or add additional context in comments.

1 Comment

This still breaks if the resulting number has decimals, e.g. 1570202647.927. To avoid this just do Math.ceil(Date.now() / 1000)
2

Node.JS uses millisecond timescales, so you are saying the current time is 1000 times more seconds than it currently is.

Just do this first var currentTimestamp = Date.now()/1000

answered Apr 25, 2019 at 13:38

1 Comment

The above will give the result in decimal format, so you can use Math.ceil(Date.now() / 1000)
1

Use Math.ceil(Date.now() / 1000) instead as Stripe uses a different format (without decimals).

answered May 30, 2021 at 14:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.