Human readable Order ID's?


#1

Hi there!

Are there any plans to support shorter/human readable Order ID’s?

Right now they’re something like Order #deaddd35-d2d2-4a23-ae32-3c68fb0c45f5, which is pretty long to give to a customer.

Thanks!


#2

Hey :wave:

This has came up a few times and I’ll share again with our product and engineering team, we’d love to do this but it’s currently not a feature.

You could attempt to do this yourself with custom flows fields and our integrations API. When a product is created you could trigger a function to increment a flow field value based on the previous or generate unique shorter IDs.

I’m happy to put together a quick solution using shorter IDs with our integrations API if this approach is of any interest.

Have a great day :grinning:


#3

Hey Jamie!

Thanks for getting back to me so quickly.

The flows API is something I have yet to wrap my head around so if you could put together a quick example that would be very much appreciated! I’m using the JS SDK if that’s of relevance.

Thanks again!


#4

Hey @HelloTiago

Awesome! We’re still figuring out the best way to deliver content around Flows that explains the true power. Essentially it’s a content API that allows you to define fields and store data as entries. You can also extend core resources like products and categories so you can attach additional data to resources.

I’ll put something together now :smile:


#5

Hi @HelloTiago :tada:

I’ve created a short function that runs on Zeit Now that you can use with our integrations API. Please head over to the GitHub repo to see how to download and install the function.

I’ve outlined the steps below needed to make this work - I’ll update the GitHub README over the next few days to include this for future reference too :smile:

1. Create a new Flow

Name
Orders

Slug
orders

Description
Extending orders resource with a custom flow

Enabled
true

2. Edit the new orders flow

3. Add a new Field to orders

Type
string

Name
Short ID (managed by integrations event)

Slug
short_id

Unique
true

Enabled
true

Description
This is a short ID managed by an order.created event.

4. Deploy the function to Zeit Now

You could also convert this to be a Serverless function that runs on AWS Lambda, Microsoft Azure, Google Functions etc. Take a note of the endpoint you get back from Zeit Now, you’ll need this in the next step.

5. Create a new integration via the integrations API endpoint

POST to https://api.moltin.com/v2/integrations with the following payload:

{
  "data": {
    "type": "integration",
    "name": "Short order ID",
    "description": "Automatically create a short order ID",
    "enabled": true,
    "observes": [
      "order.created"
    ],
    "integration_type": "webhook",
    "configuration": {
      "url": “<ZEIT_URL_GOES_HERE>”
    }
  }
}

Now if you create an order, you should see that the short_id on any order is updated with a shorter random string.

You’ll be able to see this field inside the Dashboard too.

42

Of course you could replace the cuid package with something that generates a random number, based on the current time, but I’ll leave this level of detail up to you :smile: There is also http://hashids.org which is another great library.

I hope this helps :sunny:

PS. You’ll need to the Zeit Now CLI on your machine to deploy this function.


Moltin short order ID webhook
"Unique" flow field attribute behavior
#6

Hi @notrab!

Thanks for putting this together!

I’ve followed all your steps and managed to get a Short ID being generated and showing up in the Dashboard. I’d like to present this short ID on the website to the user too, however I don’t think that’s possible with an implicit token, right? https://cl.ly/r2su

That’s strange though, as your demo React store has the same method I’m trying to use but am getting a permission error: https://github.com/moltin/react-demo-store/blob/23346c500621d4fdd2e3e0cbfa7b38dca287db6e/src/moltin.js#L44

I also tried the following but the orderInfo variable is just an empty object :confused:

Moltin.Orders.Payment(order.data.id, paymentData)
   .then((orderInfo) => {
   console.log(orderInfo);

   Moltin.Cart().Delete();
})

#7

@HelloTiago If you’re trying to retrieve an order implicitly you’ll have to pass a token as a parameter for the customer associated with the order.

// Authenticate your customer and return a JWT
const { data: { token } } = await Moltin.Customers.Token(email, password)

// Retrieve the order record
const { data: order } = await Moltin.Orders.Get(orderId, token)

#8

@Jonathan Is there no other way to access the short order ID field? We’re not creating customer accounts in our app (it’s a straightforward order page) but we’d still like to show the short ID on the “Thank you” page.


#9

Does the order object returned when you call Moltin.Cart().Checkout() not contain your custom field?


#10

@jonathan It does not, no :confused:


#11

That makes sense, as the webhook to add the short order reference is triggered on the order.created event and at that point you’ve already had the response from the checkout call.

Seemingly the only way to achieve this right now with implicit credentials is via the customer token method (suggested above :point_up: ). Unless @notrab has some other suggestions? :thinking:


#12

Hey @HelloTiago @jonathan :wave:

I think the only way to achieve what you’re asking (show short ID on confirm order screen) is to either update the page after it loads with a call to the order again after X seconds. You could fake the user confirmation screen with some indicator, when all it’s doing is polling to see if short_id is present.

This process would be a lot easier if using Flows you could provide the cart with custom data that is transferred to orders. You can see what’s possible with Flows over here.

Hopefully that gives you some thoughts, I’ll make sure to raise the cart custom data to order flow issue internally, which may unblock you and many others :smile:


#13

I think the only way to achieve what you’re asking (show short ID on confirm order screen) is to either update the page after it loads with a call to the order again after X seconds.

This is what I attempted though and ran into the issue described above: the GET Order endpoint doesn’t work via the JS SDK with Implicit tokens. Is there any chance that could be updated on your end? Make the GET order endpoint accessible with Implicit tokens?

Thanks again!


#14

Hey @HelloTiago

Unfortunately that wouldn’t be something I could see happen. The order resource contains sensitive information that should be locked behind a secret token.

You could however build your own service that exposes the short_id. For example the following function would return the short_id. You could then use this to poll for changes.

const { send } = require('micro')
const { get, router } = require('microrouter')
const cors = require('micro-cors')()
const moltinGateway = require('@moltin/sdk').gateway

const moltin = moltinGateway({
  client_id: process.env.MOLTIN_CLIENT_ID,
  client_secret: process.env.MOLTIN_CLIENT_SECRET
})

module.exports = cors(
  router(
    get('/:orderId', async (req, res) => {
      const { orderId } = req.params

      try {
        const {
          json: {
            data: { short_id }
          }
        } = await moltin.Orders.Get(orderId)

        send(res, 200, {
          short_id
        })
      } catch ({ status, json }) {
        send(res, status, json)
      }
    })
  )
)

PS. The above is using a slightly modified SDK we’re testing to be state free. You can see the package.json over on GitHub Gist.


#15

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