Reputation: 2257
I'm making my own ecommerce site with php. I want to offer my past customers coupon codes without sending a generic "10%OFF" to everyone that may spread around like wildfire. I would like every code to be unique but map to a known discount value.
Is there a plugin/framework/easy function i can use that can evaluate coupon codes (strings) on the fly without seeding my database with all the possible combinations?
Upvotes: 2
Views: 2620
Reputation: 20422
Try out Voucherify - it's an API for coupon codes. Using their web app you will be able to create a vouchers campaign, i.e. generate thousands of unique codes that map to the same discount:
Then you can integrate it into your ecommerce site with PHP SDK. To redeem a voucher:
$result = $voucherify->redeem("unique-voucher-code", "customer-id");
The result will include the specified discount in $result->voucher->discount->percent_off
if given code was valid.
Full disclosure, I'm a developer of Voucherify.
Upvotes: 3
Reputation: 12189
For the company I worked for, we created a "coupon code" generator, that allowed us to generate millions of self-validating codes, without the expense of loading them in a database.
The codes were calculated as follows:
Create and store a salt of 16 characters, or more.
Loop a counter from 0 to the number of codes - 1. For each counter:
a. generate a hash of the counter and the salt, say md5(counter + salt)
b. generate the coupon code as the counter plus the hash value
Then when a coupon code is received, we extract the counter, and hash the counter + salt, and compare it to the hash found in the code. If they are the same, we know the code is valid.
If a sale is generated using the code, we then record its use in the table, so it can't be used again.
For print media, we generated coupon codes using uppercase base-32 values (2-9,A-H,J-N,P-Z). For emails and URLs, we used modified base-64 values (see urlsafe_b64encode())
Unfortunately, I can't provide the actual code, as it's under NDA. Good luck!
Upvotes: 0
Reputation: 5374
You could structure the coupon code in a way that would allow you to determine the type of coupon without checking a DB (or you could store these in a table full of just coupon types (see below) that way you don't need to store every possible combination), then have a unique number appended to the end that could be used to check if the coupon has been used before. For example:
d8h7bb0778719945
-> d8h7bb
is the coupon type (same for all other coupons of this type) and 0778719945
is the unique number (different for all coupons, regardless of type).
Check if the coupon type is valid for the item being purchased first. If it is, check 0778719945
against your coupon database to see if it has been claimed or not.
Upvotes: 0
Reputation: 6758
Since you are writing it yourself, here's what I would suggest.
Create a new coupon type, that when applying the coupon, it checks the signed in users order history to make sure they have placed an order before.
In your coupon table, add the following fields requireSignIn
and requirePastOrders
. Create one coupon with those fields set. When the customer tries to apply that coupon, do the necessary checks to make sure they are signed in and have placed an order before. Show appropriate error messages if those tests fail.
Upvotes: 0