Exemplo n.º 1
0
 function onBeforeOrderCreate(&$order, &$do)
 {
     if (!empty($order->order_type) && $order->order_type != 'sale') {
         return true;
     }
     if (empty($order->order_payment_params)) {
         $order->order_payment_params = new stdClass();
     }
     if (empty($order->order_payment_params->userpoints)) {
         $order->order_payment_params->userpoints = new stdClass();
     }
     if (empty($order->order_payment_params->userpoints->use_points)) {
         $order->order_payment_params->userpoints->use_points = 0;
     }
     if (empty($order->order_payment_params->userpoints->earn_points)) {
         $order->order_payment_params->userpoints->earn_points = array();
     }
     $earnPoints = $this->getPointsEarned($order, 'all');
     if (!empty($earnPoints)) {
         foreach ($earnPoints as $mode => $pts) {
             if (empty($order->order_payment_params->userpoints->earn_points[$mode])) {
                 $order->order_payment_params->userpoints->earn_points[$mode] = 0;
             }
             $order->order_payment_params->userpoints->earn_points[$mode] += $pts;
         }
     }
     if ((empty($order->order_payment_method) || $order->order_payment_method != $this->name) && !empty($order->cart->additional)) {
         $ids = array();
         parent::listPlugins($this->name, $ids, false);
         foreach ($ids as $id) {
             parent::pluginParams($id);
             if (empty($this->payment_params)) {
                 continue;
             }
             if ($this->payment_params->virtual_coupon) {
                 $checkPoints = $points = $this->checkPoints($order);
                 $usePts = -1;
                 foreach ($order->cart->additional as $additional) {
                     if ($additional->name != 'USERPOINTS_USE_POINTS') {
                         continue;
                     }
                     $matches = array();
                     if (preg_match('#-([0-9]+)#', $additional->value, $matches)) {
                         $usePts = (int) $matches[1];
                     } else {
                         $usePts = substr($additional->value, 0, strpos($additional->value, ' '));
                         $usePts = (int) trim(str_replace('-', '', $usePts));
                     }
                     break;
                 }
                 if ($checkPoints > $usePts) {
                     $order->order_payment_params->userpoints->earn_points[$this->plugin_params->points_mode] += $usePts - $checkPoints;
                     $points = $usePts;
                 }
                 if ($usePts > 0) {
                     $points = $usePts;
                 }
                 if ($points !== false && $points > 0) {
                     $order->order_payment_params->userpoints->use_points += $points;
                     $order->order_payment_params->userpoints->use_mode = $this->plugin_params->points_mode;
                 }
                 break;
             }
         }
         return true;
     }
     if (parent::onBeforeOrderCreate($order, $do) === true) {
         return true;
     }
     if (!empty($order->cart->coupon->discount_code) && (preg_match('#^POINTS_[a-zA-Z0-9]{30}$#', $order->cart->coupon->discount_code) || preg_match('#^POINTS_([-a-zA-Z0-9]+)_[a-zA-Z0-9]{25}$#', $order->cart->coupon->discount_code))) {
         if (@$this->payment_params->partialpayment === 0 && $order->cart->full_total->prices[0]->price_value_without_discount != $order->cart->coupon->discount_value) {
             $do = false;
             echo JText::_('ERROR_POINTS');
             return true;
         }
     }
     $check = $this->checkPoints($order);
     $userPoints = $this->getUserPoints(null, $this->payment_params->points_mode);
     $fullOrderPoints = $this->finalPriceToPoints($order, $userPoints);
     if (($this->payment_params->partialpayment == 1 || $this->payment_params->allowshipping == 0) && ($check !== false && $check > 0) && $check < $fullOrderPoints && $userPoints) {
         $discountClass = hikashop_get('class.discount');
         $cartClass = hikashop_get('class.cart');
         $config =& hikashop_config();
         $currency = hikashop_getCurrency();
         $app = JFactory::getApplication();
         $newCoupon = new stdClass();
         $newCoupon->discount_type = 'coupon';
         $newCoupon->discount_currency_id = $currency;
         $newCoupon->discount_flat_amount = $check * $this->payment_params->value;
         $newCoupon->discount_quota = 1;
         jimport('joomla.user.helper');
         if (!empty($this->payment_params->givebackpoints)) {
             $newCoupon->discount_code = 'POINTS_' . $this->payment_params->points_mode . '_';
             $newCoupon->discount_code .= JUserHelper::genRandomPassword(25);
         } else {
             $newCoupon->discount_code = 'POINTS_';
             $newCoupon->discount_code .= JUserHelper::genRandomPassword(30);
         }
         $newCoupon->discount_published = 1;
         $discountClass->save($newCoupon);
         $coupon = $newCoupon;
         if (!empty($coupon)) {
             $cartClass->update($coupon->discount_code, 1, 0, 'coupon');
             $cartClass->loadCart(0, true);
         }
         $app->setUserState(HIKASHOP_COMPONENT . '.payment_method', null);
         $app->setUserState(HIKASHOP_COMPONENT . '.payment_id', null);
         $app->setUserState(HIKASHOP_COMPONENT . '.payment_data', null);
         $do = false;
         if (empty($order->customer)) {
             $userClass = hikashop_get('class.user');
             $order->customer = $userClass->get($order->order_user_id);
         }
         $this->addPoints(-$check, $order, JText::_('HIKASHOP_COUPON') . ' ' . $coupon->discount_code);
     }
 }