(function () { 'use strict'; angular .module('LyncPix.ShoppingCartModule') .directive('cropImage', function () { return { restrict: 'EA', scope: { cropOptions: '=', cropImage: '=' }, replace: false, link: function (scope, elm, attrs) { let id = 'canvas-' + (new Date().getTime()); elm.attr('id', id); let opts = scope.cropOptions; let image = new Image(); let canvas = document.getElementById(id); var ctx = canvas.getContext('2d'); let w = 100;//width of canvas let h = parseInt(w * parseInt(opts.height) / parseInt(opts.width)); image.src = scope.cropImage; ctx.canvas.width = w; ctx.canvas.height = h; image.onload = function () { ctx.drawImage(image, parseInt(opts.x), parseInt(opts.y), parseInt(opts.width), parseInt(opts.height), 0, 0, w, h); } } } }) .controller('CartController', CartController); /** @ngInject */ function CartController($scope, $log, $rootScope, ShoppingCart, $q, ShipmentPricing, Country, ShippingMethods, ShippingAddresses, Payment, Orders, Currency, State, toastr, $uibModal, $interval, Client) { var vm = this; var UGUID = '38A5B679-B290-48C0-A828-36C8F6727D15'; $log.debug('CartController Activated'); activate(); function activate() { vm.arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; vm.shoppingCartTotalPrice = 0; vm.data = {}; vm.shoppingCartTotalPrice = 0; vm.showShippingForm = true; vm.formMode = false; vm.submitted = false; loadCurrency(); loadShoppingCart(); loadClientManager(); $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(); }); vm.remove = remove; vm.edit = edit; vm.loadStates = loadStates; vm.loadShipmentPricing = loadShipmentPricing; vm.shippingMethodChanged = shippingMethodChanged; vm.saveShippingInfo = saveShippingInfo; vm.savePayment = savePayment; vm.order = order; } vm.getCountryFromId = function (id) { if (!vm.countries || !vm.countries.length) { return ''; } return vm.countries.reduce(function (name, row) { if (row.id == id) { name = row.name; } return name; }, ''); } vm.select = function () { if (vm.paymentMethod == "Pay Pal") { vm.payPal(); } } vm.isArray = function (data) { return angular.isArray(data); } vm.isObject = function (data) { return angular.isObject(data); } vm.isUrl = function (str) { if (typeof str === 'boolean') { return false; } var pattern = new RegExp('^(https?:\\/\\/)?' + // protocol '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.?)+[a-z]{2,}|' + // domain name '((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path '(\\?[;&a-z\\d%_.~+=-]*)?' + // query string '(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator return pattern.test(str); } vm.payWithPayPal = function () { jQuery.blockUI(); let token = $('meta[name=token]').attr('content'); var win = window.open('/storeapi/index.php?method=chainedPayment&uguid=' + UGUID + '&returnUrl=http://admin.lyncpix.com/success-payment.php&cancelUrl=http://admin.lyncpix.com/close-popup.php&shippingADDRESS=' + vm.data.shippingAddress.id+'&token='+token, 'someName', 'status=1,width=700,height=700'); var timer = $interval(function () { if (win.closed) { $interval.cancel(timer); timer = undefined; jQuery.unblockUI(); } }, 300); window.addEventListener('message', function (event) { var data = event.data || event.originalEvent.data; if (data == 'goToStep3') { toastr.success('Your payment completed successfully.', 'Congratulations'); win.close(); jQuery.unblockUI(); showPaymentSuccess(); window.removeEventListener('message', angular.noop, true); } }, false); } vm.payWithCreditCard = function () { jQuery.blockUI(); Payment.getPaymentIframe({ uguid: UGUID, shipmentPricingID: vm.data.shippingAddress.shipmentPricingID }).then(function (res) { jQuery.unblockUI(); vm.iframe = res; $uibModal.open({ templateUrl: 'credit-card.html', controller: function ($scope, $uibModalInstance, $sce, iframe, toastr) { let vm = $scope; vm.url = iframe.iframeUrl; window.addEventListener('message', receiveMessage, false); function receiveMessage(event) { var data = event.data || event.originalEvent.data; if (data == 'canceled' || data == 'error') { if (data == 'canceled') { toastr.error('Your payment was cancelled.', 'Error'); } else if (data == 'error') { toastr.error('Some error occurred during payment. Please try later.', 'Error'); } $uibModalInstance.dismiss(); } else if (data == 'goToStep3') { toastr.success('Your payment completed successfully.', 'Congratulations'); $uibModalInstance.dismiss(); showPaymentSuccess(); } window.removeEventListener('message', angular.noop, true); } }, resolve: { iframe: function () { return vm.iframe; } } }); }); } vm.changeShippingInfo = function () { vm.formMode = true; } vm.hasShippingInfo = function () { let ret = typeof vm.data.shippingAddress !== typeof undefined && vm.data.shippingAddress.email && vm.data.shippingAddress.email.length > 1; return ret; } function order() { Orders.save({ uguid: UGUID, shippingAddressID: vm.data.shippingAddress.id }).then(function () { fRedirect('payment') }); } function getPaymentProviders(sapak) { let ret = { paypal: 1, credit_card: 1 }; if (sapak.hasOwnProperty('jsonPaymentMethods')) { if (angular.isObject(sapak.jsonPaymentMethods)) { ret = sapak.jsonPaymentMethods; } else { let providers = angular.fromJson(sapak.jsonPaymentMethods); if (angular.isObject(providers)) { ret = { paypal: providers.hasOwnProperty('paypal') ? +providers.paypal : 1, credit_card: providers.hasOwnProperty('credit_card') ? +providers.credit_card : 1 } } } } return ret.paypal == 0 && ret.credit_card == 0 ? { paypal: 1, credit_card: 1 } : ret; } function loadClientManager() { Client.loadSapakManager(UGUID).then(function (res) { vm.manager = res; vm.paymentProviders = getPaymentProviders(res); vm.paymentMethods = []; if (vm.paymentProviders.paypal == 1) { vm.paymentMethods.push('paypal'); } if (vm.paymentProviders.credit_card == 1) { vm.paymentMethods.push('card'); } vm.paymentMethod = vm.paymentMethods.length > 1 ? 'card' : vm.paymentMethods[0]; console.log(vm.paymentMethods); console.log(vm.paymentMethod); console.log(vm.paymentProviders); }); } function loadCountries() { return Country.getWithShippingMethod(UGUID); } function loadStates(countryID) { return State.getAll(countryID) .then(function (res) { vm.states = res; }); } function loadLastShippingAddress() { return ShippingAddresses.getLast(UGUID); } function loadShipmentPricing() { if (vm.data.shippingAddress && vm.data.shippingAddress.countryID) { vm.shipmentPricings = []; return ShipmentPricing .getByToken(vm.data.shippingAddress.countryID, UGUID) .then(function (shipmentPricings) { vm.shipmentPricings = shipmentPricings.map(function (row) { row.price = row.price == null ? 0 : row.price; return row; }); if (shipmentPricings.length) { vm.shipmentPricing = shipmentPricings.reduce(function (obj, row) { if (row.id == vm.data.shippingAddress.shipmentPricingID) { obj = row; } return obj; }, {}); if(!vm.shipmentPricing.hasOwnProperty('price')){ vm.shipmentPricing = shipmentPricings[0]; vm.data.shippingAddress.shipmentPricingID = vm.shipmentPricing.id; } if (vm.shipmentPricing.hasOwnProperty('price')) { vm.data.shippingAddress.shippingMethodID = vm.shipmentPricing.shippingMethodID; vm.data.shippingAddress.price = vm.shipmentPricing.price; } } }); } return false; } function shippingMethodChanged() { vm.data.shippingAddress.shippingMethodID = vm.shipmentPricing.shippingMethodID; vm.data.shippingAddress.price = vm.shipmentPricing.price; } function saveShippingInfo(form) { vm.submitted = true; if (form.$invalid) { toastr.error('The highlighted fields are missing. Please select proper values for these fields to continue.', 'Error'); return false; } vm.data.shippingAddress.shipmentPricingID = vm.shipmentPricing.id; vm.data.shippingAddress.uguid = UGUID; ShippingAddresses .save(vm.data.shippingAddress) .then(function () { vm.submitted = false; vm.formMode = false; }); } 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) { //prevent hidden product properties //from being displayed on the page shoppingCart = shoppingCart.map(function (cart) { cart.options = Object.keys( cart.options).filter(function(prop_name){ return cart.properties.options.reduce(function(obj,prop){ return prop.name.toLowerCase() == prop_name.toLowerCase() ? prop : obj; },{type : 'radio'}).type != 'hidden'; }).reduce(function(obj,prop){ obj[prop] = cart.options[prop]; return obj; },{}); return cart; }) vm.shoppingCart = shoppingCart; vm.shoppingCartTotalPrice = vm.shoppingCart.reduce(function (prev, cur) { return Number(prev) + Number(cur.totalPrice) * Number(cur.count); }, 0); }); } function loadCurrency() { Currency.get(undefined, UGUID) .then(function (currency) { vm.currency = currency; }); } 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) { vm.data.shippingAddress.uguid = UGUID; let promise = loadShipmentPricing(); if(false != promise){ promise.then(function () { ShippingAddresses.save(vm.data.shippingAddress); }); } $rootScope.$emit('ShoppingCartRefreshed', true); }); } $rootScope.$on('ShoppingCartRefreshed', function () { loadShoppingCart(); }); } })();