@stravigor/cashier 0.1.0 → 0.1.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.
- package/package.json +2 -2
- package/src/billable.ts +3 -7
- package/src/cashier_manager.ts +1 -3
- package/src/cashier_provider.ts +16 -0
- package/src/checkout_builder.ts +1 -1
- package/src/customer.ts +3 -4
- package/src/index.ts +3 -0
- package/src/invoice.ts +1 -4
- package/src/subscription_builder.ts +2 -4
- package/src/subscription_item.ts +1 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stravigor/cashier",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Stripe billing for the Strav framework",
|
|
6
6
|
"license": "MIT",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
},
|
|
11
11
|
"files": ["src/", "stubs/", "package.json", "tsconfig.json"],
|
|
12
12
|
"peerDependencies": {
|
|
13
|
-
"@stravigor/core": "0.2.
|
|
13
|
+
"@stravigor/core": "0.2.6"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"stripe": "^17.4.0"
|
package/src/billable.ts
CHANGED
|
@@ -70,9 +70,7 @@ export function billable<T extends NormalizeConstructor<typeof BaseModel>>(Base:
|
|
|
70
70
|
// ----- Customer -----
|
|
71
71
|
|
|
72
72
|
/** Get or create the Stripe customer record for this user. */
|
|
73
|
-
async createOrGetStripeCustomer(
|
|
74
|
-
params?: Stripe.CustomerCreateParams
|
|
75
|
-
): Promise<CustomerData> {
|
|
73
|
+
async createOrGetStripeCustomer(params?: Stripe.CustomerCreateParams): Promise<CustomerData> {
|
|
76
74
|
return Customer.createOrGet(this, params)
|
|
77
75
|
}
|
|
78
76
|
|
|
@@ -148,7 +146,7 @@ export function billable<T extends NormalizeConstructor<typeof BaseModel>>(Base:
|
|
|
148
146
|
/** Check if the user has an active subscription to a specific price ID. */
|
|
149
147
|
async subscribedToPrice(priceId: string): Promise<boolean> {
|
|
150
148
|
const subs = await Subscription.findByUser(this)
|
|
151
|
-
return subs.some(
|
|
149
|
+
return subs.some(sub => Subscription.valid(sub) && sub.stripePriceId === priceId)
|
|
152
150
|
}
|
|
153
151
|
|
|
154
152
|
/** Check if the subscription is on a grace period. */
|
|
@@ -224,9 +222,7 @@ export function billable<T extends NormalizeConstructor<typeof BaseModel>>(Base:
|
|
|
224
222
|
}
|
|
225
223
|
|
|
226
224
|
/** Create a Stripe SetupIntent for collecting payment info without charging. */
|
|
227
|
-
async createSetupIntent(
|
|
228
|
-
params?: Stripe.SetupIntentCreateParams
|
|
229
|
-
): Promise<Stripe.SetupIntent> {
|
|
225
|
+
async createSetupIntent(params?: Stripe.SetupIntentCreateParams): Promise<Stripe.SetupIntent> {
|
|
230
226
|
return Customer.createSetupIntent(this, params)
|
|
231
227
|
}
|
|
232
228
|
|
package/src/cashier_manager.ts
CHANGED
|
@@ -58,9 +58,7 @@ export default class CashierManager {
|
|
|
58
58
|
|
|
59
59
|
static get stripe(): Stripe {
|
|
60
60
|
if (!CashierManager._stripe) {
|
|
61
|
-
throw new ConfigurationError(
|
|
62
|
-
'CashierManager not configured. Ensure STRIPE_SECRET is set.'
|
|
63
|
-
)
|
|
61
|
+
throw new ConfigurationError('CashierManager not configured. Ensure STRIPE_SECRET is set.')
|
|
64
62
|
}
|
|
65
63
|
return CashierManager._stripe
|
|
66
64
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ServiceProvider } from '@stravigor/core/core'
|
|
2
|
+
import type { Application } from '@stravigor/core/core'
|
|
3
|
+
import CashierManager from './cashier_manager.ts'
|
|
4
|
+
|
|
5
|
+
export default class CashierProvider extends ServiceProvider {
|
|
6
|
+
readonly name = 'cashier'
|
|
7
|
+
override readonly dependencies = ['database']
|
|
8
|
+
|
|
9
|
+
override register(app: Application): void {
|
|
10
|
+
app.singleton(CashierManager)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
override boot(app: Application): void {
|
|
14
|
+
app.resolve(CashierManager)
|
|
15
|
+
}
|
|
16
|
+
}
|
package/src/checkout_builder.ts
CHANGED
package/src/customer.ts
CHANGED
|
@@ -25,10 +25,9 @@ export default class Customer {
|
|
|
25
25
|
static async findByUser(user: unknown): Promise<CustomerData | null> {
|
|
26
26
|
const userId = extractUserId(user)
|
|
27
27
|
const fk = Customer.fk
|
|
28
|
-
const rows = await Customer.sql.unsafe(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
)
|
|
28
|
+
const rows = await Customer.sql.unsafe(`SELECT * FROM "customer" WHERE "${fk}" = $1 LIMIT 1`, [
|
|
29
|
+
userId,
|
|
30
|
+
])
|
|
32
31
|
return rows.length > 0 ? Customer.hydrate(rows[0] as Record<string, unknown>) : null
|
|
33
32
|
}
|
|
34
33
|
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
// Manager
|
|
2
2
|
export { default, default as CashierManager } from './cashier_manager.ts'
|
|
3
3
|
|
|
4
|
+
// Provider
|
|
5
|
+
export { default as CashierProvider } from './cashier_provider.ts'
|
|
6
|
+
|
|
4
7
|
// Static helpers
|
|
5
8
|
export { default as Customer } from './customer.ts'
|
|
6
9
|
export { default as Subscription } from './subscription.ts'
|
package/src/invoice.ts
CHANGED
|
@@ -12,10 +12,7 @@ import Customer from './customer.ts'
|
|
|
12
12
|
*/
|
|
13
13
|
export default class Invoice {
|
|
14
14
|
/** List invoices for a user. */
|
|
15
|
-
static async list(
|
|
16
|
-
user: unknown,
|
|
17
|
-
params?: Stripe.InvoiceListParams
|
|
18
|
-
): Promise<Stripe.Invoice[]> {
|
|
15
|
+
static async list(user: unknown, params?: Stripe.InvoiceListParams): Promise<Stripe.Invoice[]> {
|
|
19
16
|
const customer = await Customer.findByUser(user)
|
|
20
17
|
if (!customer) return []
|
|
21
18
|
|
|
@@ -109,7 +109,7 @@ export default class SubscriptionBuilder {
|
|
|
109
109
|
const customer = await Customer.createOrGet(user)
|
|
110
110
|
|
|
111
111
|
// 2. Build Stripe params
|
|
112
|
-
const items: Stripe.SubscriptionCreateParams.Item[] = this._items.map(
|
|
112
|
+
const items: Stripe.SubscriptionCreateParams.Item[] = this._items.map(item => ({
|
|
113
113
|
price: item.price,
|
|
114
114
|
quantity: item.quantity ?? this._quantity,
|
|
115
115
|
}))
|
|
@@ -163,9 +163,7 @@ export default class SubscriptionBuilder {
|
|
|
163
163
|
subscriptionId: localSub.id,
|
|
164
164
|
stripeId: item.id,
|
|
165
165
|
stripeProductId:
|
|
166
|
-
typeof item.price.product === 'string'
|
|
167
|
-
? item.price.product
|
|
168
|
-
: item.price.product.id,
|
|
166
|
+
typeof item.price.product === 'string' ? item.price.product : item.price.product.id,
|
|
169
167
|
stripePriceId: item.price.id,
|
|
170
168
|
quantity: item.quantity ?? null,
|
|
171
169
|
})
|
package/src/subscription_item.ts
CHANGED
|
@@ -144,9 +144,7 @@ export default class SubscriptionItem {
|
|
|
144
144
|
subscriptionId: localSubId,
|
|
145
145
|
stripeId: item.id,
|
|
146
146
|
stripeProductId:
|
|
147
|
-
typeof item.price.product === 'string'
|
|
148
|
-
? item.price.product
|
|
149
|
-
: item.price.product.id,
|
|
147
|
+
typeof item.price.product === 'string' ? item.price.product : item.price.product.id,
|
|
150
148
|
stripePriceId: item.price.id,
|
|
151
149
|
quantity: item.quantity ?? null,
|
|
152
150
|
})
|