View source
<?php
namespace Drupal\uc_cart;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\user\Entity\User;
use Drupal\uc_cart\Event\CheckoutCompleteEvent;
use Drupal\uc_order\OrderInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class CartManager implements CartManagerInterface {
protected $currentUser;
protected $session;
public function __construct(AccountProxyInterface $current_user, SessionInterface $session) {
$this->currentUser = $current_user;
$this->session = $session;
}
public function get($id = NULL) {
$id = $id ?: $this
->getId();
return new Cart($id);
}
public function emptyCart($id = NULL) {
$this
->get($id)
->emptyCart();
}
protected function getId($create = TRUE) {
if ($this->currentUser
->isAuthenticated()) {
return $this->currentUser
->id();
}
elseif (!$this->session
->has('uc_cart_id') && $create) {
$this->session
->set('uc_cart_id', md5(uniqid(rand(), TRUE)));
}
return $this->session
->has('uc_cart_id') ? $this->session
->get('uc_cart_id') : FALSE;
}
public function completeSale(OrderInterface $order, $login = TRUE) {
$this
->emptyCart();
if (empty($order->data->complete_sale)) {
$this
->completeSaleAccount($order);
if ($order
->getStateId() == 'in_checkout') {
$order
->setStatusId(uc_order_state_default('post_checkout'));
}
$order
->save();
$account = $order
->getOwner();
\Drupal::moduleHandler()
->invokeAll('uc_checkout_complete', [
$order,
$account,
]);
$event = new CheckoutCompleteEvent($order);
\Drupal::service('event_dispatcher')
->dispatch($event::EVENT_NAME, $event);
}
$type = $order->data->complete_sale;
if ($type == 'new_user' && $login && $this->currentUser
->isAnonymous()) {
if (\Drupal::config('uc_cart.settings')
->get('new_customer_login')) {
$type = 'new_user_logged_in';
user_login_finalize($order
->getOwner());
}
}
$message = \Drupal::config('uc_cart.messages')
->get($type);
$message = \Drupal::token()
->replace($message, [
'uc_order' => $order,
]);
$variables['!new_username'] = isset($order->data->new_user_name) ? $order->data->new_user_name : '';
$variables['!new_password'] = isset($order->password) ? $order->password : t('Your password');
$message = strtr($message, $variables);
return [
'#theme' => 'uc_cart_complete_sale',
'#message' => [
'#markup' => $message,
],
'#order' => $order,
];
}
protected function completeSaleAccount(OrderInterface $order) {
if ($order
->getOwnerId()) {
$order->data->complete_sale = 'logged_in';
return;
}
if ($account = user_load_by_mail($order
->getEmail())) {
$order
->setOwner($account);
$order->data->complete_sale = 'existing_user';
return;
}
$cart_config = \Drupal::config('uc_cart.settings');
$fields = [
'name' => uc_store_email_to_username($order
->getEmail()),
'mail' => $order
->getEmail(),
'init' => $order
->getEmail(),
'pass' => user_password(),
'roles' => [],
'status' => $cart_config
->get('new_customer_status_active') ? 1 : 0,
];
if (isset($order->data->new_user_name)) {
$fields['name'] = $order->data->new_user_name;
}
$account = User::create($fields);
$account
->save();
if (isset($order->data->new_user_hash)) {
\Drupal::database()
->query('UPDATE {users_field_data} SET pass = :hash WHERE uid = :uid', [
':hash' => $order->data->new_user_hash,
':uid' => $account
->id(),
]);
$account->password = t('Your password');
}
else {
$account->password = $fields['pass'];
$order->password = $fields['pass'];
}
if ($cart_config
->get('new_customer_email')) {
$type = $cart_config
->get('new_customer_status_active') ? 'register_no_approval_required' : 'register_pending_approval';
\Drupal::service('plugin.manager.mail')
->mail('user', $type, $order
->getEmail(), uc_store_mail_recipient_langcode($order
->getEmail()), [
'account' => $account,
], uc_store_email_from());
}
$order
->setOwner($account);
$order->data->new_user_name = $fields['name'];
$order->data->complete_sale = 'new_user';
}
}