/var/www/vhosts/ihelp.ro/httpdocs/src/Controller/Api
NameSizeModeActions
AjaxController.php200900644editdlrm
Edit: /var/www/vhosts/ihelp.ro/httpdocs/src/Controller/Api/AjaxController.php (20090B)
Authentication->addUnauthenticatedActions(['confirmPay', 'addRemoveProductFavorite', 'returnCitiesByCounty', 'addNewAddress', 'listNewAddress']); } public function confirmPay() { $this->autoRender = false; if ($this->request->is('post')) { $errorCode = 0; $errorType = RequestAbstract::CONFIRM_ERROR_TYPE_NONE; $errorMessage = ''; if (strcasecmp($_SERVER['REQUEST_METHOD'], 'post') == 0) { if (isset($_POST['env_key']) && isset($_POST['data'])) { if (PAYMENT_IS_LIVE) { $privateKeyFilePath = ROOT . DS . 'config' . DS . 'mobilpay' . DS . 'live' . DS . 'live.' . MOBILPAY_KEY . 'private.key'; } else { $privateKeyFilePath = ROOT . DS . 'config' . DS . 'mobilpay' . DS . 'sandbox' . DS . 'sandbox.' . MOBILPAY_KEY . 'private.key'; } try { $objPmReq = RequestAbstract::factoryFromEncrypted($_POST['env_key'], $_POST['data'], $privateKeyFilePath); $errorCode = $objPmReq->objPmNotify->errorCode; //$this->log("Data (0): " . json_encode($objPmReq), 'debug'); $orderUuid = $objPmReq->orderId; $this->loadModel('Orders'); $order = $this->Orders->find() ->contain('Users') ->where(['Orders.uuid' => $orderUuid, 'Orders.is_active' => TRUE, 'Orders.order_status_id IN ' => [STATUS_AWP, STATUS_PPAY, STATUS_CAN, STATUS_RJCT]]) ->first(); if (!empty($order)) { // TODO // Model de facut si inserat logs // $this->loadModel('PaymentLogs'); // $this->PaymentLogs->setLog($order->id,$objPmReq,$_POST['env_key'],$_POST['data'] ) if ($errorCode == "0") { switch ($objPmReq->objPmNotify->action) { case 'confirmed': if (in_array($order->order_status_id, array(STATUS_PAID, STATUS_SHIP, STATUS_AWB, STATUS_DONE, STATUS_PRFN, STATUS_RFND, STATUS_ARH))) { //Already confirmed } else { if (in_array($order->order_status_id, array(STATUS_AWP, STATUS_PPAY, STATUS_CAN, STATUS_RJCT))) { $this->Orders->setStatus($order->id, STATUS_PAID, $objPmReq->objPmNotify->errorMessage); $customer = $objPmReq->objPmNotify->customer; // TODO // trimis email confirmare plata catre cumparator // trimis email pregatire colet confirmare ridicare catre vanzator // adugare in tabela donatii } } $errorMessage = $objPmReq->objPmNotify->errorMessage; break; case 'confirmed_pending': case 'paid_pending': case 'paid': if (in_array($order->order_status_id, array(STATUS_PAID, STATUS_SHIP, STATUS_AWB, STATUS_DONE, STATUS_PRFN, STATUS_RFND, STATUS_ARH))) { //Already confirmed } else { $this->Orders->setStatus($order->id, STATUS_PPAY, $objPmReq->objPmNotify->errorMessage); $errorMessage = $objPmReq->objPmNotify->errorMessage; } break; case 'canceled': if (in_array($order->order_status_id, array(STATUS_PAID, STATUS_SHIP, STATUS_AWB, STATUS_DONE, STATUS_PRFN, STATUS_RFND, STATUS_ARH))) { //Already confirmed } else { $this->Orders->setStatus($order->id, STATUS_CPAY, $objPmReq->objPmNotify->errorMessage); $errorMessage = $objPmReq->objPmNotify->errorMessage; } break; case 'credit': if (in_array($order->order_status_id, array(STATUS_PRFN))) { $this->Orders->setStatus($order->id, STATUS_RFND, $objPmReq->objPmNotify->errorMessage); $errorMessage = $objPmReq->objPmNotify->errorMessage; } break; default: $this->Orders->setStatus($order->id, STATUS_RJCT, "Plata a fost respinsa. Err: " . RequestAbstract::ERROR_CONFIRM_INVALID_ACTION); //$this->Orders->setStatus($order->id, STATUS_WAIT, 'Plată'); $errorType = RequestAbstract::CONFIRM_ERROR_TYPE_PERMANENT; $errorCode = RequestAbstract::ERROR_CONFIRM_INVALID_ACTION; $errorMessage = 'mobilpay_refference_action paramaters is invalid'; break; } } else { $this->Orders->setStatus($order->id, STATUS_RJCT, (!empty($objPmReq->objPmNotify->errorMessage) ? $objPmReq->objPmNotify->errorMessage : 'Respinsa')); $errorMessage = $objPmReq->objPmNotify->errorMessage; } } } catch (Exception $e) { $errorType = RequestAbstract::CONFIRM_ERROR_TYPE_TEMPORARY; $errorCode = $e->getCode(); $errorMessage = $e->getMessage(); } } else { $errorType = RequestAbstract::CONFIRM_ERROR_TYPE_PERMANENT; $errorCode = RequestAbstract::ERROR_CONFIRM_INVALID_POST_PARAMETERS; $errorMessage = 'mobilpay.ro posted invalid parameters'; } } else { $errorType = RequestAbstract::CONFIRM_ERROR_TYPE_PERMANENT; $errorCode = RequestAbstract::ERROR_CONFIRM_INVALID_POST_METHOD; $errorMessage = 'invalid request metod for payment confirmation'; } header('Content-type: application/xml'); echo "\n"; if ($errorCode == 0) { echo "{$errorMessage}"; } else { echo "{$errorMessage}"; } } die(); } /** * Add addRemoveProductFavorite */ public function addRemoveProductFavorite($uuidProd = null) { $uuidProd = $_POST['uuid']; $this->autoRender = false; $response = []; $data = []; $response['isFav'] = ''; $response['message'] = ''; $response['count'] = ''; if (!empty($uuidProd)) { if ($this->request->is('ajax')) { $this->loadModel('ProductFavorites'); $favorite_identifier = $this->identifireIdFavoriteSession(); $idProd = $this->ProductFavorites->Products->find() ->where(['Products.uuid' => $uuidProd, 'Products.is_active' => true]) ->first() ->id; if (!empty($idProd)) { // if product $productFavorite = $this->ProductFavorites->newEmptyEntity(); $data = [ 'product_id' => $idProd, 'uuid' => $favorite_identifier, 'is_active' => true ]; if (!empty($this->userAuth->id) && $this->userAuth->id) { //is login // delete if product is in favorite $productFavoriteEntity = $this->ProductFavorites->find() ->where(['ProductFavorites.user_id' => $this->userAuth->id, 'ProductFavorites.product_id' => $idProd]) ->first(); if (!empty($productFavoriteEntity)) { $productFavorite = $this->ProductFavorites->get($productFavoriteEntity->id); if (!empty($productFavoriteEntity)) { if ($this->ProductFavorites->delete($productFavorite)) { $response['isFav'] = false; $response['message'] = __('Produsul a fost scos de la favorite.'); $response['count'] = $this->ProductFavorites->find()->where(['ProductFavorites.user_id' => $this->userAuth->id])->count(); //$this->Flash->success(__('Produsul a fost scos de la favorite.')); } else { $response['isFav'] = true; $response['message'] = __('Produsul nu a fost scos de la favorite.'); //$this->Flash->error(__('Produsul nu a fost scos de la favorite.')); } echo json_encode($response); return; //$this->redirect($this->referer()); } } // save in favorite $data['user_id'] = $this->userAuth->id; $productFavorite = $this->ProductFavorites->patchEntity($productFavorite, $data); if ($this->ProductFavorites->save($productFavorite)) { $response['isFav'] = true; $response['message'] = __('Produsul preferat a fost adaugat la favorite.'); $response['count'] = $this->ProductFavorites->find()->where(['ProductFavorites.user_id' => $this->userAuth->id])->count(); echo json_encode($response); //$this->Flash->success(__('Produsul preferat a fost salvat.')); return; //$this->redirect($this->referer()); } $response['isFav'] = false; $response['message'] = __('Produsul nu a putut fi adaugat la favorite.'); echo json_encode($response); //$this->Flash->error(__('Produsul nu a putut fi adaugat la favorite.')); return; //$this->redirect($this->referer()); } else { //is not login // delete if product is in favorite $productFavoriteEntity = $this->ProductFavorites->find() ->where(['ProductFavorites.user_id IS' => null, 'ProductFavorites.uuid' => $favorite_identifier, 'ProductFavorites.product_id' => $idProd]) ->first(); if (!empty($productFavoriteEntity)) { $productFavorite = $this->ProductFavorites->get($productFavoriteEntity->id); if ($this->ProductFavorites->delete($productFavorite)) { $response['isFav'] = false; $response['message'] = __('Produsul a fost scos de la favorite.'); $response['count'] = $this->ProductFavorites->find()->where(['ProductFavorites.user_id IS' => null, 'ProductFavorites.uuid' => $favorite_identifier])->count(); //$this->Flash->success(__('Produsul a fost scos de la favorite.')); } else { $response['isFav'] = true; $response['message'] = __('Produsul nu a fost scos de la favorite.'); //$this->Flash->error(__('Produsul nu a fost scos de la favorite.')); } echo json_encode($response); return; //$this->redirect($this->referer()); } // save in favorite $productFavorite = $this->ProductFavorites->patchEntity($productFavorite, $data); if ($this->ProductFavorites->save($productFavorite)) { $response['isFav'] = true; $response['message'] = __('Produsul preferat a fost adaugat la favorite.'); $response['count'] = $this->ProductFavorites->find()->where(['ProductFavorites.user_id IS' => null, 'ProductFavorites.uuid' => $favorite_identifier])->count(); echo json_encode($response); //$this->Flash->success(__('The product favorite has been saved.')); return; //$this->redirect($this->referer()); } $response['isFav'] = false; $response['message'] = __('Produsul nu a putut fi adaugat la favorite.'); echo json_encode($response); //$this->Flash->error(__('Produsul nu a putut fi adaugat la favorite.')); return; //$this->redirect($this->referer()); } } else { // the product id not exist $response['isFav'] = false; $response['message'] = __('Produsul nu exista.'); echo json_encode($response); //$this->Flash->error(__('Produsul nu exista.')); return; //$this->redirect($this->referer()); } } else { // is not post $response['isFav'] = false; $response['message'] = __('Transmitere incorecta.'); echo json_encode($response); //$this->Flash->error(__('Transmitere incorecta.')); return; //$this->redirect($this->referer()); } } else { // the uuid not exist $response['isFav'] = false; $response['message'] = __('Cod produs incorect.'); echo json_encode($response); //$this->Flash->error(__('Cod produs incorect.')); return; //$this->redirect($this->referer()); } } /** * Add address in checkout modal if is login */ public function addNewAddress() { $this->autoRender = false; $this->viewBuilder()->setLayout('ajax'); $this->loadModel('UserAddresses'); $this->loadModel('Products'); $response = []; $userAddress = null; $userId = null; $this->autoRender = false; if ($this->request->is('ajax')) { $uuidUser = $this->request->getData('user_uuid'); if ($uuidUser) { $userId = $this->fetchTable('Users')->find()->where(['uuid' => $uuidUser])->first()->id; } $userId = isset($this->userAuth->id) ? $this->userAuth->id : $userId; $userAddress = $this->UserAddresses->newEmptyEntity(); $userAddress = $this->UserAddresses->patchEntity($userAddress, $this->request->getData()); if (!empty($userId)) { $userAddress->user_id = $userId; if ($this->UserAddresses->save($userAddress)) { $this->UserAddresses->updateAll(['is_default' => false], ['user_id' => $userId]); $this->UserAddresses->id = $userAddress->id; $this->UserAddresses->saveField('is_default', true); $response['success'] = 'Adresa a fost adăugată'; $response['status'] = true; if (isset($data['product']) && $data['product']) { $session = $this->request->getSession(); if (!$session->check('Dashboard.product')) { $product = $this->Products->newEntity($data['product']); $session->write('Dashboard.product', $product); } } } } if (!empty($userAddress->getErrors())) { $response['errors'] = $userAddress->getErrors(); $response['status'] = false; } $response['response'] = $response; echo (json_encode($response)); die; } else { return $this->redirect(['prefix' => false, 'controller' => 'Pages', 'action' => 'home']); } } public function listNewAddress() { $addresses = null; if ($this->request->is('post')) { $uuidUser = $this->request->getData('uuidUser'); if ($uuidUser) { $addresses = $this->fetchTable('UserAddresses')->find() ->where(['UserAddresses.user_uuid' => $uuidUser, 'UserAddresses.is_active' => true]) ->order(['is_default' => 'DESC', 'modified' => 'DESC']) ->selectAllExcept($this->fetchTable('UserAddresses'), ['user_id', 'created', 'modified']) ->all(); } } $this->set(compact('addresses')); $this->viewBuilder()->setOption('serialize', ['addresses']); } /** * returnCitiesByCounty */ public function returnCitiesByCounty() { $this->autoRender = false; $this->viewBuilder()->setLayout('ajax'); $this->loadModel('Cities'); $response = []; $data = $_POST; if ($this->request->is('ajax') && !empty($data['id'])) { $cities = $this->Cities->find('list') ->where(['Cities.county_id' => $data['id']]) ->toArray(); if (!empty($cities)) { $response['cities'] = $cities; $response['message'] = true; } else { $response['message'] = false; } $response['response'] = $response; echo (json_encode($response)); die; } else { return $this->redirect(['prefix' => false, 'controller' => 'Pages', 'action' => 'home']); } } }