Add a tool for generating multiple codes for a promotion

zostay - June 7, 2007 - 14:26
Project:Promotion
Version:5.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:tcowley
Status:needs work
Description

A promotion administrator can specify a pattern and then have the promotion module generate N specific codes that match that pattern. This is similar to the E-Commerce Coupon module, but you pick the format of the codes generated.

This requires that multiple codes can be associated with a promotion.

#1

tcowley - August 15, 2007 - 19:15
Status:active» needs review

Change made:

Added new promotional code type of 'Auto-generated code'. This is for pre-generating a list of unique codes. (They look like this, for example: '3URF43RHTK53SLRT').

How it works:

  • User supplies the code length in characters (a number from 10 to 30 inclusive) and the number of codes to generate (a number from 1 to 100000).
  • Unique codes are generated from a unique hash assigned to the promotion.
  • Users can download the full list of codes in a .CSV file from a link on the Promotional Codes listing page (the code field is a download link if the code type is 'Auto-generated').
  • Changing the code length will change the code values (obviously), but changing the number of codes generated will not. For instance, if 10K codes are generated (and probably already distributed), and then you need another 10K codes, increasing the number of codes to 20K will produce the same codes for the first 10K codes (you don't lose them). See below for more details on how the codes are generated.
  • Users auto-generate a set of unique codes for their promotion, download them, and include them in their promotional materials.

How code generation works:

  • When any code is created, a SHA1 hash of the date and the promotion id are stored with the promotion. This hash is used to 'seed' all codes auto-generated for this promotion.
  • Editing the promotion doesn't change the seed. This allows for consistent regeneration of the same codes for a given particular promotion.
  • Codes are generated on the fly when downloaded from the Promotional Codes listing page, and also when the code is supplied on user sign up. No codes are stored in the system. They are generated algorithmically.
  • All codes within a set are definitely unique. They contain a unique id concatenated to part of an MD5 hash of that id, the promotion code and the SHA1 seed.
  • All codes generated across all your promotions are not guaranteed to be unique, because mathematically, it could happen. But it is VERY, VERY unlikely you will get duplicate codes between promotions (the same code appearing in two or more promotions). Even large sets (100K codes) of short codes (10 chars) should be ok. Across multiple promotions.
  • Codes are unique, but they are not random. Since the code is the product of a seeded MD5 hash, a serious hacker with resources could decipher the seed for your codes if they had a large enough set of them. For this reason, keep the list of generated codes safe.

#2

tcowley - August 15, 2007 - 19:17

Patch file one of two:

AttachmentSize
promotion.install.patch 1.54 KB

#3

tcowley - August 15, 2007 - 19:17

patch file two of two.

AttachmentSize
promotion.module.patch 9.07 KB

#4

tcowley - August 17, 2007 - 21:25
Assigned to:Anonymous» tcowley

#5

zostay - August 20, 2007 - 23:54

Thanks for the patch. I've not had time to mess with the Promotion module for a bit, but I'll see about patching it in as soon as I have a chance to look it over.

#6

zostay - February 19, 2008 - 23:23
Status:needs review» needs work

I've finally had time to take a look at the patch. The algorithm provided does provide a nice way to generate a series of codes and provides a pretty simple way to verify the correctness of a code without having to store all the codes in the database. The code generator is sufficiently lossy to prevent someone from guessing too, which is a bonus above the other code formats currently available.

However, I have a few concerns I'd like to discuss before incorporating the patch or a revision of it.

First, the format of the codes is limited to a jumble of hexadecimal numbered codes that contains a U in them somewhere at the mid-point. It would be nice if the codes could be prefixed and/or suffixed for marketing purposes. My marketers get nervous when there isn't something like a recognizable product or service abbreviation in the codes they hand out. It might also be nice to make the codes use the full alphanumeric range rather than just [0-9A-F].

Second, the range of possible codes is somewhat limiting. I don't imagine that our company or many others will ever need to generate more than 100,000 codes, but I can imagine wanting codes that are shorter than 10 characters that are still sufficiently random. This would be more possible if the strings used all letters rather than just the hexidecimal characters.

Third, it currently allows the number of characters specified to be up to be between 10 and 30. But unless I'm mistaken, the actual possible range is 10 to 21 since the sequence prefix is 5 characters and the length of an MD5 sum is 16, which gives you maximum length of 21.

Anyway, I'd be interested to hear your thoughts or the thoughts of others.

#7

tcowley - February 20, 2008 - 03:07

Thanks for reviewing the code and feature changes. Your concerns/recommendations sound good to me.

The original feature was designed to meet a specific client requirement, and it could certainly use the upgrade to more flexibility.

I'll roll these upgrades into the code in the next week or two, along with some revised documentation!

 
 

Drupal is a registered trademark of Dries Buytaert.