Problem with Prestashop Gift Certificates - used twice?


  • Default avatar
    lucy85    
     3 years ago
    0

    Hi,

    For the most part Awocoupon is working fine for me. But during testing I have discovered a bug. I have set up a Gift Certificate for £10, which can be purchased as expected.

    Basically, the problem is that if 2 different customers are completing different transactions at the same time, and they both enter the same gift certificate discount code and then both checkout at the same time, the discount is applied to both orders. Clearly this is not correct.

    Example:

    1. Customer A successfully purchases a £10 Gift Certificate (lets call it code: ABC123).

    2. Customer A add products to their cart totaling £17.98.

    3. At the same time Customer B also adds products to their cart totaling £17.98.

    4. Customer A and Customer B both apply the £10 Gift Certificate code ABC123 to their respective carts.

    5. Both carts now have a £10 reduction applied, leaving a balance of £7.98.

    6. Both Customer A and Customer B proceed to checkout at the same time and pay the balance of £7.98 via Paypal.

    7. Both transactions complete successfully, and both get order confirmations.

    8. Both orders appear in the Orders section of the Back Office of Prestashop, timestamped 34 seconds apart (both orders have the discount applied).

    9. History of Uses for the code ABC123 only shows the first use of the coupon - there is no record of it being used a second time within the AWOcoupon section of the backoffice.

    10. Meaning that the purchase of a single £10 Gift Certificate has resulted in a discount of £20 being given across 2 separate transactions.

    Clearly, this is not acceptable. Can anybody advise of the solution?

    Many thanks in advance
    Lucy

  • Your avatar
    seyi    
     3 years ago
    0

    Hello,

    Just tested this with prestashop cart rules also, and it behaves the same.

    What version of Prestashop are you using?
  • Default avatar
    lucy85    
     3 years ago
    0

    Hi Seyi,

    I'm using Prestashop 1.5.4.1

    Thanks
    Lucy
  • Your avatar
    seyi    
     3 years ago
    0

    Then in www/override/classes/order/Order.phpOk, then here is a solution.

    In www/modules/awocoupon/lib/couponhandler.php around line 1950 is this:
    <?php
            $coupon_ids 
    implode(',',array_keys($children_coupons));
            
    $sql 'SELECT id,num_of_uses_total,num_of_uses_percustomer,function_type,coupon_value FROM '._DB_PREFIX_.'awocoupon WHERE published=1 AND id IN ('.$coupon_ids.')';
            
    $rows awoHelper::loadObjectList($sql);
    ?>


    Add this after it
    <?php
            
    if(empty($rows)) return null;
    ?>


    Then in www/override/classes/order/Order.php, change
    <?php
    AwoCouponCouponHandler
    ::remove_coupon_code($this->id_cart,$this->id);

                
    $order_cart_rule = new OrderCartRule();
    $order_cart_rule->id_order $this->id;
    $order_cart_rule->id_cart_rule 0;
    $order_cart_rule->id_order_invoice $id_order_invoice;
    $order_cart_rule->name $name;
    $order_cart_rule->value $coupon_data['product_discount']+$coupon_data['shipping_discount'];
    $order_cart_rule->value_tax_excl $coupon_data['product_discount_notax']+$coupon_data['shipping_discount_notax'];
    $order_cart_rule->add();
    ?>


    to this
    <?php
    $rtn 
    AwoCouponCouponHandler::remove_coupon_code($this->id_cart,$this->id);

    if(!
    is_null($rtn)) {
        
    $order_cart_rule = new OrderCartRule();
        
    $order_cart_rule->id_order $this->id;
        
    $order_cart_rule->id_cart_rule 0;
        
    $order_cart_rule->id_order_invoice $id_order_invoice;
        
    $order_cart_rule->name $name;
        
    $order_cart_rule->value $coupon_data['product_discount']+$coupon_data['shipping_discount'];
        
    $order_cart_rule->value_tax_excl $coupon_data['product_discount_notax']+$coupon_data['shipping_discount_notax'];
        
    $order_cart_rule->add();
    }
    else {
        
    // remove from cart
        
    Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'awocoupon_cart` WHERE `id_cart` = '.(int)($this->id_cart).' LIMIT 1');

        
    // recalculate order
        
    $value round($coupon_data['product_discount']+$coupon_data['shipping_discount'],2);
        
    $value_tax_excl round($coupon_data['product_discount_notax']+$coupon_data['shipping_discount_notax'],2);
                    
        
    $order_invoice = new OrderInvoice($id_order_invoice);
        if (
    Validate::isLoadedObject($order_invoice)) {

            
    // Update amounts of Order Invoice
            
    $order_invoice->total_discount_tax_excl -= $value_tax_excl;
            
    $order_invoice->total_discount_tax_incl -= $value;
            
    $order_invoice->total_paid_tax_excl += $value_tax_excl;
            
    $order_invoice->total_paid_tax_incl += $value;

            
    // Update Order Invoice
            
    $order_invoice->update();
        }

        
    // Update amounts of order
        
    $this->total_discounts -= $value;
        
    $this->total_discounts_tax_incl -= $value;
        
    $this->total_discounts_tax_excl -= $value_tax_excl;
        
    $this->total_paid += $value;
        
    $this->total_paid_tax_incl += $value;
        
    $this->total_paid_tax_excl += $value_tax_excl;
                    

        
    // Delete Order Cart Rule and update Order
        
    $this->update();
    }
    ?>


    That should fix it.
  • Default avatar
    lucy85    
     3 years ago
    0

    Hi Seyi,

    Thank you for your help with this. I've made the changes you suggested above. Unfortunately it doesn't work.

    Now, when 2 customers try to check out at the same time, both transactions go through and ultimately both customers are only charged the discounted amount. However, the backoffice (and the customers account within prestashop) shows that the second customer has been charged the full amount (without the discount). But this is not in fact what has happened on the Paypal side. So the correct figure is not being passed to Paypal.

    What should I try next?

    Many thanks
    Lucy
  • Your avatar
    seyi    
     3 years ago
    0

    Hello,

    There is no more that can be done within AwoCoupon, the rest is within Prestashop.

    After you are forwarded to paypal with the incorrect total, if you make payment, does the order get marked as payment accepted, even though the full amount was not paid? If not, then it is really not a problem as it can just be refunded or the customer can pay the difference.