@stravigor/cashier 0.4.6 → 0.4.7

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stravigor/cashier",
3
- "version": "0.4.6",
3
+ "version": "0.4.7",
4
4
  "type": "module",
5
5
  "description": "Stripe billing for the Strav framework",
6
6
  "license": "MIT",
package/src/errors.ts CHANGED
@@ -23,3 +23,9 @@ export class SubscriptionNotFoundError extends CashierError {
23
23
  super(`No subscription named "${name}" found for this user.`)
24
24
  }
25
25
  }
26
+
27
+ /** Thrown when a payment method operation fails on Stripe. */
28
+ export class PaymentMethodError extends CashierError {}
29
+
30
+ /** Thrown when a subscription creation fails on Stripe. */
31
+ export class SubscriptionCreationError extends CashierError {}
package/src/index.ts CHANGED
@@ -32,6 +32,8 @@ export {
32
32
  WebhookSignatureError,
33
33
  CustomerNotFoundError,
34
34
  SubscriptionNotFoundError,
35
+ PaymentMethodError,
36
+ SubscriptionCreationError,
35
37
  } from './errors.ts'
36
38
 
37
39
  // Types
@@ -1,6 +1,7 @@
1
1
  import type Stripe from 'stripe'
2
2
  import CashierManager from './cashier_manager.ts'
3
3
  import Customer from './customer.ts'
4
+ import { PaymentMethodError } from './errors.ts'
4
5
 
5
6
  /**
6
7
  * Static helper for managing Stripe payment methods.
@@ -32,8 +33,13 @@ export default class PaymentMethod {
32
33
  await CashierManager.stripe.paymentMethods.attach(paymentMethodId, {
33
34
  customer: customer.stripeId,
34
35
  })
35
- } catch {
36
- // Already attached — ignore
36
+ } catch (err: any) {
37
+ // Only ignore "already attached" errors rethrow everything else
38
+ if (err?.code !== 'resource_already_exists') {
39
+ throw new PaymentMethodError(
40
+ `Failed to attach payment method "${paymentMethodId}": ${err?.message ?? err}`
41
+ )
42
+ }
37
43
  }
38
44
 
39
45
  // Set as default on Stripe
@@ -4,6 +4,7 @@ import CashierManager from './cashier_manager.ts'
4
4
  import Customer from './customer.ts'
5
5
  import Subscription from './subscription.ts'
6
6
  import SubscriptionItem from './subscription_item.ts'
7
+ import { SubscriptionCreationError } from './errors.ts'
7
8
  import type { SubscriptionData } from './types.ts'
8
9
 
9
10
  interface PendingItem {
@@ -142,7 +143,14 @@ export default class SubscriptionBuilder {
142
143
  }
143
144
 
144
145
  // 3. Create on Stripe
145
- const stripeSub = await CashierManager.stripe.subscriptions.create(params)
146
+ let stripeSub: Stripe.Subscription
147
+ try {
148
+ stripeSub = await CashierManager.stripe.subscriptions.create(params)
149
+ } catch (err: any) {
150
+ throw new SubscriptionCreationError(
151
+ `Failed to create Stripe subscription "${this._name}": ${err?.message ?? err}`
152
+ )
153
+ }
146
154
 
147
155
  // 4. Record locally
148
156
  const trialEndsAt = stripeSub.trial_end ? new Date(stripeSub.trial_end * 1000) : null
@@ -1,7 +1,7 @@
1
1
  import { defineSchema, t, Archetype } from '@stravigor/core/schema'
2
2
 
3
3
  export default defineSchema('receipt', {
4
- archetype: Archetype.Event,
4
+ archetype: Archetype.Component,
5
5
  parent: 'user',
6
6
  fields: {
7
7
  stripeId: t.varchar(255).required().unique().index(),