import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; import { RestURL } from './rest_url'; import { BaseRestService } from './base_rest.service'; import { CheckoutRequest, CheckoutResponse, Invoice, PaymentMethod, PortalResponse, PublicPlan, Subscription, UsageMeter, } from '../model/appdata'; /** * BillingService — shared billing API client. * * Owns all plan/subscription/payment-method/invoice calls. Consumer apps * inject this service rather than duplicating HTTP wiring. See * keel/SHARED_PAYMENT.md for the backend contract. */ @Injectable({ providedIn: 'root' }) export class BillingService extends BaseRestService { /** Public catalog — safe to call before login. */ listPlans(): Observable { return this.http.get(this.url(RestURL.plansURL)); } /** * Create a provider-hosted checkout session; caller redirects to the URL. * * The endpoint is JWT-gated and validates `priceId`, `successUrl`, * `cancelUrl` against server-side allowlists. Configure * `AllowedRedirectHosts` and `AllowedPriceIDs` on the keel deployment * or every checkout will be rejected with 400. */ createCheckout(req: CheckoutRequest): Observable { return this.http.post(this.url(RestURL.checkoutURL), req); } /** Current partner's active subscription. */ getSubscription(): Observable { return this.http.get(this.url(RestURL.subscriptionURL)); } /** Cancel auto-renew on the current subscription. */ cancelSubscription(): Observable { return this.http.post(this.url(RestURL.cancelSubURL), {}); } /** * Upgrade / downgrade the current subscription to `planId` — distinct from * first-time checkout. Routes to keel's `SubscriptionLifecycle.ChangePlan` * (atomic close+open) via an endpoint the app exposes. */ changePlan(planId: string): Observable { return this.http.post(this.url(RestURL.changePlanURL), { planId }); } /** Set the seat quantity on the current active/trial subscription. */ setSeats(seats: number): Observable { return this.http.post(this.url(RestURL.seatsURL), { seats }); } /** Partner's invoice history. */ listInvoices(): Observable { return this.http.get(this.url(RestURL.invoicesURL)); } /** Payment methods on file for the partner. */ listPaymentMethods(): Observable { return this.http.get(this.url(RestURL.paymentMethodsURL)); } /** * Create a provider customer-portal session; caller redirects to the URL so * the partner can manage their payment method / subscription on the * provider's hosted page. */ createPortalSession(): Observable { return this.http.post(this.url(RestURL.portalURL), {}); } /** Current-period usage per resource (used vs plan limit). */ listUsage(): Observable { return this.http.get(this.url(RestURL.usageURL)); } }