Zen Cart Logo
Forums / Discounts/Coupons, Gift Certificates, Newsletters, Ads / Coupon code doesn't calculate checksum

Coupon code doesn't calculate checksum

Locked

Views: 1,348

Results 1 to 2 of 2
This thread is locked. New replies are disabled.
17 Apr 2010, 8:06 AM
#1
kalastaja avatar

kalastaja

Zen Follower

Join Date:
Mar 2010
Location:
Finland
Posts:
465
Plugin Contributions:
0

Coupon code doesn't calculate checksum

////
// Create a Coupon Code

function create_coupon_code($salt="secret", $length=SECURITY_CODE_LENGTH) {

global $db;
$good_result = 0;
$id1 = '';
$check = '731';
while ($good_result == 0) {
   srand((double)microtime()*1000000); // seed the random number generator
   // Generate the code (leave room for checksum!)
   for($i = 0; $i < $length - 1; $i++) {
      $id1 .= rand(0, 9);
   }
   // Add checksum =========================================
   $checksum = 0;
   for($i = 0; $i < $length - 1; $i++) {
      $checksum = ($id1[$length - 1 - i] + 0) * ($check[$i%3] + 0);
   }
   $id1 .= strval(10 - ($checksum % 10));
   // ==========================================
   // Check that it's unique
   $query = "select coupon_code from " . TABLE_COUPONS . 
           " where coupon_code = '" . $id1 . "'";
   $rs = $db->Execute($query);
   if ($rs->RecordCount() == 0) {
      $good_result = 1;
   } else { 
      $id1 = '';
   }
}
return $id1;

}

BTW. The while-loop was not correct in the original code....

17 Apr 2010, 8:24 AM
#2
kalastaja avatar

kalastaja

Zen Follower

Join Date:
Mar 2010
Location:
Finland
Posts:
465
Plugin Contributions:
0

Re: Coupon code doesn't calculate checksum

Solved:

=============================================
$multiplier = 7;
$checksum = 0;
for ($i = strlen($id1); $i > 0; $i--) {
$checksum += substr($id1, $iLaskuri - 1, 1) * $multiplier ;
switch ($multiplier ) {
case 7: $multiplier = 3; break;
case 3: $multiplier = 1; break;
case 1: $multiplier = 7; break;
}
}
$checksum = substr(10 - ($checksum % 10), -1);
$id1 .= $checksum ;