Skip to content

Instantly share code, notes, and snippets.

@erickertz
Created December 22, 2014 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erickertz/1317b5f770a4c9ed3f41 to your computer and use it in GitHub Desktop.
Save erickertz/1317b5f770a4c9ed3f41 to your computer and use it in GitHub Desktop.
/*jshint jquery:true, browser:true, globalstrict:true, newcap: false, quotmark: single, unused:true, camelcase: true, eqeqeq: true, latedef: true, undef: true, maxlen: 80, indent: 2, trailing: true*/
/*global angular, angularCartApp */
(function() {
'use strict';
angular.module('receipt', [])
.controller('ReceiptCtrl', ['$rootScope','$scope', '$location', '$cacheFactory', '$window', 'ReceiptData', 'UOHttp', 'Tracking',
function($rootScope,$scope, $location, $cacheFactory, $window, ReceiptData, UOHttp, Tracking) {
var loadMessaging = function() {
if (!$rootScope.messaging) {
ConfigurableMessaging.getMessaging().then(function(data) {
$rootScope.messaging = data;
});
}
};
loadMessaging();
$scope.currentBreadcrumb = 4;
$scope.isGuest = false;
$scope.displayGiftWrap = false;
$scope.addEmail = false;
if($scope.userData && $scope.isUrbanUS($scope.userData.siteId)) {
$scope.addEmail = true;
}
var orderHistoryBaseUrl = (($scope.messaging && $scope.messaging.OrderHistory) ? $scope.messaging.OrderHistory.redirectUrl : "");
$scope.orderHistoryUrl = $rootScope.site_code_context + $rootScope.mobile_context_root + orderHistoryBaseUrl;
if ($location.path().toLowerCase().indexOf('receipt') > 0)
$scope.receiptPage = true;
$scope.receiptPage = true;
//Create order date for display purposes only
var date = new Date();
var curDate = ("0" + date.getDate()).slice(-2);
var curMonth = ("0" + (date.getMonth() + 1)).slice(-2);
var curYear = date.getFullYear();
if ($rootScope.userData && ($rootScope.userData.siteId !== null && ($rootScope.userData.siteId == 'UrbanOutfitters-DE' || $rootScope.userData.siteId == 'UrbanOutfitters-UK' || $rootScope.userData.siteId == 'UrbanOutfitters-FR' ))) {
$scope.orderDate = curDate + "." + curMonth + "." + curYear;
} else {
$scope.orderDate = curMonth + "." + curDate + "." + curYear;
}
$scope.orderNumber = ReceiptData.get('orderNumber');
$scope.orderEmail = ReceiptData.get('orderEmail');
$scope.shippingMethods = ReceiptData.get('shippingMethods');
$scope.isGuest = ReceiptData.get('isGuest');
$scope.profileId = ReceiptData.get('profileId');
$scope.hasLoyalty = ReceiptData.get('hasLoyalty');
$scope.tltuid = getCookie('TLTUID');
var determineSatisfactionData = function() {
if ($rootScope.userData && $rootScope.userData.siteId) {
if ($rootScope.isUrban($rootScope.userData.siteId)) {
if ($scope.messaging && $scope.messaging.Receipt && $scope.messaging.Receipt.Satisfaction) {
if ($rootScope.is_mobile_cart) {
if ($scope.messaging.Receipt.Satisfaction.Mobile && $scope.messaging.Receipt.Satisfaction.Mobile.Link
&& $scope.messaging.Receipt.Satisfaction.Mobile.Link.URL && $scope.messaging.Receipt.Satisfaction.Mobile.Link.Text) {
$scope.satisfaction = {
"URL": $scope.messaging.Receipt.Satisfaction.Mobile.Link.URL + $scope.orderEmail + '&custom_var=TLTUID:' + $scope.tltuid + '|email:' + $scope.orderEmail,
"Text": $scope.messaging.Receipt.Satisfaction.Mobile.Link.Text
};
}
} else {
if ($scope.messaging.Receipt.Satisfaction.Desktop && $scope.messaging.Receipt.Satisfaction.Desktop.Link
&& $scope.messaging.Receipt.Satisfaction.Desktop.Link.URL && $scope.messaging.Receipt.Satisfaction.Desktop.Link.Text) {
$scope.satisfaction = {
"URL": $scope.messaging.Receipt.Satisfaction.Desktop.Link.URL + $scope.orderEmail + '&custom_var=TLTUID:' + $scope.tltuid + '|email:' + $scope.orderEmail,
"Text": $scope.messaging.Receipt.Satisfaction.Desktop.Link.Text
};
}
}
}
}
}
};
$scope.$watch("messaging", function(){
if($scope.messaging){
var orderHistoryBaseUrl = (($scope.messaging && $scope.messaging.OrderHistory) ? $scope.messaging.OrderHistory.redirectUrl : "");
$scope.orderHistoryUrl = $rootScope.site_code_context + $rootScope.mobile_context_root + orderHistoryBaseUrl;
if(!(typeof $scope.messaging.Receipt === 'undefined')) {
if ($scope.messaging.Receipt.redirectUrl !== ""){
window.location.href=$scope.messaging.Receipt.redirectUrl+$scope.orderNumber;
}
}
determineSatisfactionData();
}
});
setTimeout(function(){
//trigger order completion for other pages
//if ($rootScope.is_mobile_cart) {
jQuery(window).trigger("orderComplete");
/*
} else {
var bagText = !(typeof jQuery('#bagTextLink').html() === 'undefined') ? jQuery('#bagTextLink').html().split('(')[0] : "";
jQuery('#bagTextLink').html(bagText);
}
*/
if ((typeof ReceiptData.get('orderNumber') === 'undefined')) {
window.location.href = $scope.orderHistoryUrl;
} else if (Tracking === false || $scope.cartData.commerceItems.length === 0) {
window.location.href = $scope.orderHistoryUrl;
}
}, 20);
var cartData = $scope.currentOrder;
$scope.cartData = cartData;
$scope.setCurrentOrder(cartData);
angular.forEach(cartData.commerceItems, function(item) {
item = giftCardCheck(item);
});
//Clears cookie that displays items in bag
clearCookie('UOCCI10', '""');
clearCookie('UOCCII:', '""');
//If user is guest we need to delete a few more cookies...
if ($scope.isGuest){
//temporary fix for issue of Urban production having 2 JSESSION cookies
var expires = new Date();
expires.setYear(expires.getYear() - 1);
document.cookie = "JSESSIONID=null; expires=" + expires + "; domain=.urbanoutfitters.com; path=/";
clearCookie('JSESSIONID');
clearCookie('cmTPSet');
clearCookie('lastCatURL');
clearCookie('visitCount', '0');
clearCookie('items', '0');
//clearCookie('Local storage');
}
$scope.setStateAddress = function(state){
if (state){
return state;
}
};
/*
$scope.createAccount = function(guestPassword, guestPasswordConfirm) {
$scope.error = "";
$scope.isPasswordConfirmValid = true;
$scope.isPasswordValid = true;
if (guestPassword == null || guestPassword.length < 3){
$scope.isPasswordValid = false;
$scope.error = "Please enter a valid password";
}
else if (guestPasswordConfirm == null || guestPasswordConfirm.length < 3 || guestPassword != guestPasswordConfirm){
$scope.isPasswordConfirmValid = false;
$scope.error = "Passwords must match."
}
else{
$scope.error = "";
UOHttp({
method: 'POST',
url: '/checkout/api/user/' + $scope.profileId + '/convertToRegistered',
data: {
"confirmPassword": guestPasswordConfirm,
"sterlingOrderNumber": $scope.orderNumber,
"password": guestPassword
}
}).then(function(res) {
//Once account is created, initiate login.
UOHttp({
method: 'POST',
url: '/rest/bean/uo/commerce/integrations/ProfileIntegrationService/handleLogin?atg-rest-depth=7',
headers : {
'Content-Type':'application/x-www-form-urlencoded'
},
data: 'arg1=' + $scope.orderEmail + '&arg2='+ guestPassword + '&arg3=""'
}).then(function(res) {
var redUrl = $rootScope.is_mobile_cart ? $scope.messaging.MyAccount.Mobile.redirectUrl : $scope.messaging.MyAccount.redirectUrl
$window.location = redUrl;
});
});
}
};
*/
$scope.uoonSignup = function(userName, gender) {
$scope.hasLoyaltySuccess = false;
$scope.error = "";
if (userName == null || userName == 'undefined' || gender == null || gender == 'undefined'){
if (userName == null || userName == 'undefined'){
$scope.noUsername = false;
$scope.error += "<p class='error'>Please enter a username</p>";
}
if (gender == null || gender == 'undefined'){
$scope.noGender = false;
$scope.error += "<p class='error'>Please choose a gender</p>"
}
} else {
$scope.error = "";
UOHttp({
method: 'POST',
url: '/customer/api/user/createloyalty',
data: {
"loyaltyUsername": userName,
"gender": gender,
"signUpChannel": $rootScope.is_mobile_cart ? ($rootScope.is_app_cart ? "APP" : "Mobile") : "WEB"
}
}).then(function(res) {
if (!(typeof res.errors === 'undefined') && res.errors) {
for (var i = 0; i < res.errors.length; i++) {
switch (res.errors[i].message) {
case "uo.droplet.DropletResources->userNotRegistered" :
$scope.error = "<p class='error'>You need to sign up with a UO account before you can sign up for Urban On.</p>";
break;
case "uo.droplet.DropletResources->loyaltyUsernameExist" :
$scope.error = "<p class='error'>The user name, \""+userName+"\" is already taken. Please choose something else.</p>";
break;
case "uo.droplet.DropletResources->genderNotSelected" :
$scope.error = "<p class='error'>Please select a gender!</p>";
break;
case "uo.droplet.DropletResources->loyaltyServiceDown" :
$scope.error = "<p class='error'>The signup for Urban On currently not working. Please try again later.</p>";
break;
case "uo.droplet.DropletResources->loyaltyUserAreadyRegistered" :
$scope.error = "<p class='error'>You have already registerd for Urban On, "+userName+".</p>";
break;
default:
$scope.error = "<p class='error'>An error has occured. Please try again.</p>"
}
}
return false;
} else if (res.status == 200){
$scope.hasLoyaltySuccess = true;
$scope.error = '<div class="inner loyalty-form" align="center"><h4>Congrats!<br />You turned URBAN On!</h4><img src="/images/on_get_the_app.png" alt="Get the app"><h4>Download our iPhone App to get the most out of Urban ON!</h4></div>';
} else {
$scope.error = "<p class='error'>An error has occured. Please try again.</p>";
}
return false;
});
}
};
$scope.createAccount = function(guestPassword, guestPasswordConfirm, addUoOn, userName, gender, addEmail) {
$scope.error = "";
$scope.isPasswordConfirmValid = true;
$scope.isPasswordValid = true;
if (guestPassword == null || guestPassword.length < 3) {
$scope.isPasswordValid = false;
$scope.error += "<p>Please enter a valid password</p>";
}
if (guestPasswordConfirm == null || guestPasswordConfirm.length < 3 || guestPassword != guestPasswordConfirm) {
$scope.isPasswordConfirmValid = false;
$scope.error += "<p>Passwords must match.</p>";
}
if (addUoOn) {
if (userName == null || userName == 'undefined' || gender == null || gender == 'undefined' || userName == "" || gender == ""){
if (userName == null || userName == 'undefined' || userName == ""){
$scope.noUsername = false;
$scope.error += "<p class='error'>Please enter a username</p>";
}
if (gender == null || gender == 'undefined'|| gender == ""){
$scope.noGender = false;
$scope.error += "<p class='error'>Please choose a gender</p>";
}
}
}
if (addEmail != true) {
addEmail = false;
}
if ($scope.error.length < 1) {
UOHttp({
method: 'POST',
url: '/checkout/api/user/' + $scope.profileId + '/convertToRegistered',
data: {
"confirmPassword": guestPasswordConfirm,
"sterlingOrderNumber": $scope.orderNumber,
"password": guestPassword,
"loyaltyUsername": addUoOn ? ((typeof userName === "undefined" || userName == null) ? "" : userName) : "",
"gender": addUoOn ? ((typeof gender === "undefined" || gender == null) ? "" : gender) : "",
"signUpChannel": addUoOn ? ($rootScope.is_mobile_cart ? ($rootScope.is_app_cart ? "APP" : "Mobile") : "WEB") : ""
}
}).then(function(res) {
if (!(typeof res.errors === 'undefined') && res.errors) {
for (var i = 0; i < res.errors.length; i++) {
switch (res.errors[i].message) {
case "uo.droplet.DropletResources->userNotRegistered" :
$scope.error = "<p class='error'>You need to sign up with a UO account before you can sign up for Urban On.</p>";
break;
case "uo.droplet.DropletResources->loyaltyUsernameExist" :
$scope.error = "<p class='error'>The user name, \""+userName+"\" is already taken. Please choose something else.</p>";
break;
case "uo.droplet.DropletResources->genderNotSelected" :
$scope.error = "<p class='error'>Please select a gender!</p>";
break;
case "uo.droplet.DropletResources->loyaltyServiceDown" :
$scope.error = "<p class='error'>The signup for Urban On currently not working. Please try again later.</p>";
break;
case "uo.droplet.DropletResources->loyaltyUserAreadyRegistered" :
$scope.error = "<p class='error'>You have already registerd for Urban On, "+userName+".</p>";
break;
default:
$scope.error = "<p class='error'>An error has occured. Please try again.</p>"
}
}
return false;
} else {
//Once account is created, initiate login.
UOHttp({
method: 'POST',
url: '/rest/bean/uo/commerce/integrations/ProfileIntegrationService/handleLogin?atg-rest-depth=7',
headers : {
'Content-Type':'application/x-www-form-urlencoded'
},
data: 'arg1=' + $scope.orderEmail + '&arg2='+ guestPassword + '&arg3=""'
}).then(function(res) {
var redUrl = $rootScope.site_code_context
+ ($rootScope.is_mobile_cart ? $scope.messaging.MyAccount.Mobile.redirectUrl : $scope.messaging.MyAccount.redirectUrl);
$window.location = redUrl;
});
}
});
}
return false;
};
//end of source file: src/controllers/receipt.js
}]);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment