Sequential order number

javascript

#1

I’m trying to create a custom field on my orders called order_number, which would contain a sequential order number. I need this for my tax authorities.

This is my workflow:

  1. call moltin.Cart().Checkout() to checkout/create the order,
  2. call moltin.Orders.All() to get meta.results.total and
  3. call PUT /orders and set order_number to the previously fetched meta.results.total.

The problem is that sometimes, even though orders are created 5 to 10 minutes apart, the order_number is the same. For the majority of times it works, so I’m guessing everything is ok with my code.


#2

Hi @alex

Are you calling moltin.Orders.All() immediately after checking out? There can be a delay as the new order is replicated throughout our system, my guess would be that’s what you’re seeing here.

Thanks,
James


#3

@James I’m calling it only after I get a successful response from the checkout. This is important because this order number is then sent in the order confirmation email immediately after the checkout.


#4

This sort of thing gets tricky when it comes to distributed systems. We use eventual consistency heavily in the API to guarantee speed & availability. Your solution will also fall down if multiple orders are placed at the same time.

The simplest solution I can think of that will guarantee the numbers are sequential is to use a database to store each order ID against an auto incremented ID (checkout, insert order id, get auto incremented ID back, PUT to order). That way the database will handle the complexity for you.


#5

@James This adds complexity by adding a new external system into the equation. You can imagine what happens if that DB is unavailable for some reason :sweat_smile: Also, to have an external DB just for the auto-incrementation of the order number - sounds a bit extreme.

Since orders cannot be deleted or even pampered with, would it be possible that the Moltin API would expose a new property on orders that would handle that automatically? I imagine we’re not the only ones with such a use case. Sequential order numbers are quite popular between different tax authorities :smile:


#6

Unfortunately there isn’t a simpler solution to this right now. I’ll certainly suggest this to our product team.


#7

@James: that would be awesome :wink:


#8

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.