Ejemplo n.º 1
0
 /**
  * Webhook update product action.
  *
  * @return void
  *
  * @Route("/update", methods={"POST"}, name="import-product-update")
  */
 public function updateAction()
 {
     if (isset($_SERVER['HTTP_X_HARAVAN_HMAC_SHA256'])) {
         $myApp = AppModel::findFirstById(1);
         $hmac_header = $_SERVER['HTTP_X_HARAVAN_HMAC_SHA256'];
         $data = file_get_contents('php://input');
         $verified = Utils::verify_webhook($data, $hmac_header, $myApp->sharedSecret);
         if ($verified) {
             $product = json_decode($data);
             $cleanData = strip_tags($product->body_html);
             $myStore = StoreModel::findFirst(['name = :storeName:', 'bind' => ['storeName' => $_SERVER['HTTP_HARAVAN_SHOP_DOMAIN']]]);
             // Create session information
             $this->session->get('oauth_token') != "" ? $this->session->get('oauth_token') : $this->session->set('oauth_token', $myStore->accessToken);
             $this->session->get('shop') != "" ? $this->session->get('shop') : $this->session->set('shop', $myStore->name);
             $this->session->get('sid') != "" ? $this->session->get('sid') : $this->session->set('sid', $myStore->id);
             // Get collection id from collect API
             $haravanCollection = EnHelper::getInstance('haravan', 'import')->getCollectsByProductId($product->id);
             // if category already mapped to five -> continue, else -> exit
             $myCategoryMap = CategoryMap::findFirst(['hid = :hid:', 'bind' => ['hid' => $haravanCollection[0]->collection_id]]);
             if (!$myCategoryMap) {
                 exit;
             }
             // if not found product id -> exit
             $myAds = AdsModel::findFirst(['rid = :rid:', 'bind' => ['rid' => $product->id]]);
             if (!$myAds) {
                 exit;
             }
             // Update table ADS
             $myAds->assign(['uid' => $myStore->uid, 'title' => $product->title, 'description' => $cleanData, 'price' => $product->variants[0]->price, 'seokeyword' => $product->tags, 'lastpostdate' => time()]);
             if ($myAds->update()) {
                 $this->debug($product->id);
                 // Update to product_map table
                 $myProduct = ProductMap::findFirst(['hid = :hid:', 'bind' => ['hid' => $product->id]]);
                 $myProduct->assign(['title' => $myAds->title, 'price' => $myAds->price]);
                 if ($myProduct->update()) {
                     error_log($myProduct->title . ' created success!');
                 }
                 // Delete queued data. (Production)
                 // $myProductQueue->delete();
             }
         }
     } else {
         error_log('Request not from haravan');
     }
 }