protected function processForm()
 {
     if (Tools::getValue('action') == 'tryConnect') {
         if (Tools::getValue('email') == $this->context->cookie->amzConnectEmail) {
             $customer = new Customer();
             $authentication = $customer->getByEmail(trim(Tools::getValue('email')), trim(Tools::getValue('passwd')));
             if (isset($authentication->active) && !$authentication->active) {
                 $this->errors[] = Tools::displayError('Your account isn\'t available at this time, please contact us');
             } elseif (!$authentication || !$customer->id) {
                 $this->errors[] = Tools::displayError('Authentication failed.');
             } else {
                 $authentication->save();
                 AmazonPaymentsCustomerHelper::saveCustomersAmazonReference($authentication, $this->context->cookie->amzConnectCustomerId);
                 $this->context->cookie->id_compare = isset($this->context->cookie->id_compare) ? $this->context->cookie->id_compare : CompareProduct::getIdCompareByIdCustomer($customer->id);
                 $this->context->cookie->id_customer = (int) $customer->id;
                 $this->context->cookie->customer_lastname = $customer->lastname;
                 $this->context->cookie->customer_firstname = $customer->firstname;
                 $this->context->cookie->logged = 1;
                 $customer->logged = 1;
                 $this->context->cookie->is_guest = $customer->isGuest();
                 $this->context->cookie->passwd = $customer->passwd;
                 $this->context->cookie->email = $customer->email;
                 // Add customer to the context
                 $this->context->customer = $customer;
                 if (Configuration::get('PS_CART_FOLLOWING') && (empty($this->context->cookie->id_cart) || Cart::getNbProducts($this->context->cookie->id_cart) == 0) && ($id_cart = (int) Cart::lastNoneOrderedCart($this->context->customer->id))) {
                     $this->context->cart = new Cart($id_cart);
                 } else {
                     $id_carrier = (int) $this->context->cart->id_carrier;
                     $this->context->cart->id_carrier = 0;
                     $this->context->cart->setDeliveryOption(null);
                     $this->context->cart->id_address_delivery = (int) Address::getFirstCustomerAddressId((int) $customer->id);
                     $this->context->cart->id_address_invoice = (int) Address::getFirstCustomerAddressId((int) $customer->id);
                 }
                 $this->context->cart->id_customer = (int) $customer->id;
                 $this->context->cart->secure_key = $customer->secure_key;
                 if ($this->ajax && isset($id_carrier) && $id_carrier && Configuration::get('PS_ORDER_PROCESS_TYPE')) {
                     $delivery_option = array($this->context->cart->id_address_delivery => $id_carrier . ',');
                     $this->context->cart->setDeliveryOption($delivery_option);
                 }
                 $this->context->cart->save();
                 $this->context->cookie->id_cart = (int) $this->context->cart->id;
                 $this->context->cookie->write();
                 $this->context->cart->autosetProductAddress();
                 Hook::exec('actionAuthentication');
                 // Login information have changed, so we check if the cart rules still apply
                 CartRule::autoRemoveFromCart($this->context);
                 CartRule::autoAddToCart($this->context);
                 if (Tools::getValue('toCheckout') == '1') {
                     $goto = $this->context->link->getModuleLink('amzpayments', 'amzpayments');
                 } elseif (Tools::getValue('fromCheckout') == '1') {
                     $goto = 'index.php?controller=history';
                 } elseif ($this->context->cart->nbProducts()) {
                     $goto = 'index.php?controller=order';
                 } else {
                     if (Configuration::get('PS_SSL_ENABLED')) {
                         $goto = _PS_BASE_URL_SSL_ . __PS_BASE_URI__;
                     } else {
                         $goto = _PS_BASE_URL_ . __PS_BASE_URI__;
                     }
                 }
                 Tools::redirect($goto);
             }
         }
     }
 }
 public function init()
 {
     self::$amz_payments = new AmzPayments();
     $this->isLogged = (bool) $this->context->customer->id && Customer::customerIdExistsStatic((int) $this->context->cookie->id_customer);
     parent::init();
     /* Disable some cache related bugs on the cart/order */
     header('Cache-Control: no-cache, must-revalidate');
     header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
     $this->display_column_left = false;
     $this->display_column_right = false;
     // Service initialisieren
     $this->service = self::$amz_payments->getService();
     if (Tools::isSubmit('ajax')) {
         if (Tools::isSubmit('method')) {
             switch (Tools::getValue('method')) {
                 case 'redirectAuthentication':
                 case 'setusertoshop':
                     if (Tools::getValue('access_token')) {
                         $this->context->cookie->amz_access_token = AmzPayments::prepareCookieValueForPrestaShopUse(Tools::getValue('access_token'));
                         $this->context->cookie->amz_access_token_set_time = time();
                     } else {
                         if (Tools::getValue('method') == 'redirectAuthentication') {
                             Tools::redirect('index');
                         } else {
                             error_log('Error, method not submitted and no token');
                             die('error');
                         }
                     }
                     if (Tools::getValue('action') == 'fromCheckout') {
                         $accessTokenValue = AmzPayments::prepareCookieValueForAmazonPaymentsUse(Tools::getValue('access_token'));
                     } else {
                         $accessTokenValue = Tools::getValue('access_token');
                     }
                     $d = self::$amz_payments->requestTokenInfo($accessTokenValue);
                     if ($d->aud != self::$amz_payments->client_id) {
                         if (Tools::getValue('method') == 'redirectAuthentication') {
                             Tools::redirect('index');
                         } else {
                             error_log('auth error LPA');
                             die('error');
                         }
                     }
                     $d = self::$amz_payments->requestProfile($accessTokenValue);
                     $customer_userid = $d->user_id;
                     $customer_name = $d->name;
                     $customer_email = $d->email;
                     // $postcode = $d->postal_code;
                     if ($customers_local_id = AmazonPaymentsCustomerHelper::findByAmazonCustomerId($customer_userid)) {
                         // Customer already exists - login
                         Hook::exec('actionBeforeAuthentication');
                         $customer = new Customer();
                         $authentication = AmazonPaymentsCustomerHelper::getByCustomerID($customers_local_id, true, $customer);
                         if (isset($authentication->active) && !$authentication->active) {
                             $this->errors[] = Tools::displayError('Your account isn\'t available at this time, please contact us');
                         } elseif (!$authentication || !$customer->id) {
                             $this->errors[] = Tools::displayError('Authentication failed.');
                         } else {
                             $this->context->cookie->id_compare = isset($this->context->cookie->id_compare) ? $this->context->cookie->id_compare : CompareProduct::getIdCompareByIdCustomer($customer->id);
                             $this->context->cookie->id_customer = (int) $customer->id;
                             $this->context->cookie->customer_lastname = $customer->lastname;
                             $this->context->cookie->customer_firstname = $customer->firstname;
                             $this->context->cookie->logged = 1;
                             $customer->logged = 1;
                             $this->context->cookie->is_guest = $customer->isGuest();
                             $this->context->cookie->passwd = $customer->passwd;
                             $this->context->cookie->email = $customer->email;
                             // Add customer to the context
                             $this->context->customer = $customer;
                             if (Configuration::get('PS_CART_FOLLOWING') && (empty($this->context->cookie->id_cart) || Cart::getNbProducts($this->context->cookie->id_cart) == 0) && ($id_cart = (int) Cart::lastNoneOrderedCart($this->context->customer->id))) {
                                 $this->context->cart = new Cart($id_cart);
                             } else {
                                 $id_carrier = (int) $this->context->cart->id_carrier;
                                 $this->context->cart->id_carrier = 0;
                                 $this->context->cart->setDeliveryOption(null);
                                 $this->context->cart->id_address_delivery = (int) Address::getFirstCustomerAddressId((int) $customer->id);
                                 $this->context->cart->id_address_invoice = (int) Address::getFirstCustomerAddressId((int) $customer->id);
                             }
                             $this->context->cart->id_customer = (int) $customer->id;
                             $this->context->cart->secure_key = $customer->secure_key;
                             if ($this->ajax && isset($id_carrier) && $id_carrier && Configuration::get('PS_ORDER_PROCESS_TYPE')) {
                                 $delivery_option = array($this->context->cart->id_address_delivery => $id_carrier . ',');
                                 $this->context->cart->setDeliveryOption($delivery_option);
                             }
                             $this->context->cart->save();
                             $this->context->cookie->id_cart = (int) $this->context->cart->id;
                             $this->context->cookie->write();
                             $this->context->cart->autosetProductAddress();
                             Hook::exec('actionAuthentication');
                             // Login information have changed, so we check if the cart rules still apply
                             CartRule::autoRemoveFromCart($this->context);
                             CartRule::autoAddToCart($this->context);
                             if (Tools::getValue('action') == 'fromCheckout' && isset($this->context->cookie->amz_connect_order)) {
                                 AmzPayments::switchOrderToCustomer($this->context->customer->id, $this->context->cookie->amz_connect_order, true);
                             }
                             if (Tools::getValue('action') == 'checkout') {
                                 $goto = $this->context->link->getModuleLink('amzpayments', 'amzpayments');
                             } elseif (Tools::getValue('action') == 'fromCheckout') {
                                 $goto = 'index.php?controller=history';
                             } elseif ($this->context->cart->nbProducts()) {
                                 $goto = 'index.php?controller=order';
                             } else {
                                 if (Configuration::get('PS_SSL_ENABLED')) {
                                     $goto = _PS_BASE_URL_SSL_ . __PS_BASE_URI__;
                                 } else {
                                     $goto = _PS_BASE_URL_ . __PS_BASE_URI__;
                                 }
                             }
                             if (Tools::getValue('method') == 'redirectAuthentication') {
                                 Tools::redirect($goto);
                             } else {
                                 echo $goto;
                             }
                         }
                     } else {
                         if (AmazonPaymentsCustomerHelper::findByEmailAddress($customer_email)) {
                             $this->context->cookie->amzConnectEmail = $customer_email;
                             $this->context->cookie->amzConnectCustomerId = $customer_userid;
                             $goto = $this->context->link->getModuleLink('amzpayments', 'connect_accounts');
                             if (Tools::getValue('action') && Tools::getValue('action') == 'checkout') {
                                 if (strpos($goto, '?') > 0) {
                                     $goto .= '&checkout=1';
                                 } else {
                                     $goto .= '?checkout=1';
                                 }
                             }
                             if (Tools::getValue('method') == 'redirectAuthentication') {
                                 Tools::redirect($goto);
                             } else {
                                 echo $goto;
                             }
                         } else {
                             // Customer does not exist - Create account
                             Hook::exec('actionBeforeSubmitAccount');
                             $this->create_account = true;
                             $_POST['passwd'] = md5(time() . _COOKIE_KEY_);
                             $firstname = '';
                             $lastname = '';
                             $customer_name = preg_replace("/[0-9]/", "", $customer_name);
                             if (strpos(trim($customer_name), ' ') !== false) {
                                 list($firstname, $lastname) = explode(' ', trim($customer_name));
                             } elseif (strpos(trim($customer_name), '-') !== false) {
                                 list($firstname, $lastname) = explode('-', trim($customer_name));
                             } else {
                                 $firstname = trim($customer_name);
                                 $lastname = 'Placeholder';
                             }
                             $customer = new Customer();
                             $customer->email = $customer_email;
                             $lastname_address = $lastname;
                             $firstname_address = $firstname;
                             $_POST['lastname'] = Tools::getValue('customer_lastname', $lastname_address);
                             $_POST['firstname'] = Tools::getValue('customer_firstname', $firstname_address);
                             // $addresses_types = array('address');
                             $this->errors = array_unique(array_merge($this->errors, $customer->validateController()));
                             // Check the requires fields which are settings in the BO
                             $this->errors = $this->errors + $customer->validateFieldsRequiredDatabase();
                             if (!count($this->errors)) {
                                 $customer->firstname = Tools::ucwords($customer->firstname);
                                 $customer->is_guest = 0;
                                 $customer->active = 1;
                                 if (!count($this->errors)) {
                                     if ($customer->add()) {
                                         if (!$customer->is_guest) {
                                             if (!$this->sendConfirmationMail($customer)) {
                                                 $this->errors[] = Tools::displayError('The email cannot be sent.');
                                             }
                                         }
                                         AmazonPaymentsCustomerHelper::saveCustomersAmazonReference($customer, $customer_userid);
                                         $this->updateContext($customer);
                                         $this->context->cart->update();
                                         Hook::exec('actionCustomerAccountAdd', array('_POST' => $_POST, 'newCustomer' => $customer));
                                         if (Tools::getValue('action') == 'fromCheckout' && isset($this->context->cookie->amz_connect_order)) {
                                             AmzPayments::switchOrderToCustomer($customer->id, $this->context->cookie->amz_connect_order, true);
                                         }
                                         if (Tools::getValue('action') == 'checkout') {
                                             $goto = $this->context->link->getModuleLink('amzpayments', 'amzpayments');
                                         } elseif (Tools::getValue('action') == 'fromCheckout') {
                                             $goto = 'index.php?controller=history';
                                         } elseif ($this->context->cart->nbProducts()) {
                                             $goto = 'index.php?controller=order';
                                         } else {
                                             if (Configuration::get('PS_SSL_ENABLED')) {
                                                 $goto = _PS_BASE_URL_SSL_ . __PS_BASE_URI__;
                                             } else {
                                                 $goto = _PS_BASE_URL_ . __PS_BASE_URI__;
                                             }
                                         }
                                         if (Tools::getValue('method') == 'redirectAuthentication') {
                                             Tools::redirect($goto);
                                         } else {
                                             echo $goto;
                                         }
                                     } else {
                                         $this->errors[] = Tools::displayError('An error occurred while creating your account.');
                                     }
                                 }
                             } else {
                                 error_log('Error validating customers informations');
                                 die('error');
                             }
                         }
                     }
                     die;
             }
         }
     }
 }
Exemple #3
0
 public function initContent()
 {
     $this->context->controller->addJS(self::$amz_payments->getPathUri() . 'views/js/amzpayments_checkout.js');
     $this->context->cart->id_address_delivery = null;
     $this->context->cart->id_address_invoice = null;
     parent::initContent();
     if (empty($this->context->cart->id_carrier)) {
         $checked = $this->context->cart->simulateCarrierSelectedOutput();
         $checked = (int) Cart::desintifier($checked);
         $this->context->cart->id_carrier = $checked;
         $this->context->cart->update();
         CartRule::autoRemoveFromCart($this->context);
         CartRule::autoAddToCart($this->context);
     }
     $this->_assignSummaryInformations();
     $this->_assignWrappingAndTOS();
     $selected_country = (int) Configuration::get('PS_COUNTRY_DEFAULT');
     if (Configuration::get('PS_RESTRICT_DELIVERED_COUNTRIES')) {
         $countries = Carrier::getDeliveredCountries($this->context->language->id, true, true);
     } else {
         $countries = Country::getCountries($this->context->language->id, true);
     }
     $free_shipping = false;
     foreach ($this->context->cart->getCartRules() as $rule) {
         if ($rule['free_shipping'] && !$rule['carrier_restriction']) {
             $free_shipping = true;
             break;
         }
     }
     $this->context->smarty->assign(array('advanced_payment_api' => false, 'free_shipping' => $free_shipping, 'isGuest' => isset($this->context->cookie->is_guest) ? $this->context->cookie->is_guest : 0, 'countries' => $countries, 'sl_country' => isset($selected_country) ? $selected_country : 0, 'PS_GUEST_CHECKOUT_ENABLED' => Configuration::get('PS_GUEST_CHECKOUT_ENABLED'), 'errorCarrier' => Tools::displayError('You must choose a carrier.', false), 'errorTOS' => Tools::displayError('You must accept the Terms of Service.', false), 'isPaymentStep' => (bool) Tools::getIsset(Tools::getValue('isPaymentStep')) && Tools::getValue('isPaymentStep'), 'genders' => Gender::getGenders(), 'one_phone_at_least' => (int) Configuration::get('PS_ONE_PHONE_AT_LEAST'), 'HOOK_CREATE_ACCOUNT_FORM' => Hook::exec('displayCustomerAccountForm'), 'HOOK_CREATE_ACCOUNT_TOP' => Hook::exec('displayCustomerAccountFormTop')));
     $years = Tools::dateYears();
     $months = Tools::dateMonths();
     $days = Tools::dateDays();
     $this->context->smarty->assign(array('years' => $years, 'months' => $months, 'days' => $days));
     $this->_assignCarrier();
     Tools::safePostVars();
     $blocknewsletter = Module::getInstanceByName('blocknewsletter');
     $this->context->smarty->assign('newsletter', (bool) $blocknewsletter && $blocknewsletter->active);
     $this->context->smarty->assign(array('amz_module_path' => self::$amz_payments->getPathUri(), 'amz_session' => Tools::getValue('session') ? Tools::getValue('session') : $this->context->cookie->amazon_id, 'sellerID' => Configuration::get('AMZ_MERCHANT_ID'), 'sandboxMode' => false));
     if (isset($this->context->cookie->amz_access_token) && $this->context->cookie->amz_access_token != '' && !AmazonPaymentsCustomerHelper::customerHasAmazonCustomerId($this->context->cookie->id_customer)) {
         $this->context->smarty->assign('show_amazon_account_creation_allowed', true);
     } else {
         $this->context->smarty->assign('show_amazon_account_creation_allowed', false);
     }
     $this->context->smarty->assign('preselect_create_account', Configuration::get('PRESELECT_CREATE_ACCOUNT') == 1);
     $this->context->smarty->assign('force_account_creation', Configuration::get('FORCE_ACCOUNT_CREATION') == 1);
     if (Configuration::get('TEMPLATE_VARIANT_BS') == 1) {
         $this->setTemplate('amzpayments_bs.tpl');
     } else {
         $this->setTemplate('amzpayments.tpl');
     }
 }