/catalog/controller/module/amazon_login.php

https://gitlab.com/firstrate/firstrate · PHP · 194 lines · 153 code · 39 blank · 2 comment · 39 complexity · 2fda8653f0f4861c3527ef08b2538785 MD5 · raw file

  1. <?php
  2. class ControllerModuleAmazonLogin extends Controller {
  3. public function index() {
  4. $this->load->model('payment/amazon_login_pay');
  5. if ($this->config->get('amazon_login_pay_status') && $this->config->get('amazon_login_status') && !$this->customer->isLogged() && !empty($this->request->server['HTTPS'])) {
  6. // capital L in Amazon cookie name is required, do not alter for coding standards
  7. if (isset($this->request->cookie['amazon_Login_state_cache'])) {
  8. setcookie('amazon_Login_state_cache', '', time() - 4815162342);
  9. }
  10. $amazon_payment_js = $this->model_payment_amazon_login_pay->getWidgetJs();
  11. $this->document->addScript($amazon_payment_js);
  12. $data['amazon_login_pay_client_id'] = $this->config->get('amazon_login_pay_client_id');
  13. $data['amazon_login_return_url'] = $this->url->link('module/amazon_login/login', '', 'SSL');
  14. if ($this->config->get('amazon_login_pay_test') == 'sandbox') {
  15. $data['amazon_login_pay_test'] = true;
  16. }
  17. if ($this->config->get('amazon_login_button_type')) {
  18. $data['amazon_login_button_type'] = $this->config->get('amazon_login_button_type');
  19. } else {
  20. $data['amazon_login_button_type'] = 'lwa';
  21. }
  22. if ($this->config->get('amazon_login_button_colour')) {
  23. $data['amazon_login_button_colour'] = $this->config->get('amazon_login_button_colour');
  24. } else {
  25. $data['amazon_login_button_colour'] = 'gold';
  26. }
  27. if ($this->config->get('amazon_login_button_size')) {
  28. $data['amazon_login_button_size'] = $this->config->get('amazon_login_button_size');
  29. } else {
  30. $data['amazon_login_button_size'] = 'medium';
  31. }
  32. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/amazon_login.tpl')) {
  33. return $this->load->view($this->config->get('config_template') . '/template/module/amazon_login.tpl', $data);
  34. } else {
  35. return $this->load->view('default/template/module/amazon_login.tpl', $data);
  36. }
  37. }
  38. }
  39. public function login() {
  40. $this->load->model('payment/amazon_login_pay');
  41. $this->load->model('account/customer');
  42. $this->load->model('account/customer_group');
  43. $this->load->language('payment/amazon_login_pay');
  44. unset($this->session->data['lpa']);
  45. unset($this->session->data['access_token']);
  46. if (isset($this->request->get['access_token'])) {
  47. $this->session->data['access_token'] = $this->request->get['access_token'];
  48. $user = $this->model_payment_amazon_login_pay->getUserInfo($this->request->get['access_token']);
  49. }
  50. if ((array)$user) {
  51. if (isset($user->error)) {
  52. $this->model_payment_amazon_login_pay->logger($user->error . ': ' . $user->error_description);
  53. $this->session->data['lpa']['error'] = $this->language->get('error_login');
  54. $this->response->redirect($this->url->link('payment/amazon_login_pay/loginFailure', '', 'SSL'));
  55. }
  56. $customer_info = $this->model_account_customer->getCustomerByEmail($user->email);
  57. $this->model_payment_amazon_login_pay->logger($user);
  58. if ($customer_info) {
  59. if ($this->validate($user->email)) {
  60. unset($this->session->data['guest']);
  61. $this->load->model('account/address');
  62. if ($this->config->get('config_tax_customer') == 'payment') {
  63. $this->session->data['payment_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
  64. }
  65. if ($this->config->get('config_tax_customer') == 'shipping') {
  66. $this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
  67. }
  68. $this->load->model('account/activity');
  69. $activity_data = array(
  70. 'customer_id' => $this->customer->getId(),
  71. 'name' => $this->customer->getFirstName() . ' ' . $this->customer->getLastName()
  72. );
  73. $this->model_account_activity->addActivity('login', $activity_data);
  74. $this->model_payment_amazon_login_pay->logger('Customer logged in - ID: ' . $customer_info['customer_id'] . ', Email: ' . $customer_info['email']);
  75. } else {
  76. $this->model_payment_amazon_login_pay->logger('Could not login to - ID: ' . $customer_info['customer_id'] . ', Email: ' . $customer_info['email']);
  77. $this->session->data['lpa']['error'] = $this->language->get('error_login');
  78. $this->response->redirect($this->url->link('payment/amazon_login_pay/loginFailure', '', 'SSL'));
  79. }
  80. $this->response->redirect($this->url->link('account/account', '', 'SSL'));
  81. } else {
  82. $country_id = 0;
  83. $zone_id = 0;
  84. $full_name = explode(' ', $user->name);
  85. $last_name = array_pop($full_name);
  86. $first_name = implode(' ', $full_name);
  87. $data = array(
  88. 'customer_group_id' => (int)$this->config->get('config_customer_group_id'),
  89. 'firstname' => $first_name,
  90. 'lastname' => $last_name,
  91. 'email' => $user->email,
  92. 'telephone' => '',
  93. 'fax' => '',
  94. 'password' => uniqid(rand(), true),
  95. 'company' => '',
  96. 'address_1' => '',
  97. 'address_2' => '',
  98. 'city' => '',
  99. 'postcode' => '',
  100. 'country_id' => (int)$country_id,
  101. 'zone_id' => (int)$zone_id,
  102. );
  103. $customer_id = $this->model_account_customer->addCustomer($data);
  104. $this->model_payment_amazon_login_pay->logger('Customer ID created: ' . $customer_id);
  105. if ($this->validate($user->email)) {
  106. unset($this->session->data['guest']);
  107. $this->load->model('account/address');
  108. if ($this->config->get('config_tax_customer') == 'payment') {
  109. $this->session->data['payment_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
  110. }
  111. if ($this->config->get('config_tax_customer') == 'shipping') {
  112. $this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
  113. }
  114. $this->load->model('account/activity');
  115. $activity_data = array(
  116. 'customer_id' => $this->customer->getId(),
  117. 'name' => $this->customer->getFirstName() . ' ' . $this->customer->getLastName()
  118. );
  119. $this->model_account_activity->addActivity('login', $activity_data);
  120. $this->model_payment_amazon_login_pay->logger('Customer logged in - ID: ' . $customer_id . ', Email: ' . $user->email);
  121. $this->response->redirect($this->url->link('account/account', '', 'SSL'));
  122. } else {
  123. $this->model_payment_amazon_login_pay->logger('Could not login to - ID: ' . $customer_id . ', Email: ' . $user->email);
  124. $this->session->data['lpa']['error'] = $this->language->get('error_login');
  125. $this->response->redirect($this->url->link('payment/amazon_login_pay/loginFailure', '', 'SSL'));
  126. }
  127. }
  128. } else {
  129. $this->session->data['lpa']['error'] = $this->language->get('error_login');
  130. $this->response->redirect($this->url->link('payment/amazon_login_pay/loginFailure', '', 'SSL'));
  131. }
  132. }
  133. public function logout() {
  134. unset($this->session->data['lpa']);
  135. unset($this->session->data['access_token']);
  136. // capital L in Amazon cookie name is required, do not alter for coding standards
  137. if (isset($this->request->cookie['amazon_Login_state_cache'])) {
  138. setcookie('amazon_Login_state_cache', '', time() - 4815162342);
  139. }
  140. }
  141. protected function validate($email) {
  142. if (!$this->customer->login($email, '', true)) {
  143. $this->error['warning'] = $this->language->get('error_login');
  144. }
  145. $customer_info = $this->model_account_customer->getCustomerByEmail($email);
  146. if ($customer_info && !$customer_info['approved']) {
  147. $this->error['warning'] = $this->language->get('error_approved');
  148. }
  149. if (!$this->error) {
  150. return true;
  151. } else {
  152. return false;
  153. }
  154. }
  155. }