(function() { 'use strict'; angular .module('LyncPix.ShoppingCartModule') .controller('CheckoutController', CheckoutController); /** @ngInject */ function CheckoutController($scope, $log, $rootScope, $q, ShoppingCart, ShipmentPricing, Country, ShippingMethods, ShippingAddresses, Payment, Orders, Currency, State) { var vm = this; var UGUID ='38A5B679-B290-48C0-A828-36C8F6727D15'; console.log(UGUID); $log.debug('CheckoutController Activated'); activate(); vm.test = "this is test"; vm.payPal = function( ){ window.open('/support/facebook/go-to-payment.php?uguid=' + UGUID+'&shippingADDRESS='+vm.data.shippingAddress.id, 'someName', 'status=1,width=700,height=700'); }; ///support/facebook/go-to-payment.php vm.paymentMethods = ["Credit cart", "Pay Pal"]; vm.select = function(){ if(vm.paymentMethod =="Pay Pal"){ vm.payPal(); } } function activate () { vm.data = { }; vm.shoppingCartTotalPrice = 0; vm.loadStates = loadStates; loadCurrency(); $q.all([ loadCountries(), loadLastShippingAddress() ]) .then(function(res) { vm.countries = res[0]; vm.data.shippingAddress = res[1] || { }; delete vm.data.shippingAddress.id; if(vm.countries.length && !vm.data.shippingAddress.countryID) { vm.data.shippingAddress.countryID = vm.countries[0].id; } if(vm.data.shippingAddress.countryID) { loadStates(vm.data.shippingAddress.countryID); } loadShipmentPricing(); }); loadShoppingCart(); vm.showShippingForm = true; vm.loadShipmentPricing = loadShipmentPricing; vm.shippingMethodChanged = shippingMethodChanged; vm.saveShippingInfo = saveShippingInfo; vm.savePayment = savePayment; vm.remove = remove; // vm.edit = edit; vm.order = order; window.addEventListener('message', receiveMessage, false); } vm.payWithPayPal = function () { window.open('/storeapi/index.php?method=chainedPayment&uguid=' + UGUID+'&returnUrl=http://admin.lyncpix.com/gallery.php?g=38A5B679-B290-48C0-A828-36C8F6727D15||||ShowSuccessPopUp&cancelUrl=http://admin.lyncpix.com/close-popup.php&shippingADDRESS='+vm.data.shippingAddress.id, '_self', 'status=1,width=700,height=700'); } // vm.payWithPayPal = function () { // window.open('/support/facebook/go-to-payment.php?uguid=' + UGUID+'&shippingADDRESS='+vm.data.shippingAddress.id, 'someName', 'status=1,width=700,height=700'); // } vm.payWithCreditCard = function () { jQuery.blockUI(); saveShippingInfo().then(function () { jQuery.unblockUI(); }); vm.showShippingForm = false; vm.paymentMethod = 'Credit cart'; } vm.iframeLoaded = function () { console.log('lets see'); } function receiveMessage(event) { var data = event.data || event.originalEvent.data; if(data == 'canceled' || data == 'error'){ vm.showShippingForm = true; $scope.$apply(); } else if(data === 'goToStep3') { angular.element('#save-cart-info-step-2-payment').triggerHandler('click'); } } function loadCountries () { return Country.getWithShippingMethod(UGUID); } function loadStates (countryID) { return State.getAll(countryID) .then(function(res) { vm.states = res; }); } function loadCurrency () { Currency.get(undefined, UGUID) .then(function(currency) { vm.currency = currency; }); } function loadLastShippingAddress () { return ShippingAddresses.getLast(UGUID); } function loadShipmentPricing () { if(vm.data.shippingAddress && vm.data.shippingAddress.countryID) { vm.shipmentPricings = [ ]; ShipmentPricing.search(vm.data.shippingAddress.countryID, UGUID) .then(function(shipmentPricings) { vm.shipmentPricings = shipmentPricings; if(shipmentPricings.length) { vm.shipmentPricing = angular.copy(shipmentPricings).sort(function(lhs, rhs) { return lhs.price > rhs.price; })[0]; vm.data.shippingAddress.shippingMethodID = vm.shipmentPricing.shippingMethodID; vm.data.shippingAddress.price = vm.shipmentPricing.price; } }); } } function shippingMethodChanged () { vm.data.shippingAddress.shippingMethodID = vm.shipmentPricing.shippingMethodID; vm.data.shippingAddress.price = vm.shipmentPricing.price; } vm.showChanges = function(){ console.log(vm.shippingInfoForm.$invalid); console.log(vm.shippingInfoForm); } function saveShippingInfo () { vm.data.shippingAddress.shipmentPricingID = vm.shipmentPricing.id; vm.data.shippingAddress.uguid = UGUID; ShippingAddresses.save(vm.data.shippingAddress) .then(function(shippingAddress) { vm.data.shippingAddress.id = shippingAddress.id; }); return Payment.getPaymentIframe({ uguid: vm.data.shippingAddress.uguid, shipmentPricingID: vm.data.shippingAddress.shipmentPricingID }) .then(function(res) { vm.cartTransactions = res; }); } function savePayment () { vm.data.clientCard.uguid = UGUID; console.log(vm.data.clientCard); Payment.save(vm.data.clientCard) .then(function() { }); } function loadShoppingCart () { ShoppingCart.getAll(UGUID) .then(function(shoppingCart) { vm.shoppingCart = shoppingCart; vm.shoppingCartTotalPrice = vm.shoppingCart.reduce(function(prev, cur) { return Number(prev) + Number(cur.totalPrice) * Number(cur.count) * 1.24; }, 0); }); } function remove(item) { ShoppingCart.remove(item.id, UGUID) .then(function(shoppingCart) { $rootScope.$emit('ShoppingCartRefreshed', true); }); } /* function edit(item) { item.uguid = UGUID; ShoppingCart.save(item) .then(function(shoppingCart) { $rootScope.$emit('ShoppingCartRefreshed', true); }); } */ function order() { Orders.save({ uguid: UGUID, shippingAddressID: vm.data.shippingAddress.id }) .then(function() { fRedirect('payment') }); } $rootScope.$on('ShoppingCartRefreshed', function() { loadShoppingCart(); }); } })();