diff --git a/src/includes/checkout/CheckoutGateways.php b/src/includes/checkout/CheckoutGateways.php index 266fd976..9edab89e 100644 --- a/src/includes/checkout/CheckoutGateways.php +++ b/src/includes/checkout/CheckoutGateways.php @@ -3,6 +3,8 @@ namespace VindiPaymentGateways; use Exception; +use VindiPaymentGateways\GenerateUser; + class CheckoutGateways { @@ -13,54 +15,6 @@ public function __construct() add_action('woocommerce_before_pay_action', [$this, 'save_billing_fields'], 10, 1); } - private function auto_create_user_for_order($order) - { - $billing_email = $order->get_billing_email(); - $billing_first_name = $order->get_billing_first_name(); - $billing_last_name = $order->get_billing_last_name(); - - $user_id = $this->get_or_create_user($billing_email, $billing_first_name, $billing_last_name); - - if ($user_id) { - $order->set_customer_id($user_id); - $order->save(); - } - } - - private function get_or_create_user($billing_email, $billing_first_name, $billing_last_name) - { - $user_id = $this->set_user_id($billing_email); - if ($user_id) { - return $user_id; - } - - $username = $this->generate_unique_username($billing_first_name, $billing_last_name, $billing_email); - $password = wp_generate_password(12, false); - $user_id = wp_create_user($username, $password, $billing_email); - wp_set_current_user($user_id); - wp_set_auth_cookie($user_id); - - return $user_id; - } - - private function generate_unique_username($billing_first_name, $billing_last_name, $billing_email) - { - $username = strtolower($billing_first_name . '_' . $billing_last_name); - $username = str_replace(' ', '_', $username); - $suffix = 1; - - while (username_exists($username)) { - $username = strtolower($billing_first_name . '_' . $billing_last_name . '_' . $suffix); - $suffix++; - } - - if (!isset($username) || empty($username)) { - $username = current(explode('@', $billing_email)); - } - - return $username; - } - public function filter_checkout_gateways($gateways) { $available = [ @@ -86,7 +40,7 @@ public function filter_checkout_gateways($gateways) return $gateways; } - private function clear_session($hasSessionParams,$gateways) + private function clear_session($hasSessionParams, $gateways) { if ($hasSessionParams && !$this->is_subscription_context()) { WC()->session->set('vindi-payment-link', ''); @@ -171,7 +125,8 @@ public function save_billing_fields($order) $this->validate_required_fields(); $this->update_billing_fields($order, $fields); $order->save(); - $this->auto_create_user_for_order($order); + $generate_user = new GenerateUser(); + $generate_user->auto_create_user_for_order($order); } catch (Exception $err) { wc_add_notice($err->getMessage(), 'error'); } @@ -272,13 +227,4 @@ public function validate_person_type() throw new Exception((__('CNPJ', 'vindi-payment-gateway'))); } } - - private function set_user_id($userEmail) - { - $user = get_user_by('email', $userEmail); - if ($user) { - return $user->ID; - } - return null; - } } diff --git a/src/utils/GenerateUser.php b/src/utils/GenerateUser.php new file mode 100644 index 00000000..1325cb9a --- /dev/null +++ b/src/utils/GenerateUser.php @@ -0,0 +1,63 @@ +get_billing_email(); + $billing_first_name = $order->get_billing_first_name(); + $billing_last_name = $order->get_billing_last_name(); + + $user_id = $this->get_or_create_user($billing_email, $billing_first_name, $billing_last_name); + + if ($user_id) { + $order->set_customer_id($user_id); + $order->save(); + } + } + + private function get_or_create_user($billing_email, $billing_first_name, $billing_last_name) + { + $user_id = $this->set_user_id($billing_email); + if ($user_id) { + return $user_id; + } + + $username = $this->generate_unique_username($billing_first_name, $billing_last_name, $billing_email); + $password = wp_generate_password(12, false); + $user_id = wp_create_user($username, $password, $billing_email); + wp_set_current_user($user_id); + wp_set_auth_cookie($user_id); + + return $user_id; + } + + private function set_user_id($userEmail) + { + $user = get_user_by('email', $userEmail); + if ($user) { + return $user->ID; + } + return null; + } + + private function generate_unique_username($billing_first_name, $billing_last_name, $billing_email) + { + $username = strtolower($billing_first_name . '_' . $billing_last_name); + $username = str_replace(' ', '_', $username); + $suffix = 1; + + while (username_exists($username)) { + $username = strtolower($billing_first_name . '_' . $billing_last_name . '_' . $suffix); + $suffix++; + } + + if (!isset($username) || empty($username)) { + $username = current(explode('@', $billing_email)); + } + + return $username; + } +} \ No newline at end of file