Shawn Wernig
Shawn Wernig

Reputation: 1742

Randomly generate a Self Validating "Coupon" code

Here's the use-case; I'm developing an eCommerce ticket sales website for a private event.

This event would like to send out a special one-time-use only code that must be entered in order to purchase the ticket. These will be printed on the physical copy of the invite, and is the only method of delivery.

Only if the code validates will the product (ticket) form appear. I will set a session variable, and allow purchase. After the purchase has been validated, I will store the code in the DB to ensure it is not redeemed a second time.

Restrictions:

  1. We do not know how many codes are required (2000 max
  2. We may need to generate more codes on-the-fly
  3. The codes should not be cumbersome in length, a string of 4-12 alphanumeric

I want to find a clever way to create "self-validating" codes, that by using a secret salt, and a hash or some nifty logic, determine whether a given code is valid; Then, provide some method of generating new valid codes which they can use?

A simple example of generating, and validating a series of codes is what I'm looking for, as I'm unsure of how to approach this problem.

Thanks!

Upvotes: 0

Views: 382

Answers (1)

dannymitza
dannymitza

Reputation: 59

One way that I think this can be done is to generate each code in a way that the value of few chosen characters return a sum of (let's say) multiple of 7. Even better, 1/3 of character's sum must be multiple of 7, and other 1/3 of characters sum must be (for exemple) greater than x, where x is a defined value.

Just a thought.

Upvotes: 1

Related Questions