import {catchError, tap, map} from 'rxjs/operators'; /** * @Service: Checkout Service * @Creation Date: Sept 16, 2017 * @Author: sbaireddy * @Version: 1.0 */ import { Injectable } from '@angular/core'; // import { Http, Headers, RequestOptions, Response } from '@angular/_http'; import { HttpClient } from '@angular/common/http'; // import { Observable } from 'rxjs/observable'; import { Observable } from 'rxjs/Rx'; // for debugging import { BaseService } from './base.service'; import * as ServiceConstants from '../shared/service.constants'; import Utils from '../shared/utils'; // import * as Constants from '../shared/constants'; import { restUrl } from '../rest.url'; import { KeyValue } from '../shared/modal/key.value'; import { IUsableShipping, IRealtimeInventory, IZipMarket } from '../shared/modal/common'; /** * This class provides the Checkout Service with methods shipping, payment and submit actions. */ @Injectable({providedIn:'root'}) export class CheckoutService extends BaseService { public customerInfo: any = {}; public paymentInfo: any = {}; public publicKey: any = null; /** * Creates a new UserService with the injected HttpClient. * @param {HttpClient} _http - The injected HttpClient. * @constructor */ constructor(public _http: HttpClient) { super(); } /**Gets the Customer Info */ getCustomerInfo(): any { if (!this.customerInfo) { this.customerInfo = {} } return this.customerInfo; } /**Sets the Customer Info */ setCustomerInfo(customerInfo: any): void { this.customerInfo = customerInfo; } /**Gets the Payment Info */ getPaymentInfo(): any { if (!this.paymentInfo) { this.paymentInfo = {} } return this.paymentInfo; } /**Sets the Payment Info */ setPaymentInfo(paymentInfo: any): void { this.paymentInfo = paymentInfo; } /**Gets the Payment public key */ getPaymentPublicKey(): any { if (this.publicKey) { return this.publicKey; } else { return Utils.getPaymentPublicKey(); } } /**Sets the Payment Public key */ setPaymentPublicKey(publicKey: any): void { this.publicKey = publicKey; Utils.storePaymentPublicKey(publicKey); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {IUsableShipping} The Observable for the HTTP request. */ getUsableShippingInfo(storeId: number, orderId: number, zipCode: string): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-CheckoutService.getUsableShippingInfo ()'); } // getting the request options const options = this.httpOptions(); // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue(ServiceConstants.STORE_ID, storeId)); pathParams.push(new KeyValue(ServiceConstants.ORDER_ID, orderId)); // constracting the query params const queryParams: Array = new Array(); if (zipCode) { queryParams.push(new KeyValue('zipCode', zipCode)); } // getting the final url const url = Utils.replaceAppendParams(restUrl.checkout.getShippingMode, pathParams, queryParams); // service calling // return this._http.get ('assets/data/usable-shipping.json', options) return this._http.get(url, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-CheckoutService.getUsableShippingInfo ()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {any} The Observable for the HTTP request. */ updateShippingMode(storeId: number, orderId: number, requestParams: any): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-CheckoutService.updateShippingMode ()'); } // getting the request options const options = this.httpOptions(); // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue(ServiceConstants.STORE_ID, storeId)); pathParams.push(new KeyValue(ServiceConstants.ORDER_ID, orderId)); // getting the final url const url = Utils.replacePathParams(restUrl.checkout.updateShippingMode, pathParams); // service calling return this._http.put(url, requestParams, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-CheckoutService.updateShippingMode ()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {any} The Observable for the HTTP request. */ updateShippingAddress(storeId: number, orderId: number, requestParams: any): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-CheckoutService.updateShippingAddress ()'); } // getting the request options const options = this.httpOptions(); // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue(ServiceConstants.STORE_ID, storeId)); pathParams.push(new KeyValue(ServiceConstants.ORDER_ID, orderId)); // getting the final url const url = Utils.replacePathParams(restUrl.checkout.shippingAddressUpdate, pathParams); // service calling return this._http.post(url, requestParams, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-CheckoutService.updateShippingAddress ()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {any} The Observable for the HTTP request. */ deleteShippingAddress(storeId: number, orderId: number): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-CheckoutService.deleteShippingAddress ()'); } // getting the request options const options = this.httpOptions(); // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue(ServiceConstants.STORE_ID, storeId)); pathParams.push(new KeyValue(ServiceConstants.ORDER_ID, orderId)); // getting the final url const url = Utils.replacePathParams(restUrl.checkout.shippingAddressDelete, pathParams); // service calling return this._http.delete(url, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-CheckoutService.deleteShippingAddress ()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {any} The Observable for the HTTP request. */ deleteE911Address(storeId: number, orderId: number, packageId: number, lineId: number): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-CheckoutService.deleteE911Address ()'); } // getting the request options const options = this.httpOptions(); // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue(ServiceConstants.STORE_ID, storeId)); pathParams.push(new KeyValue(ServiceConstants.ORDER_ID, orderId)); pathParams.push(new KeyValue(ServiceConstants.PACKAGE_ID, packageId)); pathParams.push(new KeyValue(ServiceConstants.LINE_ID, lineId)); // getting the final url const url = Utils.replacePathParams(restUrl.checkout.e911AddressDelete, pathParams); // service calling return this._http.delete(url, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-CheckoutService.deleteE911Address ()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {any} The Observable for the HTTP request. */ deletePpuAddress(storeId: number, orderId: number, packageId: number, lineId: number): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-CheckoutService.deletePpuAddress ()'); } // getting the request options const options = this.httpOptions(); // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue(ServiceConstants.STORE_ID, storeId)); pathParams.push(new KeyValue(ServiceConstants.ORDER_ID, orderId)); pathParams.push(new KeyValue(ServiceConstants.PACKAGE_ID, packageId)); pathParams.push(new KeyValue(ServiceConstants.LINE_ID, lineId)); // getting the final url const url = Utils.replacePathParams(restUrl.checkout.ppuAddressDelete, pathParams); // service calling return this._http.delete(url, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-CheckoutService.deletePpuAddress ()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {any} The Observable for the HTTP request. */ deleteCreditCardAddress(storeId: number, orderId: number): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-CheckoutService.deletePpuAddress ()'); } // getting the request options const options = this.httpOptions(); // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue(ServiceConstants.STORE_ID, storeId)); pathParams.push(new KeyValue(ServiceConstants.ORDER_ID, orderId)); // getting the final url const url = Utils.replacePathParams(restUrl.checkout.ccAddressDelete, pathParams); // service calling return this._http.delete(url, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-CheckoutService.deletePpuAddress ()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {any} The Observable for the HTTP request. */ deletePortingNumber(storeId: number, orderId: number, packageId: number, lineId: number): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-CheckoutService.deletePortingNumber ()'); } // getting the request options const options = this.httpOptions(); // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue(ServiceConstants.STORE_ID, storeId)); pathParams.push(new KeyValue(ServiceConstants.ORDER_ID, orderId)); pathParams.push(new KeyValue(ServiceConstants.PACKAGE_ID, packageId)); pathParams.push(new KeyValue(ServiceConstants.LINE_ID, lineId)); // getting the final url const url = Utils.replacePathParams(restUrl.checkout.portingNumberDelete, pathParams); // service calling return this._http.delete(url, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-CheckoutService.deletePortingNumber ()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {any} The Observable for the HTTP request. */ updateCustomerInfo(storeId: number, orderId: number, requestParams: any): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-CheckoutService.resendServiceAgreement ()'); } // getting the request options const options = this.httpOptions(); // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue(ServiceConstants.STORE_ID, storeId)); pathParams.push(new KeyValue(ServiceConstants.ORDER_ID, orderId)); // getting the final url const url = Utils.replacePathParams(restUrl.checkout.updateCustomerInfo, pathParams); // service calling return this._http.put(url, requestParams, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-CheckoutService.resendServiceAgreement ()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {any} The Observable for the HTTP request. */ getCardType(storeId: number, orderId: number, cardNumber: string): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-CheckoutService.getCardType ()'); } // getting the request options const options = this.httpOptions(); // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue(ServiceConstants.STORE_ID, storeId)); pathParams.push(new KeyValue(ServiceConstants.ORDER_ID, orderId)); pathParams.push(new KeyValue('cardNumber', cardNumber)); // getting the final url const url = Utils.replacePathParams(restUrl.checkout.getCardType, pathParams); // service calling // return this._http.get ('assets/data/usable-shipping.json', options) return this._http.get(url, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-CheckoutService.getCardType ()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {any} The Observable for the HTTP request. */ getPublicKey(storeId: number, orderId: number): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-CheckoutService.getPublicKey ()'); } // getting the request options const options = this.httpOptions(); // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue(ServiceConstants.STORE_ID, storeId)); pathParams.push(new KeyValue(ServiceConstants.ORDER_ID, orderId)); // getting the final url const url = Utils.replacePathParams(restUrl.checkout.getPublicKey, pathParams); // service calling // return this._http.get ('assets/data/usable-shipping.json', options) return this._http.get(url, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-CheckoutService.getPublicKey ()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {any} The Observable for the HTTP request. */ sendFinanceDiscloser(storeId: number, orderId: number, requestParams: any): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-CheckoutService.sendFinanceDiscloser ()'); } // getting the request options const options = this.httpOptions(); // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue(ServiceConstants.STORE_ID, storeId)); pathParams.push(new KeyValue(ServiceConstants.ORDER_ID, orderId)); // getting the final url const url = Utils.replacePathParams(restUrl.checkout.sendFinanceDiscloser, pathParams); // service calling return this._http.post(url, requestParams, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-CheckoutService.sendFinanceDiscloser ()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {any} The Observable for the HTTP request. */ getServiceAgreement(storeId: number, orderId: number, language: string, autoPay: boolean, emailAgreement: boolean): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-CheckoutService.getServiceAgreement ()'); } // getting the request options const options = this.httpOptions(); // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue(ServiceConstants.STORE_ID, storeId)); pathParams.push(new KeyValue(ServiceConstants.ORDER_ID, orderId)); const queryParams: Array = new Array(); if (language) { queryParams.push(new KeyValue('language', language)); } queryParams.push(new KeyValue('autoPay', autoPay)); queryParams.push(new KeyValue('emailAgreement', emailAgreement)); // getting the final url const url = Utils.replaceAppendParams(restUrl.checkout.getServiceAgreement, pathParams, queryParams); // service calling // return this._http.get ('assets/data/usable-shipping.json', options) return this._http.get(url, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-CheckoutService.getServiceAgreement ()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {IRealtimeInventory} The Observable for the HTTP request. */ realTimeInventory(storeId: number, orderId: number): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-CheckoutService.realTimeInventory ()'); } // getting the request options const options = this.httpOptions(); // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue(ServiceConstants.STORE_ID, storeId)); pathParams.push(new KeyValue(ServiceConstants.ORDER_ID, orderId)); // getting the final url const url = Utils.replacePathParams(restUrl.checkout.realTimeInventory, pathParams); // service calling // return this._http.get ('assets/data/realtime-inventory.json', options) return this._http.get(url, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-CheckoutService.realTimeInventory ()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {any} The Observable for the HTTP request. */ submitOrder(storeId: number, orderId: number, isAALOrder: boolean, requestParams: any): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-CheckoutService.submitOrder ()'); } // getting the request options const options = this.httpOptions(); options.headers = options.headers.append('storeId', '' + storeId); let flowType = 'SHIPTO-NEW'; if (isAALOrder) flowType = 'SHIPTO-AAL'; options.headers = options.headers.append('flowType', flowType); // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue(ServiceConstants.STORE_ID, storeId)); pathParams.push(new KeyValue(ServiceConstants.ORDER_ID, orderId)); // getting the final url const url = Utils.replacePathParams(restUrl.checkout.submitOrder, pathParams); // service calling return this._http.post(url, requestParams, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-CheckoutService.submitOrder ()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {any} The Observable for the HTTP request. */ validateSIMNumber(storeId: number, orderId: any, simNumber: string): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-CheckoutService.validateSIMNumber ()'); } // getting the request options const options = this.httpOptions(); // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue(ServiceConstants.STORE_ID, storeId)); pathParams.push(new KeyValue(ServiceConstants.ORDER_ID, orderId)); pathParams.push(new KeyValue(ServiceConstants.SIM_NUMBER, simNumber)); // getting the final url const url = Utils.replacePathParams(restUrl.checkout.validateSim, pathParams); // service calling // return this._http.get ('assets/data/realtime-inventory.json', options) return this._http.get(url, options).pipe( map((res: any) => res), tap(data => { console.log('server data: ', data); if (ServiceConstants.LOGGING) { console.log('[end]-CheckoutService.validateSIMNumber ()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {IZipMarket} The Observable for the HTTP request. */ lookupNpaNxx(storeId: number, orderId: any, zipCode: string): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-CheckoutService.lookupNpaNxx ()'); } // getting the request options let options = this.httpOptions(); // constracting the path params let pathParams: Array = new Array(); pathParams.push(new KeyValue(ServiceConstants.STORE_ID, storeId)); pathParams.push(new KeyValue(ServiceConstants.ORDER_ID, orderId)); pathParams.push(new KeyValue(ServiceConstants.ZIP_CODE, zipCode ? zipCode : '98006')); // getting the final url let url = Utils.replacePathParams(restUrl.checkout.lookupNpaNxx, pathParams); // service calling // return this._http.get('assets/data/npa-nxx.json', options) return this._http.get(url, options).pipe( map((res: any) => res.npaList), tap(data => { console.log('server data:', data); if (ServiceConstants.LOGGING) { console.log('[end]-CheckoutService.lookupNpaNxx()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {any} The Observable for the HTTP request. */ getPaperlessBilling(storeId: number, orderId: number, zipCode: string, accountNumber: number, accountType: string, accountSubType: string, phoneNumber: number): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-CheckoutService.getPaperlessBilling ()'); } // getting the request options const options = this.httpOptions(); // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue(ServiceConstants.STORE_ID, storeId)); pathParams.push(new KeyValue(ServiceConstants.ORDER_ID, orderId)); pathParams.push(new KeyValue('zipCode', zipCode)); // constracting the query params const queryParams: Array = new Array(); if (accountNumber) { queryParams.push(new KeyValue('accountNumber', accountNumber)); } if (accountType) { queryParams.push(new KeyValue('accountType', accountType)); } if (accountSubType) { queryParams.push(new KeyValue('accountSubType', accountSubType)); } if (phoneNumber) { queryParams.push(new KeyValue('phoneNumber', phoneNumber)); } // getting the final url const url = Utils.replaceAppendParams(restUrl.order.paperlessBilling, pathParams, queryParams); // service calling return this._http.get(url, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-CheckoutService.getPaperlessBilling ()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {any} The Observable for the HTTP request. */ submitByos(storeId: number, orderId: number, isAALOrder: boolean, requestParams: any): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-CheckoutService.submitByos ()'); } // getting the request options const options = this.httpOptions(); let flowType = 'BYOS-NEW'; if (isAALOrder) flowType = 'BYOS-AAL'; options.headers = options.headers = options.headers.append('flowType', flowType); // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue(ServiceConstants.STORE_ID, storeId)); pathParams.push(new KeyValue(ServiceConstants.ORDER_ID, orderId)); // getting the final url const url = Utils.replacePathParams(restUrl.checkout.submitByos, pathParams); // service calling return this._http.post(url, requestParams, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-CheckoutService.submitByos ()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {any} The Observable for the HTTP request. */ sendPurchaseEstimate(storeId: number, orderId: number, isAALOrder:boolean, requestParams: any): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-CheckoutService.submitByos ()'); } // getting the request options const options = this.httpOptions(); options.headers = options.headers.append('storeId', '' + storeId); let flowType = 'PED-NEW'; if (isAALOrder) flowType = 'PED-AAL'; options.headers = options.headers.append('flowType', flowType); // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue(ServiceConstants.STORE_ID, storeId)); pathParams.push(new KeyValue(ServiceConstants.ORDER_ID, orderId)); // getting the final url const url = Utils.replacePathParams(restUrl.checkout.purchaseEstimate, pathParams); // service calling return this._http.post(url, requestParams, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-CheckoutService.submitByos ()'); } }),// for debug catchError(this.handleError)); } }