protected function validate(YousticeShopRegistration $registration)
 {
     if (Tools::strlen(trim($registration->getCompanyName())) == 0) {
         return 'company_name_required';
     }
     if (Tools::strlen(trim($registration->getFirstName())) == 0) {
         return 'first_name_required';
     }
     if (Tools::strlen(trim($registration->getLastName())) == 0) {
         return 'last_name_required';
     }
     if (!filter_var($registration->getEmail(), FILTER_VALIDATE_EMAIL)) {
         return 'email_invalid';
     }
     if (!filter_var($registration->getShopUrl(), FILTER_VALIDATE_URL)) {
         return 'shop_url_invalid';
     }
     if (Tools::strlen($registration->getPassword()) < 6) {
         return 'password_less_than_6_characters';
     }
     if ($registration->getPassword() !== $registration->getVerifyPasswordValue()) {
         return 'passwords_do_not_match';
     }
     return true;
 }
 public function register(YousticeShopRegistration $registration)
 {
     $referenceString = uniqid();
     $request_data = array('orgName' => $registration->getCompanyName(), 'orgUrl' => $registration->getShopUrl(), 'orgLogo' => $registration->getShopLogo(), 'admin' => array('email' => $registration->getEmail(), 'firstName' => $registration->getFirstName(), 'lastName' => $registration->getLastName(), 'password' => $registration->getPassword(), 'primaryLang' => $registration->getAdminLang()), 'country' => $registration->getDefaultCountry(), 'subscription' => $registration->getSubscription(), 'acceptsTC' => true, 'acceptsPP' => true, 'couponCode' => $registration->getCouponCode(), 'shops' => array(array('ref' => $referenceString, 'name' => $registration->getShopName(), 'url' => $registration->getShopUrl(), 'sector' => 'RETAIL', 'currency' => $registration->getShopCurrency(), 'primaryLang' => $registration->getShopLang(), 'logo' => $registration->getShopLogo(), 'integrationInfo' => array('platform' => $this->shop_software_type, 'version' => $this->shop_software_version, 'pluginVersion' => $this->plugin_software_version))));
     $url = $this->api_production_url . 'organizations/retailers/registration';
     try {
         $this->post($url, $request_data);
     } catch (YousticeFailedRemoteConnectionException $e) {
         //shop was already registered
         if ($e->getCode() == 409) {
             throw new YousticeShopRegistrationShopAlreadyRegistered();
         }
         throw $e;
     }
     $response = $this->responseToArray();
     $shop_data = $response['shops'][0];
     if ($shop_data['ref'] != $referenceString) {
         throw new YousticeFailedRemoteConnectionException('Post Request failed: ' . $url, -1);
     }
     return $shop_data['apiKey'];
 }
 protected function checkForSentRegistration()
 {
     if (Tools::isSubmit('registration' . $this->name)) {
         $terms_and_conditions = Tools::getIsset('terms_and_conditions');
         if (!$terms_and_conditions) {
             exit(Tools::jsonEncode(array('result' => 'terms_not_accepted')));
         }
         $registration = new YousticeShopRegistration();
         $shop_currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT'));
         $shop_lang = new Language(Configuration::get('PS_LANG_DEFAULT'));
         $registration->setCompanyName(Tools::getValue('company_name', ''))->setEmail(Tools::getValue('email', ''))->setPassword(Tools::getValue('password', ''))->setFirstName(Tools::getValue('first_name', ''))->setLastName(Tools::getValue('last_name', ''))->setVerifyPasswordValue(Tools::getValue('verify_password', ''))->setShopUrl(Tools::getValue('shop_url', ''))->setCompanyIsInFrance(Tools::getIsset('company_is_in_france'))->setIsFrenchForm(Tools::getIsset('is_french_form'))->setAdminLang($this->context->language->iso_code)->setDefaultCountry($this->context->country->iso_code)->setShopName(Configuration::get('PS_SHOP_NAME'))->setShopCurrency($shop_currency->iso_code)->setShopLang($shop_lang->iso_code)->setShopLogoPath(_PS_IMG_DIR_ . Configuration::get('PS_LOGO'));
         try {
             $api_key = $this->y_api->runWithoutUpdates()->register($registration);
         } catch (YousticeShopRegistrationValidationException $e) {
             $response = Tools::jsonEncode(array('result' => $e->getMessage()));
             exit($response);
         } catch (YousticeShopRegistrationShopAlreadyRegistered $e) {
             $this->saveErrorMessage($this->l('Registration failed: Email is already in use.'));
             $response = Tools::jsonEncode(array('result' => true));
             exit($response);
         } catch (YousticeFailedRemoteConnectionException $e) {
             $response = Tools::jsonEncode(array('result' => 'request_failed'));
             exit($response);
         }
         Configuration::updateValue('YRS_API_KEY', $api_key);
         Configuration::updateValue('YRS_SANDBOX', false);
         $this->saveConfirmMessage($this->l('Settings saved. Integration with Youstice successfully configured.'));
         $response = Tools::jsonEncode(array('result' => true));
         exit($response);
     }
 }