@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 +1 -1
- package/src/errors.ts +6 -0
- package/src/index.ts +2 -0
- package/src/payment_method.ts +8 -2
- package/src/subscription_builder.ts +9 -1
- package/stubs/schemas/receipt.ts +1 -1
package/package.json
CHANGED
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
package/src/payment_method.ts
CHANGED
|
@@ -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
|
-
//
|
|
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
|
-
|
|
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
|
package/stubs/schemas/receipt.ts
CHANGED