PageRenderTime 223ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/catalog/controller/module/amazon_login.php

https://gitlab.com/shapcy/opencart
PHP | 190 lines | 149 code | 39 blank | 2 comment | 37 complexity | 676b319d983b05d840078110a4b1490a 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', '', true);
  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. return $this->load->view('module/amazon_login', $data);
  33. }
  34. }
  35. public function login() {
  36. $this->load->model('payment/amazon_login_pay');
  37. $this->load->model('account/customer');
  38. $this->load->model('account/customer_group');
  39. $this->load->language('payment/amazon_login_pay');
  40. unset($this->session->data['lpa']);
  41. unset($this->session->data['access_token']);
  42. if (isset($this->request->get['access_token'])) {
  43. $this->session->data['access_token'] = $this->request->get['access_token'];
  44. $user = $this->model_payment_amazon_login_pay->getUserInfo($this->request->get['access_token']);
  45. }
  46. if ((array)$user) {
  47. if (isset($user->error)) {
  48. $this->model_payment_amazon_login_pay->logger($user->error . ': ' . $user->error_description);
  49. $this->session->data['lpa']['error'] = $this->language->get('error_login');
  50. $this->response->redirect($this->url->link('payment/amazon_login_pay/loginFailure', '', true));
  51. }
  52. $customer_info = $this->model_account_customer->getCustomerByEmail($user->email);
  53. $this->model_payment_amazon_login_pay->logger($user);
  54. if ($customer_info) {
  55. if ($this->validate($user->email)) {
  56. unset($this->session->data['guest']);
  57. $this->load->model('account/address');
  58. if ($this->config->get('config_tax_customer') == 'payment') {
  59. $this->session->data['payment_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
  60. }
  61. if ($this->config->get('config_tax_customer') == 'shipping') {
  62. $this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
  63. }
  64. $this->load->model('account/activity');
  65. $activity_data = array(
  66. 'customer_id' => $this->customer->getId(),
  67. 'name' => $this->customer->getFirstName() . ' ' . $this->customer->getLastName()
  68. );
  69. $this->model_account_activity->addActivity('login', $activity_data);
  70. $this->model_payment_amazon_login_pay->logger('Customer logged in - ID: ' . $customer_info['customer_id'] . ', Email: ' . $customer_info['email']);
  71. } else {
  72. $this->model_payment_amazon_login_pay->logger('Could not login to - ID: ' . $customer_info['customer_id'] . ', Email: ' . $customer_info['email']);
  73. $this->session->data['lpa']['error'] = $this->language->get('error_login');
  74. $this->response->redirect($this->url->link('payment/amazon_login_pay/loginFailure', '', true));
  75. }
  76. $this->response->redirect($this->url->link('account/account', '', true));
  77. } else {
  78. $country_id = 0;
  79. $zone_id = 0;
  80. $full_name = explode(' ', $user->name);
  81. $last_name = array_pop($full_name);
  82. $first_name = implode(' ', $full_name);
  83. $data = array(
  84. 'customer_group_id' => (int)$this->config->get('config_customer_group_id'),
  85. 'firstname' => $first_name,
  86. 'lastname' => $last_name,
  87. 'email' => $user->email,
  88. 'telephone' => '',
  89. 'fax' => '',
  90. 'password' => uniqid(rand(), true),
  91. 'company' => '',
  92. 'address_1' => '',
  93. 'address_2' => '',
  94. 'city' => '',
  95. 'postcode' => '',
  96. 'country_id' => (int)$country_id,
  97. 'zone_id' => (int)$zone_id,
  98. );
  99. $customer_id = $this->model_account_customer->addCustomer($data);
  100. $this->model_payment_amazon_login_pay->logger('Customer ID created: ' . $customer_id);
  101. if ($this->validate($user->email)) {
  102. unset($this->session->data['guest']);
  103. $this->load->model('account/address');
  104. if ($this->config->get('config_tax_customer') == 'payment') {
  105. $this->session->data['payment_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
  106. }
  107. if ($this->config->get('config_tax_customer') == 'shipping') {
  108. $this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
  109. }
  110. $this->load->model('account/activity');
  111. $activity_data = array(
  112. 'customer_id' => $this->customer->getId(),
  113. 'name' => $this->customer->getFirstName() . ' ' . $this->customer->getLastName()
  114. );
  115. $this->model_account_activity->addActivity('login', $activity_data);
  116. $this->model_payment_amazon_login_pay->logger('Customer logged in - ID: ' . $customer_id . ', Email: ' . $user->email);
  117. $this->response->redirect($this->url->link('account/account', '', true));
  118. } else {
  119. $this->model_payment_amazon_login_pay->logger('Could not login to - ID: ' . $customer_id . ', Email: ' . $user->email);
  120. $this->session->data['lpa']['error'] = $this->language->get('error_login');
  121. $this->response->redirect($this->url->link('payment/amazon_login_pay/loginFailure', '', true));
  122. }
  123. }
  124. } else {
  125. $this->session->data['lpa']['error'] = $this->language->get('error_login');
  126. $this->response->redirect($this->url->link('payment/amazon_login_pay/loginFailure', '', true));
  127. }
  128. }
  129. public function logout() {
  130. unset($this->session->data['lpa']);
  131. unset($this->session->data['access_token']);
  132. // capital L in Amazon cookie name is required, do not alter for coding standards
  133. if (isset($this->request->cookie['amazon_Login_state_cache'])) {
  134. setcookie('amazon_Login_state_cache', '', time() - 4815162342);
  135. }
  136. }
  137. protected function validate($email) {
  138. if (!$this->customer->login($email, '', true)) {
  139. $this->error['warning'] = $this->language->get('error_login');
  140. }
  141. $customer_info = $this->model_account_customer->getCustomerByEmail($email);
  142. if ($customer_info && !$customer_info['approved']) {
  143. $this->error['warning'] = $this->language->get('error_approved');
  144. }
  145. if (!$this->error) {
  146. return true;
  147. } else {
  148. return false;
  149. }
  150. }
  151. }