@apexara/stripe 1.0.2

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.
Files changed (65) hide show
  1. package/README.md +1003 -0
  2. package/dist/apex-stripe.d.ts +23 -0
  3. package/dist/apex-stripe.d.ts.map +1 -0
  4. package/dist/apex-stripe.js +41 -0
  5. package/dist/client.d.ts +7 -0
  6. package/dist/client.d.ts.map +1 -0
  7. package/dist/client.js +14 -0
  8. package/dist/errors/http-error.d.ts +7 -0
  9. package/dist/errors/http-error.d.ts.map +1 -0
  10. package/dist/errors/http-error.js +14 -0
  11. package/dist/index.d.ts +16 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +48 -0
  14. package/dist/models/config.model.d.ts +31 -0
  15. package/dist/models/config.model.d.ts.map +1 -0
  16. package/dist/models/config.model.js +2 -0
  17. package/dist/models/index.d.ts +5 -0
  18. package/dist/models/index.d.ts.map +1 -0
  19. package/dist/models/index.js +20 -0
  20. package/dist/models/stripe.model.d.ts +69 -0
  21. package/dist/models/stripe.model.d.ts.map +1 -0
  22. package/dist/models/stripe.model.js +2 -0
  23. package/dist/models/subscription.model.d.ts +24 -0
  24. package/dist/models/subscription.model.d.ts.map +1 -0
  25. package/dist/models/subscription.model.js +2 -0
  26. package/dist/models/webhook.model.d.ts +43 -0
  27. package/dist/models/webhook.model.d.ts.map +1 -0
  28. package/dist/models/webhook.model.js +2 -0
  29. package/dist/services/checkout/checkout.service.d.ts +31 -0
  30. package/dist/services/checkout/checkout.service.d.ts.map +1 -0
  31. package/dist/services/checkout/checkout.service.js +119 -0
  32. package/dist/services/connected-account/connected-account.service.d.ts +25 -0
  33. package/dist/services/connected-account/connected-account.service.d.ts.map +1 -0
  34. package/dist/services/connected-account/connected-account.service.js +46 -0
  35. package/dist/services/customer/customer.service.d.ts +14 -0
  36. package/dist/services/customer/customer.service.d.ts.map +1 -0
  37. package/dist/services/customer/customer.service.js +23 -0
  38. package/dist/services/invoice/invoice.service.d.ts +24 -0
  39. package/dist/services/invoice/invoice.service.d.ts.map +1 -0
  40. package/dist/services/invoice/invoice.service.js +65 -0
  41. package/dist/services/payment-link/payment-link.service.d.ts +18 -0
  42. package/dist/services/payment-link/payment-link.service.d.ts.map +1 -0
  43. package/dist/services/payment-link/payment-link.service.js +53 -0
  44. package/dist/services/payment-method/payment-method.service.d.ts +43 -0
  45. package/dist/services/payment-method/payment-method.service.d.ts.map +1 -0
  46. package/dist/services/payment-method/payment-method.service.js +89 -0
  47. package/dist/services/price/price.service.d.ts +17 -0
  48. package/dist/services/price/price.service.d.ts.map +1 -0
  49. package/dist/services/price/price.service.js +32 -0
  50. package/dist/services/subscription/subscription.service.d.ts +89 -0
  51. package/dist/services/subscription/subscription.service.d.ts.map +1 -0
  52. package/dist/services/subscription/subscription.service.js +272 -0
  53. package/dist/tests/helpers.d.ts +5 -0
  54. package/dist/tests/helpers.d.ts.map +1 -0
  55. package/dist/tests/helpers.js +96 -0
  56. package/dist/utils/helpers.d.ts +15 -0
  57. package/dist/utils/helpers.d.ts.map +1 -0
  58. package/dist/utils/helpers.js +41 -0
  59. package/dist/webhooks/webhook.dispatcher.d.ts +25 -0
  60. package/dist/webhooks/webhook.dispatcher.d.ts.map +1 -0
  61. package/dist/webhooks/webhook.dispatcher.js +105 -0
  62. package/dist/webhooks/webhook.handler.d.ts +39 -0
  63. package/dist/webhooks/webhook.handler.d.ts.map +1 -0
  64. package/dist/webhooks/webhook.handler.js +70 -0
  65. package/package.json +57 -0
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConnectedAccountService = void 0;
4
+ class ConnectedAccountService {
5
+ constructor(stripe, config) {
6
+ this.stripe = stripe;
7
+ this.config = config;
8
+ }
9
+ /**
10
+ * Creates a new Stripe Connect account and returns its ID.
11
+ */
12
+ async createConnectedAccount(metadata) {
13
+ const account = await this.stripe.accounts.create({
14
+ type: this.config.connectedAccountType,
15
+ metadata,
16
+ });
17
+ return account.id;
18
+ }
19
+ /**
20
+ * Permanently deletes a connected account.
21
+ */
22
+ async deleteConnectedAccount(accountId) {
23
+ await this.stripe.accounts.del(accountId);
24
+ }
25
+ /**
26
+ * Creates a one-time URL for a connected account to complete onboarding or access their dashboard.
27
+ * Defaults to account_onboarding type.
28
+ */
29
+ async createAccountLink(accountId, refreshUrl, returnUrl, type = 'account_onboarding') {
30
+ const link = await this.stripe.accountLinks.create({
31
+ account: accountId,
32
+ type,
33
+ refresh_url: refreshUrl,
34
+ return_url: returnUrl,
35
+ });
36
+ return link.url;
37
+ }
38
+ /**
39
+ * Creates a login link for an Express connected account dashboard.
40
+ */
41
+ async createLoginLink(accountId) {
42
+ const link = await this.stripe.accounts.createLoginLink(accountId);
43
+ return link.url;
44
+ }
45
+ }
46
+ exports.ConnectedAccountService = ConnectedAccountService;
@@ -0,0 +1,14 @@
1
+ import Stripe from 'stripe';
2
+ export declare class CustomerService {
3
+ private readonly stripe;
4
+ constructor(stripe: Stripe);
5
+ /**
6
+ * Creates a new Stripe customer with the given name and email.
7
+ */
8
+ createCustomer(firstName: string, lastName: string, email: string): Promise<Stripe.Customer>;
9
+ /**
10
+ * Sets the default payment method on a customer's invoice settings.
11
+ */
12
+ updateCustomerDefaultPaymentMethod(customerId: string, paymentMethodId: string): Promise<void>;
13
+ }
14
+ //# sourceMappingURL=customer.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"customer.service.d.ts","sourceRoot":"","sources":["../../../src/services/customer/customer.service.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,qBAAa,eAAe;IAGxB,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAN,MAAM,EAAE,MAAM;IAEjC;;OAEG;IACU,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC;IAIzG;;OAEG;IACU,kCAAkC,CAAC,UAAU,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAM5G"}
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CustomerService = void 0;
4
+ class CustomerService {
5
+ constructor(stripe) {
6
+ this.stripe = stripe;
7
+ }
8
+ /**
9
+ * Creates a new Stripe customer with the given name and email.
10
+ */
11
+ async createCustomer(firstName, lastName, email) {
12
+ return this.stripe.customers.create({ name: `${firstName} ${lastName}`, email });
13
+ }
14
+ /**
15
+ * Sets the default payment method on a customer's invoice settings.
16
+ */
17
+ async updateCustomerDefaultPaymentMethod(customerId, paymentMethodId) {
18
+ await this.stripe.customers.update(customerId, {
19
+ invoice_settings: { default_payment_method: paymentMethodId },
20
+ });
21
+ }
22
+ }
23
+ exports.CustomerService = CustomerService;
@@ -0,0 +1,24 @@
1
+ import Stripe from 'stripe';
2
+ import { StripeServiceConfig } from './../../models/config.model';
3
+ import { CreateInvoiceParams, PendingActivationInvoice } from './../../models/stripe.model';
4
+ export declare class InvoiceService {
5
+ private readonly stripe;
6
+ private readonly config;
7
+ constructor(stripe: Stripe, config: Required<StripeServiceConfig>);
8
+ /**
9
+ * Creates, finalizes, and delivers a standalone invoice for a customer.
10
+ * Sends the invoice by email if invoiceCollectionMethod is 'send_invoice', otherwise finalizes it for auto-charge.
11
+ */
12
+ createInvoice(params: CreateInvoiceParams): Promise<Stripe.Invoice>;
13
+ /**
14
+ * Lists payment attempts for an invoice.
15
+ */
16
+ listInvoicePayments(invoiceId: string, params?: Stripe.InvoicePaymentListParams): Promise<Stripe.ApiList<Stripe.InvoicePayment>>;
17
+ /**
18
+ * Returns the pending activation invoice for a customer, or null if none exists.
19
+ * An activation invoice is an open invoice with billing_reason 'subscription_create'
20
+ * whose subscription has not yet been cancelled or expired.
21
+ */
22
+ findPendingActivationInvoice(customerId: string): Promise<PendingActivationInvoice | null>;
23
+ }
24
+ //# sourceMappingURL=invoice.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"invoice.service.d.ts","sourceRoot":"","sources":["../../../src/services/invoice/invoice.service.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,EAAE,mBAAmB,EAAE,wBAAwB,EAAE,MAAM,6BAA6B,CAAC;AAE5F,qBAAa,cAAc;IAGvB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,MAAM;gBADN,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,QAAQ,CAAC,mBAAmB,CAAC;IAExD;;;OAGG;IACU,aAAa,CAAC,MAAM,EAAE,mBAAmB,GAAG,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC;IAqBhF;;OAEG;IACU,mBAAmB,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,wBAAwB,GAAG,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;IAI7I;;;;OAIG;IACU,4BAA4B,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,wBAAwB,GAAG,IAAI,CAAC;CAgCxG"}
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InvoiceService = void 0;
4
+ class InvoiceService {
5
+ constructor(stripe, config) {
6
+ this.stripe = stripe;
7
+ this.config = config;
8
+ }
9
+ /**
10
+ * Creates, finalizes, and delivers a standalone invoice for a customer.
11
+ * Sends the invoice by email if invoiceCollectionMethod is 'send_invoice', otherwise finalizes it for auto-charge.
12
+ */
13
+ async createInvoice(params) {
14
+ const { customerId, amount, description } = params;
15
+ const invoice = await this.stripe.invoices.create({
16
+ customer: customerId,
17
+ collection_method: this.config.invoiceCollectionMethod,
18
+ days_until_due: this.config.invoiceDaysUntilDue,
19
+ pending_invoice_items_behavior: 'exclude',
20
+ metadata: params.metadata,
21
+ ...(params.transferData ? { transfer_data: params.transferData } : {}),
22
+ });
23
+ await this.stripe.invoiceItems.create({ customer: customerId, invoice: invoice.id, amount, currency: this.config.currency, description });
24
+ if (this.config.invoiceCollectionMethod === 'send_invoice') {
25
+ return this.stripe.invoices.sendInvoice(invoice.id);
26
+ }
27
+ return this.stripe.invoices.finalizeInvoice(invoice.id);
28
+ }
29
+ /**
30
+ * Lists payment attempts for an invoice.
31
+ */
32
+ async listInvoicePayments(invoiceId, params) {
33
+ return this.stripe.invoicePayments.list({ invoice: invoiceId, ...params });
34
+ }
35
+ /**
36
+ * Returns the pending activation invoice for a customer, or null if none exists.
37
+ * An activation invoice is an open invoice with billing_reason 'subscription_create'
38
+ * whose subscription has not yet been cancelled or expired.
39
+ */
40
+ async findPendingActivationInvoice(customerId) {
41
+ const invoices = await this.stripe.invoices.list({
42
+ customer: customerId,
43
+ status: 'open',
44
+ limit: 10,
45
+ });
46
+ const invoice = invoices.data.find(i => i.billing_reason === 'subscription_create');
47
+ if (!invoice) {
48
+ return null;
49
+ }
50
+ const sub = invoice.parent?.subscription_details?.subscription;
51
+ const subscriptionId = typeof sub === 'string' ? sub : sub?.id;
52
+ if (!subscriptionId) {
53
+ return null;
54
+ }
55
+ const subscription = await this.stripe.subscriptions.retrieve(subscriptionId);
56
+ if (subscription.status === 'incomplete_expired' || subscription.status === 'canceled') {
57
+ return null;
58
+ }
59
+ return {
60
+ subscriptionId,
61
+ hostedInvoiceUrl: invoice.hosted_invoice_url ?? null,
62
+ };
63
+ }
64
+ }
65
+ exports.InvoiceService = InvoiceService;
@@ -0,0 +1,18 @@
1
+ import Stripe from 'stripe';
2
+ import { StripeServiceConfig } from './../../models/config.model';
3
+ import { CreatePaymentLinkParams, PaymentLinkResult } from './../../models/stripe.model';
4
+ export declare class PaymentLinkService {
5
+ private readonly stripe;
6
+ private readonly config;
7
+ constructor(stripe: Stripe, config: Required<StripeServiceConfig>);
8
+ /**
9
+ * Creates a one-time payment link with an inline price and returns its ID, URL,
10
+ * and the associated product and price IDs.
11
+ */
12
+ createPaymentLink(params: CreatePaymentLinkParams): Promise<PaymentLinkResult>;
13
+ /**
14
+ * Deactivates a payment link and archives its associated product and price.
15
+ */
16
+ archivePaymentLink(linkId: string, productId: string, priceId: string): Promise<void>;
17
+ }
18
+ //# sourceMappingURL=payment-link.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"payment-link.service.d.ts","sourceRoot":"","sources":["../../../src/services/payment-link/payment-link.service.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,EAAE,uBAAuB,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAEzF,qBAAa,kBAAkB;IAG3B,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,MAAM;gBADN,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,QAAQ,CAAC,mBAAmB,CAAC;IAExD;;;OAGG;IACU,iBAAiB,CAAC,MAAM,EAAE,uBAAuB,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAqC3F;;OAEG;IACU,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAMnG"}
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PaymentLinkService = void 0;
4
+ class PaymentLinkService {
5
+ constructor(stripe, config) {
6
+ this.stripe = stripe;
7
+ this.config = config;
8
+ }
9
+ /**
10
+ * Creates a one-time payment link with an inline price and returns its ID, URL,
11
+ * and the associated product and price IDs.
12
+ */
13
+ async createPaymentLink(params) {
14
+ const { name, cost, redirectUrl, affiliate } = params;
15
+ const price = await this.stripe.prices.create({
16
+ currency: this.config.currency,
17
+ unit_amount: cost,
18
+ product_data: { name },
19
+ });
20
+ const linkParams = {
21
+ line_items: [{ price: price.id, quantity: 1 }],
22
+ after_completion: {
23
+ type: 'redirect',
24
+ redirect: { url: redirectUrl },
25
+ },
26
+ metadata: params.metadata,
27
+ allow_promotion_codes: this.config.allowPromotionCodes,
28
+ invoice_creation: {
29
+ enabled: this.config.orderInvoiceCreation,
30
+ invoice_data: { metadata: params.metadata },
31
+ },
32
+ };
33
+ if (affiliate) {
34
+ linkParams.transfer_data = { destination: affiliate.accountId, amount: affiliate.amount };
35
+ }
36
+ const link = await this.stripe.paymentLinks.create(linkParams);
37
+ return {
38
+ id: link.id,
39
+ url: link.url,
40
+ productId: price.product.toString(),
41
+ priceId: price.id,
42
+ };
43
+ }
44
+ /**
45
+ * Deactivates a payment link and archives its associated product and price.
46
+ */
47
+ async archivePaymentLink(linkId, productId, priceId) {
48
+ await this.stripe.paymentLinks.update(linkId, { active: false });
49
+ await this.stripe.products.update(productId, { active: false });
50
+ await this.stripe.prices.update(priceId, { active: false });
51
+ }
52
+ }
53
+ exports.PaymentLinkService = PaymentLinkService;
@@ -0,0 +1,43 @@
1
+ import Stripe from 'stripe';
2
+ export declare class PaymentMethodService {
3
+ private readonly stripe;
4
+ constructor(stripe: Stripe);
5
+ /**
6
+ * Retrieves a payment method by ID.
7
+ */
8
+ getPaymentMethod(paymentMethodId: string): Promise<Stripe.PaymentMethod>;
9
+ /**
10
+ * Lists all payment methods attached to a customer.
11
+ */
12
+ listPaymentMethods(customerId: string): Promise<Stripe.PaymentMethod[]>;
13
+ /**
14
+ * Attaches a payment method to a customer.
15
+ */
16
+ attachPaymentMethod(paymentMethodId: string, customerId: string): Promise<Stripe.PaymentMethod>;
17
+ /**
18
+ * Attaches a payment method and sets it as the default for both the customer's invoice settings
19
+ * and the subscription in a single call.
20
+ */
21
+ setDefaultPaymentMethodForCustomerAndSubscription(customerId: string, subscriptionId: string, paymentMethodId: string): Promise<void>;
22
+ /**
23
+ * Removes a payment method from a customer and subscription.
24
+ * If it is the subscription's default, a different one is assigned first.
25
+ * Throws if it is the only payment method on the customer.
26
+ * Returns the new default payment method ID if the default was reassigned, otherwise null.
27
+ */
28
+ removePaymentMethod(customerId: string, subscriptionId: string, paymentMethodId: string): Promise<{
29
+ newDefaultId: string | null;
30
+ }>;
31
+ /**
32
+ * Sets the default payment method on a subscription.
33
+ */
34
+ setDefaultPaymentMethod(subscriptionId: string, paymentMethodId: string): Promise<void>;
35
+ /**
36
+ * Sets the default payment method on a subscription only if one is not already set.
37
+ * Returns whether the update was applied.
38
+ */
39
+ setDefaultPaymentMethodIfMissing(subscriptionId: string, paymentMethodId: string): Promise<{
40
+ changed: boolean;
41
+ }>;
42
+ }
43
+ //# sourceMappingURL=payment-method.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"payment-method.service.d.ts","sourceRoot":"","sources":["../../../src/services/payment-method/payment-method.service.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAI5B,qBAAa,oBAAoB;IAG7B,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAN,MAAM,EAAE,MAAM;IAEjC;;OAEG;IACU,gBAAgB,CAAC,eAAe,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,aAAa,CAAC;IAIrF;;OAEG;IACU,kBAAkB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,aAAa,EAAE,CAAC;IAMpF;;OAEG;IACU,mBAAmB,CAAC,eAAe,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,aAAa,CAAC;IAI5G;;;OAGG;IACU,iDAAiD,CAAC,UAAU,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAUlJ;;;;;OAKG;IACU,mBAAmB,CAAC,UAAU,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,YAAY,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,CAAC;IA4B/I;;OAEG;IACU,uBAAuB,CAAC,cAAc,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAMpG;;;OAGG;IACU,gCAAgC,CAAC,cAAc,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,CAAC;CAgB9H"}
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PaymentMethodService = void 0;
4
+ const http_error_1 = require("./../../errors/http-error");
5
+ class PaymentMethodService {
6
+ constructor(stripe) {
7
+ this.stripe = stripe;
8
+ }
9
+ /**
10
+ * Retrieves a payment method by ID.
11
+ */
12
+ async getPaymentMethod(paymentMethodId) {
13
+ return this.stripe.paymentMethods.retrieve(paymentMethodId);
14
+ }
15
+ /**
16
+ * Lists all payment methods attached to a customer.
17
+ */
18
+ async listPaymentMethods(customerId) {
19
+ const result = await this.stripe.paymentMethods.list({ customer: customerId });
20
+ return result.data;
21
+ }
22
+ /**
23
+ * Attaches a payment method to a customer.
24
+ */
25
+ async attachPaymentMethod(paymentMethodId, customerId) {
26
+ return this.stripe.paymentMethods.attach(paymentMethodId, { customer: customerId });
27
+ }
28
+ /**
29
+ * Attaches a payment method and sets it as the default for both the customer's invoice settings
30
+ * and the subscription in a single call.
31
+ */
32
+ async setDefaultPaymentMethodForCustomerAndSubscription(customerId, subscriptionId, paymentMethodId) {
33
+ await this.stripe.paymentMethods.attach(paymentMethodId, { customer: customerId });
34
+ await this.stripe.customers.update(customerId, {
35
+ invoice_settings: { default_payment_method: paymentMethodId },
36
+ });
37
+ await this.stripe.subscriptions.update(subscriptionId, { default_payment_method: paymentMethodId });
38
+ }
39
+ /**
40
+ * Removes a payment method from a customer and subscription.
41
+ * If it is the subscription's default, a different one is assigned first.
42
+ * Throws if it is the only payment method on the customer.
43
+ * Returns the new default payment method ID if the default was reassigned, otherwise null.
44
+ */
45
+ async removePaymentMethod(customerId, subscriptionId, paymentMethodId) {
46
+ const paymentMethods = await this.stripe.paymentMethods.list({ customer: customerId });
47
+ if (paymentMethods.data.length <= 1) {
48
+ throw new http_error_1.HttpError(400, 'Cannot remove the only payment method. Add another payment method first.');
49
+ }
50
+ const subscription = await this.stripe.subscriptions.retrieve(subscriptionId);
51
+ const isDefault = subscription.default_payment_method === paymentMethodId;
52
+ let newDefaultId = null;
53
+ if (isDefault) {
54
+ const alternate = paymentMethods.data.find(pm => pm.id !== paymentMethodId);
55
+ if (!alternate) {
56
+ throw new http_error_1.HttpError(400, 'Cannot remove the only payment method. Add another payment method first.');
57
+ }
58
+ await this.stripe.subscriptions.update(subscriptionId, { default_payment_method: alternate.id });
59
+ newDefaultId = alternate.id;
60
+ }
61
+ await this.stripe.paymentMethods.detach(paymentMethodId);
62
+ return { newDefaultId };
63
+ }
64
+ /**
65
+ * Sets the default payment method on a subscription.
66
+ */
67
+ async setDefaultPaymentMethod(subscriptionId, paymentMethodId) {
68
+ await this.stripe.subscriptions.update(subscriptionId, {
69
+ default_payment_method: paymentMethodId,
70
+ });
71
+ }
72
+ /**
73
+ * Sets the default payment method on a subscription only if one is not already set.
74
+ * Returns whether the update was applied.
75
+ */
76
+ async setDefaultPaymentMethodIfMissing(subscriptionId, paymentMethodId) {
77
+ const subscription = await this.stripe.subscriptions.retrieve(subscriptionId, {
78
+ expand: ['default_payment_method'],
79
+ });
80
+ if (subscription.default_payment_method) {
81
+ return { changed: false };
82
+ }
83
+ await this.stripe.subscriptions.update(subscriptionId, {
84
+ default_payment_method: paymentMethodId,
85
+ });
86
+ return { changed: true };
87
+ }
88
+ }
89
+ exports.PaymentMethodService = PaymentMethodService;
@@ -0,0 +1,17 @@
1
+ import Stripe from 'stripe';
2
+ import { StripeServiceConfig } from './../../models/config.model';
3
+ import { CreateCustomPriceParams } from './../../models/stripe.model';
4
+ export declare class PriceService {
5
+ private readonly stripe;
6
+ private readonly config;
7
+ constructor(stripe: Stripe, config: Required<StripeServiceConfig>);
8
+ /**
9
+ * Creates a recurring subscription price attached to an existing Stripe product.
10
+ */
11
+ createCustomSubscriptionPrice(params: CreateCustomPriceParams): Promise<Stripe.Price>;
12
+ /**
13
+ * Archives a price so it can no longer be used for new subscriptions.
14
+ */
15
+ archivePrice(priceId: string): Promise<void>;
16
+ }
17
+ //# sourceMappingURL=price.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"price.service.d.ts","sourceRoot":"","sources":["../../../src/services/price/price.service.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAC;AAEtE,qBAAa,YAAY;IAGrB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,MAAM;gBADN,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,QAAQ,CAAC,mBAAmB,CAAC;IAExD;;OAEG;IACU,6BAA6B,CAAC,MAAM,EAAE,uBAAuB,GAAG,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC;IAclG;;OAEG;IACU,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAI1D"}
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PriceService = void 0;
4
+ class PriceService {
5
+ constructor(stripe, config) {
6
+ this.stripe = stripe;
7
+ this.config = config;
8
+ }
9
+ /**
10
+ * Creates a recurring subscription price attached to an existing Stripe product.
11
+ */
12
+ async createCustomSubscriptionPrice(params) {
13
+ return this.stripe.prices.create({
14
+ unit_amount: params.cost,
15
+ currency: this.config.currency,
16
+ recurring: {
17
+ interval: params.billingPeriod,
18
+ interval_count: params.billingIntervalCount,
19
+ },
20
+ nickname: params.label,
21
+ product: params.stripeProductId,
22
+ metadata: params.metadata,
23
+ });
24
+ }
25
+ /**
26
+ * Archives a price so it can no longer be used for new subscriptions.
27
+ */
28
+ async archivePrice(priceId) {
29
+ await this.stripe.prices.update(priceId, { active: false });
30
+ }
31
+ }
32
+ exports.PriceService = PriceService;
@@ -0,0 +1,89 @@
1
+ import Stripe from 'stripe';
2
+ import { StripeServiceConfig } from './../../models/config.model';
3
+ import { SubscriptionSyncFields } from './../../models/stripe.model';
4
+ import { ScheduleDowngradeOptions, SwitchSubscriptionPlanOptions, CreateInvoiceSubscriptionOptions, CreateInvoiceSubscriptionResult } from './../../models/subscription.model';
5
+ export declare class SubscriptionService {
6
+ private readonly stripe;
7
+ private readonly config;
8
+ constructor(stripe: Stripe, config: Required<StripeServiceConfig>);
9
+ private static readonly phaseTypeNew;
10
+ private static readonly phaseTypeDowngrade;
11
+ /**
12
+ * Retrieves a subscription with its price items expanded.
13
+ */
14
+ getSubscriptionById(subscriptionId: string): Promise<Stripe.Subscription>;
15
+ /**
16
+ * Lists all subscriptions for a customer across all statuses.
17
+ * Returns an empty array if no customerId is provided.
18
+ */
19
+ getAllUserSubscriptions(customerId: string): Promise<Stripe.Subscription[]>;
20
+ /**
21
+ * Updates a subscription with arbitrary Stripe params.
22
+ */
23
+ updateSubscription(subscriptionId: string, params: Stripe.SubscriptionUpdateParams): Promise<Stripe.Subscription>;
24
+ /**
25
+ * Immediately cancels a subscription.
26
+ */
27
+ cancelSubscription(subscriptionId: string): Promise<Stripe.Subscription>;
28
+ /**
29
+ * Schedules a subscription to cancel at the end of the current billing period.
30
+ * Throws if the subscription is not active or past_due.
31
+ */
32
+ cancelSubscriptionAtPeriodEnd(subscriptionId: string): Promise<Stripe.Subscription>;
33
+ /**
34
+ * Resumes a subscription that has cancel_at_period_end set.
35
+ * Throws if cancel_at_period_end is false or the subscription is not active or past_due.
36
+ */
37
+ resumeSubscription(subscriptionId: string): Promise<Stripe.Subscription>;
38
+ /**
39
+ * Returns the subscription's existing schedule or creates one from it.
40
+ */
41
+ getOrCreateSubscriptionSchedule(subscription: Stripe.Subscription): Promise<Stripe.SubscriptionSchedule>;
42
+ /**
43
+ * Schedules a plan downgrade to take effect at the end of the current billing period.
44
+ * Tags the schedule with { type: 'downgrade' } in its metadata so it can be identified
45
+ * and released later by releaseScheduleIfDowngrade.
46
+ */
47
+ scheduleDowngrade(subscription: Stripe.Subscription, newPriceId: string, options?: ScheduleDowngradeOptions): Promise<Stripe.SubscriptionSchedule>;
48
+ /**
49
+ * Returns the downgrade schedule for a subscription, or null if none exists or it is not tagged as a downgrade.
50
+ */
51
+ getDowngradeSchedule(subscription: Stripe.Subscription): Promise<Stripe.SubscriptionSchedule | null>;
52
+ /**
53
+ * Releases the subscription's schedule if it is tagged as a downgrade.
54
+ * Safe to call when no schedule is present — returns false in that case.
55
+ * Returns true if a schedule was released.
56
+ */
57
+ releaseScheduleIfDowngrade(subscription: Stripe.Subscription): Promise<boolean>;
58
+ /**
59
+ * Creates a pending negative invoice item that offsets the customer's next invoice.
60
+ * Pass a positive amount — it is stored as negative automatically.
61
+ * Call this before switchSubscriptionPlan when managing billing credits manually.
62
+ */
63
+ applyPlanCredit(customerId: string, subscriptionId: string, amount: number, currency: string, description?: string, metadata?: Record<string, string>): Promise<void>;
64
+ /**
65
+ * Switches a subscription to a new price immediately, resetting the billing cycle anchor to now.
66
+ * Defaults to proration_behavior: 'none' — pair with applyPlanCredit for manual credits,
67
+ * or pass proration: 'create_prorations' to let Stripe calculate what's owed.
68
+ */
69
+ switchSubscriptionPlan(subscriptionId: string, itemId: string, newPriceId: string, options?: SwitchSubscriptionPlanOptions): Promise<Stripe.Subscription>;
70
+ /**
71
+ * Creates a subscription with send_invoice collection and immediately emails the invoice.
72
+ */
73
+ createInvoiceBasedSubscription(customerId: string, priceId: string, options?: CreateInvoiceSubscriptionOptions): Promise<CreateInvoiceSubscriptionResult>;
74
+ /**
75
+ * Extracts the fields needed to keep a local subscription document in sync with Stripe.
76
+ * Falls back to the provided values if the subscription has no items.
77
+ */
78
+ buildSubscriptionSyncFields(subscription: Stripe.Subscription, fallback?: {
79
+ currentPeriodStart?: Date | null;
80
+ currentPeriodEnd?: Date | null;
81
+ itemId?: string;
82
+ }): SubscriptionSyncFields;
83
+ /**
84
+ * Returns the primary subscription item, preferring the one matching preferredItemId if provided.
85
+ */
86
+ getPrimarySubscriptionItem(subscription: Stripe.Subscription, preferredItemId?: string): Stripe.SubscriptionItem | null;
87
+ private buildDowngradePhases;
88
+ }
89
+ //# sourceMappingURL=subscription.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"subscription.service.d.ts","sourceRoot":"","sources":["../../../src/services/subscription/subscription.service.ts"],"names":[],"mappings":"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"}