{ "version": 3, "sources": ["apps/partner-center-client/src/app/shared-billing/settings/billing-settings.service.ts"], "sourcesContent": ["import { Injectable } from '@angular/core';\nimport {\n CARD_TYPE,\n CreateMerchantRequest,\n CreateMerchantRequestInterface,\n FundingType,\n GetMerchantRequest,\n GetMerchantResponseInterface,\n MerchantApiService,\n MerchantInterface,\n PaymentCard,\n PaymentCardService,\n PaymentCardType,\n UpdateMerchantRequest,\n UpdateMerchantRequestInterface,\n} from '@galaxy/billing';\nimport { SnackbarService } from '@vendasta/galaxy/snackbar-service';\nimport { BehaviorSubject, Observable, ReplaySubject } from 'rxjs';\nimport { map, share, shareReplay } from 'rxjs/operators';\n\n@Injectable({\n providedIn: 'root',\n})\nexport class BillingSettingsService {\n constructor(\n private merchantApiService: MerchantApiService,\n private paymentCardService: PaymentCardService,\n private snackbarService: SnackbarService,\n ) {}\n\n public get merchantId(): string {\n return this.merchant;\n }\n\n public set merchantId(merchantId: string) {\n this.merchant = merchantId;\n }\n\n public get merchantDetailsInfo(): Observable {\n return this.merchantDetails.asObservable();\n }\n\n public get failed(): Observable {\n return this.hasFailed.asObservable();\n }\n\n public get isMerchantConfigured(): Observable {\n return this.merchantConfigured.asObservable();\n }\n\n public get isPaymentDetailsConfigured(): Observable {\n return this.paymentDetailsConfigured$.asObservable();\n }\n\n public get paymentDetailsInfo(): Observable {\n return this.paymentDetails.asObservable();\n }\n\n public get includeInFinancialRecords$(): Observable {\n return this.merchantDetailsInfo.pipe(\n map((details) => (details && !!details.includeInFinancialRecords ? details.includeInFinancialRecords : false)),\n shareReplay(1),\n );\n }\n\n get loading(): Observable {\n return this.isLoading.asObservable().pipe(share());\n }\n\n get submitting(): Observable {\n return this.isSubmitting.asObservable().pipe(share());\n }\n\n get CreditCardInfoLoading(): Observable {\n return this.isCreditCardInfoLoading$.asObservable();\n }\n\n get loadingValue(): boolean {\n return this.isLoading.getValue();\n }\n\n get submittingValue(): boolean {\n return this.isSubmitting.getValue();\n }\n\n get CreditCardInfoLoadingValue(): boolean {\n return this.isCreditCardInfoLoading$.getValue();\n }\n merchant = '';\n emptyPaymentDetails: PaymentCard = {\n cardId: '',\n fundingType: { value: '', id: FundingType.CREDIT },\n cardType: { value: '', id: PaymentCardType.VISA },\n lastFourDigits: '',\n expiryMonth: 0,\n expiryYear: 0,\n };\n\n merchantDetails: BehaviorSubject = new BehaviorSubject({ merchantId: '' });\n paymentDetails: BehaviorSubject = new BehaviorSubject(this.emptyPaymentDetails);\n\n private hasFailed: BehaviorSubject = new BehaviorSubject(false);\n\n private isLoading: BehaviorSubject = new BehaviorSubject(true);\n private isCreditCardInfoLoading$: BehaviorSubject = new BehaviorSubject(true);\n\n private isSubmitting: BehaviorSubject = new BehaviorSubject(false);\n\n private merchantConfigured: ReplaySubject = new ReplaySubject();\n private paymentDetailsConfigured$: ReplaySubject = new ReplaySubject();\n private paymentDetailsConfigured = false;\n\n static convertApiResponseToMerchantDetails(apiResponse: GetMerchantResponseInterface): MerchantDetails {\n const merchant: MerchantInterface = apiResponse.merchant;\n return {\n merchantId: merchant.merchantId,\n companyName: merchant.companyName,\n contactName: merchant.contactName,\n address: merchant.address,\n city: merchant.city,\n state: merchant.state,\n country: merchant.country,\n zipCode: merchant.zipCode,\n phoneNumber: merchant.phoneNumber,\n emailAddress: merchant.emailAddress,\n additionalEmailAddresses: merchant.additionalEmailAddresses,\n includeInFinancialRecords: merchant.includeInFinancialRecords,\n };\n }\n\n public updateMerchantDetails(details: MerchantDetails): void {\n this.merchantDetails.next(details);\n this.saveMerchantDetails();\n }\n\n public submitCreditCard(token: string, callback: (boolean) => unknown): void {\n let requestObservable;\n if (this.paymentDetailsConfigured) {\n requestObservable = this.paymentCardService.update(this.merchantId, token);\n } else {\n requestObservable = this.paymentCardService.create(this.merchantId, token);\n }\n\n requestObservable.subscribe(\n () => {\n this.loadMerchantCreditCards();\n this.loadMerchantDetails();\n this.snackbarService.successSnack('Payment method updated successfully!');\n },\n (resp) => {\n this.snackbarService.errorSnack(resp.error.message);\n callback(false);\n },\n () => callback(true),\n );\n }\n\n loadMerchantCreditCards(showErrorMessage = true): void {\n this.isCreditCardInfoLoading$.next(true);\n\n this.paymentCardService.getAll(this.merchantId).subscribe({\n next: (c) => {\n if (!(c && c.length)) {\n this.paymentDetailsConfigured$.next(false);\n } else {\n this.paymentDetails.next(c[0]);\n this.paymentDetailsConfigured$.next(true);\n }\n this.isCreditCardInfoLoading$.next(false);\n },\n error: (err) => {\n this.isCreditCardInfoLoading$.next(false);\n this.paymentDetailsConfigured$.next(false);\n if (err.status !== 404 && showErrorMessage) {\n this.snackbarService.errorSnack('Error getting payment details');\n }\n },\n });\n this.paymentDetailsConfigured$.subscribe((isConfigured) => {\n this.paymentDetailsConfigured = isConfigured;\n });\n }\n\n private convertCardTypeToString(cardType: CARD_TYPE): string {\n switch (cardType) {\n case CARD_TYPE.AMEX:\n return 'American Express';\n case CARD_TYPE.VISA:\n return 'Visa';\n case CARD_TYPE.MASTERCARD:\n return 'Mastercard';\n case CARD_TYPE.JCB:\n return 'JCB';\n case CARD_TYPE.DINERS_CLUB:\n return 'Diners Club';\n case CARD_TYPE.DISCOVER:\n return 'Discover';\n default:\n return 'Unknown';\n }\n }\n\n saveMerchantDetails(): void {\n this.isSubmitting.next(true);\n const merchantDetails: MerchantDetails = this.merchantDetails.value;\n const body: UpdateMerchantRequestInterface = {\n merchantId: this.merchant,\n contactName: merchantDetails.contactName,\n companyName: merchantDetails.companyName,\n address: merchantDetails.address,\n city: merchantDetails.city,\n state: merchantDetails.state,\n country: merchantDetails.country,\n zipCode: merchantDetails.zipCode,\n phoneNumber: merchantDetails.phoneNumber,\n emailAddress: merchantDetails.emailAddress,\n additionalEmailAddresses: merchantDetails.additionalEmailAddresses,\n };\n\n this.merchantApiService.update(new UpdateMerchantRequest(body)).subscribe(\n () => this._handleMerchantDetailsResponse(false, true, null, 'Successfully saved contact details'),\n (error) =>\n error.status === 404\n ? this.createNewMerchantDetails(body as CreateMerchantRequestInterface)\n : this._handleMerchantDetailsResponse(true, false, null, 'Error updating contact details'),\n );\n }\n\n createNewMerchantDetails(body: CreateMerchantRequestInterface): void {\n this.merchantApiService.create(new CreateMerchantRequest(body)).subscribe(\n () => this._handleMerchantDetailsResponse(false, true, null, 'Successfully created contact details'),\n () => this._handleMerchantDetailsResponse(true, false, null, 'Error creating contact details'),\n );\n }\n\n private _handleMerchantDetailsResponse(\n hasFailed: boolean,\n isMerchantConfigured: boolean,\n details?: MerchantDetails,\n message?: string,\n ): void {\n if (details) {\n this.merchantDetails.next(details);\n }\n\n if (message) {\n hasFailed ? this.snackbarService.errorSnack(message) : this.snackbarService.successSnack(message);\n }\n\n this.hasFailed.next(hasFailed);\n this.merchantConfigured.next(isMerchantConfigured);\n this.isLoading.next(false);\n this.isSubmitting.next(false);\n }\n\n public resetCache(): void {\n this.merchant = '';\n this.paymentDetailsConfigured = false;\n this.merchantDetails.next({ merchantId: '' });\n this.merchantConfigured.next(false);\n this.paymentDetailsConfigured$.next(false);\n this.paymentDetails.next(this.emptyPaymentDetails);\n this.hasFailed.next(false);\n this.isLoading.next(true);\n this.isCreditCardInfoLoading$.next(true);\n this.isSubmitting.next(false);\n }\n\n loadMerchantDetails(): void {\n this.isLoading.next(true);\n this.hasFailed.next(false);\n const request = new GetMerchantRequest({ merchantId: this.merchant });\n this.merchantApiService.get(request).subscribe(\n (response: GetMerchantResponseInterface) => {\n const details = BillingSettingsService.convertApiResponseToMerchantDetails(response);\n this._handleMerchantDetailsResponse(false, true, details);\n },\n (error) =>\n error.status === 404\n ? this._handleMerchantDetailsResponse(false, false, null)\n : this._handleMerchantDetailsResponse(true, false, {} as MerchantDetails),\n );\n }\n}\n\nexport interface MerchantDetails {\n merchantId: string;\n companyName?: string;\n contactName?: string;\n address?: string;\n city?: string;\n state?: string;\n country?: string;\n zipCode?: string;\n phoneNumber?: string;\n emailAddress?: string;\n additionalEmailAddresses?: string[];\n includeInFinancialRecords?: boolean;\n}\n\nexport interface StripeCardData {\n name: string;\n address_line1: string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AAmB,EACjD,KAAKiB,UAAUyC,KAAK,EAAK,EACzB,KAAKxC,UAAUwC,KAAK,EAAI,EACxB,KAAKvC,yBAAyBuC,KAAK,EAAI,EACvC,KAAKtC,aAAasC,KAAK,EAAK,CAC9B,CAEAU,qBAAmB,CACjB,KAAKlD,UAAUwC,KAAK,EAAI,EACxB,KAAKzC,UAAUyC,KAAK,EAAK,EACzB,IAAMoC,EAAU,IAAIC,EAAmB,CAAEhF,WAAY,KAAKhB,QAAQ,CAAE,EACpE,KAAKH,mBAAmBoG,IAAIF,CAAO,EAAE5B,UAClC+B,GAA0C,CACzC,IAAM/D,EAAUxC,EAAuBmD,oCAAoCoD,CAAQ,EACnF,KAAKP,+BAA+B,GAAO,GAAMxD,CAAO,CAC1D,EACCsC,GACCA,EAAMO,SAAW,IACb,KAAKW,+BAA+B,GAAO,GAAO,IAAI,EACtD,KAAKA,+BAA+B,GAAM,GAAO,CAAA,CAAqB,CAAC,CAEjF,yCAnQWhG,GAAsBwG,EAAAC,CAAA,EAAAD,EAAAE,CAAA,EAAAF,EAAAG,CAAA,CAAA,CAAA,wBAAtB3G,EAAsB4G,QAAtB5G,EAAsB6G,UAAAC,WAFrB,MAAM,CAAA,EAEd,IAAO9G,EAAP+G,SAAO/G,CAAsB,GAAA", "names": ["BillingSettingsService", "constructor", "merchantApiService", "paymentCardService", "snackbarService", "merchant", "emptyPaymentDetails", "cardId", "fundingType", "value", "id", "FundingType", "CREDIT", "cardType", "PaymentCardType", "VISA", "lastFourDigits", "expiryMonth", "expiryYear", "merchantDetails", "BehaviorSubject", "merchantId", "paymentDetails", "hasFailed", "isLoading", "isCreditCardInfoLoading$", "isSubmitting", "merchantConfigured", "ReplaySubject", "paymentDetailsConfigured$", "paymentDetailsConfigured", "merchantDetailsInfo", "asObservable", "failed", "isMerchantConfigured", "isPaymentDetailsConfigured", "paymentDetailsInfo", "includeInFinancialRecords$", "pipe", "map", "details", "includeInFinancialRecords", "shareReplay", "loading", "share", "submitting", "CreditCardInfoLoading", "loadingValue", "getValue", "submittingValue", "CreditCardInfoLoadingValue", "convertApiResponseToMerchantDetails", "apiResponse", "companyName", "contactName", "address", "city", "state", "country", "zipCode", "phoneNumber", "emailAddress", "additionalEmailAddresses", "updateMerchantDetails", "next", "saveMerchantDetails", "submitCreditCard", "token", "callback", "requestObservable", "update", "create", "subscribe", "loadMerchantCreditCards", "loadMerchantDetails", "successSnack", "resp", "errorSnack", "error", "message", "showErrorMessage", "getAll", "c", "length", "err", "status", "isConfigured", "convertCardTypeToString", "CARD_TYPE", "AMEX", "MASTERCARD", "JCB", "DINERS_CLUB", "DISCOVER", "body", "UpdateMerchantRequest", "_handleMerchantDetailsResponse", "createNewMerchantDetails", "CreateMerchantRequest", "resetCache", "request", "GetMerchantRequest", "get", "response", "\u0275\u0275inject", "MerchantApiService", "PaymentCardService", "SnackbarService", "factory", "\u0275fac", "providedIn", "_BillingSettingsService"] }