/** * @Component Name: PaymentComponent * @Module: Checkout * @Module Functionality : To display the payment information * @Creation Date: August 12,2017 * @Author: sbaireddy * @Version: 1.0 */ import { Component, OnInit, Input, Output, EventEmitter, ViewChild, OnChanges, AfterViewChecked, OnDestroy } from '@angular/core'; import { NgForm } from '@angular/forms'; import Utils from '../../../shared/utils'; import CartUtils from '../../../shared/cart.utils'; import { STATES, PAYMENT_METHODS, CARD_TYPES } from '../../../shared/form.constants'; import * as Constants from '../../../shared/constants'; import CheckoutUtils from '../../../shared/checkout.utils'; import { User } from '../../../shared/modal/user'; import { SharedService } from '../../../shared/shared.service'; import { UserService } from '../../../services/user.service'; import { CartService } from '../../../services/cart.service'; import { CustomerService } from '../../../services/customer.service'; import { CheckoutService } from '../../../services/checkout.service'; declare var encrypt: any; @Component( { selector: 'payment-cmp', moduleId: module.id, templateUrl: 'payment.component.html', styleUrls: ['payment.component.css'] }) export class PaymentComponent implements OnInit, OnChanges, AfterViewChecked, OnDestroy { private COMPONENT_NAME: string = 'PaymentComponent'; storeId: number; // update storeid stateItems: any[]; paymentMethodItems: any[]; cardTypesItems: any[]; paymentModel: any = { paymentMethod: Constants.PAYMENT_METHOD_CREDIT, state: '', enrollAutopay: false }; @ViewChild('paymentForm') paymentForm: NgForm; @Output() onFormChange = new EventEmitter(); sameAsBilling: boolean = true; isValidAddress: boolean = false; disableFields: boolean = false; cart: any; user: User; @Output() cardDetailChange = new EventEmitter(null); cardTypeResponse: any = null; cardLength: number; securityCodeLength: number; saveCardType: string; isNCFPlanInCart: boolean = false; maxdate: any; customerEnrollForAutoPay: boolean; isOnlyAccCart: boolean = false; isBYOSOnlyOrder: boolean = false; karmaTestResult: any = {}; existingCardType: any; updatedCardType: any; constructor( public sharedService: SharedService, public userService: UserService, public cartService: CartService, public customerService: CustomerService, public checkoutService: CheckoutService) { console.log('[start]-CustomerComponent.constructor()'); } /** * This function is used to initialize the directive/component after Angular * first displays the data-bound properties.It is invoked only once when the * directive is instantiated.Here it is initializing all the states, payment methods and card types * @return {void} */ ngOnInit(): void { const METHOD_NAME: string = 'ngOnInit()'; this.cart = this.cartService.getCart(); this.paymentModel.storeCardDetails = true; console.log('[start]-CustomerComponent.ngOnInit()'); this.paymentModel.enrollAutopay = (this.cart.customer && this.cart.customer.creditProfile && this.cart.customer.creditProfile.autoPayIndicator) ? this.cart.customer.creditProfile.autoPayIndicator : false ; this.customerEnrollForAutoPay = (this.cart.customer && this.cart.customer.creditProfile && this.cart.customer.creditProfile.autoPayIndicator) ? this.cart.customer.creditProfile.autoPayIndicator : false ; this.cardLength = 16; // setting default card length this.securityCodeLength = 4; // setting default CVV length this.paymentModel.securityCodeLength = this.securityCodeLength; this.stateItems = STATES.filter(stateItem => stateItem); this.paymentMethodItems = PAYMENT_METHODS.filter(typeItem => typeItem); this.cardTypesItems = CARD_TYPES.filter(cardTypeItem => cardTypeItem); this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; }); this.user = this.userService.getUser(); if (this.user && this.user.permissions) { this.paymentMethodItems = PAYMENT_METHODS.filter((item) => { if (item.key === Constants.PAYMENT_METHOD_WEBPAYMENT) { return (this.user.permissions.canSubmitWebOrder && this.cart.webOrderId); } else { return true; } }); } if (this.cart) { this.isNCFPlanInCart = CartUtils.isNCFPlanInCart(this.cart); this.isOnlyAccCart = CheckoutUtils.isAccOnlyOrder(this.cart); this.isBYOSOnlyOrder = CheckoutUtils.isBYOSOnlyOrder(this.cart); if (this.isBYOSOnlyOrder) { this.initPublicKey(); } } this.saveCardType = this.paymentModel.paymentMethod; console.log('[end]-CustomerComponent.ngOnInit()'); } /** * This function is called whenever one or more data-bound input properties change * @return {void} */ ngOnChanges(): void { const METHOD_NAME: string = 'ngOnChanges()'; this.maxdate = new Date().getFullYear() + 18 + '-12'; console.log('OnChanges the objects initilized.'); } /** * This function is called after view checked * @returns void */ ngAfterViewChecked(): void { this.onFormChange.emit(this.paymentForm); } /** * Cleanup just before Angular destroys the directive/component. * Unsubscribe Observables and detach event handlers to avoid memory leaks. * @return {void} */ ngOnDestroy(): void { const METHOD_NAME: string = 'ngOnDestroy()'; console.log('Destroying the objects initilized.'); this.cart = null; this.user = null; this.paymentModel = null; this.cardTypeResponse = null; this.karmaTestResult = null; this.stateItems = null; this.paymentMethodItems = null; this.cardTypesItems = null; this.maxdate = null; } /** * This function is used to update the card type through getCardType * function of checkout service. * @return {void} */ updateCardType(): void { this.existingCardType = this.paymentModel.cardType; const METHOD_NAME: string = 'updateCardType()'; console.log('PayReviewComponent.getCardType()'); // this.cardTypeResponse = null; if (this.paymentModel && !this.paymentModel.cardType) { // this check is mandatory when they copy paste the card number if (this.paymentModel.cardNumber && this.paymentModel.cardNumber.length === 6 || this.paymentModel.cardNumber.length === 15 || this.paymentModel.cardNumber.length === 16) { console.log('Calling card type service to get card type/length and security code length.'); this.sharedService.showSpinner(true); // spinner this.checkoutService.getCardType(this.storeId, this.cart.orderId, this.paymentModel.cardNumber).subscribe(result => { this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner console.log('Successfully card type response.'); this.paymentModel = Object.assign(this.paymentModel, result); this.updatedCardType = this.paymentModel.cardType; if (this.existingCardType !== this.updatedCardType) { this.paymentModel.expiration = ''; this.paymentModel.securityCode = ''; } this.cardLength = result.cardLength; this.securityCodeLength = result.securityCodeLength; this.paymentModel.securityCodeLength = result.securityCodeLength; this.encryptCreditCard(); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); // console.error('validate payment address error=>' + error); }); } }if (this.paymentModel.cardNumber.length <= 6 ) { this.paymentModel.cardType = null; } } /** * This function is used to get the public key through * initPublicKey function of checkout service. * * @return {void} */ initPublicKey(): void { let cachePublickey = this.checkoutService.getPaymentPublicKey(); if(!(cachePublickey && cachePublickey.encryption && cachePublickey.encryption.jwkKey)) { this.sharedService.showSpinner(true); // spinner this.checkoutService.getPublicKey(this.storeId, this.cart.orderId).subscribe(result => { this.sharedService.showSpinner(false); // spinner if (result && result.encryption && result.encryption.jwkKey) { console.log('Updated payment public key into cache'); this.checkoutService.setPaymentPublicKey(result); } else { this.sharedService.showErrorMessage('_ERR_INVALID_PUBLIC_KEY', false); } }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); // console.error('error=>' + error); }); } } /** * This function is used to validate the address through validateAddress * function of customer service. * @return {void} */ validateAddress(): void { const METHOD_NAME: string = 'validateAddress()'; this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Validating payment address'); this.isValidAddress = false; console.log('validating the payment address'); if (this.paymentModel.address1 && this.paymentModel.city && this.paymentModel.state && this.paymentModel.zipCode) { const inputData: any = CartUtils.validateAddressInput(this.paymentModel); this.sharedService.showSpinner(true); // spinner this.customerService.validateAddress(this.storeId, inputData).subscribe(result => { this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner if (result.statusCode === Constants.ORDER_STATUS_S || result.statusCode === Constants.STATUS_CODE_SUPPORTED) { this.isValidAddress = true; this.paymentModel.address1 = result.addressLine1; this.paymentModel.address2 = result.addressLine2; this.paymentModel.city = result.city; this.paymentModel.state = result.stateCode; this.paymentModel.zipCode = Utils.trimZipCode(result.zipCode); this.updatePaymentInfo(); } else { this.sharedService.showErrorMessage('_ERR_INVALID_ADDRESS', false); } }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); // console.error('validate payment address error=>' + error) }); } } /** * This function is used to update the card type based on * payment method. * @param {any} paymentForm - payment form variable * @return {void} */ updatePaymentMethod(paymentForm: any): void { const METHOD_NAME: string = 'updatePaymentMethod()'; this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Updating the payment method'); // let clonePaymentModel:any = Object.assign( {}, this.paymentModel); // console.log('clonePaymentModel'+ clonePaymentModel.paymentMethod); // //make sure enroll auto pay indicator set for existing customer this.paymentModel.expiration = ''; if (this.paymentModel.paymentMethod === Constants.PAYMENT_METHOD_WEBPAYMENT) { this.paymentModel.cardType = Constants.VISA; } else { this.paymentModel.cardType = null; } // this.paymentModel = { paymentMethod:Constants.PAYMENT_METHOD_CREDIT, state: '', enrollAutopay:false }; } /** * This function is used to switch payment address to shipping or billing * @param {boolean} sameAsBilling - same as billing boolean value * @return {void} */ switchPaymentAddress(sameAsBilling: boolean): void { const METHOD_NAME: string = 'switchPaymentAddress()'; this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Switching payment address'); this.sameAsBilling !== this.sameAsBilling; /*Defect#478926 - commented out below code to still maintain the Alternate CC address upon check/uncheck toggle*/ // this.paymentModel.address1 = ''; // this.paymentModel.address2 = ''; // this.paymentModel.city = ''; // this.paymentModel.state = ''; // this.paymentModel.zipCode = ''; this.updatePaymentInfo(); } /** * This function is used to encrypt credit card number * @return {void} */ encryptCreditCard(): void { if (this.isBYOSOnlyOrder && this.checkoutService.getPaymentPublicKey()) { const publicKey = this.checkoutService.getPaymentPublicKey(); const cardNumber: string = this.paymentModel.cardNumber; if (cardNumber && cardNumber.length === Number(this.cardLength)) { const self = this; encrypt(publicKey.encryption.jwkKey, cardNumber, function (encryptedData) { // console.log('encryptedData:'+ encryptedData); self.paymentModel.encryptedCC = encryptedData; self.updatePaymentInfo(); }, function(errorData) { console.log(errorData); console.log('card encryption faild.'); }); } } } /** * This function is used to update the payment information * @return {void} */ updatePaymentInfo(): void { const METHOD_NAME: string = 'updatePaymentInfo()'; this.paymentModel.sameAsBilling = this.sameAsBilling; this.cardDetailChange.emit(this.paymentModel); } /** * This function is used to update the autopay enrollment * @param {boolean} enroll - autopay enroll boolean value * @return {void} */ updateAutoPayEnroll(enroll: boolean): void { const METHOD_NAME: string = 'updateAutoPayEnroll()'; this.cart.enrollAutopay = enroll; this.cardDetailChange.emit(this.paymentModel); } /** * This function is used to save the card details * @param {boolean} savePay - save payment details boolean value * @return {void} */ updateSavePay(savePay: boolean): void { this.cart.storeCardDetails = savePay; } }