cobuild-angular-stack
Version:
Base stack angular sass jade gulp
796 lines (722 loc) • 33.5 kB
JavaScript
(function (module) {
'use strict';
module.controller('CoupleCheckoutController', CoupleCheckoutController);
module.controller('CoupleCheckoutModalController', CoupleCheckoutModalController);
module.controller('SuccessCoupleCheckoutController', SuccessCoupleCheckoutController);
CoupleCheckoutController.$inject = ['$scope', '$rootScope', '$state', '$stateParams', 'CoupleAccount', 'Guest', 'toastr', '$localStorage', 'countries', '$timeout', '$analytics', '$uibModal', '$translate', 'Auth', 'lodash', 'Code', 'DiscountCode'];
function CoupleCheckoutController($scope, $rootScope, $state, $stateParams, CoupleAccount, Guest, toastr, $localStorage, countries, $timeout, $analytics, $uibModal, $translate, Auth, lodash, Code, DiscountCode) {
var ccCtrl = this;
var account;
$('body,html').animate({scrollTop : 0}, 200);
$scope.mercadoMedios = {};
$scope.selectPay = "";
$scope.form = {
fullname: {
isInvalid: false,
description: ""
}
}
this.getCode = function() {
var account_params = Auth.getCurrent();;
Code.find({
filter: {
where: {
coupleAccountId: String(account_params.id),
},
include: 'discountCode'
}
}).$promise.then(function(code) {
if(code.length > 0) {
ccCtrl.code = _.first(code);
ccCtrl.isCode = true;
} else {
ccCtrl.isCode = false;
}
}).catch(function(err) {
console.log(err);
});
};
this.sendCode = function() {
if(!ccCtrl.discountCode) return toastr.error("Capture código");
DiscountCode.applyCode({coupleId: account.id, code: ccCtrl.discountCode }).$promise.then(function(data) {
ccCtrl.isActiveCode = false;
ccCtrl.isCode = true;
ccCtrl.code = data.code;
ccCtrl.code.discountCode = data;
if(data.account) {
var userAccount = data.account;
$rootScope.fullAccount = $localStorage.fullAccount = userAccount.payInformationData.isPaid;
$scope.account = userAccount;
$scope.disabled = false;
$scope.order = userAccount.payInformationData;
$scope.pay = {
month: null,
year: null
};
$scope.payer = {};
$scope.paymentInfo = {};
$state.go('homeUserDiscount', {descountId: ccCtrl.code.discountCode.id});
}
}, function(err) {
console.log(err);
toastr.error(err.data.error.message);
}).catch(function(err) {
toastr.error(JSON.stringify(err));
});
}
ccCtrl.priceToDiscount = function(price) {
var code = ccCtrl.code;
if(ccCtrl.isCode) {
switch(code.discountCode.type) {
case 'amount':
price = price - code.discountCode.amount
break;
case 'percent':
price = price - ((price * code.discountCode.amount) / 100);
break;
}
} else if($rootScope.paymentDiscount > 0) {
ccCtrl.isDiscount = 1;
price = price - ((price * $rootScope.paymentDiscount) / 100);
}
return price < 0 ? 0 : price;
}
var findMercadoMedios = function(){
console.log('findMercadoMedios');
Guest.getMercadoPayments(function (medios) {
console.log('Medios = ', medios);
$scope.mercadoMedios = medios;
console.log('Medios 2 = ', $scope.mercadoMedios);
});
};
findMercadoMedios();
$scope.templates = {
prePay: 'partials/auth.pay.prepay.html',
fixedPrice: 'partials/auth.pay.fixedprice.html',
postPay: 'partials/auth.pay.postpay.html'
};
$scope.currentTemplate = "";
this.selectPercentagePay = function () {
var modalInstance = $uibModal.open({
animation: true,
templateUrl: 'partials/auth.pay.percentprice.html',
controller: ['$scope', '$uibModalInstance', function ($scope, $uibModalInstance) {
$scope.account = account;
$scope.pay = function () {
$uibModalInstance.close();
};
$scope.dismiss = function () {
$uibModalInstance.dismiss();
};
}]
});
modalInstance.result
.then(function () {
$scope.paymentInfo.method = 'percent';
ccCtrl.pay();
});
};
this.selectFixedPay = function () {
$scope.currentTemplate = $scope.templates.fixedPrice;
};
this.goToSelectPaymentType = function () {
$scope.currentTemplate = $scope.templates.prePay;
};
$scope.$state = $state;
Auth.getCurrent()
.$promise
.then(function (coupleAccount) {
account = $scope.account = coupleAccount;
$scope.order = coupleAccount.payInformationData;
$scope.disabled = false;
ccCtrl.getCode();
if ($scope.order.isPaid) {
$scope.currentTemplate = $scope.templates.postPay;
} else {
$scope.currentTemplate = $scope.templates.prePay;
}
if ($stateParams.startOn) {
switch ($stateParams.startOn) {
case 'fixedPrice':
$scope.selectPay = $stateParams.amount;
$scope.selectPayId = $stateParams.id;
ccCtrl.selectFixedPay();
break;
case 'payPerPercentage':
ccCtrl.selectPercentagePay();
break;
}
}
});
$scope.birthdate = {};
$scope.address = {};
var currentYear = new Date().getFullYear();
$scope.months = lodash.range(1, 13);
$scope.years = lodash.range(currentYear, currentYear + 11);
$scope.birthyears = lodash.range(currentYear - 90, currentYear - 17).reverse();
$scope.countries = countries;
$scope.method = 'cc';
$scope.getDaysArray = function (year, month) {
var date = new Date(year, month - 1, 1);
var result = [];
while (date.getMonth() == month - 1) {
result.push(date.getDate());
date.setDate(date.getDate() + 1);
}
return result;
};
$scope.pay = {
month: null,
year: null
};
$scope.payer = {};
$scope.paymentInfo = {};
$scope.disabled = true;
$scope.order = null;
this.prePay = function () {
var selectPay = $scope.dataUtils.payPrice.fixed;
var selectPayId = $scope.dataUtils.payPrice.fixedId;
$state.go('homeUser.pay', {startOn: 'fixedPrice', amount: ccCtrl.priceToDiscount(selectPay), id: selectPayId, code: ccCtrl.code});
};
this.prePayComplete = function () {
var selectPay = $scope.dataUtils.payPrice.fixedComplete;
var selectPayId = $scope.dataUtils.payPrice.fixedCompleteId;
$state.go('homeUser.pay', {startOn: 'fixedPrice', amount: ccCtrl.priceToDiscount(selectPay), id: selectPayId, code: ccCtrl.code});
};
//codeId
this.validNumber = function(event) {
if(event.keyCode > 47 && event.keyCode < 58) {
return true;
} else {
return event.preventDefault();
}
}
this.typeCard = function() {
if($scope.paymentInfo.pay.number && $scope.paymentInfo.pay.number.length > 0 ) {
var number = $scope.paymentInfo.pay.number;
var init = $scope.paymentInfo.pay.number.slice(0,1)
switch(init) {
case "3":
$scope.paymentInfo.otro.pay_type = "amex";
break;
case "4":
$scope.paymentInfo.otro.pay_type = "visa";
break
case "5":
case "2":
$scope.paymentInfo.otro.pay_type = "mastercard";
break;
case "6":
$scope.paymentInfo.otro.pay_type = "discover";
break
default:
$scope.paymentInfo.otro.pay_type = "";
}
} else {
}
}
setInterval(function() {
var imgs = $(".loader-image-home:visible");
if(!$(".loading-init").hasClass('hidden')) {
if(imgs.length == 5) {
$(".loader-image-home").addClass("hidden")
} else {
$("#loader_"+(imgs.length + 1)).removeClass("hidden")
}
}
}, 1000);
this.hideField = function(field) {
$scope.form[field] = {
isInvalid: false,
description: ""
}
}
this.payValidate = function () {
var expEmai = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i
$scope.form = {
fullname: {
isInvalid: false,
description: ""
}
}
var isInvalid = false;
console.log($scope.paymentInfo);
if($.trim($scope.paymentInfo.payer.fullname).length == 0) {
$scope.form.fullname = {
isInvalid: true,
description: "El nombre es requerido"
}
isInvalid = true;
}
if($.trim($scope.paymentInfo.payer.email).length == 0) {
$scope.form.email = {
isInvalid: true,
description: "El correo electrónico es requerido"
}
isInvalid = true;
} else if(!expEmai.test($scope.paymentInfo.payer.email)) {
$scope.form.email = {
isInvalid: true,
description: "El email es invalido"
}
isInvalid = true;
}
if($.trim(ccCtrl.confirmEmail).length == 0) {
$scope.form.confirmEmail = {
isInvalid: true,
description: "El email de confirmación es requerido"
}
isInvalid = true;
} else if($scope.paymentInfo.payer.email != ccCtrl.confirmEmail) {
$scope.form.confirmEmail = {
isInvalid: true,
description: "El email de confirmación es incorrecto"
}
isInvalid = true;
}
if($.trim($scope.paymentInfo.payer.phone).length == 0) {
$scope.form.phone = {
isInvalid: true,
description: "El télefono de confirmación es requerido"
}
isInvalid = true;
} else if($scope.paymentInfo.payer.phone.length < 10) {
$scope.form.phone = {
isInvalid: true,
description: "El télefono es invalido"
}
isInvalid = true;
}
if($.trim($scope.paymentInfo.payer.city).length == 0) {
$scope.form.city = {
isInvalid: true,
description: "La ciudad es requerida"
}
isInvalid = true;
}
if($.trim($scope.paymentInfo.pay.number).length == 0) {
$scope.form.number = {
isInvalid: true,
description: "El número de tarjeta es requerido"
}
isInvalid = true;
} else if(($scope.paymentInfo.otro.pay_type == 'amex' && $.trim($scope.paymentInfo.pay.number).length != 15)) {
$scope.form.number = {
isInvalid: true,
description: "El número de tarjeta debe tener 15 dígitos"
}
isInvalid = true;
} else if(($scope.paymentInfo.otro.pay_type != 'amex' && $.trim($scope.paymentInfo.pay.number).length != 16)) {
$scope.form.number = {
isInvalid: true,
description: "El número de tarjeta debe tener 16 dígitos"
}
isInvalid = true;
}
if(!$scope.paymentInfo.pay.exp_month) {
$scope.form.exp_month = {
isInvalid: true,
description: "Falta el mes de expiración"
}
isInvalid = true;
}
if(!$scope.paymentInfo.pay.exp_year) {
$scope.form.exp_year = {
isInvalid: true,
description: "Falta el año de expiración"
}
isInvalid = true;
}
if(!$scope.paymentInfo.pay.cvc) {
$scope.form.pay_cvv = {
isInvalid: true,
description: "Falta el código de seguridad cvc"
}
isInvalid = true;
}
return isInvalid;
}
this.pay = function () {
$(".loading-init").removeClass("hidden");
$scope.disabled = true;
var paymentInfo = lodash.cloneDeep($scope.paymentInfo);
paymentInfo.birthdate = $scope.birthdate;
paymentInfo.urlRegistry = $scope.url;
paymentInfo.pay.name = paymentInfo.payer.fullname;
paymentInfo.address = $scope.address;
paymentInfo.products = lodash.map($scope.products, 'id');
paymentInfo.utilDataId = $scope.selectPayId;
var $promise;
if( paymentInfo.method === 'cc' && ccCtrl.payValidate()){
$(".loading-init").addClass("hidden");
return false;
}
if (paymentInfo.method === 'cc') {
Conekta.token.create({card: paymentInfo.pay}, function (token) {
paymentInfo.pay = token.id;
$analytics.eventTrack('Account Payment', {});
getMixpanel().people.set({
'Billing info': true
});
$scope.account.$prototype$makeCardPayment({paymentInfo: paymentInfo})
.then(function (userAccount) {
ga('send', {
hitType: 'event',
eventCategory: 'submit',
eventAction: 'Pago de Mesa',
eventLabel: 'Pago de Mesa'
});
ga('send', {
hitType: 'event',
eventCategory: 'submit',
eventAction: 'Pago de Mesa (Pago Fijo)',
eventLabel: 'Pago de Mesa (Pago Fijo)'
});
ga('send', {
hitType: 'event',
eventCategory: 'submit',
eventAction: 'Pago de Mesa (Pago con Tarjeta)',
eventLabel: 'Pago de Mesa (Pago con Tarjeta)'
});
$translate('success-payment').then(function (translation) {
toastr.success(translation);
});
$scope.currentTemplate = $scope.templates.postPay;
$rootScope.fullAccount = $localStorage.fullAccount = userAccount.payInformationData.isPaid;
$scope.account = userAccount;
$scope.disabled = false;
$scope.order = userAccount.payInformationData;
$scope.pay = {
month: null,
year: null
};
$scope.payer = {};
$scope.paymentInfo = {};
$('body,html').animate({scrollTop : 0}, 200);
})
.catch(function (err) {
if(!$(".loading-init").hasClass('hidden')) {
$(".loading-init").addClass("hidden");
}
if (err.status === 402) {
return toastr.error(err.data.error.message_to_purchaser);
}
})
.finally(function () {
if(!$(".loading-init").hasClass('hidden')) {
$(".loading-init").addClass("hidden");
}
$scope.disabled = false;
});
}, function (err) {
if(!$(".loading-init").hasClass('hidden')) {
$(".loading-init").addClass("hidden");
}
toastr.error(err.message_to_purchaser);
$scope.disabled = false;
});
}else if (paymentInfo.method === 'mercado') {
console.log('paymentInfo 1234 = ', paymentInfo, $scope.account);
var createCard = {
email : paymentInfo.payer.email,
cardNumber : paymentInfo.pay.number+'',
securityCode : paymentInfo.pay.cvc,
cardExpirationMonth : paymentInfo.pay.exp_month,
cardExpirationYear : paymentInfo.pay.exp_year,
cardholderName : paymentInfo.pay.name
};
console.log('createCard = ',createCard);
Mercadopago.clearSession();
Mercadopago.createToken(createCard, function(status, response){
console.log('status, response createToken ', status, response);
if (status != 200 && status != 201) {
$translate('mercado_'+response.cause[0].code).then(function (translation) {
toastr.error(translation);
});
$scope.disabled = false;
}else{
paymentInfo.first6 = response.first_six_digits;
paymentInfo.pay = response.id;
console.log('Todo Bien!!!');
//callMercado(paymentInfo, localStorage.orderId );
$analytics.eventTrack('Account Payment', {});
getMixpanel().people.set({
'Billing info': true
});
console.log('ASi quedo el pago = ',paymentInfo);
$scope.account
.$prototype$makeMercadoPagoPayment({paymentInfo: paymentInfo})
.then(function (userAccount) {
console.log('Respuesta ', userAccount );
ga('send', {
hitType: 'event',
eventCategory: 'submit',
eventAction: 'Pago de Mesa',
eventLabel: 'Pago de Mesa'
});
ga('send', {
hitType: 'event',
eventCategory: 'submit',
eventAction: 'Pago de Mesa (Pago Fijo)',
eventLabel: 'Pago de Mesa (Pago Fijo)'
});
ga('send', {
hitType: 'event',
eventCategory: 'submit',
eventAction: 'Pago de Mesa (Pago con Tarjeta)',
eventLabel: 'Pago de Mesa (Pago con Tarjeta)'
});
$translate('success-payment').then(function (translation) {
toastr.success(translation);
});
$scope.currentTemplate = $scope.templates.postPay;
$rootScope.fullAccount = $localStorage.fullAccount = userAccount.payInformationData.isPaid;
$scope.account = userAccount;
$scope.disabled = false;
$scope.order = userAccount.payInformationData;
$scope.pay = {
month: null,
year: null
};
$scope.payer = {};
$scope.paymentInfo = {};
})
.catch(function (err) {
if(!$(".loading-init").hasClass('hidden')) {
$(".loading-init").addClass("hidden");
}
console.log('callMercado Error ',err);
toastr.error(err.data.error.message);
$scope.disabled = false;
})
.finally(function () {
if(!$(".loading-init").hasClass('hidden')) {
$(".loading-init").addClass("hidden");
}
$scope.disabled = false;
});
}
});
}else if (paymentInfo.method === 'paypal') {
console.log("llega");
$scope.account
.$prototype$makePaypalPayment({paymentInfo: paymentInfo})
.then(function (data) {
window.location.href = data.url;
})
.catch(function (data) {
if (data.data.error.httpStatusCode === 400) {
var err = data.data.error.response.details;
lodash.forEach(err, function (e) {
toastr.error(e.issue);
});
}
})
.finally(function () {
if(!$(".loading-init").hasClass('hidden')) {
$(".loading-init").addClass("hidden");
}
$scope.disabled = false;
});
} else if (paymentInfo.method === 'percent') {
$scope.account
.$prototype$markPercentPayment({})
.then(function (userAccount) {
ga('send', {
hitType: 'event',
eventCategory: 'submit',
eventAction: 'Pago de Mesa',
eventLabel: 'Pago de Mesa'
});
ga('send', {
hitType: 'event',
eventCategory: 'submit',
eventAction: 'Pago de Mesa (Pago por Porcentaje)',
eventLabel: 'Pago de Mesa (Pago por Porcentaje)'
});
$translate('success-payment').then(function (translation) {
toastr.success(translation);
});
$scope.currentTemplate = $scope.templates.postPay;
$rootScope.fullAccount = $localStorage.fullAccount = userAccount.payInformationData.isPaid;
$scope.account = userAccount;
$scope.disabled = false;
$scope.order = userAccount.payInformationData;
$scope.pay = {
month: null,
year: null
};
$scope.payer = {};
$scope.paymentInfo = {};
$('body,html').animate({scrollTop : 0}, 200);
})
.catch(function (err) {
if(!$(".loading-init").hasClass('hidden')) {
$(".loading-init").addClass("hidden");
}
if (err.status === 402) {
return toastr.error(err.data.message);
}
})
.finally(function () {
if(!$(".loading-init").hasClass('hidden')) {
$(".loading-init").addClass("hidden");
}
$scope.disabled = false;
});
}
};
$scope.$on('$stateChangeSuccess', function () {
$scope.paymentInfo = {
method: 'cc',
pay: {
month: null,
year: null
},
otro:{
pay_type: ''
},
payer: {
country: 'MX'
}
};
});
}
SuccessCoupleCheckoutController.$inject = ['$scope', '$state', '$rootScope', '$location', 'CoupleAccount', '$localStorage', '$timeout', '$translate', 'toastr', 'Auth'];
function SuccessCoupleCheckoutController($scope, $state, $rootScope, $location, CoupleAccount, $localStorage, $timeout, $translate, toastr, Auth) {
$scope = $scope.$parent;
$scope.currentTemplate = "";
$scope.showHeader = false;
var search = $location.search();
Auth
.getCurrent()
.$promise
.then(function (account) {
$scope.account = account;
return account.$prototype$confirmPaypalPayment({
paymentId: search.paymentId,
token: search.token,
payerID: search.PayerID
}, function () {
ga('send', {
hitType: 'event',
eventCategory: 'submit',
eventAction: 'Pago de Mesa',
eventLabel: 'Pago de Mesa'
});
ga('send', {
hitType: 'event',
eventCategory: 'submit',
eventAction: 'Pago de Mesa (Pago Fijo)',
eventLabel: 'Pago de Mesa (Pago Fijo)'
});
ga('send', {
hitType: 'event',
eventCategory: 'submit',
eventAction: 'Pago de Mesa (Pago con Paypal)',
eventLabel: 'Pago de Mesa (Pago con Paypal)'
});
$rootScope.fullAccount = $localStorage.fullAccount = account.payInformationData.isPaid;
$translate('success-payment').then(function (translation) {
toastr.success(translation);
}).catch(function() {
if(!$(".loading-init").hasClass('hidden')) {
$(".loading-init").addClass("hidden");
}
});
$state.go('homeUser.pay');
});
});
}
CoupleCheckoutModalController.$inject = ['$scope', '$state', 'toastr', '$uibModal', '$uibModalInstance', '$localStorage', 'CoupleAccount', 'DiscountCode', 'Auth','$rootScope', 'Code'];
function CoupleCheckoutModalController($scope, $state, toastr, $uibModal, $uibModalInstance, $localStorage, CoupleAccount, DiscountCode, Auth,$rootScope, Code) {
var ccCtrl = this;
$scope.currentTemplate = 'partials/auth.pay.prepay.modal.html';
var account = $scope.account = Auth.getCurrent();
ccCtrl.isActiveCode = false;
this.sendCode = function() {
DiscountCode.applyCode({coupleId: account.id, code: ccCtrl.discountCode }).$promise.then(function(data) {
ccCtrl.isActiveCode = false;
ccCtrl.code = data.code;
ccCtrl.code.discountCode = data;
ccCtrl.isCode = true;
if(data.account) {
var userAccount = data.account;
$rootScope.fullAccount = $localStorage.fullAccount = userAccount.payInformationData.isPaid;
$scope.account = userAccount;
$scope.disabled = false;
$scope.order = userAccount.payInformationData;
$scope.pay = {
month: null,
year: null
};
$scope.payer = {};
$scope.paymentInfo = {};
$uibModalInstance.dismiss('cancel');
$state.go('homeUserDiscount', {descountId: ccCtrl.code.discountCode.id});
}
}, function(err) {
console.log(err);
toastr.error(err.data.error.message);
}).catch(function(err) {
console.log(err);
if(!$(".loading-init").hasClass('hidden')) {
$(".loading-init").addClass("hidden");
}
toastr.error(JSON.stringify(err));
});
}
ccCtrl.priceToDiscount = function(price) {
var code = ccCtrl.code;
if(ccCtrl.isCode) {
switch(code.discountCode.type) {
case 'amount':
price = price - code.discountCode.amount
break;
case 'percent':
price = price - ((price * code.discountCode.amount) / 100);
break;
}
} else if($rootScope.paymentDiscount > 0) {
ccCtrl.isDiscount = 1;
price = price - ((price * $rootScope.paymentDiscount) / 100);
}
return price < 0 ? 0 : price;
}
this.getCode = function() {
Code.find({
filter: {
where: {
coupleAccountId: account.id,
},
include: 'discountCode'
}
}).$promise.then(function(code) {
if(code.length > 0) {
ccCtrl.code = _.first(code);
ccCtrl.isCode = true;
} else {
ccCtrl.isCode = false;
}
}).catch(function(err) {
console.log(err);
});
}
this.selectPercentagePay = function () {
$uibModalInstance.dismiss();
$state.go('homeUser.pay', {startOn: 'payPerPercentage'});
};
this.selectFixedPay = function(selectPay, selectPayId) {
$uibModalInstance.dismiss();
$state.go('homeUser.pay', {startOn: 'fixedPrice', amount: ccCtrl.priceToDiscount(selectPay), id: selectPayId, code: ccCtrl.code });
};
this.selectFixedPayComplete = function (selectPay, selectPayId) {
$uibModalInstance.dismiss();
$state.go('homeUser.pay', {startOn: 'fixedPrice', amount: ccCtrl.priceToDiscount(selectPay), id: selectPayId, code: ccCtrl.code});
};
this.bye = function () {
$uibModalInstance.dismiss();
};
this.getCode();
}
})(angular.module('uniko.authSatellizer'));