Unique token to prevent multiple orders and unwanted quote validation

KFrw
KFrw Developer Posts: 3 🧑🏻‍🚀 - Cadet

Dear Spryker Community,

I hope this message finds you well!

We’re currently working on a Spryker-based shop that utilizes a PWA for both mobile and desktop users. Recently, we’ve encountered two main challenges:

  1. Long quote validation times
  2. The risk of a cart being ordered multiple times

To address the duplicate order issue, we’ve implemented a database solution: we use the UUID generated by the Glue API and store it in the spy_sales_order table, ensuring that no cart can be ordered twice.

Now, we’re focusing on making a specific set of items within a cart unique and optimizing the quote validation process. Here’s our current approach:

  • Unique Cart Identification:
    We generate a hash from the quote_data value, which serves as a unique key representing the products in the cart.
  • Locking Mechanism:
    When the hash is created, we send it—along with a timestamp and the quote UUID—to the frontend. We then use Symfony’s Lock component to lock this combination for 5 minutes, or until the quote_data changes.
  • Efficient Storage:
    The hash is stored in Redis for fast and reliable retrieval.
  • Optimized Validation:
    We perform quote validation during hash creation, so we don’t need to repeat this step, saving valuable processing time.
  • Error Handling:
    If validation fails, we promptly return error messages to the frontend.
  • Order Authorization:
    The PWA can only proceed with an order if it provides a token that we can validate against the current quote_data.

We’re interested in your feedback on this approach:

  • Are there potential pitfalls or improvements you’d suggest?
  • Has anyone implemented a similar mechanism, and if so, what were your experiences?
  • Do you see any risks regarding hash collisions or lock management that we should be aware of?

We’re also considering using a stronger hash function (like SHA-256) and expanding the hash input to include additional factors such as stock levels and price changes, to further enhance reliability.

Thank you in advance for your insights and suggestions!

Best regards,
Karsten

Answers

  • KFrw
    KFrw Developer Posts: 3 🧑🏻‍🚀 - Cadet

    I finally figured that I understood JWT wrong and we don't need a lock as we can just trust the signed token.

    But still in anycase how do you guys handle the problem that you could order the same quote multiple times or get timeouts during the order and the customer clicks on buy again. Or multiple browser windeows / Tabs / connections from a PWA. This is as far as I know nothing spryker core resolves as Yves does everything synchronous and that leads to all those problems.

    Happy easter weekend to you all!

    Karsten