protected function addToCart($data)
 {
     if (empty($data['product_ids'])) {
         return false;
     }
     $store = $this->owner;
     $options = $store->getOptionsByName();
     if (!empty($options['add_to_cart_counter_statistics'])) {
         $option = $options['add_to_cart_counter_statistics'];
     } else {
         $option = new Option();
         $option->name = 'add_to_cart_counter_statistics';
         $option->link('store', $store);
     }
     $stats = [];
     if ($option->value) {
         $stats = Json::decode($option->value);
     }
     foreach ($data['product_ids'] as $product_id) {
         if (empty($stats[$product_id])) {
             $stats[$product_id] = 0;
         }
         $stats[$product_id]++;
     }
     $option->value = Json::encode($stats);
     $option->save();
 }
 public function saveOptions()
 {
     if ($this->store->id) {
         $options = $this->store->getOptionsByName();
         foreach ($this->attributes() as $attribute) {
             if (!empty($options[$attribute])) {
                 $option = $options[$attribute];
             } else {
                 $option = new Option();
                 $option->name = $attribute;
                 $option->link('store', $this->store);
             }
             $option->value = $this->{$attribute};
             $option->save();
         }
         return true;
     }
     return false;
 }
 public function actionIndex($store_id = 0, $order_id = 0)
 {
     $data = Yii::$app->request->post();
     if ($data) {
         if (empty($data['shop_domain'])) {
             throw new BadRequestHttpException("Shop domain is missing");
         } elseif (empty($data['order_info']['order_id'])) {
             throw new BadRequestHttpException("Order ID is missing");
         } elseif (empty($data['return_url'])) {
             throw new BadRequestHttpException("Return Url is missing");
         } elseif (empty($data['cancel_url'])) {
             throw new BadRequestHttpException("Cancel Url is missing");
         }
         $store = Store::findOne(['domain' => $data['shop_domain']]);
         if (!$store) {
             throw new BadRequestHttpException("Store not found");
         }
         // Save data to option
         $option_name = 'order_' . $data['order_info']['order_id'];
         $option = Option::findOne(['store_id' => $store->id, 'name' => $option_name]);
         if (!$option) {
             $option = new Option();
             $option->link('store', $store);
             $option->name = $option_name;
         }
         $option->value = Json::encode($data);
         $option->save();
         return $this->redirect(['index', 'store_id' => $store->id, 'order_id' => $data['order_info']['order_id']]);
     } elseif ($store_id && $order_id) {
         $store = Store::findOne($store_id);
         $option = $this->getOption($store_id, $order_id);
         $data = Json::decode($option->value);
         return $this->render('index', ['store' => $store, 'data' => $data, 'order_id' => $order_id]);
     } else {
         throw new BadRequestHttpException();
     }
 }
 public function updateHooks($event)
 {
     $store = $this->owner;
     $client = $this->getClient();
     $current_scheme = Yii::$app->request->isSecureConnection ? 'https' : 'http';
     $options = $store->getOptionsByName();
     $scripts = [];
     // Snowfall
     if (isset($options['snowfall_enable'])) {
         if (!empty($options['snowfall_script_tag_hash'])) {
             $option_hash = $options['snowfall_script_tag_hash'];
             if ($option_hash->value) {
                 // remove previos if exists
                 $client->deleteRequest('script_tags/' . $option_hash->value);
             }
         } else {
             $option_hash = new Option();
             $option_hash->name = 'snowfall_script_tag_hash';
             $option_hash->link('store', $store);
         }
         // Enable
         if ($options['snowfall_enable']->value) {
             // Script tag
             $res = $client->createRequest('script_tags', ['src' => Url::to('@web/js/lib/snowfall/snowfall.jquery.js', $current_scheme)]);
             if (!empty($res['src_hash'])) {
                 $option_hash->value = $res['src_hash'];
                 $option_hash->save();
             }
             // Template hook
             $scripts[] = '
                 var snowfallLoad = function(){
                     if ($.snowfall) {
                         $(document).snowfall({flakeCount: 100, maxSpeed: 5, maxSize: 4, shadow: true});
                     } else {
                         setTimeout(function(){
                             snowfallLoad();
                         }, 300);
                     }
                 };
                 snowfallLoad();
             ';
             // Disable
         } else {
             $option_hash->delete();
         }
     }
     // Welcome popup
     if (isset($options['welcome_popup_enable'])) {
         if ($options['welcome_popup_enable']->value) {
             $welcome_popup_data = ['title' => Html::encode($options['welcome_popup_title']->value), 'content' => nl2br(Html::encode($options['welcome_popup_content']->value))];
             $welcome_popup_data_json = Json::encode($welcome_popup_data);
             $scripts[] = "\n                    \$(function(){\n                        var welcome_popup_data = {$welcome_popup_data_json};\n                        if (!\$.cookie.get('welcome_popup_displayed')) {\n                            \$.cookie.set('welcome_popup_displayed', true);\n                            \$.ceNotification('show', {\n                                type: 'I',\n                                title: welcome_popup_data.title,\n                                message: '<div style=\"margin: 0 20px 0 20px;\">' + welcome_popup_data.content + '</div>',\n                                message_state: 'S'\n                            }, 'popupmodal');\n                        }\n                    });\n                ";
         }
     }
     // "Add to cart" counter
     if (isset($options['add_to_cart_counter_enable'])) {
         if (!empty($options['add_to_cart_counter_webhook_id'])) {
             $option_webhook = $options['add_to_cart_counter_webhook_id'];
             if ($option_webhook->value) {
                 // remove previos if exists
                 $client->deleteRequest('webhooks/' . $option_webhook->value);
             }
         } else {
             $option_webhook = new Option();
             $option_webhook->name = 'add_to_cart_counter_webhook_id';
             $option_webhook->link('store', $store);
         }
         if ($options['add_to_cart_counter_enable']->value) {
             $res = $client->createRequest('webhooks', ['event' => 'add_to_cart', 'url' => Url::to(['/webhook', 'store_id' => $store->id], $current_scheme)]);
             if (!empty($res['webhook_id'])) {
                 $option_webhook->value = $res['webhook_id'];
                 $option_webhook->save();
             }
         } else {
             $option_webhook->delete();
         }
     }
     // Payment
     if (isset($options['payment_enable'])) {
         if ($options['payment_enable']->value) {
             $data = ['processor' => Yii::$app->params['applicationName'], 'redirect_url' => Url::to(['/payment'], $current_scheme)];
             if (!empty($options['payment_processor_id'])) {
                 // Update
                 $client->updateRequest('payment_processors/' . $options['payment_processor_id']->value, $data);
             } else {
                 // Create
                 $res = $client->createRequest('payment_processors', $data);
                 if (!empty($res['processor_id'])) {
                     $option_processor = new Option();
                     $option_processor->name = 'payment_processor_id';
                     $option_processor->value = $res['processor_id'];
                     $option_processor->link('store', $store);
                     $option_processor->save();
                 }
             }
         } else {
             if (!empty($options['payment_processor_id'])) {
                 $client->deleteRequest('payment_processors/' . $options['payment_processor_id']->value);
                 $options['payment_processor_id']->delete();
             }
         }
     }
     if ($scripts) {
         $body = '{literal}' . '<script type="text/javascript">' . '(function(_, $) {' . implode(PHP_EOL, $scripts) . '}(Tygh, Tygh.$));' . '</script>' . '{/literal}';
         if (!empty($options['scripts_hook_id'])) {
             // Update
             $client->updateRequest('template_hooks/' . $options['scripts_hook_id']->value, ['body' => $body]);
         } else {
             // Create
             $res = $client->createRequest('template_hooks', ['hookname' => 'index:scripts', 'type' => 'post', 'body' => $body]);
             if (!empty($res['hook_id'])) {
                 $option_hook = new Option();
                 $option_hook->name = 'scripts_hook_id';
                 $option_hook->value = $res['hook_id'];
                 $option_hook->link('store', $store);
                 $option_hook->save();
             }
         }
     } else {
         if (!empty($options['scripts_hook_id'])) {
             $client->deleteRequest('template_hooks/' . $options['scripts_hook_id']->value);
             $options['scripts_hook_id']->delete();
         }
     }
 }