public function create($data) { $headerContent = $this->controller->getRequest()->getHeaders(); $document = $headerContent->get('Document'); $nameCity = $headerContent->get('City'); $country = $headerContent->get('Country'); $state = $headerContent->get('State'); $cep = $headerContent->get('PostalCode'); $address = $headerContent->get('Address'); $neighborhood = $headerContent->get('Neighborhood'); $name = $headerContent->get('Name'); $rg = $headerContent->get('Rg'); $email = $headerContent->get('Email'); $password = $headerContent->get('Password'); $phoneNumber = $headerContent->get('PhoneNumber'); $isNewMerchant = $headerContent->get('isNewMerchant'); $isIntegration = $headerContent->get('isIntegration'); if (!$document) { $this->controller->getResponse()->setStatusCode(401); throw new \Exception('Document Is Required'); } $merchant = null; $sysLanguage = Module::getRepository('SystemLanguage')->findOneBy(array('name' => Module::getMerchantLanguage())); if ($isNewMerchant) { $checkIsNewMerchant = $isNewMerchant->getFieldValue() == "true" ? true : intval($isNewMerchant->getFieldValue()); if (Module::getMainKey() && $checkIsNewMerchant) { if (!Module::getApiPublicKey()) { $this->controller->getResponse()->setStatusCode(403); throw new \Exception('Request Not Authenticated'); } $userExisting = Module::getRepository('User')->findOneBy(array('integrationMerchant' => Module::getRepository('IntegrationMerchant')->findOneBy(array('apiPublicKey' => Module::getApiPublicKey())))); $merchant = $userExisting->getIntegrationMerchant()->getMerchant(); if (empty($merchant->getApiPrivateKey())) { $merchant = Module::loadEntity('Merchant', $merchant->getId()); $user = Module::loadEntity('User', $userExisting->getDocument(), 'document'); if ($isIntegration) { $user->setIsIntegration($isIntegration->getFieldValue() == "true" ? true : false)->save(); } $newPublicKey = time() . $user->getDocument() . time(); $merchant->setApiPrivateKey($newPublicKey . $user->getDocument() . Module::getMainKey())->setApiPublicKey($newPublicKey)->save(); $integration_merchant = Module::loadEntity('IntegrationMerchant', $merchant->getId(), 'merchant'); $integration_merchant->setApiPublicKey($newPublicKey)->save(); if (!empty($merchant->getApiPrivateKey())) { $platform = Module::newEntity('Platform'); $platform->setUser($user)->setIsAllowedToUse(true)->save(); } } else { $this->controller->getResponse()->setStatusCode(302); throw new \Exception('User Already Authenticated'); } return new JsonModel(array('success' => true, 'merchant_id' => $merchant->getApiPublicKey())); } else { $this->controller->getResponse()->setStatusCode(401); throw new \Exception('Request Not Authorized'); } } else { if (!$nameCity || !$country || !$state || !$cep || !$address || !$neighborhood || !$name || !$rg || !$email || !$password || !$phoneNumber) { $this->controller->getResponse()->setStatusCode(401); throw new \Exception('All Fields Are Necessary'); } } $merchantAuthenticated = null; if (Module::getApiPublicKey()) { $merchantAuthenticated = Module::getRepository('Merchant')->findMerchantWithPublicKey(Module::getApiPublicKey()); if (empty($merchantAuthenticated->getApiPrivateKey())) { $this->controller->getResponse()->setStatusCode(401); throw new \Exception('Request Not Authorized'); } } $existingUser = Module::getRepository('User')->findUserByDocument($document->getFieldValue()); if (!$existingUser) { $userMerchantAuthenticated = Module::getRepository('User')->findOneBy(array('integrationMerchant' => Module::getRepository('IntegrationMerchant')->findOneBy(array('merchant' => $merchantAuthenticated)))); $existingCity = Module::getRepository('City')->findUniqueCity($nameCity->getFieldValue(), $country->getFieldValue(), $state->getFieldValue()); $city = $existingCity; if (!$existingCity) { $city = Module::newEntity('City'); $city->setName($nameCity->getFieldValue())->setCountry($country->getFieldValue())->setState($state->getFieldValue())->save(); } $addressEntity = Module::newEntity('Address'); $addressEntity->setCity($city)->setCep($cep->getFieldValue())->setSuperscription($address->getFieldValue())->setNeighborhood($neighborhood->getFieldValue())->save(); $merchant = $merchantAuthenticated; if (!$merchantAuthenticated) { $merchant = Module::newEntity('Merchant'); $merchant->setLanguage($sysLanguage)->setApiPrivateKey("")->setApiPublicKey(time() . $document->getFieldValue() . time())->save(); } $integration_merchant = Module::newEntity('IntegrationMerchant'); $integration_merchant->setMerchant($merchant)->setApiPublicKey($merchant->getApiPublicKey())->save(); $user = Module::newEntity('User'); $user->setDocument($document->getFieldValue())->setIntegrationMerchant($integration_merchant)->setAddress($addressEntity)->setName($name->getFieldValue())->setIdentificationDocument($rg->getFieldValue())->setEmail($email->getFieldValue())->setPassword($password->getFieldValue())->setPhoneNumber($phoneNumber->getFieldValue())->save(); if ($userMerchantAuthenticated) { $platform = Module::newEntity('Platform'); $platform->setUser($user)->setIsAllowedToUse(true)->save(); } } else { $this->controller->getResponse()->setStatusCode(302); throw new \Exception('User Already Exists'); } return new JsonModel(array('success' => true, 'merchant_id' => $merchant->getApiPublicKey())); }