diff --git a/cartridges/int_subscribe_pro_sfra/cartridge/controllers/Address.js b/cartridges/int_subscribe_pro_sfra/cartridge/controllers/Address.js index b47d6a6..78e9c85 100644 --- a/cartridges/int_subscribe_pro_sfra/cartridge/controllers/Address.js +++ b/cartridges/int_subscribe_pro_sfra/cartridge/controllers/Address.js @@ -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'); @@ -50,12 +48,11 @@ 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; @@ -63,91 +60,66 @@ server.replace('SaveAddress', csrfProtection.validateAjaxRequest, function (req, 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(); }); diff --git a/cartridges/int_subscribe_pro_sfra/cartridge/controllers/PaymentInstruments.js b/cartridges/int_subscribe_pro_sfra/cartridge/controllers/PaymentInstruments.js index 79e65e7..2af9d0a 100644 --- a/cartridges/int_subscribe_pro_sfra/cartridge/controllers/PaymentInstruments.js +++ b/cartridges/int_subscribe_pro_sfra/cartridge/controllers/PaymentInstruments.js @@ -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(); }); diff --git a/cartridges/int_subscribe_pro_sfra/cartridge/templates/default/account/addressBook.isml b/cartridges/int_subscribe_pro_sfra/cartridge/templates/default/account/addressBook.isml index 29fe2b0..921c0db 100644 --- a/cartridges/int_subscribe_pro_sfra/cartridge/templates/default/account/addressBook.isml +++ b/cartridges/int_subscribe_pro_sfra/cartridge/templates/default/account/addressBook.isml @@ -75,7 +75,7 @@ var updatedAddress = ""; - AddressBookAssist.onAddressUpdated(updatedAddress); + AddressBookAssist.onAddressUpdated(JSON.parse(updatedAddress)); var deletedAddress = "";