@shware/purchase 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/stripe/config.cjs
CHANGED
|
@@ -64,6 +64,10 @@ var Product = class _Product {
|
|
|
64
64
|
};
|
|
65
65
|
var StripeConfig = class _StripeConfig {
|
|
66
66
|
products = /* @__PURE__ */ new Map();
|
|
67
|
+
returnUrl;
|
|
68
|
+
cancelUrl;
|
|
69
|
+
successUrl;
|
|
70
|
+
allowPromotionCodes;
|
|
67
71
|
get productIds() {
|
|
68
72
|
return Array.from(this.products.keys());
|
|
69
73
|
}
|
|
@@ -71,10 +75,14 @@ var StripeConfig = class _StripeConfig {
|
|
|
71
75
|
this.products.set(product.id, product);
|
|
72
76
|
return this;
|
|
73
77
|
};
|
|
74
|
-
constructor() {
|
|
78
|
+
constructor(options) {
|
|
79
|
+
this.returnUrl = options.returnUrl;
|
|
80
|
+
this.cancelUrl = options.cancelUrl;
|
|
81
|
+
this.successUrl = options.successUrl;
|
|
82
|
+
this.allowPromotionCodes = options.allowPromotionCodes;
|
|
75
83
|
}
|
|
76
|
-
static create = () => {
|
|
77
|
-
return new _StripeConfig();
|
|
84
|
+
static create = (options) => {
|
|
85
|
+
return new _StripeConfig(options);
|
|
78
86
|
};
|
|
79
87
|
product = (id, plan = null) => {
|
|
80
88
|
return Product.create(this, id, plan);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/stripe/config.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport ms, { type StringValue } from 'ms';\n\nexport type PriceId = `price_${string}`;\nexport type ProductId = Lowercase<string>;\nexport type CreditConfig = { credits: number; expiresIn: StringValue };\n\nconst addMethod = Symbol('addMethod');\n\nclass Product<P extends ProductId = ProductId, I extends PriceId = never> {\n readonly id: P;\n readonly config: StripeConfig;\n readonly plan: string | null;\n readonly prices: Map<PriceId, CreditConfig>;\n\n defaultPriceId: I | null;\n\n private constructor(config: StripeConfig, id: P, plan: string | null = null) {\n this.id = id;\n this.config = config;\n this.plan = plan;\n this.prices = new Map();\n this.defaultPriceId = null;\n }\n\n static create = <P extends ProductId>(\n config: StripeConfig,\n id: P,\n plan: string | null = null\n ) => {\n return new Product<P>(config, id, plan);\n };\n\n price = <K extends PriceId>(\n priceId: K,\n credit: CreditConfig = { credits: 0, expiresIn: '0' }\n ): Product<P, I | K> => {\n this.prices.set(priceId, credit);\n return this as Product<P, I | K>;\n };\n\n default = (defaultPriceId: I) => {\n this.defaultPriceId = defaultPriceId;\n this.config[addMethod](this as never);\n return this.config;\n };\n}\n\nexport class StripeConfig {\n private products: Map<ProductId, Product> = new Map();\n\n get productIds(): ProductId[] {\n return Array.from(this.products.keys());\n }\n\n [addMethod] = (product: Product) => {\n this.products.set(product.id, product);\n return this;\n };\n\n private constructor() {}\n\n static create = () => {\n return new StripeConfig();\n };\n\n product = (id: ProductId, plan: string | null = null) => {\n return Product.create(this, id, plan);\n };\n\n getPlan = (productId: string): string => {\n const plan = this.products.get(productId as ProductId)?.plan;\n if (!plan) throw new Error(`Plan not found for product ${productId}`);\n return plan;\n };\n\n getMode = (productId: string): 'payment' | 'subscription' => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n return product.plan === null ? 'payment' : 'subscription';\n };\n\n getPriceId = (productId: string): PriceId => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return product.defaultPriceId;\n };\n\n getCreditAmount = (productId: string, priceId?: string): number => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n if (priceId) {\n const price = product.prices.get(priceId as PriceId);\n invariant(price, `Price not found for ${priceId}`);\n return price.credits;\n }\n\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return product.prices.get(product.defaultPriceId)?.credits ?? 0;\n };\n\n private getCreditExpiresIn = (productId: string, priceId?: string): number => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n if (priceId) {\n const price = product.prices.get(priceId as PriceId);\n invariant(price, `Price not found for ${priceId}`);\n return ms(price.expiresIn);\n }\n\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return ms(product.prices.get(product.defaultPriceId)?.expiresIn ?? '0');\n };\n\n getCreditExpiresAt = (productId: string, priceId?: string): string => {\n const expiresIn = this.getCreditExpiresIn(productId, priceId);\n return new Date(Date.now() + expiresIn).toISOString();\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA0B;AAC1B,gBAAqC;AAMrC,IAAM,YAAY,uBAAO,WAAW;AAEpC,IAAM,UAAN,MAAM,SAAoE;AAAA,EAC/D;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAET;AAAA,EAEQ,YAAY,QAAsB,IAAO,OAAsB,MAAM;AAC3E,SAAK,KAAK;AACV,SAAK,SAAS;AACd,SAAK,OAAO;AACZ,SAAK,SAAS,oBAAI,IAAI;AACtB,SAAK,iBAAiB;AAAA,EACxB;AAAA,EAEA,OAAO,SAAS,CACd,QACA,IACA,OAAsB,SACnB;AACH,WAAO,IAAI,SAAW,QAAQ,IAAI,IAAI;AAAA,EACxC;AAAA,EAEA,QAAQ,CACN,SACA,SAAuB,EAAE,SAAS,GAAG,WAAW,IAAI,MAC9B;AACtB,SAAK,OAAO,IAAI,SAAS,MAAM;AAC/B,WAAO;AAAA,EACT;AAAA,EAEA,UAAU,CAAC,mBAAsB;AAC/B,SAAK,iBAAiB;AACtB,SAAK,OAAO,SAAS,EAAE,IAAa;AACpC,WAAO,KAAK;AAAA,EACd;AACF;
|
|
1
|
+
{"version":3,"sources":["../../src/stripe/config.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport ms, { type StringValue } from 'ms';\n\nexport type PriceId = `price_${string}`;\nexport type ProductId = Lowercase<string>;\nexport type CreditConfig = { credits: number; expiresIn: StringValue };\n\nconst addMethod = Symbol('addMethod');\n\nclass Product<P extends ProductId = ProductId, I extends PriceId = never> {\n readonly id: P;\n readonly config: StripeConfig;\n readonly plan: string | null;\n readonly prices: Map<PriceId, CreditConfig>;\n\n defaultPriceId: I | null;\n\n private constructor(config: StripeConfig, id: P, plan: string | null = null) {\n this.id = id;\n this.config = config;\n this.plan = plan;\n this.prices = new Map();\n this.defaultPriceId = null;\n }\n\n static create = <P extends ProductId>(\n config: StripeConfig,\n id: P,\n plan: string | null = null\n ) => {\n return new Product<P>(config, id, plan);\n };\n\n price = <K extends PriceId>(\n priceId: K,\n credit: CreditConfig = { credits: 0, expiresIn: '0' }\n ): Product<P, I | K> => {\n this.prices.set(priceId, credit);\n return this as Product<P, I | K>;\n };\n\n default = (defaultPriceId: I) => {\n this.defaultPriceId = defaultPriceId;\n this.config[addMethod](this as never);\n return this.config;\n };\n}\n\ntype Options = {\n returnUrl: string;\n cancelUrl: string;\n successUrl: `${string}session_id={CHECKOUT_SESSION_ID}${string}`;\n allowPromotionCodes: boolean;\n};\n\nexport class StripeConfig {\n private products: Map<ProductId, Product> = new Map();\n\n public returnUrl: string;\n public cancelUrl: string;\n public successUrl: `${string}session_id={CHECKOUT_SESSION_ID}${string}`;\n public allowPromotionCodes: boolean;\n\n get productIds(): ProductId[] {\n return Array.from(this.products.keys());\n }\n\n [addMethod] = (product: Product) => {\n this.products.set(product.id, product);\n return this;\n };\n\n private constructor(options: Options) {\n this.returnUrl = options.returnUrl;\n this.cancelUrl = options.cancelUrl;\n this.successUrl = options.successUrl;\n this.allowPromotionCodes = options.allowPromotionCodes;\n }\n\n static create = (options: Options) => {\n return new StripeConfig(options);\n };\n\n product = (id: ProductId, plan: string | null = null) => {\n return Product.create(this, id, plan);\n };\n\n getPlan = (productId: string): string => {\n const plan = this.products.get(productId as ProductId)?.plan;\n if (!plan) throw new Error(`Plan not found for product ${productId}`);\n return plan;\n };\n\n getMode = (productId: string): 'payment' | 'subscription' => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n return product.plan === null ? 'payment' : 'subscription';\n };\n\n getPriceId = (productId: string): PriceId => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return product.defaultPriceId;\n };\n\n getCreditAmount = (productId: string, priceId?: string): number => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n if (priceId) {\n const price = product.prices.get(priceId as PriceId);\n invariant(price, `Price not found for ${priceId}`);\n return price.credits;\n }\n\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return product.prices.get(product.defaultPriceId)?.credits ?? 0;\n };\n\n private getCreditExpiresIn = (productId: string, priceId?: string): number => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n if (priceId) {\n const price = product.prices.get(priceId as PriceId);\n invariant(price, `Price not found for ${priceId}`);\n return ms(price.expiresIn);\n }\n\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return ms(product.prices.get(product.defaultPriceId)?.expiresIn ?? '0');\n };\n\n getCreditExpiresAt = (productId: string, priceId?: string): string => {\n const expiresIn = this.getCreditExpiresIn(productId, priceId);\n return new Date(Date.now() + expiresIn).toISOString();\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA0B;AAC1B,gBAAqC;AAMrC,IAAM,YAAY,uBAAO,WAAW;AAEpC,IAAM,UAAN,MAAM,SAAoE;AAAA,EAC/D;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAET;AAAA,EAEQ,YAAY,QAAsB,IAAO,OAAsB,MAAM;AAC3E,SAAK,KAAK;AACV,SAAK,SAAS;AACd,SAAK,OAAO;AACZ,SAAK,SAAS,oBAAI,IAAI;AACtB,SAAK,iBAAiB;AAAA,EACxB;AAAA,EAEA,OAAO,SA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package/dist/stripe/config.d.cts
CHANGED
|
@@ -18,12 +18,22 @@ declare class Product<P extends ProductId = ProductId, I extends PriceId = never
|
|
|
18
18
|
price: <K extends PriceId>(priceId: K, credit?: CreditConfig) => Product<P, I | K>;
|
|
19
19
|
default: (defaultPriceId: I) => StripeConfig;
|
|
20
20
|
}
|
|
21
|
+
type Options = {
|
|
22
|
+
returnUrl: string;
|
|
23
|
+
cancelUrl: string;
|
|
24
|
+
successUrl: `${string}session_id={CHECKOUT_SESSION_ID}${string}`;
|
|
25
|
+
allowPromotionCodes: boolean;
|
|
26
|
+
};
|
|
21
27
|
declare class StripeConfig {
|
|
22
28
|
private products;
|
|
29
|
+
returnUrl: string;
|
|
30
|
+
cancelUrl: string;
|
|
31
|
+
successUrl: `${string}session_id={CHECKOUT_SESSION_ID}${string}`;
|
|
32
|
+
allowPromotionCodes: boolean;
|
|
23
33
|
get productIds(): ProductId[];
|
|
24
34
|
[addMethod]: (product: Product) => this;
|
|
25
35
|
private constructor();
|
|
26
|
-
static create: () => StripeConfig;
|
|
36
|
+
static create: (options: Options) => StripeConfig;
|
|
27
37
|
product: (id: ProductId, plan?: string | null) => Product<Lowercase<string>, never>;
|
|
28
38
|
getPlan: (productId: string) => string;
|
|
29
39
|
getMode: (productId: string) => "payment" | "subscription";
|
package/dist/stripe/config.d.ts
CHANGED
|
@@ -18,12 +18,22 @@ declare class Product<P extends ProductId = ProductId, I extends PriceId = never
|
|
|
18
18
|
price: <K extends PriceId>(priceId: K, credit?: CreditConfig) => Product<P, I | K>;
|
|
19
19
|
default: (defaultPriceId: I) => StripeConfig;
|
|
20
20
|
}
|
|
21
|
+
type Options = {
|
|
22
|
+
returnUrl: string;
|
|
23
|
+
cancelUrl: string;
|
|
24
|
+
successUrl: `${string}session_id={CHECKOUT_SESSION_ID}${string}`;
|
|
25
|
+
allowPromotionCodes: boolean;
|
|
26
|
+
};
|
|
21
27
|
declare class StripeConfig {
|
|
22
28
|
private products;
|
|
29
|
+
returnUrl: string;
|
|
30
|
+
cancelUrl: string;
|
|
31
|
+
successUrl: `${string}session_id={CHECKOUT_SESSION_ID}${string}`;
|
|
32
|
+
allowPromotionCodes: boolean;
|
|
23
33
|
get productIds(): ProductId[];
|
|
24
34
|
[addMethod]: (product: Product) => this;
|
|
25
35
|
private constructor();
|
|
26
|
-
static create: () => StripeConfig;
|
|
36
|
+
static create: (options: Options) => StripeConfig;
|
|
27
37
|
product: (id: ProductId, plan?: string | null) => Product<Lowercase<string>, never>;
|
|
28
38
|
getPlan: (productId: string) => string;
|
|
29
39
|
getMode: (productId: string) => "payment" | "subscription";
|
package/dist/stripe/config.mjs
CHANGED
|
@@ -30,6 +30,10 @@ var Product = class _Product {
|
|
|
30
30
|
};
|
|
31
31
|
var StripeConfig = class _StripeConfig {
|
|
32
32
|
products = /* @__PURE__ */ new Map();
|
|
33
|
+
returnUrl;
|
|
34
|
+
cancelUrl;
|
|
35
|
+
successUrl;
|
|
36
|
+
allowPromotionCodes;
|
|
33
37
|
get productIds() {
|
|
34
38
|
return Array.from(this.products.keys());
|
|
35
39
|
}
|
|
@@ -37,10 +41,14 @@ var StripeConfig = class _StripeConfig {
|
|
|
37
41
|
this.products.set(product.id, product);
|
|
38
42
|
return this;
|
|
39
43
|
};
|
|
40
|
-
constructor() {
|
|
44
|
+
constructor(options) {
|
|
45
|
+
this.returnUrl = options.returnUrl;
|
|
46
|
+
this.cancelUrl = options.cancelUrl;
|
|
47
|
+
this.successUrl = options.successUrl;
|
|
48
|
+
this.allowPromotionCodes = options.allowPromotionCodes;
|
|
41
49
|
}
|
|
42
|
-
static create = () => {
|
|
43
|
-
return new _StripeConfig();
|
|
50
|
+
static create = (options) => {
|
|
51
|
+
return new _StripeConfig(options);
|
|
44
52
|
};
|
|
45
53
|
product = (id, plan = null) => {
|
|
46
54
|
return Product.create(this, id, plan);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/stripe/config.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport ms, { type StringValue } from 'ms';\n\nexport type PriceId = `price_${string}`;\nexport type ProductId = Lowercase<string>;\nexport type CreditConfig = { credits: number; expiresIn: StringValue };\n\nconst addMethod = Symbol('addMethod');\n\nclass Product<P extends ProductId = ProductId, I extends PriceId = never> {\n readonly id: P;\n readonly config: StripeConfig;\n readonly plan: string | null;\n readonly prices: Map<PriceId, CreditConfig>;\n\n defaultPriceId: I | null;\n\n private constructor(config: StripeConfig, id: P, plan: string | null = null) {\n this.id = id;\n this.config = config;\n this.plan = plan;\n this.prices = new Map();\n this.defaultPriceId = null;\n }\n\n static create = <P extends ProductId>(\n config: StripeConfig,\n id: P,\n plan: string | null = null\n ) => {\n return new Product<P>(config, id, plan);\n };\n\n price = <K extends PriceId>(\n priceId: K,\n credit: CreditConfig = { credits: 0, expiresIn: '0' }\n ): Product<P, I | K> => {\n this.prices.set(priceId, credit);\n return this as Product<P, I | K>;\n };\n\n default = (defaultPriceId: I) => {\n this.defaultPriceId = defaultPriceId;\n this.config[addMethod](this as never);\n return this.config;\n };\n}\n\nexport class StripeConfig {\n private products: Map<ProductId, Product> = new Map();\n\n get productIds(): ProductId[] {\n return Array.from(this.products.keys());\n }\n\n [addMethod] = (product: Product) => {\n this.products.set(product.id, product);\n return this;\n };\n\n private constructor() {}\n\n static create = () => {\n return new StripeConfig();\n };\n\n product = (id: ProductId, plan: string | null = null) => {\n return Product.create(this, id, plan);\n };\n\n getPlan = (productId: string): string => {\n const plan = this.products.get(productId as ProductId)?.plan;\n if (!plan) throw new Error(`Plan not found for product ${productId}`);\n return plan;\n };\n\n getMode = (productId: string): 'payment' | 'subscription' => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n return product.plan === null ? 'payment' : 'subscription';\n };\n\n getPriceId = (productId: string): PriceId => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return product.defaultPriceId;\n };\n\n getCreditAmount = (productId: string, priceId?: string): number => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n if (priceId) {\n const price = product.prices.get(priceId as PriceId);\n invariant(price, `Price not found for ${priceId}`);\n return price.credits;\n }\n\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return product.prices.get(product.defaultPriceId)?.credits ?? 0;\n };\n\n private getCreditExpiresIn = (productId: string, priceId?: string): number => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n if (priceId) {\n const price = product.prices.get(priceId as PriceId);\n invariant(price, `Price not found for ${priceId}`);\n return ms(price.expiresIn);\n }\n\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return ms(product.prices.get(product.defaultPriceId)?.expiresIn ?? '0');\n };\n\n getCreditExpiresAt = (productId: string, priceId?: string): string => {\n const expiresIn = this.getCreditExpiresIn(productId, priceId);\n return new Date(Date.now() + expiresIn).toISOString();\n };\n}\n"],"mappings":";AAAA,SAAS,iBAAiB;AAC1B,OAAO,QAA8B;AAMrC,IAAM,YAAY,uBAAO,WAAW;AAEpC,IAAM,UAAN,MAAM,SAAoE;AAAA,EAC/D;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAET;AAAA,EAEQ,YAAY,QAAsB,IAAO,OAAsB,MAAM;AAC3E,SAAK,KAAK;AACV,SAAK,SAAS;AACd,SAAK,OAAO;AACZ,SAAK,SAAS,oBAAI,IAAI;AACtB,SAAK,iBAAiB;AAAA,EACxB;AAAA,EAEA,OAAO,SAAS,CACd,QACA,IACA,OAAsB,SACnB;AACH,WAAO,IAAI,SAAW,QAAQ,IAAI,IAAI;AAAA,EACxC;AAAA,EAEA,QAAQ,CACN,SACA,SAAuB,EAAE,SAAS,GAAG,WAAW,IAAI,MAC9B;AACtB,SAAK,OAAO,IAAI,SAAS,MAAM;AAC/B,WAAO;AAAA,EACT;AAAA,EAEA,UAAU,CAAC,mBAAsB;AAC/B,SAAK,iBAAiB;AACtB,SAAK,OAAO,SAAS,EAAE,IAAa;AACpC,WAAO,KAAK;AAAA,EACd;AACF;
|
|
1
|
+
{"version":3,"sources":["../../src/stripe/config.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport ms, { type StringValue } from 'ms';\n\nexport type PriceId = `price_${string}`;\nexport type ProductId = Lowercase<string>;\nexport type CreditConfig = { credits: number; expiresIn: StringValue };\n\nconst addMethod = Symbol('addMethod');\n\nclass Product<P extends ProductId = ProductId, I extends PriceId = never> {\n readonly id: P;\n readonly config: StripeConfig;\n readonly plan: string | null;\n readonly prices: Map<PriceId, CreditConfig>;\n\n defaultPriceId: I | null;\n\n private constructor(config: StripeConfig, id: P, plan: string | null = null) {\n this.id = id;\n this.config = config;\n this.plan = plan;\n this.prices = new Map();\n this.defaultPriceId = null;\n }\n\n static create = <P extends ProductId>(\n config: StripeConfig,\n id: P,\n plan: string | null = null\n ) => {\n return new Product<P>(config, id, plan);\n };\n\n price = <K extends PriceId>(\n priceId: K,\n credit: CreditConfig = { credits: 0, expiresIn: '0' }\n ): Product<P, I | K> => {\n this.prices.set(priceId, credit);\n return this as Product<P, I | K>;\n };\n\n default = (defaultPriceId: I) => {\n this.defaultPriceId = defaultPriceId;\n this.config[addMethod](this as never);\n return this.config;\n };\n}\n\ntype Options = {\n returnUrl: string;\n cancelUrl: string;\n successUrl: `${string}session_id={CHECKOUT_SESSION_ID}${string}`;\n allowPromotionCodes: boolean;\n};\n\nexport class StripeConfig {\n private products: Map<ProductId, Product> = new Map();\n\n public returnUrl: string;\n public cancelUrl: string;\n public successUrl: `${string}session_id={CHECKOUT_SESSION_ID}${string}`;\n public allowPromotionCodes: boolean;\n\n get productIds(): ProductId[] {\n return Array.from(this.products.keys());\n }\n\n [addMethod] = (product: Product) => {\n this.products.set(product.id, product);\n return this;\n };\n\n private constructor(options: Options) {\n this.returnUrl = options.returnUrl;\n this.cancelUrl = options.cancelUrl;\n this.successUrl = options.successUrl;\n this.allowPromotionCodes = options.allowPromotionCodes;\n }\n\n static create = (options: Options) => {\n return new StripeConfig(options);\n };\n\n product = (id: ProductId, plan: string | null = null) => {\n return Product.create(this, id, plan);\n };\n\n getPlan = (productId: string): string => {\n const plan = this.products.get(productId as ProductId)?.plan;\n if (!plan) throw new Error(`Plan not found for product ${productId}`);\n return plan;\n };\n\n getMode = (productId: string): 'payment' | 'subscription' => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n return product.plan === null ? 'payment' : 'subscription';\n };\n\n getPriceId = (productId: string): PriceId => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return product.defaultPriceId;\n };\n\n getCreditAmount = (productId: string, priceId?: string): number => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n if (priceId) {\n const price = product.prices.get(priceId as PriceId);\n invariant(price, `Price not found for ${priceId}`);\n return price.credits;\n }\n\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return product.prices.get(product.defaultPriceId)?.credits ?? 0;\n };\n\n private getCreditExpiresIn = (productId: string, priceId?: string): number => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n if (priceId) {\n const price = product.prices.get(priceId as PriceId);\n invariant(price, `Price not found for ${priceId}`);\n return ms(price.expiresIn);\n }\n\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return ms(product.prices.get(product.defaultPriceId)?.expiresIn ?? '0');\n };\n\n getCreditExpiresAt = (productId: string, priceId?: string): string => {\n const expiresIn = this.getCreditExpiresIn(productId, priceId);\n return new Date(Date.now() + expiresIn).toISOString();\n };\n}\n"],"mappings":";AAAA,SAAS,iBAAiB;AAC1B,OAAO,QAA8B;AAMrC,IAAM,YAAY,uBAAO,WAAW;AAEpC,IAAM,UAAN,MAAM,SAAoE;AAAA,EAC/D;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAET;AAAA,EAEQ,YAAY,QAAsB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