@stackbe/sdk 0.13.0 → 0.14.0
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/dist/index.d.mts +207 -1
- package/dist/index.d.ts +207 -1
- package/dist/index.js +198 -0
- package/dist/index.mjs +197 -0
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -565,6 +565,56 @@ interface PaymentWebhookPayload {
|
|
|
565
565
|
status: 'succeeded' | 'failed';
|
|
566
566
|
failureReason?: string;
|
|
567
567
|
}
|
|
568
|
+
/** Payment method details */
|
|
569
|
+
interface PaymentMethod {
|
|
570
|
+
id: string;
|
|
571
|
+
customerId: string;
|
|
572
|
+
gateway: 'stripe' | 'paypal' | 'authorize_net';
|
|
573
|
+
type: string;
|
|
574
|
+
last4?: string;
|
|
575
|
+
brand?: string;
|
|
576
|
+
expiryMonth?: number;
|
|
577
|
+
expiryYear?: number;
|
|
578
|
+
isDefault: boolean;
|
|
579
|
+
status: 'active' | 'expired' | 'failed';
|
|
580
|
+
createdAt: string;
|
|
581
|
+
}
|
|
582
|
+
/** Create setup session options */
|
|
583
|
+
interface CreateSetupSessionOptions {
|
|
584
|
+
successUrl: string;
|
|
585
|
+
cancelUrl: string;
|
|
586
|
+
}
|
|
587
|
+
/** Setup session response */
|
|
588
|
+
interface SetupSessionResponse {
|
|
589
|
+
sessionId: string;
|
|
590
|
+
url: string;
|
|
591
|
+
clientSecret?: string;
|
|
592
|
+
}
|
|
593
|
+
/** Attach payment method options */
|
|
594
|
+
interface AttachPaymentMethodOptions {
|
|
595
|
+
gatewayPaymentMethodId: string;
|
|
596
|
+
setAsDefault?: boolean;
|
|
597
|
+
}
|
|
598
|
+
/** Charge subscription result */
|
|
599
|
+
interface ChargeResult {
|
|
600
|
+
chargeId: string;
|
|
601
|
+
success: boolean;
|
|
602
|
+
status: 'succeeded' | 'failed' | 'pending' | 'requires_action';
|
|
603
|
+
gatewayChargeId?: string;
|
|
604
|
+
errorCode?: string;
|
|
605
|
+
errorMessage?: string;
|
|
606
|
+
requiresAction?: boolean;
|
|
607
|
+
actionUrl?: string;
|
|
608
|
+
actionExpiresAt?: string;
|
|
609
|
+
}
|
|
610
|
+
/** Migration to StackBE-managed result */
|
|
611
|
+
interface MigrateToStackbeManagedResult {
|
|
612
|
+
subscriptionId: string;
|
|
613
|
+
billingMode: 'stackbe_managed';
|
|
614
|
+
paymentMethodId: string;
|
|
615
|
+
nextBillingDate: string;
|
|
616
|
+
message: string;
|
|
617
|
+
}
|
|
568
618
|
type SubscriptionCreatedEvent = WebhookEvent<'subscription_created', SubscriptionWebhookPayload>;
|
|
569
619
|
type SubscriptionUpdatedEvent = WebhookEvent<'subscription_updated', SubscriptionWebhookPayload>;
|
|
570
620
|
type SubscriptionCancelledEvent = WebhookEvent<'subscription_cancelled', SubscriptionWebhookPayload>;
|
|
@@ -1348,6 +1398,160 @@ declare class SubscriptionsClient {
|
|
|
1348
1398
|
checkAccess(subscriptionId: string): Promise<{
|
|
1349
1399
|
hasAccess: boolean;
|
|
1350
1400
|
}>;
|
|
1401
|
+
/**
|
|
1402
|
+
* Manually charge a StackBE-managed subscription.
|
|
1403
|
+
*
|
|
1404
|
+
* This is useful for:
|
|
1405
|
+
* - Testing billing in development
|
|
1406
|
+
* - Recovering from failed automatic charges
|
|
1407
|
+
* - Charging ahead of schedule
|
|
1408
|
+
*
|
|
1409
|
+
* The subscription must be in `stackbe_managed` billing mode.
|
|
1410
|
+
*
|
|
1411
|
+
* @example
|
|
1412
|
+
* ```typescript
|
|
1413
|
+
* const result = await stackbe.subscriptions.charge('sub_123');
|
|
1414
|
+
*
|
|
1415
|
+
* if (result.success) {
|
|
1416
|
+
* console.log('Charge successful:', result.chargeId);
|
|
1417
|
+
* } else if (result.requiresAction) {
|
|
1418
|
+
* // Redirect customer to 3DS authentication
|
|
1419
|
+
* window.location.href = result.actionUrl;
|
|
1420
|
+
* } else {
|
|
1421
|
+
* console.log('Charge failed:', result.errorMessage);
|
|
1422
|
+
* }
|
|
1423
|
+
* ```
|
|
1424
|
+
*/
|
|
1425
|
+
charge(subscriptionId: string): Promise<ChargeResult>;
|
|
1426
|
+
/**
|
|
1427
|
+
* Migrate a subscription from gateway-managed to StackBE-managed billing.
|
|
1428
|
+
*
|
|
1429
|
+
* This will:
|
|
1430
|
+
* 1. Extract the payment method from the gateway subscription
|
|
1431
|
+
* 2. Store it in StackBE's PaymentMethod table
|
|
1432
|
+
* 3. Set the subscription to `stackbe_managed` billing mode
|
|
1433
|
+
* 4. Cancel the gateway subscription at period end
|
|
1434
|
+
* 5. StackBE will handle renewals going forward
|
|
1435
|
+
*
|
|
1436
|
+
* Use cases:
|
|
1437
|
+
* - Migrate existing Stripe subscriptions to unified StackBE billing
|
|
1438
|
+
* - Prepare for multi-gateway support
|
|
1439
|
+
* - Gain more control over billing timing and retry logic
|
|
1440
|
+
*
|
|
1441
|
+
* @example
|
|
1442
|
+
* ```typescript
|
|
1443
|
+
* const result = await stackbe.subscriptions.migrateToStackbeManaged('sub_123');
|
|
1444
|
+
*
|
|
1445
|
+
* console.log('Migration complete');
|
|
1446
|
+
* console.log('Payment method:', result.paymentMethodId);
|
|
1447
|
+
* console.log('Next billing date:', result.nextBillingDate);
|
|
1448
|
+
* ```
|
|
1449
|
+
*/
|
|
1450
|
+
migrateToStackbeManaged(subscriptionId: string): Promise<MigrateToStackbeManagedResult>;
|
|
1451
|
+
}
|
|
1452
|
+
|
|
1453
|
+
declare class PaymentMethodsClient {
|
|
1454
|
+
private http;
|
|
1455
|
+
constructor(http: HttpClient);
|
|
1456
|
+
/**
|
|
1457
|
+
* Create a setup session for collecting a payment method.
|
|
1458
|
+
*
|
|
1459
|
+
* Returns a URL to redirect the customer to for secure payment method collection.
|
|
1460
|
+
* This is similar to Stripe Checkout but in setup mode - no charge is made.
|
|
1461
|
+
*
|
|
1462
|
+
* @example
|
|
1463
|
+
* ```typescript
|
|
1464
|
+
* const session = await stackbe.paymentMethods.createSetupSession('cust_123', {
|
|
1465
|
+
* successUrl: 'https://yourapp.com/payment-methods/success',
|
|
1466
|
+
* cancelUrl: 'https://yourapp.com/payment-methods/cancel',
|
|
1467
|
+
* });
|
|
1468
|
+
*
|
|
1469
|
+
* // Redirect customer to payment method collection
|
|
1470
|
+
* window.location.href = session.url;
|
|
1471
|
+
* ```
|
|
1472
|
+
*/
|
|
1473
|
+
createSetupSession(customerId: string, options: CreateSetupSessionOptions): Promise<SetupSessionResponse>;
|
|
1474
|
+
/**
|
|
1475
|
+
* Attach a payment method to a customer.
|
|
1476
|
+
*
|
|
1477
|
+
* After the customer completes the setup session, call this to store
|
|
1478
|
+
* the payment method in StackBE for future charges.
|
|
1479
|
+
*
|
|
1480
|
+
* @example
|
|
1481
|
+
* ```typescript
|
|
1482
|
+
* // After redirect from setup session
|
|
1483
|
+
* const paymentMethod = await stackbe.paymentMethods.attach('cust_123', {
|
|
1484
|
+
* gatewayPaymentMethodId: 'pm_1234...', // From setup session completion
|
|
1485
|
+
* setAsDefault: true,
|
|
1486
|
+
* });
|
|
1487
|
+
*
|
|
1488
|
+
* console.log(`Added ${paymentMethod.brand} ending in ${paymentMethod.last4}`);
|
|
1489
|
+
* ```
|
|
1490
|
+
*/
|
|
1491
|
+
attach(customerId: string, options: AttachPaymentMethodOptions): Promise<PaymentMethod>;
|
|
1492
|
+
/**
|
|
1493
|
+
* List all payment methods for a customer.
|
|
1494
|
+
*
|
|
1495
|
+
* @example
|
|
1496
|
+
* ```typescript
|
|
1497
|
+
* const paymentMethods = await stackbe.paymentMethods.list('cust_123');
|
|
1498
|
+
*
|
|
1499
|
+
* for (const pm of paymentMethods) {
|
|
1500
|
+
* console.log(`${pm.brand} **** ${pm.last4} ${pm.isDefault ? '(default)' : ''}`);
|
|
1501
|
+
* }
|
|
1502
|
+
* ```
|
|
1503
|
+
*/
|
|
1504
|
+
list(customerId: string, options?: {
|
|
1505
|
+
status?: string;
|
|
1506
|
+
}): Promise<PaymentMethod[]>;
|
|
1507
|
+
/**
|
|
1508
|
+
* Get a specific payment method.
|
|
1509
|
+
*
|
|
1510
|
+
* @example
|
|
1511
|
+
* ```typescript
|
|
1512
|
+
* const pm = await stackbe.paymentMethods.get('cust_123', 'pm_123');
|
|
1513
|
+
* console.log(`Expires: ${pm.expiryMonth}/${pm.expiryYear}`);
|
|
1514
|
+
* ```
|
|
1515
|
+
*/
|
|
1516
|
+
get(customerId: string, paymentMethodId: string): Promise<PaymentMethod>;
|
|
1517
|
+
/**
|
|
1518
|
+
* Set a payment method as the default for a customer.
|
|
1519
|
+
*
|
|
1520
|
+
* The default payment method is used for subscription renewals.
|
|
1521
|
+
*
|
|
1522
|
+
* @example
|
|
1523
|
+
* ```typescript
|
|
1524
|
+
* await stackbe.paymentMethods.setDefault('cust_123', 'pm_456');
|
|
1525
|
+
* console.log('Default payment method updated');
|
|
1526
|
+
* ```
|
|
1527
|
+
*/
|
|
1528
|
+
setDefault(customerId: string, paymentMethodId: string): Promise<PaymentMethod>;
|
|
1529
|
+
/**
|
|
1530
|
+
* Remove a payment method.
|
|
1531
|
+
*
|
|
1532
|
+
* Cannot remove a payment method that is being used by an active subscription.
|
|
1533
|
+
*
|
|
1534
|
+
* @example
|
|
1535
|
+
* ```typescript
|
|
1536
|
+
* await stackbe.paymentMethods.remove('cust_123', 'pm_old');
|
|
1537
|
+
* console.log('Payment method removed');
|
|
1538
|
+
* ```
|
|
1539
|
+
*/
|
|
1540
|
+
remove(customerId: string, paymentMethodId: string): Promise<{
|
|
1541
|
+
success: boolean;
|
|
1542
|
+
}>;
|
|
1543
|
+
/**
|
|
1544
|
+
* Get the default payment method for a customer.
|
|
1545
|
+
*
|
|
1546
|
+
* @example
|
|
1547
|
+
* ```typescript
|
|
1548
|
+
* const defaultPM = await stackbe.paymentMethods.getDefault('cust_123');
|
|
1549
|
+
* if (defaultPM) {
|
|
1550
|
+
* console.log(`Default: ${defaultPM.brand} **** ${defaultPM.last4}`);
|
|
1551
|
+
* }
|
|
1552
|
+
* ```
|
|
1553
|
+
*/
|
|
1554
|
+
getDefault(customerId: string): Promise<PaymentMethod | null>;
|
|
1351
1555
|
}
|
|
1352
1556
|
|
|
1353
1557
|
interface AuthClientOptions {
|
|
@@ -2665,6 +2869,8 @@ declare class StackBE {
|
|
|
2665
2869
|
readonly checkout: CheckoutClient;
|
|
2666
2870
|
/** Subscription management */
|
|
2667
2871
|
readonly subscriptions: SubscriptionsClient;
|
|
2872
|
+
/** Payment method management (StackBE-managed billing) */
|
|
2873
|
+
readonly paymentMethods: PaymentMethodsClient;
|
|
2668
2874
|
/** Customer authentication (magic links) */
|
|
2669
2875
|
readonly auth: AuthClient;
|
|
2670
2876
|
/** Organization management (B2B multi-user) */
|
|
@@ -2783,4 +2989,4 @@ declare class StackBE {
|
|
|
2783
2989
|
}): (req: any, res: any, next: any) => Promise<any>;
|
|
2784
2990
|
}
|
|
2785
2991
|
|
|
2786
|
-
export { type AddMemberOptions, type AffiliateCommission, type AffiliateCommissionsResponse, type AffiliateEnrollment, type AffiliateInfo, type AffiliateStats, AffiliatesClient, AnalyticsClient, type AnalyticsFilterOptions, type AnyWebhookEvent, AuthClient, type BillingPortalSession, type CancelSubscriptionOptions, type CancelSubscriptionResponse, type CheckEntitlementResponse, type CheckUsageResponse, CheckoutClient, type CheckoutSessionResponse, type Coupon, type CouponDiscountType, type CouponDuration, CouponsClient, type CreateCheckoutOptions, type CreateCouponOptions, type CreateCustomerOptions, type CreateEarlyAccessSignupOptions, type CreateFeatureRequestOptions, type CreateOrganizationOptions, type Customer, type CustomerCreatedEvent, type CustomerFeatureActivity, type CustomerInvoice, type CustomerInvoicesResponse, type CustomerPaymentMethod, type CustomerSubscriptionHistory, type CustomerSubscriptionHistoryResponse, type CustomerUpdatedEvent, type CustomerUsageResponse, type CustomerWebhookPayload, type CustomerWithSubscription, CustomersClient, type DashboardMetrics, type DunningStatus, EarlyAccessClient, type EarlyAccessSignup, type EarlyAccessSignupListResponse, type EnrollOptions, EntitlementsClient, type EntitlementsResponse, type FeatureRequest, type FeatureRequestComment, type FeatureRequestImage, type FeatureRequestListResponse, FeatureRequestsClient, type FeatureUsagePoint, type GetSubscriptionOptions, type GrowthChartPoint, type GrowthMetrics, type InterestedCustomer, type InterestedCustomersResponse, type InviteMemberOptions, type ListEarlyAccessOptions, type ListFeatureRequestsOptions, type ListPlansOptions, type ListProductsOptions, type MRRHistoryPoint, type MagicLinkOptions, type MagicLinkResponse, type Organization, type OrganizationInvite, type OrganizationMember, OrganizationsClient, type PaymentFailedEvent, type PaymentSucceededEvent, type PaymentWebhookPayload, type PerformanceMetrics, type Plan, type PlanMetrics, PlansClient, type PopularEndpoint, type Product, ProductsClient, type RequestCountPoint, type RevenueByPlan, type SessionResponse, StackBE, type StackBEConfig, StackBEError, type StackBEErrorCode, type StackBEErrorResponse, type Subscription, type SubscriptionCancelledEvent, type SubscriptionCreatedEvent, type SubscriptionPlan, type SubscriptionRenewedEvent, type SubscriptionUpdatedEvent, type SubscriptionWebhookPayload, type SubscriptionWithPlan, SubscriptionsClient, type SwitchOrganizationResponse, type TimeRangeOptions, type TrackReferralResponse, type TrackUsageOptions, type TrackUsageResponse, type TrialEndedEvent, type TrialMetrics, type TrialStartedEvent, type TrialStatus, type UpcomingInvoice, type UpdateCouponOptions, type UpdateCustomerOptions, type UpdateOrganizationOptions, type UpdateSubscriptionOptions, UsageClient, type UsageMetric, type ValidateCouponResult, type VerifyTokenResponse, type WebhookEvent, type WebhookEventType };
|
|
2992
|
+
export { type AddMemberOptions, type AffiliateCommission, type AffiliateCommissionsResponse, type AffiliateEnrollment, type AffiliateInfo, type AffiliateStats, AffiliatesClient, AnalyticsClient, type AnalyticsFilterOptions, type AnyWebhookEvent, type AttachPaymentMethodOptions, AuthClient, type BillingPortalSession, type CancelSubscriptionOptions, type CancelSubscriptionResponse, type ChargeResult, type CheckEntitlementResponse, type CheckUsageResponse, CheckoutClient, type CheckoutSessionResponse, type Coupon, type CouponDiscountType, type CouponDuration, CouponsClient, type CreateCheckoutOptions, type CreateCouponOptions, type CreateCustomerOptions, type CreateEarlyAccessSignupOptions, type CreateFeatureRequestOptions, type CreateOrganizationOptions, type CreateSetupSessionOptions, type Customer, type CustomerCreatedEvent, type CustomerFeatureActivity, type CustomerInvoice, type CustomerInvoicesResponse, type CustomerPaymentMethod, type CustomerSubscriptionHistory, type CustomerSubscriptionHistoryResponse, type CustomerUpdatedEvent, type CustomerUsageResponse, type CustomerWebhookPayload, type CustomerWithSubscription, CustomersClient, type DashboardMetrics, type DunningStatus, EarlyAccessClient, type EarlyAccessSignup, type EarlyAccessSignupListResponse, type EnrollOptions, EntitlementsClient, type EntitlementsResponse, type FeatureRequest, type FeatureRequestComment, type FeatureRequestImage, type FeatureRequestListResponse, FeatureRequestsClient, type FeatureUsagePoint, type GetSubscriptionOptions, type GrowthChartPoint, type GrowthMetrics, type InterestedCustomer, type InterestedCustomersResponse, type InviteMemberOptions, type ListEarlyAccessOptions, type ListFeatureRequestsOptions, type ListPlansOptions, type ListProductsOptions, type MRRHistoryPoint, type MagicLinkOptions, type MagicLinkResponse, type MigrateToStackbeManagedResult, type Organization, type OrganizationInvite, type OrganizationMember, OrganizationsClient, type PaymentFailedEvent, type PaymentMethod, PaymentMethodsClient, type PaymentSucceededEvent, type PaymentWebhookPayload, type PerformanceMetrics, type Plan, type PlanMetrics, PlansClient, type PopularEndpoint, type Product, ProductsClient, type RequestCountPoint, type RevenueByPlan, type SessionResponse, type SetupSessionResponse, StackBE, type StackBEConfig, StackBEError, type StackBEErrorCode, type StackBEErrorResponse, type Subscription, type SubscriptionCancelledEvent, type SubscriptionCreatedEvent, type SubscriptionPlan, type SubscriptionRenewedEvent, type SubscriptionUpdatedEvent, type SubscriptionWebhookPayload, type SubscriptionWithPlan, SubscriptionsClient, type SwitchOrganizationResponse, type TimeRangeOptions, type TrackReferralResponse, type TrackUsageOptions, type TrackUsageResponse, type TrialEndedEvent, type TrialMetrics, type TrialStartedEvent, type TrialStatus, type UpcomingInvoice, type UpdateCouponOptions, type UpdateCustomerOptions, type UpdateOrganizationOptions, type UpdateSubscriptionOptions, UsageClient, type UsageMetric, type ValidateCouponResult, type VerifyTokenResponse, type WebhookEvent, type WebhookEventType };
|
package/dist/index.d.ts
CHANGED
|
@@ -565,6 +565,56 @@ interface PaymentWebhookPayload {
|
|
|
565
565
|
status: 'succeeded' | 'failed';
|
|
566
566
|
failureReason?: string;
|
|
567
567
|
}
|
|
568
|
+
/** Payment method details */
|
|
569
|
+
interface PaymentMethod {
|
|
570
|
+
id: string;
|
|
571
|
+
customerId: string;
|
|
572
|
+
gateway: 'stripe' | 'paypal' | 'authorize_net';
|
|
573
|
+
type: string;
|
|
574
|
+
last4?: string;
|
|
575
|
+
brand?: string;
|
|
576
|
+
expiryMonth?: number;
|
|
577
|
+
expiryYear?: number;
|
|
578
|
+
isDefault: boolean;
|
|
579
|
+
status: 'active' | 'expired' | 'failed';
|
|
580
|
+
createdAt: string;
|
|
581
|
+
}
|
|
582
|
+
/** Create setup session options */
|
|
583
|
+
interface CreateSetupSessionOptions {
|
|
584
|
+
successUrl: string;
|
|
585
|
+
cancelUrl: string;
|
|
586
|
+
}
|
|
587
|
+
/** Setup session response */
|
|
588
|
+
interface SetupSessionResponse {
|
|
589
|
+
sessionId: string;
|
|
590
|
+
url: string;
|
|
591
|
+
clientSecret?: string;
|
|
592
|
+
}
|
|
593
|
+
/** Attach payment method options */
|
|
594
|
+
interface AttachPaymentMethodOptions {
|
|
595
|
+
gatewayPaymentMethodId: string;
|
|
596
|
+
setAsDefault?: boolean;
|
|
597
|
+
}
|
|
598
|
+
/** Charge subscription result */
|
|
599
|
+
interface ChargeResult {
|
|
600
|
+
chargeId: string;
|
|
601
|
+
success: boolean;
|
|
602
|
+
status: 'succeeded' | 'failed' | 'pending' | 'requires_action';
|
|
603
|
+
gatewayChargeId?: string;
|
|
604
|
+
errorCode?: string;
|
|
605
|
+
errorMessage?: string;
|
|
606
|
+
requiresAction?: boolean;
|
|
607
|
+
actionUrl?: string;
|
|
608
|
+
actionExpiresAt?: string;
|
|
609
|
+
}
|
|
610
|
+
/** Migration to StackBE-managed result */
|
|
611
|
+
interface MigrateToStackbeManagedResult {
|
|
612
|
+
subscriptionId: string;
|
|
613
|
+
billingMode: 'stackbe_managed';
|
|
614
|
+
paymentMethodId: string;
|
|
615
|
+
nextBillingDate: string;
|
|
616
|
+
message: string;
|
|
617
|
+
}
|
|
568
618
|
type SubscriptionCreatedEvent = WebhookEvent<'subscription_created', SubscriptionWebhookPayload>;
|
|
569
619
|
type SubscriptionUpdatedEvent = WebhookEvent<'subscription_updated', SubscriptionWebhookPayload>;
|
|
570
620
|
type SubscriptionCancelledEvent = WebhookEvent<'subscription_cancelled', SubscriptionWebhookPayload>;
|
|
@@ -1348,6 +1398,160 @@ declare class SubscriptionsClient {
|
|
|
1348
1398
|
checkAccess(subscriptionId: string): Promise<{
|
|
1349
1399
|
hasAccess: boolean;
|
|
1350
1400
|
}>;
|
|
1401
|
+
/**
|
|
1402
|
+
* Manually charge a StackBE-managed subscription.
|
|
1403
|
+
*
|
|
1404
|
+
* This is useful for:
|
|
1405
|
+
* - Testing billing in development
|
|
1406
|
+
* - Recovering from failed automatic charges
|
|
1407
|
+
* - Charging ahead of schedule
|
|
1408
|
+
*
|
|
1409
|
+
* The subscription must be in `stackbe_managed` billing mode.
|
|
1410
|
+
*
|
|
1411
|
+
* @example
|
|
1412
|
+
* ```typescript
|
|
1413
|
+
* const result = await stackbe.subscriptions.charge('sub_123');
|
|
1414
|
+
*
|
|
1415
|
+
* if (result.success) {
|
|
1416
|
+
* console.log('Charge successful:', result.chargeId);
|
|
1417
|
+
* } else if (result.requiresAction) {
|
|
1418
|
+
* // Redirect customer to 3DS authentication
|
|
1419
|
+
* window.location.href = result.actionUrl;
|
|
1420
|
+
* } else {
|
|
1421
|
+
* console.log('Charge failed:', result.errorMessage);
|
|
1422
|
+
* }
|
|
1423
|
+
* ```
|
|
1424
|
+
*/
|
|
1425
|
+
charge(subscriptionId: string): Promise<ChargeResult>;
|
|
1426
|
+
/**
|
|
1427
|
+
* Migrate a subscription from gateway-managed to StackBE-managed billing.
|
|
1428
|
+
*
|
|
1429
|
+
* This will:
|
|
1430
|
+
* 1. Extract the payment method from the gateway subscription
|
|
1431
|
+
* 2. Store it in StackBE's PaymentMethod table
|
|
1432
|
+
* 3. Set the subscription to `stackbe_managed` billing mode
|
|
1433
|
+
* 4. Cancel the gateway subscription at period end
|
|
1434
|
+
* 5. StackBE will handle renewals going forward
|
|
1435
|
+
*
|
|
1436
|
+
* Use cases:
|
|
1437
|
+
* - Migrate existing Stripe subscriptions to unified StackBE billing
|
|
1438
|
+
* - Prepare for multi-gateway support
|
|
1439
|
+
* - Gain more control over billing timing and retry logic
|
|
1440
|
+
*
|
|
1441
|
+
* @example
|
|
1442
|
+
* ```typescript
|
|
1443
|
+
* const result = await stackbe.subscriptions.migrateToStackbeManaged('sub_123');
|
|
1444
|
+
*
|
|
1445
|
+
* console.log('Migration complete');
|
|
1446
|
+
* console.log('Payment method:', result.paymentMethodId);
|
|
1447
|
+
* console.log('Next billing date:', result.nextBillingDate);
|
|
1448
|
+
* ```
|
|
1449
|
+
*/
|
|
1450
|
+
migrateToStackbeManaged(subscriptionId: string): Promise<MigrateToStackbeManagedResult>;
|
|
1451
|
+
}
|
|
1452
|
+
|
|
1453
|
+
declare class PaymentMethodsClient {
|
|
1454
|
+
private http;
|
|
1455
|
+
constructor(http: HttpClient);
|
|
1456
|
+
/**
|
|
1457
|
+
* Create a setup session for collecting a payment method.
|
|
1458
|
+
*
|
|
1459
|
+
* Returns a URL to redirect the customer to for secure payment method collection.
|
|
1460
|
+
* This is similar to Stripe Checkout but in setup mode - no charge is made.
|
|
1461
|
+
*
|
|
1462
|
+
* @example
|
|
1463
|
+
* ```typescript
|
|
1464
|
+
* const session = await stackbe.paymentMethods.createSetupSession('cust_123', {
|
|
1465
|
+
* successUrl: 'https://yourapp.com/payment-methods/success',
|
|
1466
|
+
* cancelUrl: 'https://yourapp.com/payment-methods/cancel',
|
|
1467
|
+
* });
|
|
1468
|
+
*
|
|
1469
|
+
* // Redirect customer to payment method collection
|
|
1470
|
+
* window.location.href = session.url;
|
|
1471
|
+
* ```
|
|
1472
|
+
*/
|
|
1473
|
+
createSetupSession(customerId: string, options: CreateSetupSessionOptions): Promise<SetupSessionResponse>;
|
|
1474
|
+
/**
|
|
1475
|
+
* Attach a payment method to a customer.
|
|
1476
|
+
*
|
|
1477
|
+
* After the customer completes the setup session, call this to store
|
|
1478
|
+
* the payment method in StackBE for future charges.
|
|
1479
|
+
*
|
|
1480
|
+
* @example
|
|
1481
|
+
* ```typescript
|
|
1482
|
+
* // After redirect from setup session
|
|
1483
|
+
* const paymentMethod = await stackbe.paymentMethods.attach('cust_123', {
|
|
1484
|
+
* gatewayPaymentMethodId: 'pm_1234...', // From setup session completion
|
|
1485
|
+
* setAsDefault: true,
|
|
1486
|
+
* });
|
|
1487
|
+
*
|
|
1488
|
+
* console.log(`Added ${paymentMethod.brand} ending in ${paymentMethod.last4}`);
|
|
1489
|
+
* ```
|
|
1490
|
+
*/
|
|
1491
|
+
attach(customerId: string, options: AttachPaymentMethodOptions): Promise<PaymentMethod>;
|
|
1492
|
+
/**
|
|
1493
|
+
* List all payment methods for a customer.
|
|
1494
|
+
*
|
|
1495
|
+
* @example
|
|
1496
|
+
* ```typescript
|
|
1497
|
+
* const paymentMethods = await stackbe.paymentMethods.list('cust_123');
|
|
1498
|
+
*
|
|
1499
|
+
* for (const pm of paymentMethods) {
|
|
1500
|
+
* console.log(`${pm.brand} **** ${pm.last4} ${pm.isDefault ? '(default)' : ''}`);
|
|
1501
|
+
* }
|
|
1502
|
+
* ```
|
|
1503
|
+
*/
|
|
1504
|
+
list(customerId: string, options?: {
|
|
1505
|
+
status?: string;
|
|
1506
|
+
}): Promise<PaymentMethod[]>;
|
|
1507
|
+
/**
|
|
1508
|
+
* Get a specific payment method.
|
|
1509
|
+
*
|
|
1510
|
+
* @example
|
|
1511
|
+
* ```typescript
|
|
1512
|
+
* const pm = await stackbe.paymentMethods.get('cust_123', 'pm_123');
|
|
1513
|
+
* console.log(`Expires: ${pm.expiryMonth}/${pm.expiryYear}`);
|
|
1514
|
+
* ```
|
|
1515
|
+
*/
|
|
1516
|
+
get(customerId: string, paymentMethodId: string): Promise<PaymentMethod>;
|
|
1517
|
+
/**
|
|
1518
|
+
* Set a payment method as the default for a customer.
|
|
1519
|
+
*
|
|
1520
|
+
* The default payment method is used for subscription renewals.
|
|
1521
|
+
*
|
|
1522
|
+
* @example
|
|
1523
|
+
* ```typescript
|
|
1524
|
+
* await stackbe.paymentMethods.setDefault('cust_123', 'pm_456');
|
|
1525
|
+
* console.log('Default payment method updated');
|
|
1526
|
+
* ```
|
|
1527
|
+
*/
|
|
1528
|
+
setDefault(customerId: string, paymentMethodId: string): Promise<PaymentMethod>;
|
|
1529
|
+
/**
|
|
1530
|
+
* Remove a payment method.
|
|
1531
|
+
*
|
|
1532
|
+
* Cannot remove a payment method that is being used by an active subscription.
|
|
1533
|
+
*
|
|
1534
|
+
* @example
|
|
1535
|
+
* ```typescript
|
|
1536
|
+
* await stackbe.paymentMethods.remove('cust_123', 'pm_old');
|
|
1537
|
+
* console.log('Payment method removed');
|
|
1538
|
+
* ```
|
|
1539
|
+
*/
|
|
1540
|
+
remove(customerId: string, paymentMethodId: string): Promise<{
|
|
1541
|
+
success: boolean;
|
|
1542
|
+
}>;
|
|
1543
|
+
/**
|
|
1544
|
+
* Get the default payment method for a customer.
|
|
1545
|
+
*
|
|
1546
|
+
* @example
|
|
1547
|
+
* ```typescript
|
|
1548
|
+
* const defaultPM = await stackbe.paymentMethods.getDefault('cust_123');
|
|
1549
|
+
* if (defaultPM) {
|
|
1550
|
+
* console.log(`Default: ${defaultPM.brand} **** ${defaultPM.last4}`);
|
|
1551
|
+
* }
|
|
1552
|
+
* ```
|
|
1553
|
+
*/
|
|
1554
|
+
getDefault(customerId: string): Promise<PaymentMethod | null>;
|
|
1351
1555
|
}
|
|
1352
1556
|
|
|
1353
1557
|
interface AuthClientOptions {
|
|
@@ -2665,6 +2869,8 @@ declare class StackBE {
|
|
|
2665
2869
|
readonly checkout: CheckoutClient;
|
|
2666
2870
|
/** Subscription management */
|
|
2667
2871
|
readonly subscriptions: SubscriptionsClient;
|
|
2872
|
+
/** Payment method management (StackBE-managed billing) */
|
|
2873
|
+
readonly paymentMethods: PaymentMethodsClient;
|
|
2668
2874
|
/** Customer authentication (magic links) */
|
|
2669
2875
|
readonly auth: AuthClient;
|
|
2670
2876
|
/** Organization management (B2B multi-user) */
|
|
@@ -2783,4 +2989,4 @@ declare class StackBE {
|
|
|
2783
2989
|
}): (req: any, res: any, next: any) => Promise<any>;
|
|
2784
2990
|
}
|
|
2785
2991
|
|
|
2786
|
-
export { type AddMemberOptions, type AffiliateCommission, type AffiliateCommissionsResponse, type AffiliateEnrollment, type AffiliateInfo, type AffiliateStats, AffiliatesClient, AnalyticsClient, type AnalyticsFilterOptions, type AnyWebhookEvent, AuthClient, type BillingPortalSession, type CancelSubscriptionOptions, type CancelSubscriptionResponse, type CheckEntitlementResponse, type CheckUsageResponse, CheckoutClient, type CheckoutSessionResponse, type Coupon, type CouponDiscountType, type CouponDuration, CouponsClient, type CreateCheckoutOptions, type CreateCouponOptions, type CreateCustomerOptions, type CreateEarlyAccessSignupOptions, type CreateFeatureRequestOptions, type CreateOrganizationOptions, type Customer, type CustomerCreatedEvent, type CustomerFeatureActivity, type CustomerInvoice, type CustomerInvoicesResponse, type CustomerPaymentMethod, type CustomerSubscriptionHistory, type CustomerSubscriptionHistoryResponse, type CustomerUpdatedEvent, type CustomerUsageResponse, type CustomerWebhookPayload, type CustomerWithSubscription, CustomersClient, type DashboardMetrics, type DunningStatus, EarlyAccessClient, type EarlyAccessSignup, type EarlyAccessSignupListResponse, type EnrollOptions, EntitlementsClient, type EntitlementsResponse, type FeatureRequest, type FeatureRequestComment, type FeatureRequestImage, type FeatureRequestListResponse, FeatureRequestsClient, type FeatureUsagePoint, type GetSubscriptionOptions, type GrowthChartPoint, type GrowthMetrics, type InterestedCustomer, type InterestedCustomersResponse, type InviteMemberOptions, type ListEarlyAccessOptions, type ListFeatureRequestsOptions, type ListPlansOptions, type ListProductsOptions, type MRRHistoryPoint, type MagicLinkOptions, type MagicLinkResponse, type Organization, type OrganizationInvite, type OrganizationMember, OrganizationsClient, type PaymentFailedEvent, type PaymentSucceededEvent, type PaymentWebhookPayload, type PerformanceMetrics, type Plan, type PlanMetrics, PlansClient, type PopularEndpoint, type Product, ProductsClient, type RequestCountPoint, type RevenueByPlan, type SessionResponse, StackBE, type StackBEConfig, StackBEError, type StackBEErrorCode, type StackBEErrorResponse, type Subscription, type SubscriptionCancelledEvent, type SubscriptionCreatedEvent, type SubscriptionPlan, type SubscriptionRenewedEvent, type SubscriptionUpdatedEvent, type SubscriptionWebhookPayload, type SubscriptionWithPlan, SubscriptionsClient, type SwitchOrganizationResponse, type TimeRangeOptions, type TrackReferralResponse, type TrackUsageOptions, type TrackUsageResponse, type TrialEndedEvent, type TrialMetrics, type TrialStartedEvent, type TrialStatus, type UpcomingInvoice, type UpdateCouponOptions, type UpdateCustomerOptions, type UpdateOrganizationOptions, type UpdateSubscriptionOptions, UsageClient, type UsageMetric, type ValidateCouponResult, type VerifyTokenResponse, type WebhookEvent, type WebhookEventType };
|
|
2992
|
+
export { type AddMemberOptions, type AffiliateCommission, type AffiliateCommissionsResponse, type AffiliateEnrollment, type AffiliateInfo, type AffiliateStats, AffiliatesClient, AnalyticsClient, type AnalyticsFilterOptions, type AnyWebhookEvent, type AttachPaymentMethodOptions, AuthClient, type BillingPortalSession, type CancelSubscriptionOptions, type CancelSubscriptionResponse, type ChargeResult, type CheckEntitlementResponse, type CheckUsageResponse, CheckoutClient, type CheckoutSessionResponse, type Coupon, type CouponDiscountType, type CouponDuration, CouponsClient, type CreateCheckoutOptions, type CreateCouponOptions, type CreateCustomerOptions, type CreateEarlyAccessSignupOptions, type CreateFeatureRequestOptions, type CreateOrganizationOptions, type CreateSetupSessionOptions, type Customer, type CustomerCreatedEvent, type CustomerFeatureActivity, type CustomerInvoice, type CustomerInvoicesResponse, type CustomerPaymentMethod, type CustomerSubscriptionHistory, type CustomerSubscriptionHistoryResponse, type CustomerUpdatedEvent, type CustomerUsageResponse, type CustomerWebhookPayload, type CustomerWithSubscription, CustomersClient, type DashboardMetrics, type DunningStatus, EarlyAccessClient, type EarlyAccessSignup, type EarlyAccessSignupListResponse, type EnrollOptions, EntitlementsClient, type EntitlementsResponse, type FeatureRequest, type FeatureRequestComment, type FeatureRequestImage, type FeatureRequestListResponse, FeatureRequestsClient, type FeatureUsagePoint, type GetSubscriptionOptions, type GrowthChartPoint, type GrowthMetrics, type InterestedCustomer, type InterestedCustomersResponse, type InviteMemberOptions, type ListEarlyAccessOptions, type ListFeatureRequestsOptions, type ListPlansOptions, type ListProductsOptions, type MRRHistoryPoint, type MagicLinkOptions, type MagicLinkResponse, type MigrateToStackbeManagedResult, type Organization, type OrganizationInvite, type OrganizationMember, OrganizationsClient, type PaymentFailedEvent, type PaymentMethod, PaymentMethodsClient, type PaymentSucceededEvent, type PaymentWebhookPayload, type PerformanceMetrics, type Plan, type PlanMetrics, PlansClient, type PopularEndpoint, type Product, ProductsClient, type RequestCountPoint, type RevenueByPlan, type SessionResponse, type SetupSessionResponse, StackBE, type StackBEConfig, StackBEError, type StackBEErrorCode, type StackBEErrorResponse, type Subscription, type SubscriptionCancelledEvent, type SubscriptionCreatedEvent, type SubscriptionPlan, type SubscriptionRenewedEvent, type SubscriptionUpdatedEvent, type SubscriptionWebhookPayload, type SubscriptionWithPlan, SubscriptionsClient, type SwitchOrganizationResponse, type TimeRangeOptions, type TrackReferralResponse, type TrackUsageOptions, type TrackUsageResponse, type TrialEndedEvent, type TrialMetrics, type TrialStartedEvent, type TrialStatus, type UpcomingInvoice, type UpdateCouponOptions, type UpdateCustomerOptions, type UpdateOrganizationOptions, type UpdateSubscriptionOptions, UsageClient, type UsageMetric, type ValidateCouponResult, type VerifyTokenResponse, type WebhookEvent, type WebhookEventType };
|
package/dist/index.js
CHANGED
|
@@ -30,6 +30,7 @@ __export(index_exports, {
|
|
|
30
30
|
EntitlementsClient: () => EntitlementsClient,
|
|
31
31
|
FeatureRequestsClient: () => FeatureRequestsClient,
|
|
32
32
|
OrganizationsClient: () => OrganizationsClient,
|
|
33
|
+
PaymentMethodsClient: () => PaymentMethodsClient,
|
|
33
34
|
PlansClient: () => PlansClient,
|
|
34
35
|
ProductsClient: () => ProductsClient,
|
|
35
36
|
StackBE: () => StackBE,
|
|
@@ -1168,6 +1169,201 @@ var SubscriptionsClient = class {
|
|
|
1168
1169
|
`/v1/subscriptions/${subscriptionId}/has-access`
|
|
1169
1170
|
);
|
|
1170
1171
|
}
|
|
1172
|
+
// ============================================
|
|
1173
|
+
// StackBE-Managed Billing Methods
|
|
1174
|
+
// ============================================
|
|
1175
|
+
/**
|
|
1176
|
+
* Manually charge a StackBE-managed subscription.
|
|
1177
|
+
*
|
|
1178
|
+
* This is useful for:
|
|
1179
|
+
* - Testing billing in development
|
|
1180
|
+
* - Recovering from failed automatic charges
|
|
1181
|
+
* - Charging ahead of schedule
|
|
1182
|
+
*
|
|
1183
|
+
* The subscription must be in `stackbe_managed` billing mode.
|
|
1184
|
+
*
|
|
1185
|
+
* @example
|
|
1186
|
+
* ```typescript
|
|
1187
|
+
* const result = await stackbe.subscriptions.charge('sub_123');
|
|
1188
|
+
*
|
|
1189
|
+
* if (result.success) {
|
|
1190
|
+
* console.log('Charge successful:', result.chargeId);
|
|
1191
|
+
* } else if (result.requiresAction) {
|
|
1192
|
+
* // Redirect customer to 3DS authentication
|
|
1193
|
+
* window.location.href = result.actionUrl;
|
|
1194
|
+
* } else {
|
|
1195
|
+
* console.log('Charge failed:', result.errorMessage);
|
|
1196
|
+
* }
|
|
1197
|
+
* ```
|
|
1198
|
+
*/
|
|
1199
|
+
async charge(subscriptionId) {
|
|
1200
|
+
return this.http.post(
|
|
1201
|
+
`/v1/subscriptions/${subscriptionId}/charge`
|
|
1202
|
+
);
|
|
1203
|
+
}
|
|
1204
|
+
/**
|
|
1205
|
+
* Migrate a subscription from gateway-managed to StackBE-managed billing.
|
|
1206
|
+
*
|
|
1207
|
+
* This will:
|
|
1208
|
+
* 1. Extract the payment method from the gateway subscription
|
|
1209
|
+
* 2. Store it in StackBE's PaymentMethod table
|
|
1210
|
+
* 3. Set the subscription to `stackbe_managed` billing mode
|
|
1211
|
+
* 4. Cancel the gateway subscription at period end
|
|
1212
|
+
* 5. StackBE will handle renewals going forward
|
|
1213
|
+
*
|
|
1214
|
+
* Use cases:
|
|
1215
|
+
* - Migrate existing Stripe subscriptions to unified StackBE billing
|
|
1216
|
+
* - Prepare for multi-gateway support
|
|
1217
|
+
* - Gain more control over billing timing and retry logic
|
|
1218
|
+
*
|
|
1219
|
+
* @example
|
|
1220
|
+
* ```typescript
|
|
1221
|
+
* const result = await stackbe.subscriptions.migrateToStackbeManaged('sub_123');
|
|
1222
|
+
*
|
|
1223
|
+
* console.log('Migration complete');
|
|
1224
|
+
* console.log('Payment method:', result.paymentMethodId);
|
|
1225
|
+
* console.log('Next billing date:', result.nextBillingDate);
|
|
1226
|
+
* ```
|
|
1227
|
+
*/
|
|
1228
|
+
async migrateToStackbeManaged(subscriptionId) {
|
|
1229
|
+
return this.http.post(
|
|
1230
|
+
`/v1/subscriptions/${subscriptionId}/migrate-to-stackbe-managed`
|
|
1231
|
+
);
|
|
1232
|
+
}
|
|
1233
|
+
};
|
|
1234
|
+
|
|
1235
|
+
// src/payment-methods.ts
|
|
1236
|
+
var PaymentMethodsClient = class {
|
|
1237
|
+
constructor(http) {
|
|
1238
|
+
this.http = http;
|
|
1239
|
+
}
|
|
1240
|
+
/**
|
|
1241
|
+
* Create a setup session for collecting a payment method.
|
|
1242
|
+
*
|
|
1243
|
+
* Returns a URL to redirect the customer to for secure payment method collection.
|
|
1244
|
+
* This is similar to Stripe Checkout but in setup mode - no charge is made.
|
|
1245
|
+
*
|
|
1246
|
+
* @example
|
|
1247
|
+
* ```typescript
|
|
1248
|
+
* const session = await stackbe.paymentMethods.createSetupSession('cust_123', {
|
|
1249
|
+
* successUrl: 'https://yourapp.com/payment-methods/success',
|
|
1250
|
+
* cancelUrl: 'https://yourapp.com/payment-methods/cancel',
|
|
1251
|
+
* });
|
|
1252
|
+
*
|
|
1253
|
+
* // Redirect customer to payment method collection
|
|
1254
|
+
* window.location.href = session.url;
|
|
1255
|
+
* ```
|
|
1256
|
+
*/
|
|
1257
|
+
async createSetupSession(customerId, options) {
|
|
1258
|
+
return this.http.post(
|
|
1259
|
+
`/v1/apps/{appId}/customers/${customerId}/payment-methods/setup`,
|
|
1260
|
+
options
|
|
1261
|
+
);
|
|
1262
|
+
}
|
|
1263
|
+
/**
|
|
1264
|
+
* Attach a payment method to a customer.
|
|
1265
|
+
*
|
|
1266
|
+
* After the customer completes the setup session, call this to store
|
|
1267
|
+
* the payment method in StackBE for future charges.
|
|
1268
|
+
*
|
|
1269
|
+
* @example
|
|
1270
|
+
* ```typescript
|
|
1271
|
+
* // After redirect from setup session
|
|
1272
|
+
* const paymentMethod = await stackbe.paymentMethods.attach('cust_123', {
|
|
1273
|
+
* gatewayPaymentMethodId: 'pm_1234...', // From setup session completion
|
|
1274
|
+
* setAsDefault: true,
|
|
1275
|
+
* });
|
|
1276
|
+
*
|
|
1277
|
+
* console.log(`Added ${paymentMethod.brand} ending in ${paymentMethod.last4}`);
|
|
1278
|
+
* ```
|
|
1279
|
+
*/
|
|
1280
|
+
async attach(customerId, options) {
|
|
1281
|
+
return this.http.post(
|
|
1282
|
+
`/v1/apps/{appId}/customers/${customerId}/payment-methods`,
|
|
1283
|
+
options
|
|
1284
|
+
);
|
|
1285
|
+
}
|
|
1286
|
+
/**
|
|
1287
|
+
* List all payment methods for a customer.
|
|
1288
|
+
*
|
|
1289
|
+
* @example
|
|
1290
|
+
* ```typescript
|
|
1291
|
+
* const paymentMethods = await stackbe.paymentMethods.list('cust_123');
|
|
1292
|
+
*
|
|
1293
|
+
* for (const pm of paymentMethods) {
|
|
1294
|
+
* console.log(`${pm.brand} **** ${pm.last4} ${pm.isDefault ? '(default)' : ''}`);
|
|
1295
|
+
* }
|
|
1296
|
+
* ```
|
|
1297
|
+
*/
|
|
1298
|
+
async list(customerId, options) {
|
|
1299
|
+
const params = {};
|
|
1300
|
+
if (options?.status) params.status = options.status;
|
|
1301
|
+
return this.http.get(
|
|
1302
|
+
`/v1/apps/{appId}/customers/${customerId}/payment-methods`,
|
|
1303
|
+
params
|
|
1304
|
+
);
|
|
1305
|
+
}
|
|
1306
|
+
/**
|
|
1307
|
+
* Get a specific payment method.
|
|
1308
|
+
*
|
|
1309
|
+
* @example
|
|
1310
|
+
* ```typescript
|
|
1311
|
+
* const pm = await stackbe.paymentMethods.get('cust_123', 'pm_123');
|
|
1312
|
+
* console.log(`Expires: ${pm.expiryMonth}/${pm.expiryYear}`);
|
|
1313
|
+
* ```
|
|
1314
|
+
*/
|
|
1315
|
+
async get(customerId, paymentMethodId) {
|
|
1316
|
+
return this.http.get(
|
|
1317
|
+
`/v1/apps/{appId}/customers/${customerId}/payment-methods/${paymentMethodId}`
|
|
1318
|
+
);
|
|
1319
|
+
}
|
|
1320
|
+
/**
|
|
1321
|
+
* Set a payment method as the default for a customer.
|
|
1322
|
+
*
|
|
1323
|
+
* The default payment method is used for subscription renewals.
|
|
1324
|
+
*
|
|
1325
|
+
* @example
|
|
1326
|
+
* ```typescript
|
|
1327
|
+
* await stackbe.paymentMethods.setDefault('cust_123', 'pm_456');
|
|
1328
|
+
* console.log('Default payment method updated');
|
|
1329
|
+
* ```
|
|
1330
|
+
*/
|
|
1331
|
+
async setDefault(customerId, paymentMethodId) {
|
|
1332
|
+
return this.http.post(
|
|
1333
|
+
`/v1/apps/{appId}/customers/${customerId}/payment-methods/${paymentMethodId}/default`
|
|
1334
|
+
);
|
|
1335
|
+
}
|
|
1336
|
+
/**
|
|
1337
|
+
* Remove a payment method.
|
|
1338
|
+
*
|
|
1339
|
+
* Cannot remove a payment method that is being used by an active subscription.
|
|
1340
|
+
*
|
|
1341
|
+
* @example
|
|
1342
|
+
* ```typescript
|
|
1343
|
+
* await stackbe.paymentMethods.remove('cust_123', 'pm_old');
|
|
1344
|
+
* console.log('Payment method removed');
|
|
1345
|
+
* ```
|
|
1346
|
+
*/
|
|
1347
|
+
async remove(customerId, paymentMethodId) {
|
|
1348
|
+
return this.http.delete(
|
|
1349
|
+
`/v1/apps/{appId}/customers/${customerId}/payment-methods/${paymentMethodId}`
|
|
1350
|
+
);
|
|
1351
|
+
}
|
|
1352
|
+
/**
|
|
1353
|
+
* Get the default payment method for a customer.
|
|
1354
|
+
*
|
|
1355
|
+
* @example
|
|
1356
|
+
* ```typescript
|
|
1357
|
+
* const defaultPM = await stackbe.paymentMethods.getDefault('cust_123');
|
|
1358
|
+
* if (defaultPM) {
|
|
1359
|
+
* console.log(`Default: ${defaultPM.brand} **** ${defaultPM.last4}`);
|
|
1360
|
+
* }
|
|
1361
|
+
* ```
|
|
1362
|
+
*/
|
|
1363
|
+
async getDefault(customerId) {
|
|
1364
|
+
const paymentMethods = await this.list(customerId, { status: "active" });
|
|
1365
|
+
return paymentMethods.find((pm) => pm.isDefault) || null;
|
|
1366
|
+
}
|
|
1171
1367
|
};
|
|
1172
1368
|
|
|
1173
1369
|
// src/auth.ts
|
|
@@ -2566,6 +2762,7 @@ var StackBE = class {
|
|
|
2566
2762
|
this.customers = new CustomersClient(this.http, config.appId);
|
|
2567
2763
|
this.checkout = new CheckoutClient(this.http, config.appId);
|
|
2568
2764
|
this.subscriptions = new SubscriptionsClient(this.http);
|
|
2765
|
+
this.paymentMethods = new PaymentMethodsClient(this.http);
|
|
2569
2766
|
this.auth = new AuthClient(this.http, config.appId, {
|
|
2570
2767
|
sessionCacheTTL: config.sessionCacheTTL,
|
|
2571
2768
|
devCallbackUrl: config.devCallbackUrl
|
|
@@ -2721,6 +2918,7 @@ var StackBE = class {
|
|
|
2721
2918
|
EntitlementsClient,
|
|
2722
2919
|
FeatureRequestsClient,
|
|
2723
2920
|
OrganizationsClient,
|
|
2921
|
+
PaymentMethodsClient,
|
|
2724
2922
|
PlansClient,
|
|
2725
2923
|
ProductsClient,
|
|
2726
2924
|
StackBE,
|
package/dist/index.mjs
CHANGED
|
@@ -1127,6 +1127,201 @@ var SubscriptionsClient = class {
|
|
|
1127
1127
|
`/v1/subscriptions/${subscriptionId}/has-access`
|
|
1128
1128
|
);
|
|
1129
1129
|
}
|
|
1130
|
+
// ============================================
|
|
1131
|
+
// StackBE-Managed Billing Methods
|
|
1132
|
+
// ============================================
|
|
1133
|
+
/**
|
|
1134
|
+
* Manually charge a StackBE-managed subscription.
|
|
1135
|
+
*
|
|
1136
|
+
* This is useful for:
|
|
1137
|
+
* - Testing billing in development
|
|
1138
|
+
* - Recovering from failed automatic charges
|
|
1139
|
+
* - Charging ahead of schedule
|
|
1140
|
+
*
|
|
1141
|
+
* The subscription must be in `stackbe_managed` billing mode.
|
|
1142
|
+
*
|
|
1143
|
+
* @example
|
|
1144
|
+
* ```typescript
|
|
1145
|
+
* const result = await stackbe.subscriptions.charge('sub_123');
|
|
1146
|
+
*
|
|
1147
|
+
* if (result.success) {
|
|
1148
|
+
* console.log('Charge successful:', result.chargeId);
|
|
1149
|
+
* } else if (result.requiresAction) {
|
|
1150
|
+
* // Redirect customer to 3DS authentication
|
|
1151
|
+
* window.location.href = result.actionUrl;
|
|
1152
|
+
* } else {
|
|
1153
|
+
* console.log('Charge failed:', result.errorMessage);
|
|
1154
|
+
* }
|
|
1155
|
+
* ```
|
|
1156
|
+
*/
|
|
1157
|
+
async charge(subscriptionId) {
|
|
1158
|
+
return this.http.post(
|
|
1159
|
+
`/v1/subscriptions/${subscriptionId}/charge`
|
|
1160
|
+
);
|
|
1161
|
+
}
|
|
1162
|
+
/**
|
|
1163
|
+
* Migrate a subscription from gateway-managed to StackBE-managed billing.
|
|
1164
|
+
*
|
|
1165
|
+
* This will:
|
|
1166
|
+
* 1. Extract the payment method from the gateway subscription
|
|
1167
|
+
* 2. Store it in StackBE's PaymentMethod table
|
|
1168
|
+
* 3. Set the subscription to `stackbe_managed` billing mode
|
|
1169
|
+
* 4. Cancel the gateway subscription at period end
|
|
1170
|
+
* 5. StackBE will handle renewals going forward
|
|
1171
|
+
*
|
|
1172
|
+
* Use cases:
|
|
1173
|
+
* - Migrate existing Stripe subscriptions to unified StackBE billing
|
|
1174
|
+
* - Prepare for multi-gateway support
|
|
1175
|
+
* - Gain more control over billing timing and retry logic
|
|
1176
|
+
*
|
|
1177
|
+
* @example
|
|
1178
|
+
* ```typescript
|
|
1179
|
+
* const result = await stackbe.subscriptions.migrateToStackbeManaged('sub_123');
|
|
1180
|
+
*
|
|
1181
|
+
* console.log('Migration complete');
|
|
1182
|
+
* console.log('Payment method:', result.paymentMethodId);
|
|
1183
|
+
* console.log('Next billing date:', result.nextBillingDate);
|
|
1184
|
+
* ```
|
|
1185
|
+
*/
|
|
1186
|
+
async migrateToStackbeManaged(subscriptionId) {
|
|
1187
|
+
return this.http.post(
|
|
1188
|
+
`/v1/subscriptions/${subscriptionId}/migrate-to-stackbe-managed`
|
|
1189
|
+
);
|
|
1190
|
+
}
|
|
1191
|
+
};
|
|
1192
|
+
|
|
1193
|
+
// src/payment-methods.ts
|
|
1194
|
+
var PaymentMethodsClient = class {
|
|
1195
|
+
constructor(http) {
|
|
1196
|
+
this.http = http;
|
|
1197
|
+
}
|
|
1198
|
+
/**
|
|
1199
|
+
* Create a setup session for collecting a payment method.
|
|
1200
|
+
*
|
|
1201
|
+
* Returns a URL to redirect the customer to for secure payment method collection.
|
|
1202
|
+
* This is similar to Stripe Checkout but in setup mode - no charge is made.
|
|
1203
|
+
*
|
|
1204
|
+
* @example
|
|
1205
|
+
* ```typescript
|
|
1206
|
+
* const session = await stackbe.paymentMethods.createSetupSession('cust_123', {
|
|
1207
|
+
* successUrl: 'https://yourapp.com/payment-methods/success',
|
|
1208
|
+
* cancelUrl: 'https://yourapp.com/payment-methods/cancel',
|
|
1209
|
+
* });
|
|
1210
|
+
*
|
|
1211
|
+
* // Redirect customer to payment method collection
|
|
1212
|
+
* window.location.href = session.url;
|
|
1213
|
+
* ```
|
|
1214
|
+
*/
|
|
1215
|
+
async createSetupSession(customerId, options) {
|
|
1216
|
+
return this.http.post(
|
|
1217
|
+
`/v1/apps/{appId}/customers/${customerId}/payment-methods/setup`,
|
|
1218
|
+
options
|
|
1219
|
+
);
|
|
1220
|
+
}
|
|
1221
|
+
/**
|
|
1222
|
+
* Attach a payment method to a customer.
|
|
1223
|
+
*
|
|
1224
|
+
* After the customer completes the setup session, call this to store
|
|
1225
|
+
* the payment method in StackBE for future charges.
|
|
1226
|
+
*
|
|
1227
|
+
* @example
|
|
1228
|
+
* ```typescript
|
|
1229
|
+
* // After redirect from setup session
|
|
1230
|
+
* const paymentMethod = await stackbe.paymentMethods.attach('cust_123', {
|
|
1231
|
+
* gatewayPaymentMethodId: 'pm_1234...', // From setup session completion
|
|
1232
|
+
* setAsDefault: true,
|
|
1233
|
+
* });
|
|
1234
|
+
*
|
|
1235
|
+
* console.log(`Added ${paymentMethod.brand} ending in ${paymentMethod.last4}`);
|
|
1236
|
+
* ```
|
|
1237
|
+
*/
|
|
1238
|
+
async attach(customerId, options) {
|
|
1239
|
+
return this.http.post(
|
|
1240
|
+
`/v1/apps/{appId}/customers/${customerId}/payment-methods`,
|
|
1241
|
+
options
|
|
1242
|
+
);
|
|
1243
|
+
}
|
|
1244
|
+
/**
|
|
1245
|
+
* List all payment methods for a customer.
|
|
1246
|
+
*
|
|
1247
|
+
* @example
|
|
1248
|
+
* ```typescript
|
|
1249
|
+
* const paymentMethods = await stackbe.paymentMethods.list('cust_123');
|
|
1250
|
+
*
|
|
1251
|
+
* for (const pm of paymentMethods) {
|
|
1252
|
+
* console.log(`${pm.brand} **** ${pm.last4} ${pm.isDefault ? '(default)' : ''}`);
|
|
1253
|
+
* }
|
|
1254
|
+
* ```
|
|
1255
|
+
*/
|
|
1256
|
+
async list(customerId, options) {
|
|
1257
|
+
const params = {};
|
|
1258
|
+
if (options?.status) params.status = options.status;
|
|
1259
|
+
return this.http.get(
|
|
1260
|
+
`/v1/apps/{appId}/customers/${customerId}/payment-methods`,
|
|
1261
|
+
params
|
|
1262
|
+
);
|
|
1263
|
+
}
|
|
1264
|
+
/**
|
|
1265
|
+
* Get a specific payment method.
|
|
1266
|
+
*
|
|
1267
|
+
* @example
|
|
1268
|
+
* ```typescript
|
|
1269
|
+
* const pm = await stackbe.paymentMethods.get('cust_123', 'pm_123');
|
|
1270
|
+
* console.log(`Expires: ${pm.expiryMonth}/${pm.expiryYear}`);
|
|
1271
|
+
* ```
|
|
1272
|
+
*/
|
|
1273
|
+
async get(customerId, paymentMethodId) {
|
|
1274
|
+
return this.http.get(
|
|
1275
|
+
`/v1/apps/{appId}/customers/${customerId}/payment-methods/${paymentMethodId}`
|
|
1276
|
+
);
|
|
1277
|
+
}
|
|
1278
|
+
/**
|
|
1279
|
+
* Set a payment method as the default for a customer.
|
|
1280
|
+
*
|
|
1281
|
+
* The default payment method is used for subscription renewals.
|
|
1282
|
+
*
|
|
1283
|
+
* @example
|
|
1284
|
+
* ```typescript
|
|
1285
|
+
* await stackbe.paymentMethods.setDefault('cust_123', 'pm_456');
|
|
1286
|
+
* console.log('Default payment method updated');
|
|
1287
|
+
* ```
|
|
1288
|
+
*/
|
|
1289
|
+
async setDefault(customerId, paymentMethodId) {
|
|
1290
|
+
return this.http.post(
|
|
1291
|
+
`/v1/apps/{appId}/customers/${customerId}/payment-methods/${paymentMethodId}/default`
|
|
1292
|
+
);
|
|
1293
|
+
}
|
|
1294
|
+
/**
|
|
1295
|
+
* Remove a payment method.
|
|
1296
|
+
*
|
|
1297
|
+
* Cannot remove a payment method that is being used by an active subscription.
|
|
1298
|
+
*
|
|
1299
|
+
* @example
|
|
1300
|
+
* ```typescript
|
|
1301
|
+
* await stackbe.paymentMethods.remove('cust_123', 'pm_old');
|
|
1302
|
+
* console.log('Payment method removed');
|
|
1303
|
+
* ```
|
|
1304
|
+
*/
|
|
1305
|
+
async remove(customerId, paymentMethodId) {
|
|
1306
|
+
return this.http.delete(
|
|
1307
|
+
`/v1/apps/{appId}/customers/${customerId}/payment-methods/${paymentMethodId}`
|
|
1308
|
+
);
|
|
1309
|
+
}
|
|
1310
|
+
/**
|
|
1311
|
+
* Get the default payment method for a customer.
|
|
1312
|
+
*
|
|
1313
|
+
* @example
|
|
1314
|
+
* ```typescript
|
|
1315
|
+
* const defaultPM = await stackbe.paymentMethods.getDefault('cust_123');
|
|
1316
|
+
* if (defaultPM) {
|
|
1317
|
+
* console.log(`Default: ${defaultPM.brand} **** ${defaultPM.last4}`);
|
|
1318
|
+
* }
|
|
1319
|
+
* ```
|
|
1320
|
+
*/
|
|
1321
|
+
async getDefault(customerId) {
|
|
1322
|
+
const paymentMethods = await this.list(customerId, { status: "active" });
|
|
1323
|
+
return paymentMethods.find((pm) => pm.isDefault) || null;
|
|
1324
|
+
}
|
|
1130
1325
|
};
|
|
1131
1326
|
|
|
1132
1327
|
// src/auth.ts
|
|
@@ -2525,6 +2720,7 @@ var StackBE = class {
|
|
|
2525
2720
|
this.customers = new CustomersClient(this.http, config.appId);
|
|
2526
2721
|
this.checkout = new CheckoutClient(this.http, config.appId);
|
|
2527
2722
|
this.subscriptions = new SubscriptionsClient(this.http);
|
|
2723
|
+
this.paymentMethods = new PaymentMethodsClient(this.http);
|
|
2528
2724
|
this.auth = new AuthClient(this.http, config.appId, {
|
|
2529
2725
|
sessionCacheTTL: config.sessionCacheTTL,
|
|
2530
2726
|
devCallbackUrl: config.devCallbackUrl
|
|
@@ -2679,6 +2875,7 @@ export {
|
|
|
2679
2875
|
EntitlementsClient,
|
|
2680
2876
|
FeatureRequestsClient,
|
|
2681
2877
|
OrganizationsClient,
|
|
2878
|
+
PaymentMethodsClient,
|
|
2682
2879
|
PlansClient,
|
|
2683
2880
|
ProductsClient,
|
|
2684
2881
|
StackBE,
|
package/package.json
CHANGED