Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2010
    Location
    Finland
    Posts
    463
    Plugin Contributions
    0

    Default 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....

  2. #2
    Join Date
    Mar 2010
    Location
    Finland
    Posts
    463
    Plugin Contributions
    0

    Default 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 ;
    =============================================

 

 

Similar Threads

  1. Replies: 3
    Last Post: 28 Aug 2015, 06:50 PM
  2. Calculate Sales Tax based on pre-coupon amount?
    By hockey2112 in forum Currencies & Sales Taxes, VAT, GST, etc.
    Replies: 0
    Last Post: 6 Jul 2011, 10:57 PM
  3. Simple mod to calculate $ 'saved' with a coupon
    By ladyada in forum All Other Contributions/Addons
    Replies: 0
    Last Post: 30 Nov 2009, 05:10 AM
  4. Calculate after coupon total before checkout?
    By sassayone in forum Discounts/Coupons, Gift Certificates, Newsletters, Ads
    Replies: 7
    Last Post: 1 Nov 2007, 12:15 AM
  5. Shipping doesn't calculate correctly
    By KTNaturals in forum Built-in Shipping and Payment Modules
    Replies: 6
    Last Post: 18 Feb 2007, 04:59 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR