Files
vitanovaPackages/dist/fesm2022/marketplaces-payment.mjs.map

1 line
20 KiB
Plaintext
Raw Normal View History

{"version":3,"file":"marketplaces-payment.mjs","sources":["../../src/payment.config.ts","../../src/marketplace-context.ts","../../src/payment.gateway.ts","../../src/payment.component.ts","../../src/marketplaces-payment.ts"],"sourcesContent":["import { EnvironmentProviders, InjectionToken, makeEnvironmentProviders } from '@angular/core';\n\nexport interface MarketplacesPaymentConfig {\n /** Central payment service URL. It is not the tenant API URL. */\n apiUrl: string;\n marketplaceDomain?: string | (() => string);\n paymentsPath?: string;\n pollIntervalMs?: number;\n}\nexport const MARKETPLACES_PAYMENT_CONFIG = new InjectionToken<MarketplacesPaymentConfig>('@marketplaces/payment config');\nexport function provideMarketplacesPayment(config: MarketplacesPaymentConfig): EnvironmentProviders {\n return makeEnvironmentProviders([{\n provide: MARKETPLACES_PAYMENT_CONFIG,\n useValue: {\n ...config,\n apiUrl: config.apiUrl.replace(/\\/$/, ''),\n paymentsPath: config.paymentsPath ?? '/api/v1/payments',\n pollIntervalMs: config.pollIntervalMs ?? 1500,\n },\n }]);\n}\n","import { HttpHeaders } from '@angular/common/http';\nimport { Injectable, inject } from '@angular/core';\nimport { MARKETPLACES_PAYMENT_CONFIG } from './payment.config';\n\nexport const MARKETPLACE_DOMAIN_HEADER = 'X-Marketplace-Domain';\nexport function normalizeMarketplaceDomain(domain: string): string {\n return domain.trim().toLowerCase().replace(/\\.$/, '');\n}\n@Injectable({ providedIn: 'root' })\nexport class PaymentMarketplaceContext {\n private readonly config = inject(MARKETPLACES_PAYMENT_CONFIG);\n domain(): string {\n const configured = this.config.marketplaceDomain;\n const domain = typeof configured === 'function' ? configured() : configured ?? (typeof location === 'undefined' ? '' : location.hostname);\n return normalizeMarketplaceDomain(domain);\n }\n headers(): HttpHeaders {\n const domain = this.domain();\n if (!domain) throw new Error('Marketplace domain cannot be resolved');\n return new HttpHeaders({ [MARKETPLACE_DOMAIN_HEADER]: domain });\n }\n}\n","import { HttpClient, HttpErrorResponse } from '@angular/common/http';\nimport { Injectable, InjectionToken, inject } from '@angular/core';\nimport { Observable, catchError, throwError } from 'rxjs';\nimport { PaymentMarketplaceContext } from './marketplace-context';\nimport { MARKETPLACES_PAYMENT_CONFIG } from './payment.config';\nimport { PaymentAttempt, PaymentFailure, PaymentMethod, PaymentRequest } from './payment.models';\n\nexport interface MarketplacesPaymentGateway {\n create(method: PaymentMethod, request: PaymentRequest): Observable<PaymentAttempt>;\n status(paymentId: string, method: PaymentMethod): Observable<PaymentAttempt>;\n cancel(paymentId: string): Observable<void>;\n}\nexport const MARKETPLACES_PAYMENT_GATEWAY = new InjectionToken<MarketplacesPaymentGateway>(\n '@marketplaces/payment gateway', { providedIn: 'root', factory: () => inject(HttpMarketplacesPaymentGateway) }\n);\n@Injectable({ providedIn: 'root' })\nexport class HttpMarketplacesPaymentGateway implements MarketplacesPaymentGateway {\n private readonly http = inject(HttpClient);\n private readonly config = inject(MARKETPLACES_PAYMENT_CONFIG);\n private readonly context = inject(PaymentMarketplaceContext);\n create(method: PaymentMethod, request: PaymentRequest): Observable<PaymentAttempt> {\n return this.http.post<PaymentAttempt>(this.baseUrl(), { ...request, method }, { headers: this.context.headers() }).pipe(this.handle(method));\n }\n status(paymentId: string, method: PaymentMethod): Observable<PaymentAttempt> {\n return this.http.get<PaymentAttempt>(`${this.baseUrl()}/${encodeURIComponent(paymentId)}`, { headers: this.context.headers() }).pipe(this.handle(method));\n }\n cancel(paymentId: string): Observable<void> {\n return this.http.post<void>(`${this.baseUrl()}/${encodeURIComponent(paymentId)}/cancel`, {}, { headers: this.context.headers() });\n }\n private baseUrl(): string {\n const path = this.config.paymentsPath ?? '';\n