|
| 1 | +import type { CommerceMoneyAmountJSON } from '@clerk/types'; |
| 2 | + |
| 3 | +import { type CommerceMoneyAmount, CommercePlan } from './CommercePlan'; |
| 4 | +import type { CommerceSubscriptionItemJSON } from './JSON'; |
| 5 | + |
| 6 | +/** |
| 7 | + * @experimental This is an experimental API for the Billing feature that is available under a public beta, and the API is subject to change. |
| 8 | + * It is advised to pin the SDK version to avoid breaking changes. |
| 9 | + */ |
| 10 | +export class CommerceSubscriptionItem { |
| 11 | + constructor( |
| 12 | + /** |
| 13 | + * The unique identifier for the subscription item. |
| 14 | + */ |
| 15 | + readonly id: string, |
| 16 | + /** |
| 17 | + * The status of the subscription item. |
| 18 | + */ |
| 19 | + readonly status: CommerceSubscriptionItemJSON['status'], |
| 20 | + /** |
| 21 | + * The plan period for the subscription item. |
| 22 | + */ |
| 23 | + readonly planPeriod: 'month' | 'annual', |
| 24 | + /** |
| 25 | + * The start of the current period. |
| 26 | + */ |
| 27 | + readonly periodStart: number, |
| 28 | + /** |
| 29 | + * The next payment information. |
| 30 | + */ |
| 31 | + readonly nextPayment: { |
| 32 | + amount: number; |
| 33 | + date: number; |
| 34 | + } | null, |
| 35 | + /** |
| 36 | + * The current amount for the subscription item. |
| 37 | + */ |
| 38 | + readonly amount: CommerceMoneyAmount | null | undefined, |
| 39 | + /** |
| 40 | + * The plan associated with this subscription item. |
| 41 | + */ |
| 42 | + readonly plan: CommercePlan, |
| 43 | + /** |
| 44 | + * The plan ID. |
| 45 | + */ |
| 46 | + readonly planId: string, |
| 47 | + /** |
| 48 | + * The end of the current period. |
| 49 | + */ |
| 50 | + readonly periodEnd?: number, |
| 51 | + /** |
| 52 | + * When the subscription item was canceled. |
| 53 | + */ |
| 54 | + readonly canceledAt?: number, |
| 55 | + /** |
| 56 | + * When the subscription item became past due. |
| 57 | + */ |
| 58 | + readonly pastDueAt?: number, |
| 59 | + /** |
| 60 | + * The lifetime amount paid for this subscription item. |
| 61 | + */ |
| 62 | + readonly lifetimePaid?: CommerceMoneyAmount | null, |
| 63 | + ) {} |
| 64 | + |
| 65 | + static fromJSON(data: CommerceSubscriptionItemJSON): CommerceSubscriptionItem { |
| 66 | + function formatAmountJSON( |
| 67 | + amount: CommerceMoneyAmountJSON | null | undefined, |
| 68 | + ): CommerceMoneyAmount | null | undefined; |
| 69 | + function formatAmountJSON( |
| 70 | + amount: CommerceMoneyAmountJSON | null | undefined, |
| 71 | + ): CommerceMoneyAmount | null | undefined { |
| 72 | + if (!amount) { |
| 73 | + return amount; |
| 74 | + } |
| 75 | + |
| 76 | + return { |
| 77 | + amount: amount.amount, |
| 78 | + amountFormatted: amount.amount_formatted, |
| 79 | + currency: amount.currency, |
| 80 | + currencySymbol: amount.currency_symbol, |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + return new CommerceSubscriptionItem( |
| 85 | + data.id, |
| 86 | + data.status, |
| 87 | + data.plan_period, |
| 88 | + data.period_start, |
| 89 | + data.next_payment, |
| 90 | + formatAmountJSON(data.amount), |
| 91 | + CommercePlan.fromJSON(data.plan), |
| 92 | + data.plan_id, |
| 93 | + data.period_end, |
| 94 | + data.canceled_at, |
| 95 | + data.past_due_at, |
| 96 | + formatAmountJSON(data.lifetime_paid), |
| 97 | + ); |
| 98 | + } |
| 99 | +} |
0 commit comments