@stacksjs/payments 0.70.88 → 0.70.90

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 (37) hide show
  1. package/dist/billable/charge.d.ts +9 -0
  2. package/dist/billable/charge.js +64 -0
  3. package/dist/billable/checkout.d.ts +6 -0
  4. package/dist/billable/checkout.js +43 -0
  5. package/dist/billable/customer.d.ts +15 -0
  6. package/dist/billable/customer.js +117 -0
  7. package/dist/billable/index.d.ts +13 -0
  8. package/dist/billable/index.js +12 -0
  9. package/dist/billable/intent.d.ts +6 -0
  10. package/dist/billable/intent.js +18 -0
  11. package/dist/billable/invoice.d.ts +6 -0
  12. package/dist/billable/invoice.js +14 -0
  13. package/dist/billable/payment-method.d.ts +18 -0
  14. package/dist/billable/payment-method.js +119 -0
  15. package/dist/billable/price.d.ts +6 -0
  16. package/dist/billable/price.js +19 -0
  17. package/dist/billable/product.d.ts +37 -0
  18. package/dist/billable/product.js +101 -0
  19. package/dist/billable/setup-products.d.ts +2 -0
  20. package/dist/billable/setup-products.js +34 -0
  21. package/dist/billable/subscription.d.ts +12 -0
  22. package/dist/billable/subscription.js +138 -0
  23. package/dist/billable/transaction.d.ts +14 -0
  24. package/dist/billable/transaction.js +25 -0
  25. package/dist/billable/webhook.d.ts +155 -0
  26. package/dist/billable/webhook.js +153 -0
  27. package/dist/drivers/stripe.d.ts +8 -0
  28. package/dist/drivers/stripe.js +25 -0
  29. package/dist/idempotency.d.ts +26 -0
  30. package/dist/idempotency.js +13 -0
  31. package/dist/index.d.ts +16 -0
  32. package/dist/index.js +5 -0
  33. package/dist/payment.d.ts +182 -0
  34. package/dist/payment.js +199 -0
  35. package/dist/stripe.backup.d.ts +1 -0
  36. package/dist/stripe.backup.js +0 -0
  37. package/package.json +3 -3
@@ -0,0 +1,182 @@
1
+ import type { UserModel } from '@stacksjs/orm';
2
+ import type Stripe from 'stripe';
3
+ /**
4
+ * Create a one-time charge
5
+ */
6
+ export declare function charge(user: UserModel, amount: number, paymentMethod: string, options?: Partial<Stripe.PaymentIntentCreateParams>): Promise<Stripe.PaymentIntent>;
7
+ /**
8
+ * Create a payment intent (for client-side confirmation)
9
+ */
10
+ export declare function createPayment(user: UserModel, amount: number, options?: Partial<Stripe.PaymentIntentCreateParams>): Promise<Stripe.PaymentIntent>;
11
+ /**
12
+ * Refund a payment
13
+ */
14
+ export declare function refund(paymentIntentId: string, amount?: number, options?: Partial<Stripe.RefundCreateParams>): Promise<Stripe.Refund>;
15
+ /**
16
+ * Create a Stripe Checkout session
17
+ */
18
+ export declare function checkout(user: UserModel, lineItems: Stripe.Checkout.SessionCreateParams.LineItem[], options?: Partial<Stripe.Checkout.SessionCreateParams>): Promise<Stripe.Checkout.Session>;
19
+ /**
20
+ * Create a subscription checkout session
21
+ */
22
+ export declare function subscriptionCheckout(user: UserModel, priceId: string, options?: Partial<Stripe.Checkout.SessionCreateParams>): Promise<Stripe.Checkout.Session>;
23
+ /**
24
+ * Create a new subscription
25
+ */
26
+ export declare function subscribe(user: UserModel, lookupKey: string, options?: Partial<Stripe.SubscriptionCreateParams>): Promise<Stripe.Subscription>;
27
+ /**
28
+ * Cancel a subscription
29
+ */
30
+ export declare function cancelSubscription(subscriptionId: string, immediately?: boolean): Promise<Stripe.Subscription>;
31
+ /**
32
+ * Check if user has an active subscription
33
+ */
34
+ export declare function hasActiveSubscription(user: UserModel, type?: string): Promise<boolean>;
35
+ /**
36
+ * Update subscription to a new price
37
+ */
38
+ export declare function changeSubscription(user: UserModel, newLookupKey: string, type?: string): Promise<Stripe.Subscription>;
39
+ /**
40
+ * Get or create a Stripe customer for a user
41
+ */
42
+ export declare function getOrCreateCustomer(user: UserModel, options?: Stripe.CustomerCreateParams): Promise<Stripe.Customer>;
43
+ /**
44
+ * Update customer details
45
+ */
46
+ export declare function updateCustomer(user: UserModel, options: Stripe.CustomerCreateParams): Promise<Stripe.Customer>;
47
+ /**
48
+ * Delete a customer from Stripe
49
+ */
50
+ export declare function deleteCustomer(user: UserModel): Promise<Stripe.DeletedCustomer>;
51
+ /**
52
+ * Add a payment method to a customer
53
+ */
54
+ export declare function addPaymentMethod(user: UserModel, paymentMethodId: string): Promise<Stripe.PaymentMethod>;
55
+ /**
56
+ * Set the default payment method
57
+ */
58
+ export declare function setDefaultPaymentMethod(user: UserModel, paymentMethodId: string): Promise<Stripe.Customer>;
59
+ /**
60
+ * Remove a payment method.
61
+ * Accepts either a numeric database ID or a Stripe payment method ID string (pm_xxx).
62
+ */
63
+ export declare function removePaymentMethod(user: UserModel, paymentMethodId: string | number): Promise<Stripe.PaymentMethod>;
64
+ /**
65
+ * Create a setup intent for adding payment methods
66
+ */
67
+ export declare function createSetupIntent(user: UserModel, options?: Partial<Stripe.SetupIntentCreateParams>): Promise<Stripe.SetupIntent>;
68
+ /**
69
+ * Get user's invoices
70
+ */
71
+ export declare function getInvoices(user: UserModel): Promise<Stripe.ApiList<Stripe.Invoice>>;
72
+ /**
73
+ * Create an invoice
74
+ */
75
+ export declare function createInvoice(customerId: string, options?: Partial<Stripe.InvoiceCreateParams>): Promise<Stripe.Invoice>;
76
+ /**
77
+ * Pay an invoice
78
+ */
79
+ export declare function payInvoice(invoiceId: string): Promise<Stripe.Invoice>;
80
+ /**
81
+ * Create a product with a price
82
+ */
83
+ export declare function createProduct(name: string, price: number, options?: {
84
+ currency?: string
85
+ interval?: 'day' | 'week' | 'month' | 'year'
86
+ description?: string
87
+ metadata?: Stripe.MetadataParam
88
+ }): Promise<{ product: Stripe.Product, price: Stripe.Price }>;
89
+ /**
90
+ * Get a price by lookup key
91
+ */
92
+ export declare function getPrice(lookupKey: string): Promise<Stripe.Price | undefined>;
93
+ /**
94
+ * List all products
95
+ */
96
+ export declare function listProducts(options?: Stripe.ProductListParams): Promise<Stripe.ApiList<Stripe.Product>>;
97
+ /**
98
+ * Create a discount coupon
99
+ */
100
+ export declare function createCoupon(options: {
101
+ percentOff?: number
102
+ amountOff?: number
103
+ currency?: string
104
+ duration?: 'forever' | 'once' | 'repeating'
105
+ durationInMonths?: number
106
+ name?: string
107
+ maxRedemptions?: number
108
+ }): Promise<Stripe.Coupon>;
109
+ /**
110
+ * Create a promotion code from a coupon
111
+ */
112
+ export declare function createPromoCode(couponId: string, code: string, options?: Partial<Stripe.PromotionCodeCreateParams>): Promise<Stripe.PromotionCode>;
113
+ /**
114
+ * Validate a promo code
115
+ */
116
+ export declare function validatePromoCode(code: string): Promise<Stripe.PromotionCode | null>;
117
+ /**
118
+ * Format amount for display.
119
+ *
120
+ * Currency defaults to the project's configured payment currency
121
+ * (`config.payment.currency` / `STRIPE_CURRENCY`), falling back to USD only
122
+ * when nothing else is set. Locale follows the same pattern via
123
+ * `config.app.locale` so EU merchants see €1.234,56 not $1,234.56.
124
+ */
125
+ export declare function formatAmount(amount: number, currency?: string): string;
126
+ /**
127
+ * Convert dollars to cents
128
+ */
129
+ export declare function toCents(dollars: number): number;
130
+ /**
131
+ * Convert cents to dollars
132
+ */
133
+ export declare function toDollars(cents: number): number;
134
+ // =============================================================================
135
+ // Payment Facade Object
136
+ // =============================================================================
137
+ export declare const Payment: {
138
+ charge: typeof charge;
139
+ createPayment: typeof createPayment;
140
+ refund: typeof refund;
141
+ checkout: typeof checkout;
142
+ subscriptionCheckout: typeof subscriptionCheckout;
143
+ subscribe: typeof subscribe;
144
+ cancelSubscription: typeof cancelSubscription;
145
+ hasActiveSubscription: typeof hasActiveSubscription;
146
+ changeSubscription: typeof changeSubscription;
147
+ getOrCreateCustomer: typeof getOrCreateCustomer;
148
+ updateCustomer: typeof updateCustomer;
149
+ deleteCustomer: typeof deleteCustomer;
150
+ addPaymentMethod: typeof addPaymentMethod;
151
+ setDefaultPaymentMethod: typeof setDefaultPaymentMethod;
152
+ removePaymentMethod: typeof removePaymentMethod;
153
+ createSetupIntent: typeof createSetupIntent;
154
+ getInvoices: typeof getInvoices;
155
+ createInvoice: typeof createInvoice;
156
+ payInvoice: typeof payInvoice;
157
+ createProduct: typeof createProduct;
158
+ getPrice: typeof getPrice;
159
+ listProducts: typeof listProducts;
160
+ createCoupon: typeof createCoupon;
161
+ createPromoCode: typeof createPromoCode;
162
+ validatePromoCode: typeof validatePromoCode;
163
+ formatAmount: typeof formatAmount;
164
+ toCents: typeof toCents;
165
+ toDollars: typeof toDollars;
166
+ webhook: unknown;
167
+ onPaymentIntent: typeof onPaymentIntent;
168
+ onSubscription: typeof onSubscription;
169
+ onInvoice: typeof onInvoice;
170
+ onCheckout: typeof onCheckout;
171
+ onCharge: typeof onCharge;
172
+ processWebhook: typeof processWebhook;
173
+ stripe: typeof stripe;
174
+ customer: unknown;
175
+ subscription: unknown;
176
+ invoice: unknown;
177
+ paymentMethod: unknown;
178
+ product: unknown;
179
+ price: unknown;
180
+ coupon: unknown
181
+ };
182
+ export default Payment;
@@ -0,0 +1,199 @@
1
+ import { stripe } from "./drivers/stripe";
2
+ import { manageCharge } from "./billable/charge";
3
+ import { manageCheckout } from "./billable/checkout";
4
+ import { manageCustomer } from "./billable/customer";
5
+ import { manageInvoice } from "./billable/invoice";
6
+ import { managePaymentMethod } from "./billable/payment-method";
7
+ import { managePrice } from "./billable/price";
8
+ import { manageCoupon, managePriceExtended, manageProduct } from "./billable/product";
9
+ import { manageSubscription } from "./billable/subscription";
10
+ import { manageSetupIntent } from "./billable/intent";
11
+ import {
12
+ manageWebhook,
13
+ onCharge,
14
+ onCheckout,
15
+ onInvoice,
16
+ onPaymentIntent,
17
+ onSubscription,
18
+ processWebhook
19
+ } from "./billable/webhook";
20
+ export async function charge(user, amount, paymentMethod, options = {}) {
21
+ return manageCharge.charge(user, amount, paymentMethod, options);
22
+ }
23
+ export async function createPayment(user, amount, options = {}) {
24
+ return manageCharge.createPayment(user, amount, options);
25
+ }
26
+ export async function refund(paymentIntentId, amount, options = {}) {
27
+ return manageCharge.refund(paymentIntentId, {
28
+ amount,
29
+ ...options
30
+ });
31
+ }
32
+ export async function checkout(user, lineItems, options = {}) {
33
+ return manageCheckout.create(user, {
34
+ line_items: lineItems,
35
+ mode: "payment",
36
+ ...options
37
+ });
38
+ }
39
+ export async function subscriptionCheckout(user, priceId, options = {}) {
40
+ return manageCheckout.create(user, {
41
+ line_items: [{ price: priceId, quantity: 1 }],
42
+ mode: "subscription",
43
+ ...options
44
+ });
45
+ }
46
+ export async function subscribe(user, lookupKey, options = {}) {
47
+ return manageSubscription.create(user, "default", lookupKey, options);
48
+ }
49
+ export async function cancelSubscription(subscriptionId, immediately = !1) {
50
+ return manageSubscription.cancel(subscriptionId, {
51
+ prorate: !immediately,
52
+ invoice_now: immediately
53
+ });
54
+ }
55
+ export async function hasActiveSubscription(user, type = "default") {
56
+ return manageSubscription.isValid(user, type);
57
+ }
58
+ export async function changeSubscription(user, newLookupKey, type = "default") {
59
+ return manageSubscription.update(user, type, newLookupKey, {});
60
+ }
61
+ export async function getOrCreateCustomer(user, options = {}) {
62
+ return manageCustomer.createOrGetStripeUser(user, options);
63
+ }
64
+ export async function updateCustomer(user, options) {
65
+ return manageCustomer.updateStripeCustomer(user, options);
66
+ }
67
+ export async function deleteCustomer(user) {
68
+ return manageCustomer.deleteStripeUser(user);
69
+ }
70
+ export async function addPaymentMethod(user, paymentMethodId) {
71
+ return managePaymentMethod.addPaymentMethod(user, paymentMethodId);
72
+ }
73
+ export async function setDefaultPaymentMethod(user, paymentMethodId) {
74
+ return managePaymentMethod.setUserDefaultPayment(user, paymentMethodId);
75
+ }
76
+ export async function removePaymentMethod(user, paymentMethodId) {
77
+ return managePaymentMethod.deletePaymentMethod(user, paymentMethodId);
78
+ }
79
+ export async function createSetupIntent(user, options = {}) {
80
+ return manageSetupIntent.create(user, options);
81
+ }
82
+ export async function getInvoices(user) {
83
+ return manageInvoice.list(user);
84
+ }
85
+ export async function createInvoice(customerId, options = {}) {
86
+ return stripe.invoices.create({
87
+ customer: customerId,
88
+ ...options
89
+ });
90
+ }
91
+ export async function payInvoice(invoiceId) {
92
+ return stripe.invoices.pay(invoiceId);
93
+ }
94
+ export async function createProduct(name, price, options = {}) {
95
+ const { currency = "usd", interval, description, metadata } = options, priceParams = {
96
+ unit_amount: price,
97
+ currency
98
+ };
99
+ if (interval)
100
+ priceParams.recurring = { interval };
101
+ return manageProduct.createWithPrice({
102
+ name,
103
+ description,
104
+ metadata
105
+ }, priceParams);
106
+ }
107
+ export async function getPrice(lookupKey) {
108
+ return managePrice.retrieveByLookupKey(lookupKey);
109
+ }
110
+ export async function listProducts(options = {}) {
111
+ return manageProduct.list(options);
112
+ }
113
+ export async function createCoupon(options) {
114
+ const params = {
115
+ duration: options.duration || "once"
116
+ };
117
+ if (options.percentOff)
118
+ params.percent_off = options.percentOff;
119
+ else if (options.amountOff) {
120
+ params.amount_off = options.amountOff;
121
+ params.currency = options.currency || "usd";
122
+ }
123
+ if (options.name)
124
+ params.name = options.name;
125
+ if (options.durationInMonths)
126
+ params.duration_in_months = options.durationInMonths;
127
+ if (options.maxRedemptions)
128
+ params.max_redemptions = options.maxRedemptions;
129
+ return manageCoupon.create(params);
130
+ }
131
+ export async function createPromoCode(couponId, code, options = {}) {
132
+ return manageCoupon.createPromotionCode({
133
+ promotion: { type: "coupon", coupon: couponId },
134
+ code,
135
+ ...options
136
+ });
137
+ }
138
+ export async function validatePromoCode(code) {
139
+ return manageCoupon.retrievePromotionCode(code);
140
+ }
141
+ export function formatAmount(amount, currency) {
142
+ const cfg = globalThis.config || {}, ccy = (currency || cfg.payment?.currency || cfg.billing?.currency || process.env.STRIPE_CURRENCY || "usd").toLowerCase(), locale = cfg.app?.locale || "en-US";
143
+ return new Intl.NumberFormat(locale, {
144
+ style: "currency",
145
+ currency: ccy.toUpperCase()
146
+ }).format(amount / 100);
147
+ }
148
+ export function toCents(dollars) {
149
+ return Math.round(dollars * 100);
150
+ }
151
+ export function toDollars(cents) {
152
+ return cents / 100;
153
+ }
154
+ export const Payment = {
155
+ charge,
156
+ createPayment,
157
+ refund,
158
+ checkout,
159
+ subscriptionCheckout,
160
+ subscribe,
161
+ cancelSubscription,
162
+ hasActiveSubscription,
163
+ changeSubscription,
164
+ getOrCreateCustomer,
165
+ updateCustomer,
166
+ deleteCustomer,
167
+ addPaymentMethod,
168
+ setDefaultPaymentMethod,
169
+ removePaymentMethod,
170
+ createSetupIntent,
171
+ getInvoices,
172
+ createInvoice,
173
+ payInvoice,
174
+ createProduct,
175
+ getPrice,
176
+ listProducts,
177
+ createCoupon,
178
+ createPromoCode,
179
+ validatePromoCode,
180
+ formatAmount,
181
+ toCents,
182
+ toDollars,
183
+ webhook: manageWebhook,
184
+ onPaymentIntent,
185
+ onSubscription,
186
+ onInvoice,
187
+ onCheckout,
188
+ onCharge,
189
+ processWebhook,
190
+ stripe,
191
+ customer: manageCustomer,
192
+ subscription: manageSubscription,
193
+ invoice: manageInvoice,
194
+ paymentMethod: managePaymentMethod,
195
+ product: manageProduct,
196
+ price: managePriceExtended,
197
+ coupon: manageCoupon
198
+ };
199
+ export default Payment;
@@ -0,0 +1 @@
1
+
File without changes
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@stacksjs/payments",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "0.70.88",
5
+ "version": "0.70.90",
6
6
  "description": "The Stacks payments package. Currently supporting Stripe.",
7
7
  "author": "Chris Breuer",
8
8
  "contributors": [
@@ -67,9 +67,9 @@
67
67
  }
68
68
  },
69
69
  "devDependencies": {
70
- "@stacksjs/config": "0.70.88",
70
+ "@stacksjs/config": "0.70.90",
71
71
  "better-dx": "^0.2.16",
72
- "@stacksjs/utils": "0.70.88",
72
+ "@stacksjs/utils": "0.70.90",
73
73
  "@stripe/stripe-js": "^8.7.0",
74
74
  "stripe": "^21.0.1"
75
75
  }