Skip to content

Commit

Permalink
Use append/prepend instead of replace in SFRA controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
imkingdavid committed Oct 24, 2019
1 parent ccb4853 commit 13b0648
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 150 deletions.
154 changes: 63 additions & 91 deletions cartridges/int_subscribe_pro_sfra/cartridge/controllers/Address.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

var server = require('server');

var URLUtils = require('dw/web/URLUtils');
var Resource = require('dw/web/Resource');
var csrfProtection = require('*/cartridge/scripts/middleware/csrf');
var userLoggedIn = require('*/cartridge/scripts/middleware/userLoggedIn');
var consentTracking = require('*/cartridge/scripts/middleware/consentTracking');
Expand Down Expand Up @@ -50,104 +48,78 @@ server.get('SetSPAddressID', function (req, res, next) {
next();
});

server.replace('SaveAddress', csrfProtection.validateAjaxRequest, function (req, res, next) {
server.prepend('SaveAddress', csrfProtection.validateAjaxRequest, function (req, res, next) {
if (!subproEnabled) {
return next();
}
var CustomerMgr = require('dw/customer/CustomerMgr');
var Transaction = require('dw/system/Transaction');
var formErrors = require('*/cartridge/scripts/formErrors');
var accountHelpers = require('*/cartridge/scripts/helpers/accountHelpers');

var addressForm = server.forms.getForm('address');
var addressFormObj = addressForm.toObject();
addressFormObj.addressForm = addressForm;
var customer = CustomerMgr.getCustomerByCustomerNumber(
req.currentCustomer.profile.customerNo
);
var addressBook = customer.getProfile().getAddressBook();
if (addressForm.valid) {
res.setViewData(addressFormObj);
this.on('route:BeforeComplete', function () { // eslint-disable-line no-shadow
var formInfo = res.getViewData();
Transaction.wrap(function () {
var isNewAddress = !req.querystring.addressId;
var address = !isNewAddress
? addressBook.getAddress(req.querystring.addressId)
: addressBook.createAddress(formInfo.addressId);
if (address) {
if (!isNewAddress && subproEnabled) {
session.privacy.updatedOldAddress = {
sp: addressHelper.getSubproAddress(address, session.customer.profile, true, true),
sfcc: address
};
}

if (req.querystring.addressId) {
address.setID(formInfo.addressId);
}

address.setAddress1(formInfo.address1 || '');
address.setAddress2(formInfo.address2 || '');
address.setCity(formInfo.city || '');
address.setFirstName(formInfo.firstName || '');
address.setLastName(formInfo.lastName || '');
address.setPhone(formInfo.phone || '');
address.setPostalCode(formInfo.postalCode || '');

if (formInfo.states && formInfo.states.stateCode) {
address.setStateCode(formInfo.states.stateCode);
}

if (formInfo.country) {
address.setCountryCode(formInfo.country);
}

address.setJobTitle(formInfo.jobTitle || '');
address.setPostBox(formInfo.postBox || '');
address.setSalutation(formInfo.salutation || '');
address.setSecondName(formInfo.secondName || '');
address.setCompanyName(formInfo.companyName || '');
address.setSuffix(formInfo.suffix || '');
address.setSuite(formInfo.suite || '');
address.setJobTitle(formInfo.title || '');

// Send account edited email
accountHelpers.sendAccountEditedEmail(customer.profile);

if (subproEnabled) {
var spAddress = addressHelper.getSubproAddress(address, session.customer.profile, false, true);
if (isNewAddress) {
session.privacy.newAddress = {
sp: spAddress,
sfcc: address
};
} else {
session.privacy.updatedNewAddress = {
sp: spAddress,
sfcc: address
};
}
}

res.json({
success: true,
redirectUrl: URLUtils.url('Address-List').toString()
});
} else {
formInfo.addressForm.valid = false;
formInfo.addressForm.addressId.valid = false;
formInfo.addressForm.addressId.error = Resource.msg('error.message.idalreadyexists', 'forms', null);
res.json({
success: false,
fields: formErrors.getFormErrors(addressForm)
});
}
});
});
} else {
res.json({
success: false,
fields: formErrors.getFormErrors(addressForm)
});

// Make sure our append() function knows whether we're creating or updating
var isNewAddress = !req.querystring.addressId;
session.privacy.spUpdateAddress = !isNewAddress;
if (isNewAddress) {
return next();
}

var address = addressBook.getAddress(req.querystring.addressId);

if (!address) {
return next();
}

session.privacy.updatedOldAddress = {
sp: addressHelper.getSubproAddress(address, session.customer.profile, true, true),
sfcc: address
};

return next();
});

server.append('SaveAddress', csrfProtection.validateAjaxRequest, function (req, res, next) {
this.on('route:Complete', function () {
if (!subproEnabled) {
return next();
}
var CustomerMgr = require('dw/customer/CustomerMgr');
var addressForm = server.forms.getForm('address');
var addressFormObj = addressForm.toObject();
addressFormObj.addressForm = addressForm;
var customer = CustomerMgr.getCustomerByCustomerNumber(
req.currentCustomer.profile.customerNo
);
var addressBook = customer.getProfile().getAddressBook();

var addressId = req.querystring.addressId ? req.querystring.addressId : addressFormObj.addressId;
if (!addressId) {
return next();
}
var address = addressBook.getAddress(addressId);
if (!address) {
return next();
}

var spAddress = addressHelper.getSubproAddress(address, session.customer.profile, false, true);
if (session.privacy.spUpdateAddress) {
session.privacy.updatedNewAddress = {
sp: spAddress,
sfcc: address
};
} else {
session.privacy.newAddress = {
sp: spAddress,
sfcc: address
};
}

session.privacy.spUpdateAddress = null;
});
return next();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,65 +143,36 @@ server.get('SetSPPaymentProfileID', function (req, res, next) {
next();
});

server.replace('SavePayment', csrfProtection.validateAjaxRequest, function (req, res, next) {
var formErrors = require('*/cartridge/scripts/formErrors');
var HookMgr = require('dw/system/HookMgr');
var PaymentMgr = require('dw/order/PaymentMgr');
var dwOrderPaymentInstrument = require('dw/order/PaymentInstrument');
var accountHelpers = require('*/cartridge/scripts/helpers/accountHelpers');

var paymentForm = server.forms.getForm('creditCard');
var result = getDetailsObject(paymentForm);

if (paymentForm.valid && !verifyCard(result, paymentForm)) {
res.setViewData(result);
this.on('route:BeforeComplete', function (req, res) { // eslint-disable-line no-shadow
var URLUtils = require('dw/web/URLUtils');
var CustomerMgr = require('dw/customer/CustomerMgr');
var Transaction = require('dw/system/Transaction');

var formInfo = res.getViewData();
var customer = CustomerMgr.getCustomerByCustomerNumber(
req.currentCustomer.profile.customerNo
);
var wallet = customer.getProfile().getWallet();

Transaction.wrap(function () {
var paymentInstrument = wallet.createPaymentInstrument(dwOrderPaymentInstrument.METHOD_CREDIT_CARD);
paymentInstrument.setCreditCardHolder(formInfo.name);
paymentInstrument.setCreditCardNumber(formInfo.cardNumber);
paymentInstrument.setCreditCardType(formInfo.cardType);
paymentInstrument.setCreditCardExpirationMonth(formInfo.expirationMonth);
paymentInstrument.setCreditCardExpirationYear(formInfo.expirationYear);

var processor = PaymentMgr.getPaymentMethod(dwOrderPaymentInstrument.METHOD_CREDIT_CARD).getPaymentProcessor();
var token = HookMgr.callHook(
'app.payment.processor.' + processor.ID.toLowerCase(),
'createToken'
);

paymentInstrument.setCreditCardToken(token);

session.privacy.newCard = {
sp: paymentsHelper.getSubscriptionPaymentProfile(session.customer.profile, paymentInstrument, {}, false),
sfcc: paymentInstrument
};
});

// Send account edited email
accountHelpers.sendAccountEditedEmail(customer.profile);

res.json({
success: true,
redirectUrl: URLUtils.url('PaymentInstruments-List').toString()
});
});
} else {
res.json({
success: false,
fields: formErrors.getFormErrors(paymentForm)
});
server.append('SavePayment', csrfProtection.validateAjaxRequest, function (req, res, next) {
var CustomerMgr = require('dw/customer/CustomerMgr');
if (!subproEnabled) {
return next();
}
this.on('route:Complete', function (req, res) { // eslint-disable-line no-shadow
var viewData = res.getViewData();
var cardNum = viewData.cardNumber;
var last4 = cardNum.substring(cardNum.length - 4);
var customer = CustomerMgr.getCustomerByCustomerNumber(
req.currentCustomer.profile.customerNo
);
var wallet = customer.getProfile().getWallet();
var savedCard = null;
var savedCards = wallet.getPaymentInstruments('CREDIT_CARD');
for (var i = 0; i < savedCards.length; i++) {
if (savedCards[i].getCreditCardNumberLastDigits() == last4) {
savedCard = savedCards[i];
break;
}
}
if (!savedCard) {
return next();
}

session.privacy.newCard = {
sp: paymentsHelper.getSubscriptionPaymentProfile(session.customer.profile, savedCard, {}, false),
sfcc: savedCard
};
});
return next();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
</isif>
<isif condition="${pdict.updatedAddress != 'null'}">
var updatedAddress = "<isprint value="${pdict.updatedAddress}" encoding="jsblock" />";
AddressBookAssist.onAddressUpdated(updatedAddress);
AddressBookAssist.onAddressUpdated(JSON.parse(updatedAddress));
</isif>
<isif condition="${pdict.deletedAddress != 'null'}">
var deletedAddress = "<isprint value="${pdict.deletedAddress}" encoding="jsblock" />";
Expand Down

0 comments on commit 13b0648

Please sign in to comment.