Skip to content

Commit

Permalink
Set guest email (#40)
Browse files Browse the repository at this point in the history
* Set guest email

* bump version
  • Loading branch information
steve-gr4vy authored Aug 11, 2023
1 parent cc96ce5 commit d4cebc9
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 6 deletions.
12 changes: 12 additions & 0 deletions Api/TransactionRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ public function setPaymentInformation(
\Gr4vy\Magento\Api\Data\TransactionInterface $transactionData
);

/**
* Set Guest Email - store a guest email against the session
* @param string
* @param string
* @return boolean
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function setGuestEmail(
$cartId,
$email
);

/**
* Retrieve Transaction
* @param string $transactionId
Expand Down
14 changes: 12 additions & 2 deletions Helper/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,19 @@ public function updateGr4vyBuyerAddress($address = null)
*/
public function updateGr4vyBuyerAddressFromQuote($quote)
{

$external_identifier = null;
$customer = $this->getCurrentCustomer();
$buyerModel = $this->buyerRepository->getByExternalIdentifier($customer->getId(), $this->gr4vyHelper->getGr4vyId());

if (!$this->customerSession->isLoggedIn()) {
$visitor = $this->visitorSession->getVisitorData();
//we prefix external_identifer with visitor_ so it doesn't clash with customer ID
$external_identifier = "visitor_" . $visitor["visitor_id"];
}
else {
$external_identifier = $customer->getId();
}

$buyerModel = $this->buyerRepository->getByExternalIdentifier($external_identifier, $this->gr4vyHelper->getGr4vyId());

$billing_details = null;

Expand Down
9 changes: 7 additions & 2 deletions Model/Client/Buyer.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,13 @@ public function createBuyer($external_identifier, $display_name)

try {
$buyer = $this->getGr4vyConfig()->addBuyer($buyer_request);

return $buyer["id"];
if (array_key_exists("id", $buyer)) {
return $buyer["id"];
}
else {
$this->gr4vyLogger->logMixed(["buyer"=>$buyer], "createBuyer response");
return self::ERROR_DUPLICATE;
}
}
catch (\Exception $e) {
$this->gr4vyLogger->logException($e);
Expand Down
12 changes: 12 additions & 0 deletions Model/TransactionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,18 @@ public function setPaymentInformation(
return true;
}

/**
* {@inheritdoc}
*/
public function setGuestEmail($cartId, $email)
{
$quote = $this->getQuoteModel($cartId);
$quote->setCustomerEmail($email);
$quote->save();

return true;
}

/**
* retrieve fully loaded quote model to interact with Quote Properly
*
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "gr4vy/magento",
"description": "Magento 2 Payment module from Gr4vy",
"type": "magento2-module",
"version": "1.1.3",
"version": "1.1.4",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down
6 changes: 6 additions & 0 deletions etc/webapi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
<resource ref="anonymous" />
</resources>
</route>
<route url="/V1/gr4vy-payment/set-guest-email" method="POST">
<service class="Gr4vy\Magento\Api\TransactionRepositoryInterface" method="setGuestEmail"/>
<resources>
<resource ref="anonymous" />
</resources>
</route>
<!--end of custom api-->
<route url="/V1/gr4vy-payment/transaction" method="POST">
<service class="Gr4vy\Magento\Api\TransactionRepositoryInterface" method="save"/>
Expand Down
7 changes: 6 additions & 1 deletion view/frontend/requirejs-config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
var config = {
map: {
'*': {
'Magento_Checkout/js/view/form/element/email':'Gr4vy_Magento/js/view/form/element/email'
}
},
config: {
mixins: {
'Magento_Checkout/js/action/get-payment-information': {
Expand All @@ -18,4 +23,4 @@ var config = {
}
}
}
};
};
234 changes: 234 additions & 0 deletions view/frontend/web/js/view/form/element/email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

define([
'jquery',
'uiComponent',
'ko',
'mage/storage',
'Magento_Customer/js/model/customer',
'Magento_Customer/js/action/check-email-availability',
'Magento_Customer/js/action/login',
'Magento_Checkout/js/model/quote',
'Magento_Checkout/js/checkout-data',
'Magento_Checkout/js/model/full-screen-loader',
'Magento_Checkout/js/model/url-builder',
'mage/validation'
], function ($, Component, ko, storage, customer, checkEmailAvailability, loginAction, quote, checkoutData, fullScreenLoader, urlBuilder) {
'use strict';

var validatedEmail;

if (!checkoutData.getValidatedEmailValue() &&
window.checkoutConfig.validatedEmailValue
) {
checkoutData.setInputFieldEmailValue(window.checkoutConfig.validatedEmailValue);
checkoutData.setValidatedEmailValue(window.checkoutConfig.validatedEmailValue);
}

validatedEmail = checkoutData.getValidatedEmailValue();

if (validatedEmail && !customer.isLoggedIn()) {
quote.guestEmail = validatedEmail;
}

return Component.extend({
defaults: {
template: 'Magento_Checkout/form/element/email',
email: checkoutData.getInputFieldEmailValue(),
emailFocused: false,
isLoading: false,
isPasswordVisible: false,
listens: {
email: 'emailHasChanged',
emailFocused: 'validateEmail'
},
ignoreTmpls: {
email: true
}
},
checkDelay: 2000,
checkRequest: null,
isEmailCheckComplete: null,
isCustomerLoggedIn: customer.isLoggedIn,
forgotPasswordUrl: window.checkoutConfig.forgotPasswordUrl,
emailCheckTimeout: 0,

/**
* Initializes regular properties of instance.
*
* @returns {Object} Chainable.
*/
initConfig: function () {
this._super();

this.isPasswordVisible = this.resolveInitialPasswordVisibility();

return this;
},

/**
* Initializes observable properties of instance
*
* @returns {Object} Chainable.
*/
initObservable: function () {
this._super()
.observe(['email', 'emailFocused', 'isLoading', 'isPasswordVisible']);

return this;
},

/**
* Callback on changing email property
*/
emailHasChanged: function () {
var self = this;

clearTimeout(this.emailCheckTimeout);

if (self.validateEmail()) {
quote.guestEmail = self.email();
checkoutData.setValidatedEmailValue(self.email());
if (!isCustomerLoggedIn) {
var serviceUrl = urlBuilder.createUrl('/gr4vy-payment/set-guest-email', {});
var payload = {
cartId: quote.getQuoteId(),
email: self.email(),
};
storage.post(
serviceUrl,
JSON.stringify(payload)
).done(
function (response) {
console.log("guest email", response);

}
).fail(
function (response) {
console.log("guest email error", response);
}
);
}

}
this.emailCheckTimeout = setTimeout(function () {
if (self.validateEmail()) {
self.checkEmailAvailability();
} else {
self.isPasswordVisible(false);
}
}, self.checkDelay);

checkoutData.setInputFieldEmailValue(self.email());
},

/**
* Check email existing.
*/
checkEmailAvailability: function () {
this.validateRequest();
this.isEmailCheckComplete = $.Deferred();
this.isLoading(true);
this.checkRequest = checkEmailAvailability(this.isEmailCheckComplete, this.email());

$.when(this.isEmailCheckComplete).done(function () {
this.isPasswordVisible(false);
checkoutData.setCheckedEmailValue('');
}.bind(this)).fail(function () {
this.isPasswordVisible(true);
checkoutData.setCheckedEmailValue(this.email());
}.bind(this)).always(function () {
this.isLoading(false);
}.bind(this));
},

/**
* If request has been sent -> abort it.
* ReadyStates for request aborting:
* 1 - The request has been set up
* 2 - The request has been sent
* 3 - The request is in process
*/
validateRequest: function () {
if (this.checkRequest != null && $.inArray(this.checkRequest.readyState, [1, 2, 3])) {
this.checkRequest.abort();
this.checkRequest = null;
}
},

/**
* Local email validation.
*
* @param {Boolean} focused - input focus.
* @returns {Boolean} - validation result.
*/
validateEmail: function (focused) {
var loginFormSelector = 'form[data-role=email-with-possible-login]',
usernameSelector = loginFormSelector + ' input[name=username]',
loginForm = $(loginFormSelector),
validator,
valid;

loginForm.validation();

if (focused === false && !!this.email()) {
valid = !!$(usernameSelector).valid();

if (valid) {
$(usernameSelector).removeAttr('aria-invalid aria-describedby');
}

return valid;
}

if (loginForm.is(':visible')) {
validator = loginForm.validate();

return validator.check(usernameSelector);
}

return true;
},

/**
* Log in form submitting callback.
*
* @param {HTMLElement} loginForm - form element.
*/
login: function (loginForm) {
var loginData = {},
formDataArray = $(loginForm).serializeArray();

formDataArray.forEach(function (entry) {
loginData[entry.name] = entry.value;
});

if (this.isPasswordVisible() && $(loginForm).validation() && $(loginForm).validation('isValid')) {
fullScreenLoader.startLoader();
loginAction(loginData).always(function () {
fullScreenLoader.stopLoader();
});
}
},

/**
* Resolves an initial state of a login form.
*
* @returns {Boolean} - initial visibility state.
*/
resolveInitialPasswordVisibility: function () {
if (checkoutData.getInputFieldEmailValue() !== '' && checkoutData.getCheckedEmailValue() !== '') {
return true;
}

if (checkoutData.getInputFieldEmailValue() !== '') {
return checkoutData.getInputFieldEmailValue() === checkoutData.getCheckedEmailValue();
}

return false;
}
});
});

0 comments on commit d4cebc9

Please sign in to comment.