Esempio n. 1
0
 function providerSignupAction()
 {
     if ($this->request->isPost()) {
         $name = $this->request->getPost('name');
         $email = $this->request->getPost('email');
         $password = $this->request->getPost('password');
         $cpassword = $this->request->getPost('cpassword');
         $timezone = $this->request->getPost('timezone');
         $phone = $this->request->getPost('phone');
         $address = $this->request->getPost('address');
         $categories = $this->request->getPost('categories');
         $membership = $this->request->getPost('membership');
         $card_token = $this->request->getPost('card_token');
         $errors = array();
         $fields = array('name', 'email', 'password', 'cpassword', 'phone', 'address', 'membership', 'card_token');
         $fieldsEntered = 0;
         foreach ($fields as $field) {
             if (trim(${$field}) != '') {
                 $fieldsEntered++;
             }
         }
         if ($fieldsEntered < count($fields)) {
             array_push($errors, "Some fields were not entered.");
         }
         if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
             array_push($errors, "Email is invalid.");
         }
         if (strlen($password) < 6) {
             array_push($errors, "Password must be at least 6 characters long.");
         }
         if ($password != $cpassword) {
             array_push($errors, "Passwords don't match.");
         }
         if (!preg_match('/^\\(?[0-9]{3}\\)?|[0-9]{3}[-. ]? [0-9]{3}[-. ]?[0-9]{4}$/', $phone)) {
             array_push($errors, "Invalid phone number format.");
         }
         $apiLink = $this->config->maps->api_link;
         $geoData = file_get_contents($apiLink . urlencode($address));
         if ($geoData === FALSE) {
             array_push($errors, "Invalid address.");
         }
         $provider = Providers::findFirst("email = '{$email}'");
         if ($provider) {
             array_push($errors, "Email is already taken.");
         }
         if (!count($errors)) {
             $salt = bin2hex(openssl_random_pseudo_bytes(16, $cstrong));
             $provider = new Providers();
             $provider->name = $name;
             $provider->email = $email;
             $provider->salt = $salt;
             $provider->password = md5($salt . $password);
             $provider->timezone = $timezone;
             $provider->membership = $membership;
             require_once "../vendor/stripe-php-master/init.php";
             \Stripe\Stripe::setApiKey($this->config->stripe->secret_key);
             $plan = Memberships::findFirst("id = '{$membership}'");
             $amount = (int) $plan->total * 100;
             $duration = $plan->duration;
             $date = new DateTime(date());
             $date->add(new DateInterval('P' . $duration . 'M'));
             $provider->expiry_date = $date->format('Y-m-d H:i:s');
             $account = \Stripe\Account::create(array("managed" => true, "country" => "US"));
             $provider->stripe_account_token = $account->id;
             $customer = \Stripe\Customer::create(array("description" => $name, "email" => $email, "source" => $card_token));
             $provider->stripe_customer_token = $customer->id;
             $provider->stripe_card_token = $customer->default_source;
             $providerPhone = new ProviderPhones();
             $providerPhone->telephone = $phone;
             $provider->providerPhones = $providerPhone;
             $providerAddress = new ProviderAddresses();
             $geoJSON = json_decode($geoData);
             if ($geoJSON->status == "OK") {
                 $geometry = $geoJSON->results[0]->geometry->location;
                 $lat = $geometry->lat;
                 $lng = $geometry->lng;
             } else {
                 $lat = 0;
                 $lng = 0;
             }
             $providerAddress->latitude = $lat;
             $providerAddress->longitude = $lng;
             $providerAddress->address = $address;
             $provider->providerAddresses = $providerAddress;
             $providerCategories = array();
             foreach ($categories as $category) {
                 $providerCategory = new ProviderCategories();
                 $providerCategory->cid = $category;
                 array_push($providerCategories, $providerCategory);
             }
             $provider->providerCategories = $providerCategories;
             if ($provider->create()) {
                 \Stripe\Charge::create(array("amount" => $amount, "currency" => "usd", "customer" => $provider->stripe_customer_token, "source" => $provider->stripe_card_token, "description" => "{$plan->name} plan subscription"));
                 $this->response->redirect('/login?success');
                 $this->view->disable();
             } else {
                 array_push($errors, "An error occurred during the signup process.");
             }
         }
         $this->view->errors = $errors;
     }
     $timezones = (require "../app/config/timezones.php");
     $this->view->timezones = $timezones;
     $memberships = Memberships::find("id > 1");
     $this->view->memberships = $memberships;
     $categories = Categories::find();
     $this->view->categories = $categories;
     echo $this->view->render('auth', 'providerSignup');
 }