@fantastic.dev/better-stripe 0.4.0-bootstrap.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/CHANGELOG.md +318 -0
- package/README.md +1117 -0
- package/dist/client/billing/checkout.d.ts +75 -0
- package/dist/client/billing/checkout.d.ts.map +1 -0
- package/dist/client/billing/checkout.js +260 -0
- package/dist/client/billing/checkout.js.map +1 -0
- package/dist/client/billing/invoices.d.ts +37 -0
- package/dist/client/billing/invoices.d.ts.map +1 -0
- package/dist/client/billing/invoices.js +29 -0
- package/dist/client/billing/invoices.js.map +1 -0
- package/dist/client/billing/subscriptions.d.ts +179 -0
- package/dist/client/billing/subscriptions.d.ts.map +1 -0
- package/dist/client/billing/subscriptions.js +346 -0
- package/dist/client/billing/subscriptions.js.map +1 -0
- package/dist/client/connect/disputes.d.ts +112 -0
- package/dist/client/connect/disputes.d.ts.map +1 -0
- package/dist/client/connect/disputes.js +186 -0
- package/dist/client/connect/disputes.js.map +1 -0
- package/dist/client/connect/paymentMethodUtils.d.ts +17 -0
- package/dist/client/connect/paymentMethodUtils.d.ts.map +1 -0
- package/dist/client/connect/paymentMethodUtils.js +24 -0
- package/dist/client/connect/paymentMethodUtils.js.map +1 -0
- package/dist/client/connect/paymentMethods.d.ts +23 -0
- package/dist/client/connect/paymentMethods.d.ts.map +1 -0
- package/dist/client/connect/paymentMethods.js +27 -0
- package/dist/client/connect/paymentMethods.js.map +1 -0
- package/dist/client/connect/payouts.d.ts +47 -0
- package/dist/client/connect/payouts.d.ts.map +1 -0
- package/dist/client/connect/payouts.js +52 -0
- package/dist/client/connect/payouts.js.map +1 -0
- package/dist/client/connect/refundActor.d.ts +50 -0
- package/dist/client/connect/refundActor.d.ts.map +1 -0
- package/dist/client/connect/refundActor.js +17 -0
- package/dist/client/connect/refundActor.js.map +1 -0
- package/dist/client/connect/refunds.d.ts +75 -0
- package/dist/client/connect/refunds.d.ts.map +1 -0
- package/dist/client/connect/refunds.js +141 -0
- package/dist/client/connect/refunds.js.map +1 -0
- package/dist/client/connect/transfers.d.ts +189 -0
- package/dist/client/connect/transfers.d.ts.map +1 -0
- package/dist/client/connect/transfers.js +347 -0
- package/dist/client/connect/transfers.js.map +1 -0
- package/dist/client/constants.d.ts +15 -0
- package/dist/client/constants.d.ts.map +1 -0
- package/dist/client/constants.js +14 -0
- package/dist/client/constants.js.map +1 -0
- package/dist/client/core/accountLinks.d.ts +68 -0
- package/dist/client/core/accountLinks.d.ts.map +1 -0
- package/dist/client/core/accountLinks.js +131 -0
- package/dist/client/core/accountLinks.js.map +1 -0
- package/dist/client/core/accountStatus.d.ts +12 -0
- package/dist/client/core/accountStatus.d.ts.map +1 -0
- package/dist/client/core/accountStatus.js +56 -0
- package/dist/client/core/accountStatus.js.map +1 -0
- package/dist/client/core/accounts.d.ts +181 -0
- package/dist/client/core/accounts.d.ts.map +1 -0
- package/dist/client/core/accounts.js +359 -0
- package/dist/client/core/accounts.js.map +1 -0
- package/dist/client/core/config.d.ts +5 -0
- package/dist/client/core/config.d.ts.map +1 -0
- package/dist/client/core/config.js +11 -0
- package/dist/client/core/config.js.map +1 -0
- package/dist/client/core/descriptors.d.ts +24 -0
- package/dist/client/core/descriptors.d.ts.map +1 -0
- package/dist/client/core/descriptors.js +43 -0
- package/dist/client/core/descriptors.js.map +1 -0
- package/dist/client/core/fees.d.ts +83 -0
- package/dist/client/core/fees.d.ts.map +1 -0
- package/dist/client/core/fees.js +178 -0
- package/dist/client/core/fees.js.map +1 -0
- package/dist/client/errors.d.ts +20 -0
- package/dist/client/errors.d.ts.map +1 -0
- package/dist/client/errors.js +31 -0
- package/dist/client/errors.js.map +1 -0
- package/dist/client/helpers.d.ts +20 -0
- package/dist/client/helpers.d.ts.map +1 -0
- package/dist/client/helpers.js +46 -0
- package/dist/client/helpers.js.map +1 -0
- package/dist/client/index.d.ts +880 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +791 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/products/prices.d.ts +61 -0
- package/dist/client/products/prices.d.ts.map +1 -0
- package/dist/client/products/prices.js +92 -0
- package/dist/client/products/prices.js.map +1 -0
- package/dist/client/products/products.d.ts +65 -0
- package/dist/client/products/products.d.ts.map +1 -0
- package/dist/client/products/products.js +186 -0
- package/dist/client/products/products.js.map +1 -0
- package/dist/client/stripe-types.d.ts +44 -0
- package/dist/client/stripe-types.d.ts.map +1 -0
- package/dist/client/stripe-types.js +2 -0
- package/dist/client/stripe-types.js.map +1 -0
- package/dist/client/types/documents.d.ts +195 -0
- package/dist/client/types/documents.d.ts.map +1 -0
- package/dist/client/types/documents.js +2 -0
- package/dist/client/types/documents.js.map +1 -0
- package/dist/client/types/events.d.ts +43 -0
- package/dist/client/types/events.d.ts.map +1 -0
- package/dist/client/types/events.js +2 -0
- package/dist/client/types/events.js.map +1 -0
- package/dist/client/types/index.d.ts +5 -0
- package/dist/client/types/index.d.ts.map +1 -0
- package/dist/client/types/index.js +2 -0
- package/dist/client/types/index.js.map +1 -0
- package/dist/client/types/options.d.ts +79 -0
- package/dist/client/types/options.d.ts.map +1 -0
- package/dist/client/types/options.js +2 -0
- package/dist/client/types/options.js.map +1 -0
- package/dist/client/types/triggers.d.ts +101 -0
- package/dist/client/types/triggers.d.ts.map +1 -0
- package/dist/client/types/triggers.js +2 -0
- package/dist/client/types/triggers.js.map +1 -0
- package/dist/client/types.d.ts +2 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +2 -0
- package/dist/client/types.js.map +1 -0
- package/dist/client/utils/data.d.ts +10 -0
- package/dist/client/utils/data.d.ts.map +1 -0
- package/dist/client/utils/data.js +13 -0
- package/dist/client/utils/data.js.map +1 -0
- package/dist/client/utils/owner.d.ts +22 -0
- package/dist/client/utils/owner.d.ts.map +1 -0
- package/dist/client/utils/owner.js +20 -0
- package/dist/client/utils/owner.js.map +1 -0
- package/dist/client/utils/stripeDashboardUrl.d.ts +12 -0
- package/dist/client/utils/stripeDashboardUrl.d.ts.map +1 -0
- package/dist/client/utils/stripeDashboardUrl.js +39 -0
- package/dist/client/utils/stripeDashboardUrl.js.map +1 -0
- package/dist/client/utils/webhookEndpoints.d.ts +83 -0
- package/dist/client/utils/webhookEndpoints.d.ts.map +1 -0
- package/dist/client/utils/webhookEndpoints.js +185 -0
- package/dist/client/utils/webhookEndpoints.js.map +1 -0
- package/dist/client/webhooks/handler.d.ts +4 -0
- package/dist/client/webhooks/handler.d.ts.map +1 -0
- package/dist/client/webhooks/handler.js +170 -0
- package/dist/client/webhooks/handler.js.map +1 -0
- package/dist/client/webhooks/helpers.d.ts +56 -0
- package/dist/client/webhooks/helpers.d.ts.map +1 -0
- package/dist/client/webhooks/helpers.js +229 -0
- package/dist/client/webhooks/helpers.js.map +1 -0
- package/dist/client/webhooks/hooks.d.ts +11 -0
- package/dist/client/webhooks/hooks.d.ts.map +1 -0
- package/dist/client/webhooks/hooks.js +134 -0
- package/dist/client/webhooks/hooks.js.map +1 -0
- package/dist/client/webhooks/index.d.ts +23 -0
- package/dist/client/webhooks/index.d.ts.map +1 -0
- package/dist/client/webhooks/index.js +35 -0
- package/dist/client/webhooks/index.js.map +1 -0
- package/dist/client/webhooks/processors.d.ts +4 -0
- package/dist/client/webhooks/processors.d.ts.map +1 -0
- package/dist/client/webhooks/processors.js +932 -0
- package/dist/client/webhooks/processors.js.map +1 -0
- package/dist/client/webhooks/v2.d.ts +11 -0
- package/dist/client/webhooks/v2.d.ts.map +1 -0
- package/dist/client/webhooks/v2.js +94 -0
- package/dist/client/webhooks/v2.js.map +1 -0
- package/dist/client/webhooks.d.ts +2 -0
- package/dist/client/webhooks.d.ts.map +1 -0
- package/dist/client/webhooks.js +4 -0
- package/dist/client/webhooks.js.map +1 -0
- package/dist/component/_generated/component.d.ts +1262 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +129 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +79 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/billing/mutations.d.ts +86 -0
- package/dist/component/billing/mutations.d.ts.map +1 -0
- package/dist/component/billing/mutations.js +131 -0
- package/dist/component/billing/mutations.js.map +1 -0
- package/dist/component/billing/queries.d.ts +425 -0
- package/dist/component/billing/queries.d.ts.map +1 -0
- package/dist/component/billing/queries.js +304 -0
- package/dist/component/billing/queries.js.map +1 -0
- package/dist/component/billing/schema.d.ts +228 -0
- package/dist/component/billing/schema.d.ts.map +1 -0
- package/dist/component/billing/schema.js +33 -0
- package/dist/component/billing/schema.js.map +1 -0
- package/dist/component/billing/validators.d.ts +364 -0
- package/dist/component/billing/validators.d.ts.map +1 -0
- package/dist/component/billing/validators.js +98 -0
- package/dist/component/billing/validators.js.map +1 -0
- package/dist/component/connect/mutations.d.ts +182 -0
- package/dist/component/connect/mutations.d.ts.map +1 -0
- package/dist/component/connect/mutations.js +480 -0
- package/dist/component/connect/mutations.js.map +1 -0
- package/dist/component/connect/queries.d.ts +347 -0
- package/dist/component/connect/queries.d.ts.map +1 -0
- package/dist/component/connect/queries.js +449 -0
- package/dist/component/connect/queries.js.map +1 -0
- package/dist/component/connect/schema.d.ts +258 -0
- package/dist/component/connect/schema.d.ts.map +1 -0
- package/dist/component/connect/schema.js +70 -0
- package/dist/component/connect/schema.js.map +1 -0
- package/dist/component/connect/validators.d.ts +468 -0
- package/dist/component/connect/validators.d.ts.map +1 -0
- package/dist/component/connect/validators.js +250 -0
- package/dist/component/connect/validators.js.map +1 -0
- package/dist/component/convex.config.d.ts +5 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +17 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/core/mutations.d.ts +52 -0
- package/dist/component/core/mutations.d.ts.map +1 -0
- package/dist/component/core/mutations.js +153 -0
- package/dist/component/core/mutations.js.map +1 -0
- package/dist/component/core/queries.d.ts +95 -0
- package/dist/component/core/queries.d.ts.map +1 -0
- package/dist/component/core/queries.js +86 -0
- package/dist/component/core/queries.js.map +1 -0
- package/dist/component/core/schema.d.ts +41 -0
- package/dist/component/core/schema.d.ts.map +1 -0
- package/dist/component/core/schema.js +12 -0
- package/dist/component/core/schema.js.map +1 -0
- package/dist/component/core/validators.d.ts +65 -0
- package/dist/component/core/validators.d.ts.map +1 -0
- package/dist/component/core/validators.js +35 -0
- package/dist/component/core/validators.js.map +1 -0
- package/dist/component/lib/fees.d.ts +74 -0
- package/dist/component/lib/fees.d.ts.map +1 -0
- package/dist/component/lib/fees.js +51 -0
- package/dist/component/lib/fees.js.map +1 -0
- package/dist/component/lib/idempotency.d.ts +9 -0
- package/dist/component/lib/idempotency.d.ts.map +1 -0
- package/dist/component/lib/idempotency.js +18 -0
- package/dist/component/lib/idempotency.js.map +1 -0
- package/dist/component/lib/reversals.d.ts +48 -0
- package/dist/component/lib/reversals.d.ts.map +1 -0
- package/dist/component/lib/reversals.js +79 -0
- package/dist/component/lib/reversals.js.map +1 -0
- package/dist/component/lib/stripeFactory.d.ts +7 -0
- package/dist/component/lib/stripeFactory.d.ts.map +1 -0
- package/dist/component/lib/stripeFactory.js +28 -0
- package/dist/component/lib/stripeFactory.js.map +1 -0
- package/dist/component/products/mutations.d.ts +22 -0
- package/dist/component/products/mutations.d.ts.map +1 -0
- package/dist/component/products/mutations.js +63 -0
- package/dist/component/products/mutations.js.map +1 -0
- package/dist/component/products/queries.d.ts +109 -0
- package/dist/component/products/queries.d.ts.map +1 -0
- package/dist/component/products/queries.js +122 -0
- package/dist/component/products/queries.js.map +1 -0
- package/dist/component/products/schema.d.ts +58 -0
- package/dist/component/products/schema.d.ts.map +1 -0
- package/dist/component/products/schema.js +20 -0
- package/dist/component/products/schema.js.map +1 -0
- package/dist/component/products/validators.d.ts +86 -0
- package/dist/component/products/validators.d.ts.map +1 -0
- package/dist/component/products/validators.js +47 -0
- package/dist/component/products/validators.js.map +1 -0
- package/dist/component/schema.d.ts +555 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +28 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/webhooks/mutations.d.ts +30 -0
- package/dist/component/webhooks/mutations.d.ts.map +1 -0
- package/dist/component/webhooks/mutations.js +124 -0
- package/dist/component/webhooks/mutations.js.map +1 -0
- package/dist/component/webhooks/queries.d.ts +35 -0
- package/dist/component/webhooks/queries.d.ts.map +1 -0
- package/dist/component/webhooks/queries.js +41 -0
- package/dist/component/webhooks/queries.js.map +1 -0
- package/dist/component/webhooks/schema.d.ts +22 -0
- package/dist/component/webhooks/schema.d.ts.map +1 -0
- package/dist/component/webhooks/schema.js +9 -0
- package/dist/component/webhooks/schema.js.map +1 -0
- package/dist/component/webhooks/validators.d.ts +37 -0
- package/dist/component/webhooks/validators.d.ts.map +1 -0
- package/dist/component/webhooks/validators.js +22 -0
- package/dist/component/webhooks/validators.js.map +1 -0
- package/dist/react/components/AccountCloseButton.d.ts +42 -0
- package/dist/react/components/AccountCloseButton.d.ts.map +1 -0
- package/dist/react/components/AccountCloseButton.js +33 -0
- package/dist/react/components/AccountCloseButton.js.map +1 -0
- package/dist/react/components/AccountCloseCard.d.ts +31 -0
- package/dist/react/components/AccountCloseCard.d.ts.map +1 -0
- package/dist/react/components/AccountCloseCard.js +22 -0
- package/dist/react/components/AccountCloseCard.js.map +1 -0
- package/dist/react/components/AccountCreateButton.d.ts +37 -0
- package/dist/react/components/AccountCreateButton.d.ts.map +1 -0
- package/dist/react/components/AccountCreateButton.js +30 -0
- package/dist/react/components/AccountCreateButton.js.map +1 -0
- package/dist/react/components/AccountCreateCard.d.ts +48 -0
- package/dist/react/components/AccountCreateCard.d.ts.map +1 -0
- package/dist/react/components/AccountCreateCard.js +29 -0
- package/dist/react/components/AccountCreateCard.js.map +1 -0
- package/dist/react/components/AccountLoginButton.d.ts +31 -0
- package/dist/react/components/AccountLoginButton.d.ts.map +1 -0
- package/dist/react/components/AccountLoginButton.js +31 -0
- package/dist/react/components/AccountLoginButton.js.map +1 -0
- package/dist/react/components/AccountLoginCard.d.ts +33 -0
- package/dist/react/components/AccountLoginCard.d.ts.map +1 -0
- package/dist/react/components/AccountLoginCard.js +19 -0
- package/dist/react/components/AccountLoginCard.js.map +1 -0
- package/dist/react/components/AccountOnboardingButton.d.ts +37 -0
- package/dist/react/components/AccountOnboardingButton.d.ts.map +1 -0
- package/dist/react/components/AccountOnboardingButton.js +30 -0
- package/dist/react/components/AccountOnboardingButton.js.map +1 -0
- package/dist/react/components/AccountOnboardingCard.d.ts +56 -0
- package/dist/react/components/AccountOnboardingCard.d.ts.map +1 -0
- package/dist/react/components/AccountOnboardingCard.js +36 -0
- package/dist/react/components/AccountOnboardingCard.js.map +1 -0
- package/dist/react/components/AddCardForm.d.ts +18 -0
- package/dist/react/components/AddCardForm.d.ts.map +1 -0
- package/dist/react/components/AddCardForm.js +49 -0
- package/dist/react/components/AddCardForm.js.map +1 -0
- package/dist/react/components/BillingPortalLink.d.ts +23 -0
- package/dist/react/components/BillingPortalLink.d.ts.map +1 -0
- package/dist/react/components/BillingPortalLink.js +55 -0
- package/dist/react/components/BillingPortalLink.js.map +1 -0
- package/dist/react/components/BuyerBillingView.d.ts +80 -0
- package/dist/react/components/BuyerBillingView.d.ts.map +1 -0
- package/dist/react/components/BuyerBillingView.js +44 -0
- package/dist/react/components/BuyerBillingView.js.map +1 -0
- package/dist/react/components/CheckoutSessionProvider.d.ts +34 -0
- package/dist/react/components/CheckoutSessionProvider.d.ts.map +1 -0
- package/dist/react/components/CheckoutSessionProvider.js +38 -0
- package/dist/react/components/CheckoutSessionProvider.js.map +1 -0
- package/dist/react/components/CheckoutSessionProviderWithKey.d.ts +36 -0
- package/dist/react/components/CheckoutSessionProviderWithKey.d.ts.map +1 -0
- package/dist/react/components/CheckoutSessionProviderWithKey.js +36 -0
- package/dist/react/components/CheckoutSessionProviderWithKey.js.map +1 -0
- package/dist/react/components/CheckoutStatus.d.ts +25 -0
- package/dist/react/components/CheckoutStatus.d.ts.map +1 -0
- package/dist/react/components/CheckoutStatus.js +22 -0
- package/dist/react/components/CheckoutStatus.js.map +1 -0
- package/dist/react/components/ConnectProvider.d.ts +29 -0
- package/dist/react/components/ConnectProvider.d.ts.map +1 -0
- package/dist/react/components/ConnectProvider.js +25 -0
- package/dist/react/components/ConnectProvider.js.map +1 -0
- package/dist/react/components/ConnectRequirements.d.ts +55 -0
- package/dist/react/components/ConnectRequirements.d.ts.map +1 -0
- package/dist/react/components/ConnectRequirements.js +58 -0
- package/dist/react/components/ConnectRequirements.js.map +1 -0
- package/dist/react/components/ConnectStatusBadge.d.ts +26 -0
- package/dist/react/components/ConnectStatusBadge.d.ts.map +1 -0
- package/dist/react/components/ConnectStatusBadge.js +20 -0
- package/dist/react/components/ConnectStatusBadge.js.map +1 -0
- package/dist/react/components/DeletePaymentMethodDialog.d.ts +25 -0
- package/dist/react/components/DeletePaymentMethodDialog.d.ts.map +1 -0
- package/dist/react/components/DeletePaymentMethodDialog.js +16 -0
- package/dist/react/components/DeletePaymentMethodDialog.js.map +1 -0
- package/dist/react/components/DisputeDetail.d.ts +42 -0
- package/dist/react/components/DisputeDetail.d.ts.map +1 -0
- package/dist/react/components/DisputeDetail.js +34 -0
- package/dist/react/components/DisputeDetail.js.map +1 -0
- package/dist/react/components/DisputesList.d.ts +42 -0
- package/dist/react/components/DisputesList.d.ts.map +1 -0
- package/dist/react/components/DisputesList.js +47 -0
- package/dist/react/components/DisputesList.js.map +1 -0
- package/dist/react/components/EarningsSummary.d.ts +53 -0
- package/dist/react/components/EarningsSummary.d.ts.map +1 -0
- package/dist/react/components/EarningsSummary.js +37 -0
- package/dist/react/components/EarningsSummary.js.map +1 -0
- package/dist/react/components/EmbeddedCheckout.d.ts +18 -0
- package/dist/react/components/EmbeddedCheckout.d.ts.map +1 -0
- package/dist/react/components/EmbeddedCheckout.js +17 -0
- package/dist/react/components/EmbeddedCheckout.js.map +1 -0
- package/dist/react/components/EmbeddedDisputes.d.ts +21 -0
- package/dist/react/components/EmbeddedDisputes.d.ts.map +1 -0
- package/dist/react/components/EmbeddedDisputes.js +17 -0
- package/dist/react/components/EmbeddedDisputes.js.map +1 -0
- package/dist/react/components/EvidenceForm.d.ts +63 -0
- package/dist/react/components/EvidenceForm.d.ts.map +1 -0
- package/dist/react/components/EvidenceForm.js +79 -0
- package/dist/react/components/EvidenceForm.js.map +1 -0
- package/dist/react/components/IntervalSelector.d.ts +20 -0
- package/dist/react/components/IntervalSelector.d.ts.map +1 -0
- package/dist/react/components/IntervalSelector.js +15 -0
- package/dist/react/components/IntervalSelector.js.map +1 -0
- package/dist/react/components/PaymentMethodActions.d.ts +27 -0
- package/dist/react/components/PaymentMethodActions.d.ts.map +1 -0
- package/dist/react/components/PaymentMethodActions.js +23 -0
- package/dist/react/components/PaymentMethodActions.js.map +1 -0
- package/dist/react/components/PaymentMethodsList.d.ts +32 -0
- package/dist/react/components/PaymentMethodsList.d.ts.map +1 -0
- package/dist/react/components/PaymentMethodsList.js +18 -0
- package/dist/react/components/PaymentMethodsList.js.map +1 -0
- package/dist/react/components/PayoutSchedule.d.ts +55 -0
- package/dist/react/components/PayoutSchedule.d.ts.map +1 -0
- package/dist/react/components/PayoutSchedule.js +51 -0
- package/dist/react/components/PayoutSchedule.js.map +1 -0
- package/dist/react/components/PriceBadge.d.ts +19 -0
- package/dist/react/components/PriceBadge.d.ts.map +1 -0
- package/dist/react/components/PriceBadge.js +25 -0
- package/dist/react/components/PriceBadge.js.map +1 -0
- package/dist/react/components/PriceCard.d.ts +34 -0
- package/dist/react/components/PriceCard.d.ts.map +1 -0
- package/dist/react/components/PriceCard.js +20 -0
- package/dist/react/components/PriceCard.js.map +1 -0
- package/dist/react/components/PricePicker.d.ts +28 -0
- package/dist/react/components/PricePicker.d.ts.map +1 -0
- package/dist/react/components/PricePicker.js +32 -0
- package/dist/react/components/PricePicker.js.map +1 -0
- package/dist/react/components/SplitBreakdown.d.ts +45 -0
- package/dist/react/components/SplitBreakdown.d.ts.map +1 -0
- package/dist/react/components/SplitBreakdown.js +48 -0
- package/dist/react/components/SplitBreakdown.js.map +1 -0
- package/dist/react/components/StripeProvider.d.ts +30 -0
- package/dist/react/components/StripeProvider.d.ts.map +1 -0
- package/dist/react/components/StripeProvider.js +38 -0
- package/dist/react/components/StripeProvider.js.map +1 -0
- package/dist/react/components/StripeProviderWithKey.d.ts +33 -0
- package/dist/react/components/StripeProviderWithKey.d.ts.map +1 -0
- package/dist/react/components/StripeProviderWithKey.js +44 -0
- package/dist/react/components/StripeProviderWithKey.js.map +1 -0
- package/dist/react/components/SubscriptionActions.d.ts +31 -0
- package/dist/react/components/SubscriptionActions.d.ts.map +1 -0
- package/dist/react/components/SubscriptionActions.js +30 -0
- package/dist/react/components/SubscriptionActions.js.map +1 -0
- package/dist/react/components/SubscriptionCard.d.ts +41 -0
- package/dist/react/components/SubscriptionCard.d.ts.map +1 -0
- package/dist/react/components/SubscriptionCard.js +18 -0
- package/dist/react/components/SubscriptionCard.js.map +1 -0
- package/dist/react/components/SubscriptionLineItems.d.ts +23 -0
- package/dist/react/components/SubscriptionLineItems.d.ts.map +1 -0
- package/dist/react/components/SubscriptionLineItems.js +20 -0
- package/dist/react/components/SubscriptionLineItems.js.map +1 -0
- package/dist/react/components/TrialAlert.d.ts +19 -0
- package/dist/react/components/TrialAlert.d.ts.map +1 -0
- package/dist/react/components/TrialAlert.js +16 -0
- package/dist/react/components/TrialAlert.js.map +1 -0
- package/dist/react/hooks/useAccount.d.ts +14 -0
- package/dist/react/hooks/useAccount.d.ts.map +1 -0
- package/dist/react/hooks/useAccount.js +18 -0
- package/dist/react/hooks/useAccount.js.map +1 -0
- package/dist/react/hooks/useAccountOnboarding.d.ts +15 -0
- package/dist/react/hooks/useAccountOnboarding.d.ts.map +1 -0
- package/dist/react/hooks/useAccountOnboarding.js +19 -0
- package/dist/react/hooks/useAccountOnboarding.js.map +1 -0
- package/dist/react/hooks/useCheckout.d.ts +14 -0
- package/dist/react/hooks/useCheckout.d.ts.map +1 -0
- package/dist/react/hooks/useCheckout.js +18 -0
- package/dist/react/hooks/useCheckout.js.map +1 -0
- package/dist/react/hooks/useCheckoutSession.d.ts +62 -0
- package/dist/react/hooks/useCheckoutSession.d.ts.map +1 -0
- package/dist/react/hooks/useCheckoutSession.js +68 -0
- package/dist/react/hooks/useCheckoutSession.js.map +1 -0
- package/dist/react/hooks/useConfirmPayment.d.ts +38 -0
- package/dist/react/hooks/useConfirmPayment.d.ts.map +1 -0
- package/dist/react/hooks/useConfirmPayment.js +91 -0
- package/dist/react/hooks/useConfirmPayment.js.map +1 -0
- package/dist/react/hooks/useDisputeWithCountdown.d.ts +25 -0
- package/dist/react/hooks/useDisputeWithCountdown.d.ts.map +1 -0
- package/dist/react/hooks/useDisputeWithCountdown.js +19 -0
- package/dist/react/hooks/useDisputeWithCountdown.js.map +1 -0
- package/dist/react/hooks/useDisputes.d.ts +14 -0
- package/dist/react/hooks/useDisputes.d.ts.map +1 -0
- package/dist/react/hooks/useDisputes.js +15 -0
- package/dist/react/hooks/useDisputes.js.map +1 -0
- package/dist/react/hooks/useEarnings.d.ts +92 -0
- package/dist/react/hooks/useEarnings.d.ts.map +1 -0
- package/dist/react/hooks/useEarnings.js +35 -0
- package/dist/react/hooks/useEarnings.js.map +1 -0
- package/dist/react/hooks/useInvoices.d.ts +11 -0
- package/dist/react/hooks/useInvoices.d.ts.map +1 -0
- package/dist/react/hooks/useInvoices.js +11 -0
- package/dist/react/hooks/useInvoices.js.map +1 -0
- package/dist/react/hooks/usePaymentMethodActions.d.ts +74 -0
- package/dist/react/hooks/usePaymentMethodActions.d.ts.map +1 -0
- package/dist/react/hooks/usePaymentMethodActions.js +156 -0
- package/dist/react/hooks/usePaymentMethodActions.js.map +1 -0
- package/dist/react/hooks/usePaymentMethods.d.ts +23 -0
- package/dist/react/hooks/usePaymentMethods.d.ts.map +1 -0
- package/dist/react/hooks/usePaymentMethods.js +18 -0
- package/dist/react/hooks/usePaymentMethods.js.map +1 -0
- package/dist/react/hooks/usePrices.d.ts +12 -0
- package/dist/react/hooks/usePrices.d.ts.map +1 -0
- package/dist/react/hooks/usePrices.js +16 -0
- package/dist/react/hooks/usePrices.js.map +1 -0
- package/dist/react/hooks/useProducts.d.ts +10 -0
- package/dist/react/hooks/useProducts.d.ts.map +1 -0
- package/dist/react/hooks/useProducts.js +11 -0
- package/dist/react/hooks/useProducts.js.map +1 -0
- package/dist/react/hooks/useSplitBreakdown.d.ts +58 -0
- package/dist/react/hooks/useSplitBreakdown.d.ts.map +1 -0
- package/dist/react/hooks/useSplitBreakdown.js +59 -0
- package/dist/react/hooks/useSplitBreakdown.js.map +1 -0
- package/dist/react/hooks/useStripeConfig.d.ts +14 -0
- package/dist/react/hooks/useStripeConfig.d.ts.map +1 -0
- package/dist/react/hooks/useStripeConfig.js +21 -0
- package/dist/react/hooks/useStripeConfig.js.map +1 -0
- package/dist/react/hooks/useStripeMode.d.ts +3 -0
- package/dist/react/hooks/useStripeMode.d.ts.map +1 -0
- package/dist/react/hooks/useStripeMode.js +6 -0
- package/dist/react/hooks/useStripeMode.js.map +1 -0
- package/dist/react/hooks/useStripePublishableKey.d.ts +2 -0
- package/dist/react/hooks/useStripePublishableKey.d.ts.map +1 -0
- package/dist/react/hooks/useStripePublishableKey.js +6 -0
- package/dist/react/hooks/useStripePublishableKey.js.map +1 -0
- package/dist/react/hooks/useSubscription.d.ts +17 -0
- package/dist/react/hooks/useSubscription.d.ts.map +1 -0
- package/dist/react/hooks/useSubscription.js +30 -0
- package/dist/react/hooks/useSubscription.js.map +1 -0
- package/dist/react/hooks/useSubscriptions.d.ts +11 -0
- package/dist/react/hooks/useSubscriptions.d.ts.map +1 -0
- package/dist/react/hooks/useSubscriptions.js +13 -0
- package/dist/react/hooks/useSubscriptions.js.map +1 -0
- package/dist/react/index.d.ts +108 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +87 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/lib/connect-requirement-keys.d.ts +14 -0
- package/dist/react/lib/connect-requirement-keys.d.ts.map +1 -0
- package/dist/react/lib/connect-requirement-keys.js +57 -0
- package/dist/react/lib/connect-requirement-keys.js.map +1 -0
- package/dist/react/lib/price-helpers.d.ts +31 -0
- package/dist/react/lib/price-helpers.d.ts.map +1 -0
- package/dist/react/lib/price-helpers.js +46 -0
- package/dist/react/lib/price-helpers.js.map +1 -0
- package/dist/react/lib/stripe-element-styles.d.ts +91 -0
- package/dist/react/lib/stripe-element-styles.d.ts.map +1 -0
- package/dist/react/lib/stripe-element-styles.js +131 -0
- package/dist/react/lib/stripe-element-styles.js.map +1 -0
- package/dist/react/lib/subscription-helpers.d.ts +33 -0
- package/dist/react/lib/subscription-helpers.d.ts.map +1 -0
- package/dist/react/lib/subscription-helpers.js +47 -0
- package/dist/react/lib/subscription-helpers.js.map +1 -0
- package/dist/react/types.d.ts +20 -0
- package/dist/react/types.d.ts.map +1 -0
- package/dist/react/types.js +2 -0
- package/dist/react/types.js.map +1 -0
- package/dist/testing/assert-test-env.d.ts +6 -0
- package/dist/testing/assert-test-env.d.ts.map +1 -0
- package/dist/testing/assert-test-env.js +15 -0
- package/dist/testing/assert-test-env.js.map +1 -0
- package/dist/testing/fixtures.d.ts +38 -0
- package/dist/testing/fixtures.d.ts.map +1 -0
- package/dist/testing/fixtures.js +54 -0
- package/dist/testing/fixtures.js.map +1 -0
- package/dist/testing/index.d.ts +4 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +4 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/mock-webhooks.d.ts +19 -0
- package/dist/testing/mock-webhooks.d.ts.map +1 -0
- package/dist/testing/mock-webhooks.js +81 -0
- package/dist/testing/mock-webhooks.js.map +1 -0
- package/package.json +131 -0
- package/src/client/billing/checkout.test.ts +1234 -0
- package/src/client/billing/checkout.ts +416 -0
- package/src/client/billing/invoices.test.ts +237 -0
- package/src/client/billing/invoices.ts +93 -0
- package/src/client/billing/subscriptions.test.ts +987 -0
- package/src/client/billing/subscriptions.ts +604 -0
- package/src/client/component-api-types.test.ts +137 -0
- package/src/client/connect/disputes.test.ts +487 -0
- package/src/client/connect/disputes.ts +302 -0
- package/src/client/connect/paymentMethodUtils.test.ts +83 -0
- package/src/client/connect/paymentMethodUtils.ts +28 -0
- package/src/client/connect/paymentMethods.test.ts +66 -0
- package/src/client/connect/paymentMethods.ts +55 -0
- package/src/client/connect/payouts.test.ts +132 -0
- package/src/client/connect/payouts.ts +105 -0
- package/src/client/connect/refundActor.test.ts +121 -0
- package/src/client/connect/refundActor.ts +60 -0
- package/src/client/connect/refunds.test.ts +341 -0
- package/src/client/connect/refunds.ts +256 -0
- package/src/client/connect/transfers.test.ts +492 -0
- package/src/client/connect/transfers.ts +572 -0
- package/src/client/constants.ts +15 -0
- package/src/client/core/accountLinks.test.ts +378 -0
- package/src/client/core/accountLinks.ts +211 -0
- package/src/client/core/accountStatus.test.ts +102 -0
- package/src/client/core/accountStatus.ts +91 -0
- package/src/client/core/accounts.test.ts +1496 -0
- package/src/client/core/accounts.ts +667 -0
- package/src/client/core/config.test.ts +68 -0
- package/src/client/core/config.ts +27 -0
- package/src/client/core/descriptors.test.ts +68 -0
- package/src/client/core/descriptors.ts +46 -0
- package/src/client/core/fees.test.ts +377 -0
- package/src/client/core/fees.ts +268 -0
- package/src/client/errors.test.ts +66 -0
- package/src/client/errors.ts +70 -0
- package/src/client/helpers.test.ts +74 -0
- package/src/client/helpers.ts +94 -0
- package/src/client/index.test.ts +1848 -0
- package/src/client/index.ts +1872 -0
- package/src/client/products/prices.test.ts +345 -0
- package/src/client/products/prices.ts +194 -0
- package/src/client/products/products.test.ts +476 -0
- package/src/client/products/products.ts +335 -0
- package/src/client/setup.test.ts +47 -0
- package/src/client/stripe-types.ts +108 -0
- package/src/client/types/documents.ts +245 -0
- package/src/client/types/events.ts +72 -0
- package/src/client/types/index.ts +43 -0
- package/src/client/types/options.ts +103 -0
- package/src/client/types/triggers.ts +293 -0
- package/src/client/types.ts +35 -0
- package/src/client/utils/data.test.ts +59 -0
- package/src/client/utils/data.ts +22 -0
- package/src/client/utils/owner.test.ts +35 -0
- package/src/client/utils/owner.ts +27 -0
- package/src/client/utils/stripeDashboardUrl.test.ts +71 -0
- package/src/client/utils/stripeDashboardUrl.ts +71 -0
- package/src/client/utils/webhookEndpoints.test.ts +454 -0
- package/src/client/utils/webhookEndpoints.ts +271 -0
- package/src/client/webhook-handlers.test.ts +603 -0
- package/src/client/webhooks/handler.test.ts +604 -0
- package/src/client/webhooks/handler.ts +256 -0
- package/src/client/webhooks/helpers.test.ts +330 -0
- package/src/client/webhooks/helpers.ts +277 -0
- package/src/client/webhooks/hooks.test.ts +111 -0
- package/src/client/webhooks/hooks.ts +177 -0
- package/src/client/webhooks/index.ts +51 -0
- package/src/client/webhooks/processors.test.ts +2392 -0
- package/src/client/webhooks/processors.ts +1166 -0
- package/src/client/webhooks/v2.test.ts +396 -0
- package/src/client/webhooks/v2.ts +124 -0
- package/src/client/webhooks.test.ts +1689 -0
- package/src/client/webhooks.ts +3 -0
- package/src/component/_generated/api.ts +86 -0
- package/src/component/_generated/component.ts +1758 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +164 -0
- package/src/component/billing/mutations.ts +155 -0
- package/src/component/billing/queries.ts +378 -0
- package/src/component/billing/schema.ts +40 -0
- package/src/component/billing/validators.ts +137 -0
- package/src/component/config-queries.test.ts +47 -0
- package/src/component/connect/mutations.ts +624 -0
- package/src/component/connect/queries.ts +537 -0
- package/src/component/connect/schema.ts +90 -0
- package/src/component/connect/validators.ts +333 -0
- package/src/component/connect.test.ts +721 -0
- package/src/component/convex.config.ts +17 -0
- package/src/component/core/mutations.ts +182 -0
- package/src/component/core/queries.ts +100 -0
- package/src/component/core/schema.ts +13 -0
- package/src/component/core/validators.ts +55 -0
- package/src/component/earnings-totals.test.ts +196 -0
- package/src/component/fee-routing.test.ts +124 -0
- package/src/component/lib/fees.ts +64 -0
- package/src/component/lib/idempotency.test.ts +37 -0
- package/src/component/lib/idempotency.ts +20 -0
- package/src/component/lib/reversals.ts +113 -0
- package/src/component/lib/stripeFactory.ts +30 -0
- package/src/component/products/mutations.ts +73 -0
- package/src/component/products/queries.ts +149 -0
- package/src/component/products/schema.ts +22 -0
- package/src/component/products/validators.ts +63 -0
- package/src/component/public.test.ts +1858 -0
- package/src/component/reversal-races.test.ts +920 -0
- package/src/component/schema.ts +45 -0
- package/src/component/subscription-scoping.test.ts +118 -0
- package/src/component/transfers.test.ts +402 -0
- package/src/component/triggers.test.ts +359 -0
- package/src/component/webhooks/mutations.ts +151 -0
- package/src/component/webhooks/queries.ts +44 -0
- package/src/component/webhooks/schema.ts +10 -0
- package/src/component/webhooks/validators.ts +30 -0
- package/src/react/components/AccountCloseButton.tsx +91 -0
- package/src/react/components/AccountCloseCard.tsx +84 -0
- package/src/react/components/AccountCreateButton.tsx +83 -0
- package/src/react/components/AccountCreateCard.tsx +123 -0
- package/src/react/components/AccountLoginButton.tsx +69 -0
- package/src/react/components/AccountLoginCard.tsx +99 -0
- package/src/react/components/AccountOnboardingButton.tsx +83 -0
- package/src/react/components/AccountOnboardingCard.test.ts +27 -0
- package/src/react/components/AccountOnboardingCard.tsx +152 -0
- package/src/react/components/AddCardForm.tsx +80 -0
- package/src/react/components/BillingPortalLink.tsx +93 -0
- package/src/react/components/BuyerBillingView.render.test.tsx +207 -0
- package/src/react/components/BuyerBillingView.tsx +193 -0
- package/src/react/components/CheckoutSessionProvider.tsx +70 -0
- package/src/react/components/CheckoutSessionProviderWithKey.tsx +99 -0
- package/src/react/components/CheckoutStatus.tsx +71 -0
- package/src/react/components/ConnectProvider.tsx +58 -0
- package/src/react/components/ConnectRequirements.tsx +159 -0
- package/src/react/components/ConnectStatusBadge.tsx +60 -0
- package/src/react/components/DeletePaymentMethodDialog.tsx +61 -0
- package/src/react/components/DisputeDetail.render.test.tsx +143 -0
- package/src/react/components/DisputeDetail.tsx +111 -0
- package/src/react/components/DisputesList.render.test.tsx +244 -0
- package/src/react/components/DisputesList.tsx +120 -0
- package/src/react/components/EarningsSummary.render.test.tsx +153 -0
- package/src/react/components/EarningsSummary.tsx +131 -0
- package/src/react/components/EmbeddedCheckout.test.ts +25 -0
- package/src/react/components/EmbeddedCheckout.tsx +54 -0
- package/src/react/components/EmbeddedDisputes.render.test.tsx +142 -0
- package/src/react/components/EmbeddedDisputes.tsx +38 -0
- package/src/react/components/EvidenceForm.render.test.tsx +215 -0
- package/src/react/components/EvidenceForm.tsx +176 -0
- package/src/react/components/IntervalSelector.tsx +58 -0
- package/src/react/components/PaymentMethodActions.tsx +68 -0
- package/src/react/components/PaymentMethodsList.tsx +81 -0
- package/src/react/components/PayoutSchedule.render.test.tsx +178 -0
- package/src/react/components/PayoutSchedule.tsx +148 -0
- package/src/react/components/PriceBadge.render.test.tsx +134 -0
- package/src/react/components/PriceBadge.tsx +49 -0
- package/src/react/components/PriceCard.render.test.tsx +135 -0
- package/src/react/components/PriceCard.tsx +83 -0
- package/src/react/components/PricePicker.tsx +95 -0
- package/src/react/components/SplitBreakdown.render.test.tsx +168 -0
- package/src/react/components/SplitBreakdown.tsx +121 -0
- package/src/react/components/StripeProvider.test.ts +45 -0
- package/src/react/components/StripeProvider.tsx +80 -0
- package/src/react/components/StripeProviderWithKey.tsx +95 -0
- package/src/react/components/SubscriptionActions.tsx +79 -0
- package/src/react/components/SubscriptionCard.tsx +92 -0
- package/src/react/components/SubscriptionLineItems.tsx +62 -0
- package/src/react/components/TrialAlert.render.test.tsx +132 -0
- package/src/react/components/TrialAlert.tsx +44 -0
- package/src/react/hooks/skipSentinel.test.ts +85 -0
- package/src/react/hooks/useAccount.ts +28 -0
- package/src/react/hooks/useAccountOnboarding.test.ts +75 -0
- package/src/react/hooks/useAccountOnboarding.ts +35 -0
- package/src/react/hooks/useCheckout.ts +33 -0
- package/src/react/hooks/useCheckoutSession.ts +110 -0
- package/src/react/hooks/useConfirmPayment.ts +117 -0
- package/src/react/hooks/useDisputeWithCountdown.test.ts +90 -0
- package/src/react/hooks/useDisputeWithCountdown.ts +48 -0
- package/src/react/hooks/useDisputes.test.ts +75 -0
- package/src/react/hooks/useDisputes.ts +28 -0
- package/src/react/hooks/useEarnings.test.ts +159 -0
- package/src/react/hooks/useEarnings.ts +132 -0
- package/src/react/hooks/useInvoices.test.ts +79 -0
- package/src/react/hooks/useInvoices.ts +25 -0
- package/src/react/hooks/usePaymentMethodActions.ts +206 -0
- package/src/react/hooks/usePaymentMethods.ts +44 -0
- package/src/react/hooks/usePrices.ts +26 -0
- package/src/react/hooks/useProducts.test.ts +75 -0
- package/src/react/hooks/useProducts.ts +24 -0
- package/src/react/hooks/useSplitBreakdown.test.ts +151 -0
- package/src/react/hooks/useSplitBreakdown.ts +118 -0
- package/src/react/hooks/useStripeConfig.ts +31 -0
- package/src/react/hooks/useStripeMode.test.ts +49 -0
- package/src/react/hooks/useStripeMode.ts +9 -0
- package/src/react/hooks/useStripePublishableKey.test.ts +35 -0
- package/src/react/hooks/useStripePublishableKey.ts +9 -0
- package/src/react/hooks/useSubscription.ts +52 -0
- package/src/react/hooks/useSubscriptions.ts +33 -0
- package/src/react/index.payment-element.test.ts +41 -0
- package/src/react/index.test.ts +167 -0
- package/src/react/index.ts +279 -0
- package/src/react/lib/connect-requirement-keys.test.ts +73 -0
- package/src/react/lib/connect-requirement-keys.ts +66 -0
- package/src/react/lib/price-helpers.test.ts +199 -0
- package/src/react/lib/price-helpers.ts +68 -0
- package/src/react/lib/stripe-element-styles.test.ts +94 -0
- package/src/react/lib/stripe-element-styles.ts +178 -0
- package/src/react/lib/subscription-helpers.test.ts +127 -0
- package/src/react/lib/subscription-helpers.ts +64 -0
- package/src/react/types.ts +32 -0
- package/src/test.ts +5 -0
- package/src/testing/assert-test-env.test.ts +33 -0
- package/src/testing/assert-test-env.ts +20 -0
- package/src/testing/fixtures.ts +95 -0
- package/src/testing/index.ts +13 -0
- package/src/testing/mock-webhooks.test.ts +97 -0
- package/src/testing/mock-webhooks.ts +101 -0
- package/src/vite-env.d.ts +4 -0
|
@@ -0,0 +1,2392 @@
|
|
|
1
|
+
// @vitest-environment edge-runtime
|
|
2
|
+
/**
|
|
3
|
+
* Tests for the webhook event processor — the field-normalization surface that
|
|
4
|
+
* turns Stripe wire objects into component upsert payloads. This is the riskiest
|
|
5
|
+
* correctness area in the library: a wrong default or a mishandled
|
|
6
|
+
* string-vs-object union silently corrupts a row with no error. We drive each
|
|
7
|
+
* event type through `processEvent` and assert the EXACT payload handed to the
|
|
8
|
+
* component mutation, covering every union branch, the `userId: ""` sentinel,
|
|
9
|
+
* trial derivation, the payment-status collapse, and the checkout metadata
|
|
10
|
+
* merge.
|
|
11
|
+
*
|
|
12
|
+
* `whCtx` is built WITHOUT `config.webhooks`, so `dispatchUpsert` routes through
|
|
13
|
+
* `ctx.runMutation(componentRef(component, <path>), data)` — letting us read the
|
|
14
|
+
* normalized payload straight off the runMutation mock.
|
|
15
|
+
*/
|
|
16
|
+
import type Stripe from "stripe";
|
|
17
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
18
|
+
|
|
19
|
+
import { computeReversalSlices } from "../../component/lib/reversals.js";
|
|
20
|
+
import type { WebhookContext } from "./helpers.js";
|
|
21
|
+
import { processEvent } from "./processors.js";
|
|
22
|
+
|
|
23
|
+
const TO_REF = Symbol.for("toReferencePath");
|
|
24
|
+
|
|
25
|
+
/** Fake component exposing every query/mutation ref processors.ts resolves. */
|
|
26
|
+
function makeComponent() {
|
|
27
|
+
const ref = (path: string) => ({ [TO_REF]: `betterStripe/${path}` });
|
|
28
|
+
return {
|
|
29
|
+
products: {
|
|
30
|
+
queries: {
|
|
31
|
+
getProductByStripeId: ref("products/queries/getProductByStripeId"),
|
|
32
|
+
},
|
|
33
|
+
mutations: {
|
|
34
|
+
upsertProduct: ref("products/mutations/upsertProduct"),
|
|
35
|
+
upsertPrice: ref("products/mutations/upsertPrice"),
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
billing: {
|
|
39
|
+
queries: {
|
|
40
|
+
getCheckoutSessionByStripeId: ref(
|
|
41
|
+
"billing/queries/getCheckoutSessionByStripeId",
|
|
42
|
+
),
|
|
43
|
+
},
|
|
44
|
+
mutations: {
|
|
45
|
+
upsertSubscription: ref("billing/mutations/upsertSubscription"),
|
|
46
|
+
upsertCheckoutSession: ref("billing/mutations/upsertCheckoutSession"),
|
|
47
|
+
upsertInvoice: ref("billing/mutations/upsertInvoice"),
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
connect: {
|
|
51
|
+
mutations: {
|
|
52
|
+
upsertPayment: ref("connect/mutations/upsertPayment"),
|
|
53
|
+
upsertPayout: ref("connect/mutations/upsertPayout"),
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function makeCtx(opts?: { query?: unknown; queryThrows?: boolean }) {
|
|
60
|
+
const runQuery = vi.fn();
|
|
61
|
+
if (opts?.queryThrows) runQuery.mockRejectedValue(new Error("not found"));
|
|
62
|
+
else runQuery.mockResolvedValue(opts?.query ?? null);
|
|
63
|
+
// `claimReversalSlices` returns the slices the reversal engines execute
|
|
64
|
+
// (BTS-63). Fake only its storage: the slice math is the REAL shared
|
|
65
|
+
// computeReversalSlices, applied to this ctx's ledger legs.
|
|
66
|
+
const runMutation = vi.fn(
|
|
67
|
+
async (ref: Record<symbol, string>, args: Record<string, unknown>) => {
|
|
68
|
+
const path = ref?.[TO_REF] ?? "";
|
|
69
|
+
if (!String(path).endsWith("connect/mutations/claimReversalSlices")) {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
const legs = (Array.isArray(opts?.query) ? opts.query : []) as {
|
|
73
|
+
stripeTransferId: string;
|
|
74
|
+
amount: number;
|
|
75
|
+
reversedAmount?: number;
|
|
76
|
+
reversalClaimedAmount?: number;
|
|
77
|
+
}[];
|
|
78
|
+
const slices = computeReversalSlices(
|
|
79
|
+
legs.map((t) => ({
|
|
80
|
+
stripeTransferId: t.stripeTransferId,
|
|
81
|
+
amount: t.amount,
|
|
82
|
+
frontier: Math.max(t.reversedAmount ?? 0, t.reversalClaimedAmount ?? 0),
|
|
83
|
+
})),
|
|
84
|
+
args.mode as Parameters<typeof computeReversalSlices>[1],
|
|
85
|
+
);
|
|
86
|
+
return {
|
|
87
|
+
replay: false,
|
|
88
|
+
slices: slices.map((s) => ({
|
|
89
|
+
...s,
|
|
90
|
+
confirmed:
|
|
91
|
+
legs.find((t) => t.stripeTransferId === s.stripeTransferId)
|
|
92
|
+
?.reversedAmount ?? 0,
|
|
93
|
+
})),
|
|
94
|
+
};
|
|
95
|
+
},
|
|
96
|
+
);
|
|
97
|
+
return { runQuery, runMutation };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
let trCreateSeq = 0;
|
|
101
|
+
|
|
102
|
+
/** Minimal transfer-reversal shape the mocks record/return (BTS-69). */
|
|
103
|
+
type MockReversal = {
|
|
104
|
+
id: string;
|
|
105
|
+
amount: number;
|
|
106
|
+
metadata: Record<string, string> | null;
|
|
107
|
+
/** Test-only join back to the reversed transfer id. */
|
|
108
|
+
_transfer?: string;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
function makeStripe() {
|
|
112
|
+
return {
|
|
113
|
+
products: { retrieve: vi.fn() },
|
|
114
|
+
subscriptions: {
|
|
115
|
+
retrieve: vi.fn(),
|
|
116
|
+
update: vi.fn().mockResolvedValue({}),
|
|
117
|
+
cancel: vi.fn().mockResolvedValue({}),
|
|
118
|
+
},
|
|
119
|
+
invoices: {
|
|
120
|
+
update: vi.fn().mockResolvedValue({}),
|
|
121
|
+
retrieve: vi.fn(),
|
|
122
|
+
},
|
|
123
|
+
transfers: {
|
|
124
|
+
create: vi.fn(async (_params: unknown) => ({ id: `tr_${++trCreateSeq}` })),
|
|
125
|
+
createReversal: vi.fn(
|
|
126
|
+
async (
|
|
127
|
+
_id: unknown,
|
|
128
|
+
_params?: unknown,
|
|
129
|
+
_opts?: unknown,
|
|
130
|
+
): Promise<MockReversal> => ({ id: "trr_x", amount: 0, metadata: null }),
|
|
131
|
+
),
|
|
132
|
+
// No prior reversals by default; the first-invoice-fee adoption guard
|
|
133
|
+
// (BTS-69) lists these before reversing.
|
|
134
|
+
listReversals: vi.fn(
|
|
135
|
+
async (_id: unknown, _opts?: unknown): Promise<{ data: MockReversal[] }> => ({
|
|
136
|
+
data: [],
|
|
137
|
+
}),
|
|
138
|
+
),
|
|
139
|
+
},
|
|
140
|
+
invoicePayments: {
|
|
141
|
+
list: vi.fn(
|
|
142
|
+
async (_params: unknown): Promise<{ data: Record<string, unknown>[] }> => ({
|
|
143
|
+
data: [{ payment: { charge: "ch_inv" } }],
|
|
144
|
+
}),
|
|
145
|
+
),
|
|
146
|
+
},
|
|
147
|
+
paymentIntents: {
|
|
148
|
+
retrieve: vi.fn(),
|
|
149
|
+
update: vi.fn().mockResolvedValue({}),
|
|
150
|
+
},
|
|
151
|
+
charges: { retrieve: vi.fn() },
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function makeWhCtx(overrides?: {
|
|
156
|
+
ctx?: ReturnType<typeof makeCtx>;
|
|
157
|
+
stripe?: ReturnType<typeof makeStripe>;
|
|
158
|
+
config?: Record<string, unknown>;
|
|
159
|
+
}): WebhookContext & {
|
|
160
|
+
ctx: ReturnType<typeof makeCtx>;
|
|
161
|
+
stripe: ReturnType<typeof makeStripe>;
|
|
162
|
+
} {
|
|
163
|
+
const ctx = overrides?.ctx ?? makeCtx();
|
|
164
|
+
const stripe = overrides?.stripe ?? makeStripe();
|
|
165
|
+
return {
|
|
166
|
+
ctx,
|
|
167
|
+
component: makeComponent(),
|
|
168
|
+
stripe,
|
|
169
|
+
webhookSecret: "whsec_test",
|
|
170
|
+
...(overrides?.config ? { config: overrides.config } : {}),
|
|
171
|
+
// no `config.webhooks` → dispatchUpsert uses the direct component path
|
|
172
|
+
} as unknown as WebhookContext & {
|
|
173
|
+
ctx: ReturnType<typeof makeCtx>;
|
|
174
|
+
stripe: ReturnType<typeof makeStripe>;
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Wrap an object as a minimal Stripe.Event of the given type. */
|
|
179
|
+
function event(type: string, object: unknown): Stripe.Event {
|
|
180
|
+
return { type, data: { object } } as unknown as Stripe.Event;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** The payload + resolved ref path from the Nth runMutation call. */
|
|
184
|
+
function dispatchedPayload(ctx: ReturnType<typeof makeCtx>, n = 0) {
|
|
185
|
+
const call = ctx.runMutation.mock.calls[n];
|
|
186
|
+
return { path: call[0][TO_REF] as string, data: call[1] as Record<string, unknown> };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
beforeEach(() => {
|
|
190
|
+
vi.restoreAllMocks();
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
describe("processEvent — products", () => {
|
|
194
|
+
it("normalizes a product.created event, carrying a Connect account id", async () => {
|
|
195
|
+
const whCtx = makeWhCtx();
|
|
196
|
+
await processEvent(
|
|
197
|
+
whCtx,
|
|
198
|
+
event("product.created", {
|
|
199
|
+
id: "prod_1",
|
|
200
|
+
account: "acct_connect",
|
|
201
|
+
name: "Widget",
|
|
202
|
+
description: null,
|
|
203
|
+
active: true,
|
|
204
|
+
metadata: { k: "v" },
|
|
205
|
+
}),
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
const { path, data } = dispatchedPayload(whCtx.ctx);
|
|
209
|
+
expect(path).toBe("betterStripe/products/mutations/upsertProduct");
|
|
210
|
+
expect(data).toEqual({
|
|
211
|
+
stripeProductId: "prod_1",
|
|
212
|
+
accountId: "acct_connect",
|
|
213
|
+
name: "Widget",
|
|
214
|
+
description: undefined,
|
|
215
|
+
active: true,
|
|
216
|
+
metadata: { k: "v" },
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("leaves accountId undefined when no Connect account is present", async () => {
|
|
221
|
+
const whCtx = makeWhCtx();
|
|
222
|
+
await processEvent(
|
|
223
|
+
whCtx,
|
|
224
|
+
event("product.updated", {
|
|
225
|
+
id: "prod_2",
|
|
226
|
+
name: "Gadget",
|
|
227
|
+
description: "d",
|
|
228
|
+
active: false,
|
|
229
|
+
metadata: {},
|
|
230
|
+
}),
|
|
231
|
+
);
|
|
232
|
+
expect(dispatchedPayload(whCtx.ctx).data.accountId).toBeUndefined();
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
describe("processEvent — prices", () => {
|
|
237
|
+
const price = {
|
|
238
|
+
id: "price_1",
|
|
239
|
+
product: "prod_1",
|
|
240
|
+
nickname: "Monthly",
|
|
241
|
+
unit_amount: 1500,
|
|
242
|
+
currency: "usd",
|
|
243
|
+
active: true,
|
|
244
|
+
type: "recurring",
|
|
245
|
+
recurring: { interval: "month", interval_count: 3 },
|
|
246
|
+
metadata: {},
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
it("dispatches a price upsert when the product already exists in the DB", async () => {
|
|
250
|
+
const ctx = makeCtx({ query: { _id: "internal_prod_1" } });
|
|
251
|
+
const whCtx = makeWhCtx({ ctx });
|
|
252
|
+
|
|
253
|
+
await processEvent(whCtx, event("price.created", price));
|
|
254
|
+
|
|
255
|
+
// last runMutation is the price upsert (no auto-create happened)
|
|
256
|
+
const calls = ctx.runMutation.mock.calls;
|
|
257
|
+
const upsert = calls[calls.length - 1];
|
|
258
|
+
expect(upsert[0][TO_REF]).toBe("betterStripe/products/mutations/upsertPrice");
|
|
259
|
+
expect(upsert[1]).toMatchObject({
|
|
260
|
+
stripePriceId: "price_1",
|
|
261
|
+
productId: "internal_prod_1",
|
|
262
|
+
stripeProductId: "prod_1",
|
|
263
|
+
unitAmount: 1500,
|
|
264
|
+
interval: "month",
|
|
265
|
+
intervalCount: 3,
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it("auto-creates a missing product, then dispatches the price", async () => {
|
|
270
|
+
// first getProductByStripeId → null, second (after create) → the row
|
|
271
|
+
const runQuery = vi
|
|
272
|
+
.fn()
|
|
273
|
+
.mockResolvedValueOnce(null)
|
|
274
|
+
.mockResolvedValueOnce({ _id: "internal_new" });
|
|
275
|
+
const ctx = {
|
|
276
|
+
runQuery,
|
|
277
|
+
runMutation: vi.fn().mockResolvedValue(undefined),
|
|
278
|
+
};
|
|
279
|
+
const stripe = makeStripe();
|
|
280
|
+
stripe.products.retrieve.mockResolvedValue({
|
|
281
|
+
id: "prod_1",
|
|
282
|
+
name: "Recovered",
|
|
283
|
+
description: null,
|
|
284
|
+
active: true,
|
|
285
|
+
metadata: {},
|
|
286
|
+
});
|
|
287
|
+
const whCtx = makeWhCtx({ ctx: ctx as never, stripe });
|
|
288
|
+
|
|
289
|
+
await processEvent(whCtx, event("price.updated", price));
|
|
290
|
+
|
|
291
|
+
expect(stripe.products.retrieve).toHaveBeenCalledWith("prod_1");
|
|
292
|
+
// product upsert then price upsert both ran
|
|
293
|
+
const paths = ctx.runMutation.mock.calls.map((c) => c[0][TO_REF]);
|
|
294
|
+
expect(paths).toContain("betterStripe/products/mutations/upsertProduct");
|
|
295
|
+
expect(paths).toContain("betterStripe/products/mutations/upsertPrice");
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it("skips the price (no dispatch) when product auto-create fails", async () => {
|
|
299
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
300
|
+
const ctx = makeCtx({ query: null });
|
|
301
|
+
const stripe = makeStripe();
|
|
302
|
+
stripe.products.retrieve.mockRejectedValue(new Error("gone"));
|
|
303
|
+
const whCtx = makeWhCtx({ ctx, stripe });
|
|
304
|
+
|
|
305
|
+
await processEvent(whCtx, event("price.created", price));
|
|
306
|
+
|
|
307
|
+
// no upsertPrice dispatch happened
|
|
308
|
+
const paths = ctx.runMutation.mock.calls.map((c) => c[0][TO_REF]);
|
|
309
|
+
expect(paths).not.toContain("betterStripe/products/mutations/upsertPrice");
|
|
310
|
+
expect(warn).toHaveBeenCalled();
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
it("skips a price with no resolvable product id", async () => {
|
|
314
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
315
|
+
const ctx = makeCtx({ query: null });
|
|
316
|
+
const stripe = makeStripe();
|
|
317
|
+
const whCtx = makeWhCtx({ ctx, stripe });
|
|
318
|
+
|
|
319
|
+
await processEvent(
|
|
320
|
+
whCtx,
|
|
321
|
+
event("price.created", { ...price, product: null }),
|
|
322
|
+
);
|
|
323
|
+
|
|
324
|
+
// never even attempted to retrieve a product
|
|
325
|
+
expect(stripe.products.retrieve).not.toHaveBeenCalled();
|
|
326
|
+
expect(ctx.runMutation).not.toHaveBeenCalled();
|
|
327
|
+
expect(warn).toHaveBeenCalled();
|
|
328
|
+
});
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
describe("processEvent — subscriptions", () => {
|
|
332
|
+
const baseSub = {
|
|
333
|
+
id: "sub_1",
|
|
334
|
+
status: "active",
|
|
335
|
+
customer: "acct_cust",
|
|
336
|
+
cancel_at_period_end: false,
|
|
337
|
+
canceled_at: null,
|
|
338
|
+
trial_start: null,
|
|
339
|
+
trial_end: null,
|
|
340
|
+
metadata: { userId: "user_1", orgId: "org_1" },
|
|
341
|
+
items: {
|
|
342
|
+
data: [
|
|
343
|
+
{
|
|
344
|
+
price: { id: "price_x" },
|
|
345
|
+
quantity: 2,
|
|
346
|
+
current_period_start: 1700000000,
|
|
347
|
+
current_period_end: 1702592000,
|
|
348
|
+
},
|
|
349
|
+
],
|
|
350
|
+
},
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
it("normalizes a created subscription with identifiers, trial, period, and price", async () => {
|
|
354
|
+
const whCtx = makeWhCtx();
|
|
355
|
+
await processEvent(
|
|
356
|
+
whCtx,
|
|
357
|
+
event("customer.subscription.created", baseSub),
|
|
358
|
+
);
|
|
359
|
+
|
|
360
|
+
const { path, data } = dispatchedPayload(whCtx.ctx);
|
|
361
|
+
expect(path).toBe("betterStripe/billing/mutations/upsertSubscription");
|
|
362
|
+
expect(data).toMatchObject({
|
|
363
|
+
stripeSubscriptionId: "sub_1",
|
|
364
|
+
accountId: "acct_cust",
|
|
365
|
+
userId: "user_1",
|
|
366
|
+
orgId: "org_1",
|
|
367
|
+
status: "active",
|
|
368
|
+
priceId: "price_x",
|
|
369
|
+
quantity: 2,
|
|
370
|
+
cancelAtPeriodEnd: false,
|
|
371
|
+
isTrialing: false,
|
|
372
|
+
});
|
|
373
|
+
expect(data.currentPeriodStart).toBe(
|
|
374
|
+
new Date(1700000000 * 1000).toISOString(),
|
|
375
|
+
);
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
it("routes a deleted subscription through the subscriptionDeleted dispatcher", async () => {
|
|
379
|
+
const whCtx = makeWhCtx();
|
|
380
|
+
await processEvent(
|
|
381
|
+
whCtx,
|
|
382
|
+
event("customer.subscription.deleted", baseSub),
|
|
383
|
+
);
|
|
384
|
+
// both dispatchers map to the same upsertSubscription path, so assert the
|
|
385
|
+
// event still produces exactly one subscription upsert
|
|
386
|
+
expect(whCtx.ctx.runMutation).toHaveBeenCalledTimes(1);
|
|
387
|
+
expect(dispatchedPayload(whCtx.ctx).path).toBe(
|
|
388
|
+
"betterStripe/billing/mutations/upsertSubscription",
|
|
389
|
+
);
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
it("derives accountId from an expanded customer object", async () => {
|
|
393
|
+
const whCtx = makeWhCtx();
|
|
394
|
+
await processEvent(
|
|
395
|
+
whCtx,
|
|
396
|
+
event("customer.subscription.updated", {
|
|
397
|
+
...baseSub,
|
|
398
|
+
customer: { id: "acct_obj" },
|
|
399
|
+
}),
|
|
400
|
+
);
|
|
401
|
+
expect(dispatchedPayload(whCtx.ctx).data.accountId).toBe("acct_obj");
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
it("prefers the V2 customer_account over the legacy customer id", async () => {
|
|
405
|
+
const whCtx = makeWhCtx();
|
|
406
|
+
await processEvent(
|
|
407
|
+
whCtx,
|
|
408
|
+
event("customer.subscription.created", {
|
|
409
|
+
...baseSub,
|
|
410
|
+
customer: "cus_legacy",
|
|
411
|
+
customer_account: "acct_v2",
|
|
412
|
+
}),
|
|
413
|
+
);
|
|
414
|
+
expect(dispatchedPayload(whCtx.ctx).data.accountId).toBe("acct_v2");
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
it("falls back to the empty-string userId sentinel when metadata is absent", async () => {
|
|
418
|
+
const whCtx = makeWhCtx();
|
|
419
|
+
await processEvent(
|
|
420
|
+
whCtx,
|
|
421
|
+
event("customer.subscription.updated", { ...baseSub, metadata: null }),
|
|
422
|
+
);
|
|
423
|
+
const { data } = dispatchedPayload(whCtx.ctx);
|
|
424
|
+
expect(data.userId).toBe("");
|
|
425
|
+
expect(data.orgId).toBeUndefined();
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
it("leaves price/period/quantity undefined when the subscription has no items", async () => {
|
|
429
|
+
const whCtx = makeWhCtx();
|
|
430
|
+
await processEvent(
|
|
431
|
+
whCtx,
|
|
432
|
+
event("customer.subscription.updated", {
|
|
433
|
+
...baseSub,
|
|
434
|
+
items: { data: [] },
|
|
435
|
+
}),
|
|
436
|
+
);
|
|
437
|
+
const { data } = dispatchedPayload(whCtx.ctx);
|
|
438
|
+
expect(data.priceId).toBeUndefined();
|
|
439
|
+
expect(data.quantity).toBeUndefined();
|
|
440
|
+
expect(data.currentPeriodStart).toBeUndefined();
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
it("marks a trialing subscription and converts the trial epochs", async () => {
|
|
444
|
+
const whCtx = makeWhCtx();
|
|
445
|
+
await processEvent(
|
|
446
|
+
whCtx,
|
|
447
|
+
event("customer.subscription.trial_will_end", {
|
|
448
|
+
...baseSub,
|
|
449
|
+
status: "trialing",
|
|
450
|
+
trial_start: 1700000000,
|
|
451
|
+
trial_end: 1700600000,
|
|
452
|
+
}),
|
|
453
|
+
);
|
|
454
|
+
const { data } = dispatchedPayload(whCtx.ctx);
|
|
455
|
+
expect(data.isTrialing).toBe(true);
|
|
456
|
+
expect(data.trialEnd).toBe(new Date(1700600000 * 1000).toISOString());
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
// BTS-33: when a recurring charge fails, Stripe drives the subscription to
|
|
460
|
+
// `past_due` (then `unpaid` once retries are exhausted) via
|
|
461
|
+
// customer.subscription.updated. The raw delinquent status must reach the
|
|
462
|
+
// table unchanged so `onSubscriptionUpdated` consumers can act on it.
|
|
463
|
+
it.each(["past_due", "unpaid"] as const)(
|
|
464
|
+
"passes through the delinquent subscription status %s",
|
|
465
|
+
async (status) => {
|
|
466
|
+
const whCtx = makeWhCtx();
|
|
467
|
+
await processEvent(
|
|
468
|
+
whCtx,
|
|
469
|
+
event("customer.subscription.updated", { ...baseSub, status }),
|
|
470
|
+
);
|
|
471
|
+
expect(dispatchedPayload(whCtx.ctx).data.status).toBe(status);
|
|
472
|
+
},
|
|
473
|
+
);
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
describe("processEvent — checkout", () => {
|
|
477
|
+
const session = {
|
|
478
|
+
id: "cs_1",
|
|
479
|
+
customer: "acct_c",
|
|
480
|
+
mode: "subscription",
|
|
481
|
+
status: "complete",
|
|
482
|
+
client_secret: "cs_secret",
|
|
483
|
+
url: "https://pay",
|
|
484
|
+
metadata: { userId: "user_1", new: "fromEvent" },
|
|
485
|
+
line_items: { data: [{ price: { id: "price_co" } }] },
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
it("merges stored metadata with event metadata (event wins on conflicts)", async () => {
|
|
489
|
+
const ctx = makeCtx({
|
|
490
|
+
query: { metadata: { stored: "fromDb", new: "old" } },
|
|
491
|
+
});
|
|
492
|
+
const whCtx = makeWhCtx({ ctx });
|
|
493
|
+
|
|
494
|
+
await processEvent(whCtx, event("checkout.session.completed", session));
|
|
495
|
+
|
|
496
|
+
const calls = ctx.runMutation.mock.calls;
|
|
497
|
+
const upsert = calls[calls.length - 1];
|
|
498
|
+
expect(upsert[0][TO_REF]).toBe(
|
|
499
|
+
"betterStripe/billing/mutations/upsertCheckoutSession",
|
|
500
|
+
);
|
|
501
|
+
expect(upsert[1]).toMatchObject({
|
|
502
|
+
stripeSessionId: "cs_1",
|
|
503
|
+
priceId: "price_co",
|
|
504
|
+
mode: "subscription",
|
|
505
|
+
status: "complete",
|
|
506
|
+
metadata: { stored: "fromDb", new: "fromEvent", userId: "user_1" },
|
|
507
|
+
});
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
it("uses only event metadata when the session is seen for the first time", async () => {
|
|
511
|
+
const ctx = makeCtx({ queryThrows: true });
|
|
512
|
+
const whCtx = makeWhCtx({ ctx });
|
|
513
|
+
|
|
514
|
+
await processEvent(whCtx, event("checkout.session.completed", session));
|
|
515
|
+
|
|
516
|
+
const calls = ctx.runMutation.mock.calls;
|
|
517
|
+
const upsert = calls[calls.length - 1];
|
|
518
|
+
expect(upsert[1].metadata).toEqual({ userId: "user_1", new: "fromEvent" });
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
it("prefers the V2 customer_account over the legacy customer id", async () => {
|
|
522
|
+
const ctx = makeCtx({ queryThrows: true });
|
|
523
|
+
const whCtx = makeWhCtx({ ctx });
|
|
524
|
+
|
|
525
|
+
await processEvent(
|
|
526
|
+
whCtx,
|
|
527
|
+
event("checkout.session.completed", {
|
|
528
|
+
...session,
|
|
529
|
+
customer: "cus_legacy",
|
|
530
|
+
customer_account: "acct_v2",
|
|
531
|
+
}),
|
|
532
|
+
);
|
|
533
|
+
|
|
534
|
+
const calls = ctx.runMutation.mock.calls;
|
|
535
|
+
const upsert = calls[calls.length - 1];
|
|
536
|
+
expect(upsert[1].accountId).toBe("acct_v2");
|
|
537
|
+
});
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
describe("processEvent — invoices", () => {
|
|
541
|
+
it("normalizes an invoice and resolves the parent subscription id", async () => {
|
|
542
|
+
const whCtx = makeWhCtx();
|
|
543
|
+
await processEvent(
|
|
544
|
+
whCtx,
|
|
545
|
+
event("invoice.paid", {
|
|
546
|
+
id: "in_1",
|
|
547
|
+
customer: "acct_i",
|
|
548
|
+
currency: "usd",
|
|
549
|
+
amount_due: 1000,
|
|
550
|
+
amount_paid: 1000,
|
|
551
|
+
status: "paid",
|
|
552
|
+
period_start: 1700000000,
|
|
553
|
+
period_end: 1702592000,
|
|
554
|
+
hosted_invoice_url: "https://inv",
|
|
555
|
+
invoice_pdf: "https://pdf",
|
|
556
|
+
metadata: {},
|
|
557
|
+
parent: { subscription_details: { subscription: "sub_parent" } },
|
|
558
|
+
}),
|
|
559
|
+
);
|
|
560
|
+
|
|
561
|
+
const { path, data } = dispatchedPayload(whCtx.ctx);
|
|
562
|
+
expect(path).toBe("betterStripe/billing/mutations/upsertInvoice");
|
|
563
|
+
expect(data).toMatchObject({
|
|
564
|
+
stripeInvoiceId: "in_1",
|
|
565
|
+
accountId: "acct_i",
|
|
566
|
+
subscriptionId: "sub_parent",
|
|
567
|
+
status: "paid",
|
|
568
|
+
amountDue: 1000,
|
|
569
|
+
amountPaid: 1000,
|
|
570
|
+
});
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
it("defaults a missing invoice status to draft, derives accountId from an expanded customer, and leaves subscriptionId undefined", async () => {
|
|
574
|
+
const whCtx = makeWhCtx();
|
|
575
|
+
await processEvent(
|
|
576
|
+
whCtx,
|
|
577
|
+
event("invoice.created", {
|
|
578
|
+
id: "in_2",
|
|
579
|
+
customer: { id: "acct_obj_i" },
|
|
580
|
+
currency: "usd",
|
|
581
|
+
amount_due: 0,
|
|
582
|
+
amount_paid: 0,
|
|
583
|
+
status: null,
|
|
584
|
+
period_start: null,
|
|
585
|
+
period_end: null,
|
|
586
|
+
metadata: {},
|
|
587
|
+
}),
|
|
588
|
+
);
|
|
589
|
+
const { data } = dispatchedPayload(whCtx.ctx);
|
|
590
|
+
expect(data.status).toBe("draft");
|
|
591
|
+
expect(data.accountId).toBe("acct_obj_i");
|
|
592
|
+
expect(data.subscriptionId).toBeUndefined();
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
it("prefers the V2 customer_account over the legacy customer id", async () => {
|
|
596
|
+
const whCtx = makeWhCtx();
|
|
597
|
+
await processEvent(
|
|
598
|
+
whCtx,
|
|
599
|
+
event("invoice.paid", {
|
|
600
|
+
id: "in_3",
|
|
601
|
+
customer: "cus_legacy",
|
|
602
|
+
customer_account: "acct_v2",
|
|
603
|
+
currency: "usd",
|
|
604
|
+
amount_due: 0,
|
|
605
|
+
amount_paid: 0,
|
|
606
|
+
status: "paid",
|
|
607
|
+
period_start: null,
|
|
608
|
+
period_end: null,
|
|
609
|
+
metadata: {},
|
|
610
|
+
}),
|
|
611
|
+
);
|
|
612
|
+
expect(dispatchedPayload(whCtx.ctx).data.accountId).toBe("acct_v2");
|
|
613
|
+
});
|
|
614
|
+
|
|
615
|
+
// BTS-33: a failed subscription charge transitions the invoice (Stripe leaves
|
|
616
|
+
// it `open` for retry) and carries the smart-retry schedule so a dunning hook
|
|
617
|
+
// can surface when Stripe will try again.
|
|
618
|
+
it("transitions state and surfaces retry metadata on invoice.payment_failed", async () => {
|
|
619
|
+
const whCtx = makeWhCtx();
|
|
620
|
+
await processEvent(
|
|
621
|
+
whCtx,
|
|
622
|
+
event("invoice.payment_failed", {
|
|
623
|
+
id: "in_failed",
|
|
624
|
+
customer: "acct_i",
|
|
625
|
+
currency: "usd",
|
|
626
|
+
amount_due: 5000,
|
|
627
|
+
amount_paid: 0,
|
|
628
|
+
status: "open",
|
|
629
|
+
attempt_count: 1,
|
|
630
|
+
next_payment_attempt: 1700003600,
|
|
631
|
+
period_start: null,
|
|
632
|
+
period_end: null,
|
|
633
|
+
metadata: {},
|
|
634
|
+
parent: { subscription_details: { subscription: "sub_delinquent" } },
|
|
635
|
+
}),
|
|
636
|
+
);
|
|
637
|
+
|
|
638
|
+
const { path, data } = dispatchedPayload(whCtx.ctx);
|
|
639
|
+
expect(path).toBe("betterStripe/billing/mutations/upsertInvoice");
|
|
640
|
+
expect(data).toMatchObject({
|
|
641
|
+
stripeInvoiceId: "in_failed",
|
|
642
|
+
subscriptionId: "sub_delinquent",
|
|
643
|
+
status: "open",
|
|
644
|
+
amountPaid: 0,
|
|
645
|
+
attemptCount: 1,
|
|
646
|
+
});
|
|
647
|
+
expect(data.nextPaymentAttempt).toBe(
|
|
648
|
+
new Date(1700003600 * 1000).toISOString(),
|
|
649
|
+
);
|
|
650
|
+
});
|
|
651
|
+
});
|
|
652
|
+
|
|
653
|
+
describe("processEvent — payments (status collapse)", () => {
|
|
654
|
+
const base = {
|
|
655
|
+
id: "pi_1",
|
|
656
|
+
customer: "acct_p",
|
|
657
|
+
amount: 2000,
|
|
658
|
+
currency: "usd",
|
|
659
|
+
metadata: {},
|
|
660
|
+
};
|
|
661
|
+
|
|
662
|
+
it.each([
|
|
663
|
+
["succeeded", "succeeded"],
|
|
664
|
+
["canceled", "canceled"],
|
|
665
|
+
["processing", "processing"],
|
|
666
|
+
["requires_action", "requires_action"],
|
|
667
|
+
["requires_confirmation", "requires_action"],
|
|
668
|
+
["requires_payment_method", "requires_action"],
|
|
669
|
+
["requires_capture", "requires_action"],
|
|
670
|
+
])("maps Stripe status %s to app status %s", async (stripeStatus, expected) => {
|
|
671
|
+
const whCtx = makeWhCtx();
|
|
672
|
+
await processEvent(
|
|
673
|
+
whCtx,
|
|
674
|
+
event("payment_intent.succeeded", { ...base, status: stripeStatus }),
|
|
675
|
+
);
|
|
676
|
+
expect(dispatchedPayload(whCtx.ctx).data.status).toBe(expected);
|
|
677
|
+
});
|
|
678
|
+
|
|
679
|
+
it("maps any unrecognized status to failed", async () => {
|
|
680
|
+
const whCtx = makeWhCtx();
|
|
681
|
+
await processEvent(
|
|
682
|
+
whCtx,
|
|
683
|
+
event("payment_intent.payment_failed", {
|
|
684
|
+
...base,
|
|
685
|
+
status: "some_new_state",
|
|
686
|
+
}),
|
|
687
|
+
);
|
|
688
|
+
const { path, data } = dispatchedPayload(whCtx.ctx);
|
|
689
|
+
expect(path).toBe("betterStripe/connect/mutations/upsertPayment");
|
|
690
|
+
expect(data.status).toBe("failed");
|
|
691
|
+
});
|
|
692
|
+
|
|
693
|
+
it("prefers the V2 customer_account over the legacy customer id", async () => {
|
|
694
|
+
const whCtx = makeWhCtx();
|
|
695
|
+
await processEvent(
|
|
696
|
+
whCtx,
|
|
697
|
+
event("payment_intent.succeeded", {
|
|
698
|
+
...base,
|
|
699
|
+
customer: "cus_legacy",
|
|
700
|
+
customer_account: "acct_v2",
|
|
701
|
+
status: "succeeded",
|
|
702
|
+
}),
|
|
703
|
+
);
|
|
704
|
+
expect(dispatchedPayload(whCtx.ctx).data.accountId).toBe("acct_v2");
|
|
705
|
+
});
|
|
706
|
+
});
|
|
707
|
+
|
|
708
|
+
describe("processEvent — payouts", () => {
|
|
709
|
+
it("normalizes a payout, taking destination as a string id", async () => {
|
|
710
|
+
const whCtx = makeWhCtx();
|
|
711
|
+
await processEvent(
|
|
712
|
+
whCtx,
|
|
713
|
+
event("payout.paid", {
|
|
714
|
+
id: "po_1",
|
|
715
|
+
destination: "ba_123",
|
|
716
|
+
amount: 5000,
|
|
717
|
+
currency: "usd",
|
|
718
|
+
status: "paid",
|
|
719
|
+
arrival_date: 1700000000,
|
|
720
|
+
method: "standard",
|
|
721
|
+
metadata: {},
|
|
722
|
+
}),
|
|
723
|
+
);
|
|
724
|
+
const { path, data } = dispatchedPayload(whCtx.ctx);
|
|
725
|
+
expect(path).toBe("betterStripe/connect/mutations/upsertPayout");
|
|
726
|
+
expect(data).toMatchObject({
|
|
727
|
+
stripePayoutId: "po_1",
|
|
728
|
+
accountId: "ba_123",
|
|
729
|
+
amount: 5000,
|
|
730
|
+
status: "paid",
|
|
731
|
+
method: "standard",
|
|
732
|
+
});
|
|
733
|
+
});
|
|
734
|
+
|
|
735
|
+
it("falls back to an empty-string accountId for an expanded destination without id", async () => {
|
|
736
|
+
const whCtx = makeWhCtx();
|
|
737
|
+
await processEvent(
|
|
738
|
+
whCtx,
|
|
739
|
+
event("payout.failed", {
|
|
740
|
+
id: "po_2",
|
|
741
|
+
destination: {},
|
|
742
|
+
amount: 100,
|
|
743
|
+
currency: "usd",
|
|
744
|
+
status: "failed",
|
|
745
|
+
arrival_date: null,
|
|
746
|
+
metadata: {},
|
|
747
|
+
}),
|
|
748
|
+
);
|
|
749
|
+
expect(dispatchedPayload(whCtx.ctx).data.accountId).toBe("");
|
|
750
|
+
});
|
|
751
|
+
});
|
|
752
|
+
|
|
753
|
+
describe("processEvent — unhandled", () => {
|
|
754
|
+
it("logs and dispatches nothing for an unknown event type", async () => {
|
|
755
|
+
const info = vi.spyOn(console, "info").mockImplementation(() => {});
|
|
756
|
+
const whCtx = makeWhCtx();
|
|
757
|
+
await processEvent(whCtx, event("customer.created", { id: "cus_1" }));
|
|
758
|
+
expect(whCtx.ctx.runMutation).not.toHaveBeenCalled();
|
|
759
|
+
expect(info).toHaveBeenCalledWith(
|
|
760
|
+
expect.stringContaining("Unhandled event type: customer.created"),
|
|
761
|
+
);
|
|
762
|
+
});
|
|
763
|
+
});
|
|
764
|
+
|
|
765
|
+
describe("processEvent — fee & transfer capture (BTS-20)", () => {
|
|
766
|
+
it("denormalizes application_fee + transfer destination onto a payment", async () => {
|
|
767
|
+
const whCtx = makeWhCtx();
|
|
768
|
+
await processEvent(
|
|
769
|
+
whCtx,
|
|
770
|
+
event("payment_intent.succeeded", {
|
|
771
|
+
id: "pi_fee",
|
|
772
|
+
amount: 10000,
|
|
773
|
+
currency: "usd",
|
|
774
|
+
status: "succeeded",
|
|
775
|
+
application_fee_amount: 1000,
|
|
776
|
+
transfer_data: { destination: "acct_store" },
|
|
777
|
+
metadata: {},
|
|
778
|
+
}),
|
|
779
|
+
);
|
|
780
|
+
expect(dispatchedPayload(whCtx.ctx).data).toMatchObject({
|
|
781
|
+
stripePaymentIntentId: "pi_fee",
|
|
782
|
+
chargeType: "destination",
|
|
783
|
+
destinationAccountId: "acct_store",
|
|
784
|
+
applicationFeeAmount: 1000,
|
|
785
|
+
feeCollectedAmount: 1000,
|
|
786
|
+
});
|
|
787
|
+
});
|
|
788
|
+
|
|
789
|
+
it("handles a transfer destination given as an expanded object", async () => {
|
|
790
|
+
const whCtx = makeWhCtx();
|
|
791
|
+
await processEvent(
|
|
792
|
+
whCtx,
|
|
793
|
+
event("payment_intent.succeeded", {
|
|
794
|
+
id: "pi_fee2",
|
|
795
|
+
amount: 10000,
|
|
796
|
+
currency: "usd",
|
|
797
|
+
status: "succeeded",
|
|
798
|
+
transfer_data: { destination: { id: "acct_store" } },
|
|
799
|
+
metadata: {},
|
|
800
|
+
}),
|
|
801
|
+
);
|
|
802
|
+
expect(dispatchedPayload(whCtx.ctx).data.destinationAccountId).toBe(
|
|
803
|
+
"acct_store",
|
|
804
|
+
);
|
|
805
|
+
});
|
|
806
|
+
|
|
807
|
+
it("leaves fee fields unset for a plain payment", async () => {
|
|
808
|
+
const whCtx = makeWhCtx();
|
|
809
|
+
await processEvent(
|
|
810
|
+
whCtx,
|
|
811
|
+
event("payment_intent.succeeded", {
|
|
812
|
+
id: "pi_plain",
|
|
813
|
+
amount: 10000,
|
|
814
|
+
currency: "usd",
|
|
815
|
+
status: "succeeded",
|
|
816
|
+
metadata: {},
|
|
817
|
+
}),
|
|
818
|
+
);
|
|
819
|
+
const { data } = dispatchedPayload(whCtx.ctx);
|
|
820
|
+
expect(data.chargeType).toBeUndefined();
|
|
821
|
+
expect(data.destinationAccountId).toBeUndefined();
|
|
822
|
+
expect(data.applicationFeeAmount).toBeUndefined();
|
|
823
|
+
});
|
|
824
|
+
|
|
825
|
+
it("does not denormalize fees for a non-succeeded payment intent", async () => {
|
|
826
|
+
const whCtx = makeWhCtx();
|
|
827
|
+
await processEvent(
|
|
828
|
+
whCtx,
|
|
829
|
+
event("payment_intent.payment_failed", {
|
|
830
|
+
id: "pi_failed",
|
|
831
|
+
amount: 10000,
|
|
832
|
+
currency: "usd",
|
|
833
|
+
status: "requires_payment_method",
|
|
834
|
+
application_fee_amount: 1000,
|
|
835
|
+
transfer_data: { destination: "acct_store" },
|
|
836
|
+
metadata: {},
|
|
837
|
+
}),
|
|
838
|
+
);
|
|
839
|
+
const { data } = dispatchedPayload(whCtx.ctx);
|
|
840
|
+
expect(data.status).toBe("requires_action"); // requires_payment_method → requires_action
|
|
841
|
+
expect(data.feeCollectedAmount).toBeUndefined();
|
|
842
|
+
expect(data.applicationFeeAmount).toBeUndefined();
|
|
843
|
+
expect(data.destinationAccountId).toBeUndefined();
|
|
844
|
+
expect(data.chargeType).toBeUndefined();
|
|
845
|
+
});
|
|
846
|
+
|
|
847
|
+
// Note: the platform fee lives on the charge/PaymentIntent, not the typed
|
|
848
|
+
// Stripe.Invoice, so fee denormalization is captured on the `payments` row
|
|
849
|
+
// (above) rather than the invoice.
|
|
850
|
+
});
|
|
851
|
+
|
|
852
|
+
describe("processEvent — split transfer engine (BTS-22)", () => {
|
|
853
|
+
it("fans funds out to recipients on a separate-charges payment_intent.succeeded", async () => {
|
|
854
|
+
const stripe = makeStripe();
|
|
855
|
+
const whCtx = makeWhCtx({ stripe });
|
|
856
|
+
|
|
857
|
+
await processEvent(
|
|
858
|
+
whCtx,
|
|
859
|
+
event("payment_intent.succeeded", {
|
|
860
|
+
id: "pi_split",
|
|
861
|
+
amount: 10000,
|
|
862
|
+
currency: "usd",
|
|
863
|
+
status: "succeeded",
|
|
864
|
+
latest_charge: "ch_split",
|
|
865
|
+
metadata: {
|
|
866
|
+
bsChargeType: "separate",
|
|
867
|
+
bsFeeConfig: JSON.stringify({ percent: 10 }),
|
|
868
|
+
bsSplit: JSON.stringify([
|
|
869
|
+
{ destinationAccountId: "acct_store", role: "store", percent: 80 },
|
|
870
|
+
{ destinationAccountId: "acct_aff", role: "affiliate", percent: 5 },
|
|
871
|
+
]),
|
|
872
|
+
},
|
|
873
|
+
}),
|
|
874
|
+
);
|
|
875
|
+
|
|
876
|
+
expect(stripe.transfers.create).toHaveBeenCalledTimes(2);
|
|
877
|
+
expect(stripe.transfers.create.mock.calls[0][0]).toMatchObject({
|
|
878
|
+
amount: 8000,
|
|
879
|
+
destination: "acct_store",
|
|
880
|
+
source_transaction: "ch_split",
|
|
881
|
+
});
|
|
882
|
+
expect(stripe.transfers.create.mock.calls[1][0]).toMatchObject({
|
|
883
|
+
amount: 500,
|
|
884
|
+
destination: "acct_aff",
|
|
885
|
+
});
|
|
886
|
+
});
|
|
887
|
+
|
|
888
|
+
it("throws (not silently skips) when a separate sale has no latest_charge", async () => {
|
|
889
|
+
const stripe = makeStripe();
|
|
890
|
+
const whCtx = makeWhCtx({ stripe });
|
|
891
|
+
|
|
892
|
+
await expect(
|
|
893
|
+
processEvent(
|
|
894
|
+
whCtx,
|
|
895
|
+
event("payment_intent.succeeded", {
|
|
896
|
+
id: "pi_nocharge",
|
|
897
|
+
amount: 10000,
|
|
898
|
+
currency: "usd",
|
|
899
|
+
status: "succeeded",
|
|
900
|
+
latest_charge: null,
|
|
901
|
+
metadata: {
|
|
902
|
+
bsChargeType: "separate",
|
|
903
|
+
bsSplit: JSON.stringify([
|
|
904
|
+
{ destinationAccountId: "acct_store", role: "store", percent: 90 },
|
|
905
|
+
]),
|
|
906
|
+
},
|
|
907
|
+
}),
|
|
908
|
+
),
|
|
909
|
+
).rejects.toThrow(/latest_charge|transfer/i);
|
|
910
|
+
});
|
|
911
|
+
|
|
912
|
+
it("throws when a separate sale has malformed bsSplit metadata", async () => {
|
|
913
|
+
const stripe = makeStripe();
|
|
914
|
+
const whCtx = makeWhCtx({ stripe });
|
|
915
|
+
|
|
916
|
+
await expect(
|
|
917
|
+
processEvent(
|
|
918
|
+
whCtx,
|
|
919
|
+
event("payment_intent.succeeded", {
|
|
920
|
+
id: "pi_badjson",
|
|
921
|
+
amount: 10000,
|
|
922
|
+
currency: "usd",
|
|
923
|
+
status: "succeeded",
|
|
924
|
+
latest_charge: "ch_x",
|
|
925
|
+
metadata: { bsChargeType: "separate", bsSplit: "{not-json" },
|
|
926
|
+
}),
|
|
927
|
+
),
|
|
928
|
+
).rejects.toThrow(/malformed/i);
|
|
929
|
+
expect(stripe.transfers.create).not.toHaveBeenCalled();
|
|
930
|
+
});
|
|
931
|
+
|
|
932
|
+
it("splits a recurring subscription invoice on invoice.paid (BTS-52)", async () => {
|
|
933
|
+
const stripe = makeStripe();
|
|
934
|
+
stripe.subscriptions.retrieve.mockResolvedValue({
|
|
935
|
+
id: "sub_1",
|
|
936
|
+
metadata: {
|
|
937
|
+
bsChargeType: "separate",
|
|
938
|
+
bsFeeConfig: JSON.stringify({ percent: 10 }),
|
|
939
|
+
bsSplit: JSON.stringify([
|
|
940
|
+
{ destinationAccountId: "acct_store", role: "store", percent: 80 },
|
|
941
|
+
{ destinationAccountId: "acct_aff", role: "affiliate", percent: 5 },
|
|
942
|
+
]),
|
|
943
|
+
},
|
|
944
|
+
});
|
|
945
|
+
const whCtx = makeWhCtx({ stripe });
|
|
946
|
+
|
|
947
|
+
await processEvent(
|
|
948
|
+
whCtx,
|
|
949
|
+
event("invoice.paid", {
|
|
950
|
+
id: "in_1",
|
|
951
|
+
currency: "usd",
|
|
952
|
+
amount_due: 10000,
|
|
953
|
+
amount_paid: 10000,
|
|
954
|
+
status: "paid",
|
|
955
|
+
metadata: {},
|
|
956
|
+
parent: { subscription_details: { subscription: "sub_1" } },
|
|
957
|
+
}),
|
|
958
|
+
);
|
|
959
|
+
|
|
960
|
+
expect(stripe.transfers.create).toHaveBeenCalledTimes(2);
|
|
961
|
+
expect(stripe.transfers.create.mock.calls[0][0]).toMatchObject({
|
|
962
|
+
amount: 8000,
|
|
963
|
+
destination: "acct_store",
|
|
964
|
+
source_transaction: "ch_inv",
|
|
965
|
+
});
|
|
966
|
+
expect(stripe.transfers.create.mock.calls[1][0]).toMatchObject({
|
|
967
|
+
amount: 500,
|
|
968
|
+
destination: "acct_aff",
|
|
969
|
+
});
|
|
970
|
+
});
|
|
971
|
+
|
|
972
|
+
it("skips a zero-amount invoice without throwing (no charge to split)", async () => {
|
|
973
|
+
const stripe = makeStripe();
|
|
974
|
+
stripe.subscriptions.retrieve.mockResolvedValue({
|
|
975
|
+
id: "sub_1",
|
|
976
|
+
metadata: {
|
|
977
|
+
bsChargeType: "separate",
|
|
978
|
+
bsSplit: JSON.stringify([
|
|
979
|
+
{ destinationAccountId: "acct_store", role: "store", percent: 80 },
|
|
980
|
+
]),
|
|
981
|
+
},
|
|
982
|
+
});
|
|
983
|
+
const whCtx = makeWhCtx({ stripe });
|
|
984
|
+
|
|
985
|
+
await processEvent(
|
|
986
|
+
whCtx,
|
|
987
|
+
event("invoice.paid", {
|
|
988
|
+
id: "in_zero",
|
|
989
|
+
currency: "usd",
|
|
990
|
+
amount_due: 0,
|
|
991
|
+
amount_paid: 0, // 100%-off coupon / credit → no charge
|
|
992
|
+
status: "paid",
|
|
993
|
+
metadata: {},
|
|
994
|
+
parent: { subscription_details: { subscription: "sub_1" } },
|
|
995
|
+
}),
|
|
996
|
+
);
|
|
997
|
+
// No charge resolution, no transfers, no throw.
|
|
998
|
+
expect(stripe.invoicePayments.list).not.toHaveBeenCalled();
|
|
999
|
+
expect(stripe.transfers.create).not.toHaveBeenCalled();
|
|
1000
|
+
});
|
|
1001
|
+
|
|
1002
|
+
it("does not split a normal (non-separate) subscription invoice", async () => {
|
|
1003
|
+
const stripe = makeStripe();
|
|
1004
|
+
stripe.subscriptions.retrieve.mockResolvedValue({ id: "sub_1", metadata: {} });
|
|
1005
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1006
|
+
|
|
1007
|
+
await processEvent(
|
|
1008
|
+
whCtx,
|
|
1009
|
+
event("invoice.paid", {
|
|
1010
|
+
id: "in_2",
|
|
1011
|
+
currency: "usd",
|
|
1012
|
+
amount_due: 10000,
|
|
1013
|
+
amount_paid: 10000,
|
|
1014
|
+
status: "paid",
|
|
1015
|
+
metadata: {},
|
|
1016
|
+
parent: { subscription_details: { subscription: "sub_1" } },
|
|
1017
|
+
}),
|
|
1018
|
+
);
|
|
1019
|
+
expect(stripe.transfers.create).not.toHaveBeenCalled();
|
|
1020
|
+
});
|
|
1021
|
+
|
|
1022
|
+
it("[BTS-27] claws back the charge's transfers on charge.dispute.created", async () => {
|
|
1023
|
+
const stripe = makeStripe();
|
|
1024
|
+
// The charge funded a store + affiliate transfer.
|
|
1025
|
+
const ctx = makeCtx({
|
|
1026
|
+
query: [
|
|
1027
|
+
{ stripeTransferId: "tr_store", destinationAccountId: "acct_store", role: "store", amount: 8000 },
|
|
1028
|
+
{ stripeTransferId: "tr_aff", destinationAccountId: "acct_aff", role: "affiliate", amount: 1000 },
|
|
1029
|
+
],
|
|
1030
|
+
});
|
|
1031
|
+
const whCtx = makeWhCtx({ stripe, ctx });
|
|
1032
|
+
|
|
1033
|
+
await processEvent(
|
|
1034
|
+
whCtx,
|
|
1035
|
+
event("charge.dispute.created", {
|
|
1036
|
+
id: "dp_1",
|
|
1037
|
+
charge: "ch_1",
|
|
1038
|
+
amount: 9000,
|
|
1039
|
+
currency: "usd",
|
|
1040
|
+
status: "needs_response",
|
|
1041
|
+
reason: "fraudulent",
|
|
1042
|
+
is_charge_refundable: true,
|
|
1043
|
+
evidence_details: { due_by: 1700000000 },
|
|
1044
|
+
metadata: {},
|
|
1045
|
+
}),
|
|
1046
|
+
);
|
|
1047
|
+
|
|
1048
|
+
// Both recipient transfers reversed, with the dispute id as the idempotency op.
|
|
1049
|
+
expect(stripe.transfers.createReversal).toHaveBeenCalledWith(
|
|
1050
|
+
"tr_store",
|
|
1051
|
+
{ amount: 8000 },
|
|
1052
|
+
{ idempotencyKey: "bs_rev_dp_1_tr_store" },
|
|
1053
|
+
);
|
|
1054
|
+
expect(stripe.transfers.createReversal).toHaveBeenCalledWith(
|
|
1055
|
+
"tr_aff",
|
|
1056
|
+
{ amount: 1000 },
|
|
1057
|
+
{ idempotencyKey: "bs_rev_dp_1_tr_aff" },
|
|
1058
|
+
);
|
|
1059
|
+
});
|
|
1060
|
+
|
|
1061
|
+
it("does not claw back transfers on a dispute that isn't newly created", async () => {
|
|
1062
|
+
const stripe = makeStripe();
|
|
1063
|
+
const ctx = makeCtx({
|
|
1064
|
+
query: [
|
|
1065
|
+
{ stripeTransferId: "tr_store", destinationAccountId: "acct_store", role: "store", amount: 8000 },
|
|
1066
|
+
],
|
|
1067
|
+
});
|
|
1068
|
+
const whCtx = makeWhCtx({ stripe, ctx });
|
|
1069
|
+
|
|
1070
|
+
await processEvent(
|
|
1071
|
+
whCtx,
|
|
1072
|
+
event("charge.dispute.updated", {
|
|
1073
|
+
id: "dp_2",
|
|
1074
|
+
charge: "ch_1",
|
|
1075
|
+
amount: 8000,
|
|
1076
|
+
currency: "usd",
|
|
1077
|
+
status: "under_review",
|
|
1078
|
+
reason: "fraudulent",
|
|
1079
|
+
is_charge_refundable: true,
|
|
1080
|
+
evidence_details: { due_by: 1700000000 },
|
|
1081
|
+
metadata: {},
|
|
1082
|
+
}),
|
|
1083
|
+
);
|
|
1084
|
+
expect(stripe.transfers.createReversal).not.toHaveBeenCalled();
|
|
1085
|
+
});
|
|
1086
|
+
|
|
1087
|
+
it("[BTS-29] reinstates transfers when a dispute closes as won", async () => {
|
|
1088
|
+
const stripe = makeStripe();
|
|
1089
|
+
const ctx = makeCtx({
|
|
1090
|
+
query: [
|
|
1091
|
+
{ stripeTransferId: "tr_store", destinationAccountId: "acct_store", role: "store", amount: 8000, currency: "usd", reversedAmount: 8000 },
|
|
1092
|
+
],
|
|
1093
|
+
});
|
|
1094
|
+
const whCtx = makeWhCtx({ stripe, ctx });
|
|
1095
|
+
|
|
1096
|
+
await processEvent(
|
|
1097
|
+
whCtx,
|
|
1098
|
+
event("charge.dispute.closed", {
|
|
1099
|
+
id: "dp_1",
|
|
1100
|
+
charge: "ch_1",
|
|
1101
|
+
amount: 8000,
|
|
1102
|
+
currency: "usd",
|
|
1103
|
+
status: "won",
|
|
1104
|
+
reason: "fraudulent",
|
|
1105
|
+
is_charge_refundable: true,
|
|
1106
|
+
evidence_details: { due_by: 1700000000 },
|
|
1107
|
+
metadata: {},
|
|
1108
|
+
}),
|
|
1109
|
+
);
|
|
1110
|
+
|
|
1111
|
+
expect(stripe.transfers.create).toHaveBeenCalledWith(
|
|
1112
|
+
expect.objectContaining({ amount: 8000, destination: "acct_store" }),
|
|
1113
|
+
{ idempotencyKey: "bs_reinstate_dp_1_acct_store_store" },
|
|
1114
|
+
);
|
|
1115
|
+
});
|
|
1116
|
+
|
|
1117
|
+
it("[BTS-29] does not reinstate when a dispute closes as lost", async () => {
|
|
1118
|
+
const stripe = makeStripe();
|
|
1119
|
+
const ctx = makeCtx({
|
|
1120
|
+
query: [
|
|
1121
|
+
{ stripeTransferId: "tr_store", destinationAccountId: "acct_store", role: "store", amount: 8000, currency: "usd", reversedAmount: 8000 },
|
|
1122
|
+
],
|
|
1123
|
+
});
|
|
1124
|
+
const whCtx = makeWhCtx({ stripe, ctx });
|
|
1125
|
+
|
|
1126
|
+
await processEvent(
|
|
1127
|
+
whCtx,
|
|
1128
|
+
event("charge.dispute.closed", {
|
|
1129
|
+
id: "dp_2",
|
|
1130
|
+
charge: "ch_1",
|
|
1131
|
+
amount: 8000,
|
|
1132
|
+
currency: "usd",
|
|
1133
|
+
status: "lost",
|
|
1134
|
+
reason: "fraudulent",
|
|
1135
|
+
is_charge_refundable: true,
|
|
1136
|
+
evidence_details: { due_by: 1700000000 },
|
|
1137
|
+
metadata: {},
|
|
1138
|
+
}),
|
|
1139
|
+
);
|
|
1140
|
+
expect(stripe.transfers.create).not.toHaveBeenCalled();
|
|
1141
|
+
});
|
|
1142
|
+
|
|
1143
|
+
it("[BTS-28] cancels the disputed subscription at period end (default)", async () => {
|
|
1144
|
+
const stripe = makeStripe();
|
|
1145
|
+
stripe.invoicePayments.list.mockResolvedValue({
|
|
1146
|
+
data: [{ invoice: "in_1", payment: { charge: "ch_1" } }],
|
|
1147
|
+
});
|
|
1148
|
+
stripe.invoices.retrieve.mockResolvedValue({
|
|
1149
|
+
id: "in_1",
|
|
1150
|
+
parent: { subscription_details: { subscription: "sub_1" } },
|
|
1151
|
+
});
|
|
1152
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1153
|
+
|
|
1154
|
+
await processEvent(
|
|
1155
|
+
whCtx,
|
|
1156
|
+
event("charge.dispute.created", {
|
|
1157
|
+
id: "dp_1",
|
|
1158
|
+
charge: "ch_1",
|
|
1159
|
+
payment_intent: "pi_1",
|
|
1160
|
+
amount: 5000,
|
|
1161
|
+
currency: "usd",
|
|
1162
|
+
status: "needs_response",
|
|
1163
|
+
reason: "fraudulent",
|
|
1164
|
+
is_charge_refundable: true,
|
|
1165
|
+
evidence_details: { due_by: 1700000000 },
|
|
1166
|
+
metadata: {},
|
|
1167
|
+
}),
|
|
1168
|
+
);
|
|
1169
|
+
|
|
1170
|
+
expect(stripe.subscriptions.update).toHaveBeenCalledWith(
|
|
1171
|
+
"sub_1",
|
|
1172
|
+
{ cancel_at_period_end: true },
|
|
1173
|
+
{ idempotencyKey: "bs_dispute_cancel_dp_1" },
|
|
1174
|
+
);
|
|
1175
|
+
expect(stripe.subscriptions.cancel).not.toHaveBeenCalled();
|
|
1176
|
+
});
|
|
1177
|
+
|
|
1178
|
+
it("[BTS-28] does not cancel when autoCancelOnDispute is off", async () => {
|
|
1179
|
+
const stripe = makeStripe();
|
|
1180
|
+
stripe.invoicePayments.list.mockResolvedValue({
|
|
1181
|
+
data: [{ invoice: "in_1", payment: { charge: "ch_1" } }],
|
|
1182
|
+
});
|
|
1183
|
+
stripe.invoices.retrieve.mockResolvedValue({
|
|
1184
|
+
id: "in_1",
|
|
1185
|
+
parent: { subscription_details: { subscription: "sub_1" } },
|
|
1186
|
+
});
|
|
1187
|
+
const whCtx = makeWhCtx({ stripe, config: { autoCancelOnDispute: false } });
|
|
1188
|
+
|
|
1189
|
+
await processEvent(
|
|
1190
|
+
whCtx,
|
|
1191
|
+
event("charge.dispute.created", {
|
|
1192
|
+
id: "dp_2",
|
|
1193
|
+
charge: "ch_1",
|
|
1194
|
+
payment_intent: "pi_1",
|
|
1195
|
+
amount: 5000,
|
|
1196
|
+
currency: "usd",
|
|
1197
|
+
status: "needs_response",
|
|
1198
|
+
reason: "fraudulent",
|
|
1199
|
+
is_charge_refundable: true,
|
|
1200
|
+
evidence_details: { due_by: 1700000000 },
|
|
1201
|
+
metadata: {},
|
|
1202
|
+
}),
|
|
1203
|
+
);
|
|
1204
|
+
expect(stripe.subscriptions.update).not.toHaveBeenCalled();
|
|
1205
|
+
});
|
|
1206
|
+
|
|
1207
|
+
it("[BTS-28] cancels immediately when configured", async () => {
|
|
1208
|
+
const stripe = makeStripe();
|
|
1209
|
+
stripe.invoicePayments.list.mockResolvedValue({
|
|
1210
|
+
data: [{ invoice: "in_1", payment: { charge: "ch_1" } }],
|
|
1211
|
+
});
|
|
1212
|
+
stripe.invoices.retrieve.mockResolvedValue({
|
|
1213
|
+
id: "in_1",
|
|
1214
|
+
parent: { subscription_details: { subscription: "sub_1" } },
|
|
1215
|
+
});
|
|
1216
|
+
const whCtx = makeWhCtx({
|
|
1217
|
+
stripe,
|
|
1218
|
+
config: { cancelDisputedSubscriptionImmediately: true },
|
|
1219
|
+
});
|
|
1220
|
+
|
|
1221
|
+
await processEvent(
|
|
1222
|
+
whCtx,
|
|
1223
|
+
event("charge.dispute.created", {
|
|
1224
|
+
id: "dp_3",
|
|
1225
|
+
charge: "ch_1",
|
|
1226
|
+
payment_intent: "pi_1",
|
|
1227
|
+
amount: 5000,
|
|
1228
|
+
currency: "usd",
|
|
1229
|
+
status: "needs_response",
|
|
1230
|
+
reason: "fraudulent",
|
|
1231
|
+
is_charge_refundable: true,
|
|
1232
|
+
evidence_details: { due_by: 1700000000 },
|
|
1233
|
+
metadata: {},
|
|
1234
|
+
}),
|
|
1235
|
+
);
|
|
1236
|
+
expect(stripe.subscriptions.cancel).toHaveBeenCalledWith(
|
|
1237
|
+
"sub_1",
|
|
1238
|
+
undefined,
|
|
1239
|
+
{ idempotencyKey: "bs_dispute_cancel_dp_3" },
|
|
1240
|
+
);
|
|
1241
|
+
});
|
|
1242
|
+
|
|
1243
|
+
it("[BTS-26] stores the charge's statement descriptor on the dispute", async () => {
|
|
1244
|
+
const stripe = makeStripe();
|
|
1245
|
+
stripe.charges.retrieve.mockResolvedValue({
|
|
1246
|
+
id: "ch_1",
|
|
1247
|
+
calculated_statement_descriptor: "ACME STORE",
|
|
1248
|
+
});
|
|
1249
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1250
|
+
|
|
1251
|
+
await processEvent(
|
|
1252
|
+
whCtx,
|
|
1253
|
+
event("charge.dispute.created", {
|
|
1254
|
+
id: "dp_desc",
|
|
1255
|
+
charge: "ch_1",
|
|
1256
|
+
amount: 5000,
|
|
1257
|
+
currency: "usd",
|
|
1258
|
+
status: "needs_response",
|
|
1259
|
+
reason: "fraudulent",
|
|
1260
|
+
is_charge_refundable: true,
|
|
1261
|
+
evidence_details: { due_by: 1700000000 },
|
|
1262
|
+
metadata: {},
|
|
1263
|
+
}),
|
|
1264
|
+
);
|
|
1265
|
+
const { data } = dispatchedPayload(whCtx.ctx);
|
|
1266
|
+
expect(data.statementDescriptor).toBe("ACME STORE");
|
|
1267
|
+
});
|
|
1268
|
+
|
|
1269
|
+
it("[BTS-26] still processes the dispute when the charge lookup fails (descriptor is best-effort)", async () => {
|
|
1270
|
+
const stripe = makeStripe();
|
|
1271
|
+
stripe.charges.retrieve.mockRejectedValue(new Error("stripe down"));
|
|
1272
|
+
const ctx = makeCtx({
|
|
1273
|
+
query: [
|
|
1274
|
+
{ stripeTransferId: "tr_store", destinationAccountId: "acct_store", role: "store", amount: 9000 },
|
|
1275
|
+
],
|
|
1276
|
+
});
|
|
1277
|
+
const whCtx = makeWhCtx({ stripe, ctx });
|
|
1278
|
+
|
|
1279
|
+
await processEvent(
|
|
1280
|
+
whCtx,
|
|
1281
|
+
event("charge.dispute.created", {
|
|
1282
|
+
id: "dp_bf",
|
|
1283
|
+
charge: "ch_1",
|
|
1284
|
+
amount: 9000,
|
|
1285
|
+
currency: "usd",
|
|
1286
|
+
status: "needs_response",
|
|
1287
|
+
reason: "fraudulent",
|
|
1288
|
+
is_charge_refundable: true,
|
|
1289
|
+
evidence_details: { due_by: 1700000000 },
|
|
1290
|
+
metadata: {},
|
|
1291
|
+
}),
|
|
1292
|
+
);
|
|
1293
|
+
|
|
1294
|
+
// Dispute still recorded (without a descriptor) and clawback still ran.
|
|
1295
|
+
const { path, data } = dispatchedPayload(whCtx.ctx);
|
|
1296
|
+
expect(path).toBe("betterStripe/connect/mutations/upsertDispute");
|
|
1297
|
+
expect(data.statementDescriptor).toBeUndefined();
|
|
1298
|
+
expect(stripe.transfers.createReversal).toHaveBeenCalledTimes(1);
|
|
1299
|
+
});
|
|
1300
|
+
|
|
1301
|
+
it("[BTS-27] claws back a single-recipient transfer on dispute", async () => {
|
|
1302
|
+
const stripe = makeStripe();
|
|
1303
|
+
const ctx = makeCtx({
|
|
1304
|
+
query: [
|
|
1305
|
+
{ stripeTransferId: "tr_only", destinationAccountId: "acct_store", role: "store", amount: 9000 },
|
|
1306
|
+
],
|
|
1307
|
+
});
|
|
1308
|
+
const whCtx = makeWhCtx({ stripe, ctx });
|
|
1309
|
+
|
|
1310
|
+
await processEvent(
|
|
1311
|
+
whCtx,
|
|
1312
|
+
event("charge.dispute.created", {
|
|
1313
|
+
id: "dp_single",
|
|
1314
|
+
charge: "ch_1",
|
|
1315
|
+
amount: 9000,
|
|
1316
|
+
currency: "usd",
|
|
1317
|
+
status: "needs_response",
|
|
1318
|
+
reason: "fraudulent",
|
|
1319
|
+
is_charge_refundable: true,
|
|
1320
|
+
evidence_details: { due_by: 1700000000 },
|
|
1321
|
+
metadata: {},
|
|
1322
|
+
}),
|
|
1323
|
+
);
|
|
1324
|
+
|
|
1325
|
+
expect(stripe.transfers.createReversal).toHaveBeenCalledTimes(1);
|
|
1326
|
+
expect(stripe.transfers.createReversal).toHaveBeenCalledWith(
|
|
1327
|
+
"tr_only",
|
|
1328
|
+
{ amount: 9000 },
|
|
1329
|
+
{ idempotencyKey: "bs_rev_dp_single_tr_only" },
|
|
1330
|
+
);
|
|
1331
|
+
});
|
|
1332
|
+
|
|
1333
|
+
it("[BTS-26] populates evidenceDueBy from the dispute's evidence_details", async () => {
|
|
1334
|
+
const whCtx = makeWhCtx();
|
|
1335
|
+
await processEvent(
|
|
1336
|
+
whCtx,
|
|
1337
|
+
event("charge.dispute.created", {
|
|
1338
|
+
id: "dp_1",
|
|
1339
|
+
charge: "ch_1",
|
|
1340
|
+
payment_intent: "pi_1",
|
|
1341
|
+
amount: 5000,
|
|
1342
|
+
currency: "usd",
|
|
1343
|
+
status: "needs_response",
|
|
1344
|
+
reason: "fraudulent",
|
|
1345
|
+
is_charge_refundable: true,
|
|
1346
|
+
evidence_details: { due_by: 1700000000 },
|
|
1347
|
+
metadata: {},
|
|
1348
|
+
}),
|
|
1349
|
+
);
|
|
1350
|
+
const { path, data } = dispatchedPayload(whCtx.ctx);
|
|
1351
|
+
expect(path).toBe("betterStripe/connect/mutations/upsertDispute");
|
|
1352
|
+
expect(data.evidenceDueBy).toBe(new Date(1700000000 * 1000).toISOString());
|
|
1353
|
+
});
|
|
1354
|
+
|
|
1355
|
+
it("does not create transfers for a normal (non-split) payment", async () => {
|
|
1356
|
+
const stripe = makeStripe();
|
|
1357
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1358
|
+
|
|
1359
|
+
await processEvent(
|
|
1360
|
+
whCtx,
|
|
1361
|
+
event("payment_intent.succeeded", {
|
|
1362
|
+
id: "pi_plain",
|
|
1363
|
+
amount: 10000,
|
|
1364
|
+
currency: "usd",
|
|
1365
|
+
status: "succeeded",
|
|
1366
|
+
latest_charge: "ch_plain",
|
|
1367
|
+
metadata: {},
|
|
1368
|
+
}),
|
|
1369
|
+
);
|
|
1370
|
+
expect(stripe.transfers.create).not.toHaveBeenCalled();
|
|
1371
|
+
});
|
|
1372
|
+
});
|
|
1373
|
+
|
|
1374
|
+
describe("processEvent — per-invoice statement descriptor (BTS-32)", () => {
|
|
1375
|
+
const subInvoice = (overrides: Record<string, unknown> = {}) => ({
|
|
1376
|
+
id: "in_sd",
|
|
1377
|
+
customer: "acct_buyer",
|
|
1378
|
+
currency: "usd",
|
|
1379
|
+
amount_due: 10000,
|
|
1380
|
+
amount_paid: 0,
|
|
1381
|
+
status: "draft",
|
|
1382
|
+
metadata: {},
|
|
1383
|
+
parent: { subscription_details: { subscription: "sub_sd" } },
|
|
1384
|
+
...overrides,
|
|
1385
|
+
});
|
|
1386
|
+
|
|
1387
|
+
it("sets the invoice statement_descriptor from the subscription's bsStatementDescriptor marker", async () => {
|
|
1388
|
+
const stripe = makeStripe();
|
|
1389
|
+
stripe.subscriptions.retrieve.mockResolvedValue({
|
|
1390
|
+
id: "sub_sd",
|
|
1391
|
+
metadata: { bsStatementDescriptor: "MAYAS FITNESS" },
|
|
1392
|
+
});
|
|
1393
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1394
|
+
|
|
1395
|
+
await processEvent(whCtx, event("invoice.created", subInvoice()));
|
|
1396
|
+
|
|
1397
|
+
expect(stripe.invoices.update).toHaveBeenCalledWith("in_sd", {
|
|
1398
|
+
statement_descriptor: "MAYAS FITNESS",
|
|
1399
|
+
});
|
|
1400
|
+
});
|
|
1401
|
+
|
|
1402
|
+
it("coexists with the per-invoice fee: both updates land on a doubly-flagged subscription", async () => {
|
|
1403
|
+
const stripe = makeStripe();
|
|
1404
|
+
stripe.subscriptions.retrieve.mockResolvedValue({
|
|
1405
|
+
id: "sub_sd",
|
|
1406
|
+
metadata: {
|
|
1407
|
+
bsFeeMode: "per_invoice",
|
|
1408
|
+
bsFeeConfig: JSON.stringify({ percent: 2.9, fixed: 30 }),
|
|
1409
|
+
bsStatementDescriptor: "MAYAS FITNESS",
|
|
1410
|
+
},
|
|
1411
|
+
});
|
|
1412
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1413
|
+
|
|
1414
|
+
await processEvent(whCtx, event("invoice.created", subInvoice()));
|
|
1415
|
+
|
|
1416
|
+
expect(stripe.invoices.update).toHaveBeenCalledWith("in_sd", {
|
|
1417
|
+
application_fee_amount: 320,
|
|
1418
|
+
metadata: { bsFeeApplied: "1" },
|
|
1419
|
+
});
|
|
1420
|
+
expect(stripe.invoices.update).toHaveBeenCalledWith("in_sd", {
|
|
1421
|
+
statement_descriptor: "MAYAS FITNESS",
|
|
1422
|
+
});
|
|
1423
|
+
});
|
|
1424
|
+
|
|
1425
|
+
it("does nothing when the subscription carries no descriptor marker", async () => {
|
|
1426
|
+
const stripe = makeStripe();
|
|
1427
|
+
stripe.subscriptions.retrieve.mockResolvedValue({
|
|
1428
|
+
id: "sub_sd",
|
|
1429
|
+
metadata: {},
|
|
1430
|
+
});
|
|
1431
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1432
|
+
|
|
1433
|
+
await processEvent(whCtx, event("invoice.created", subInvoice()));
|
|
1434
|
+
expect(stripe.invoices.update).not.toHaveBeenCalled();
|
|
1435
|
+
});
|
|
1436
|
+
|
|
1437
|
+
it("never overwrites an already-set statement_descriptor (idempotent on retries)", async () => {
|
|
1438
|
+
const stripe = makeStripe();
|
|
1439
|
+
stripe.subscriptions.retrieve.mockResolvedValue({
|
|
1440
|
+
id: "sub_sd",
|
|
1441
|
+
metadata: { bsStatementDescriptor: "MAYAS FITNESS" },
|
|
1442
|
+
});
|
|
1443
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1444
|
+
|
|
1445
|
+
await processEvent(
|
|
1446
|
+
whCtx,
|
|
1447
|
+
event(
|
|
1448
|
+
"invoice.created",
|
|
1449
|
+
subInvoice({ statement_descriptor: "APP SET THIS" }),
|
|
1450
|
+
),
|
|
1451
|
+
);
|
|
1452
|
+
expect(stripe.invoices.update).not.toHaveBeenCalled();
|
|
1453
|
+
});
|
|
1454
|
+
|
|
1455
|
+
it("skips a marker that fails defensive validation instead of sending it to Stripe", async () => {
|
|
1456
|
+
const stripe = makeStripe();
|
|
1457
|
+
stripe.subscriptions.retrieve.mockResolvedValue({
|
|
1458
|
+
id: "sub_sd",
|
|
1459
|
+
// Should be impossible (validated at set time) but webhooks are
|
|
1460
|
+
// defensive: never forward a rule-breaking descriptor.
|
|
1461
|
+
metadata: { bsStatementDescriptor: "BAD*NAME" },
|
|
1462
|
+
});
|
|
1463
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1464
|
+
|
|
1465
|
+
await processEvent(whCtx, event("invoice.created", subInvoice()));
|
|
1466
|
+
expect(stripe.invoices.update).not.toHaveBeenCalled();
|
|
1467
|
+
});
|
|
1468
|
+
|
|
1469
|
+
it("never breaks invoice processing when the descriptor update fails (cosmetic, reconciled next cycle)", async () => {
|
|
1470
|
+
const stripe = makeStripe();
|
|
1471
|
+
stripe.subscriptions.retrieve.mockResolvedValue({
|
|
1472
|
+
id: "sub_sd",
|
|
1473
|
+
metadata: { bsStatementDescriptor: "MAYAS FITNESS" },
|
|
1474
|
+
});
|
|
1475
|
+
stripe.invoices.update.mockRejectedValue(new Error("stripe down"));
|
|
1476
|
+
const errSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
1477
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1478
|
+
|
|
1479
|
+
await expect(
|
|
1480
|
+
processEvent(whCtx, event("invoice.created", subInvoice())),
|
|
1481
|
+
).resolves.toBeUndefined();
|
|
1482
|
+
|
|
1483
|
+
expect(errSpy).toHaveBeenCalled();
|
|
1484
|
+
// The invoice row still lands in the component DB.
|
|
1485
|
+
const paths = (whCtx.ctx.runMutation as ReturnType<typeof vi.fn>).mock.calls;
|
|
1486
|
+
expect(paths.length).toBeGreaterThan(0);
|
|
1487
|
+
});
|
|
1488
|
+
});
|
|
1489
|
+
|
|
1490
|
+
describe("processEvent — per-invoice fixed/tier fee (BTS-51)", () => {
|
|
1491
|
+
const flaggedInvoice = (overrides: Record<string, unknown> = {}) => ({
|
|
1492
|
+
id: "in_fee",
|
|
1493
|
+
customer: "acct_buyer",
|
|
1494
|
+
currency: "usd",
|
|
1495
|
+
amount_due: 10000,
|
|
1496
|
+
amount_paid: 0,
|
|
1497
|
+
status: "draft",
|
|
1498
|
+
metadata: {},
|
|
1499
|
+
parent: { subscription_details: { subscription: "sub_1" } },
|
|
1500
|
+
...overrides,
|
|
1501
|
+
});
|
|
1502
|
+
|
|
1503
|
+
it("applies a fixed/tier application_fee_amount on invoice.created for a flagged subscription", async () => {
|
|
1504
|
+
const stripe = makeStripe();
|
|
1505
|
+
stripe.subscriptions.retrieve.mockResolvedValue({
|
|
1506
|
+
id: "sub_1",
|
|
1507
|
+
metadata: {
|
|
1508
|
+
bsFeeMode: "per_invoice",
|
|
1509
|
+
bsFeeConfig: JSON.stringify({ percent: 2.9, fixed: 30 }),
|
|
1510
|
+
},
|
|
1511
|
+
});
|
|
1512
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1513
|
+
|
|
1514
|
+
await processEvent(whCtx, event("invoice.created", flaggedInvoice()));
|
|
1515
|
+
|
|
1516
|
+
// round(10000 * 0.029) + 30 = 320
|
|
1517
|
+
expect(stripe.invoices.update).toHaveBeenCalledWith("in_fee", {
|
|
1518
|
+
application_fee_amount: 320,
|
|
1519
|
+
metadata: { bsFeeApplied: "1" },
|
|
1520
|
+
});
|
|
1521
|
+
});
|
|
1522
|
+
|
|
1523
|
+
it("does nothing for a subscription that is not flagged", async () => {
|
|
1524
|
+
const stripe = makeStripe();
|
|
1525
|
+
stripe.subscriptions.retrieve.mockResolvedValue({
|
|
1526
|
+
id: "sub_1",
|
|
1527
|
+
metadata: {},
|
|
1528
|
+
});
|
|
1529
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1530
|
+
|
|
1531
|
+
await processEvent(whCtx, event("invoice.created", flaggedInvoice()));
|
|
1532
|
+
expect(stripe.invoices.update).not.toHaveBeenCalled();
|
|
1533
|
+
});
|
|
1534
|
+
|
|
1535
|
+
it("is idempotent — skips invoices already marked bsFeeApplied", async () => {
|
|
1536
|
+
const stripe = makeStripe();
|
|
1537
|
+
// The BTS-32 descriptor processor shares this event and may retrieve the
|
|
1538
|
+
// subscription; give it an unflagged sub so only the fee path is observed.
|
|
1539
|
+
stripe.subscriptions.retrieve.mockResolvedValue({
|
|
1540
|
+
id: "sub_1",
|
|
1541
|
+
metadata: {},
|
|
1542
|
+
});
|
|
1543
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1544
|
+
|
|
1545
|
+
await processEvent(
|
|
1546
|
+
whCtx,
|
|
1547
|
+
event("invoice.created", flaggedInvoice({ metadata: { bsFeeApplied: "1" } })),
|
|
1548
|
+
);
|
|
1549
|
+
expect(stripe.invoices.update).not.toHaveBeenCalled();
|
|
1550
|
+
});
|
|
1551
|
+
|
|
1552
|
+
it("does not send a non-finite fee from a shape-invalid config", async () => {
|
|
1553
|
+
const stripe = makeStripe();
|
|
1554
|
+
stripe.subscriptions.retrieve.mockResolvedValue({
|
|
1555
|
+
id: "sub_1",
|
|
1556
|
+
metadata: {
|
|
1557
|
+
bsFeeMode: "per_invoice",
|
|
1558
|
+
// No `percent` → computeFee returns NaN; must not reach Stripe.
|
|
1559
|
+
bsFeeConfig: JSON.stringify({ fixed: 30 }),
|
|
1560
|
+
},
|
|
1561
|
+
});
|
|
1562
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1563
|
+
|
|
1564
|
+
await processEvent(whCtx, event("invoice.created", flaggedInvoice()));
|
|
1565
|
+
expect(stripe.invoices.update).not.toHaveBeenCalled();
|
|
1566
|
+
});
|
|
1567
|
+
|
|
1568
|
+
it("does nothing for an invoice with no parent subscription", async () => {
|
|
1569
|
+
const stripe = makeStripe();
|
|
1570
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1571
|
+
|
|
1572
|
+
await processEvent(
|
|
1573
|
+
whCtx,
|
|
1574
|
+
event("invoice.created", flaggedInvoice({ parent: null })),
|
|
1575
|
+
);
|
|
1576
|
+
expect(stripe.subscriptions.retrieve).not.toHaveBeenCalled();
|
|
1577
|
+
expect(stripe.invoices.update).not.toHaveBeenCalled();
|
|
1578
|
+
});
|
|
1579
|
+
|
|
1580
|
+
// BTS-68: the first subscription invoice is finalized (`open`) synchronously at
|
|
1581
|
+
// creation, so `invoice.created` fires with a NON-draft invoice and the
|
|
1582
|
+
// `invoices.update` that applies the fee is rejected by Stripe (monetary values
|
|
1583
|
+
// are uneditable once finalized). This characterizes the silent miss the fix
|
|
1584
|
+
// must compensate for elsewhere — it must not crash the webhook.
|
|
1585
|
+
it("silently no-ops (no crash) when the first invoice is already finalized at invoice.created", async () => {
|
|
1586
|
+
const stripe = makeStripe();
|
|
1587
|
+
stripe.subscriptions.retrieve.mockResolvedValue({
|
|
1588
|
+
id: "sub_1",
|
|
1589
|
+
metadata: {
|
|
1590
|
+
bsFeeMode: "per_invoice",
|
|
1591
|
+
bsFeeConfig: JSON.stringify({ percent: 2.9, fixed: 30 }),
|
|
1592
|
+
},
|
|
1593
|
+
});
|
|
1594
|
+
// Stripe rejects a monetary update on a finalized invoice.
|
|
1595
|
+
stripe.invoices.update.mockRejectedValue(
|
|
1596
|
+
new Error("This invoice is no longer a draft."),
|
|
1597
|
+
);
|
|
1598
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1599
|
+
|
|
1600
|
+
await expect(
|
|
1601
|
+
processEvent(
|
|
1602
|
+
whCtx,
|
|
1603
|
+
event("invoice.created", flaggedInvoice({ status: "open" })),
|
|
1604
|
+
),
|
|
1605
|
+
).resolves.toBeUndefined();
|
|
1606
|
+
// The fee was NOT collected on the first invoice via the draft path.
|
|
1607
|
+
expect(stripe.invoices.update).toHaveBeenCalled(); // attempted, rejected
|
|
1608
|
+
});
|
|
1609
|
+
});
|
|
1610
|
+
|
|
1611
|
+
describe("processEvent — first-invoice fee correction (BTS-68)", () => {
|
|
1612
|
+
/** A paid first invoice for a per_invoice destination-charge subscription. */
|
|
1613
|
+
const paidFirstInvoice = (overrides: Record<string, unknown> = {}) => ({
|
|
1614
|
+
id: "in_first",
|
|
1615
|
+
customer: "acct_buyer",
|
|
1616
|
+
currency: "usd",
|
|
1617
|
+
amount_due: 10000,
|
|
1618
|
+
amount_paid: 10000,
|
|
1619
|
+
status: "paid",
|
|
1620
|
+
application_fee_amount: null,
|
|
1621
|
+
metadata: {},
|
|
1622
|
+
parent: { subscription_details: { subscription: "sub_1" } },
|
|
1623
|
+
...overrides,
|
|
1624
|
+
});
|
|
1625
|
+
|
|
1626
|
+
/** Flag sub_1 as per_invoice with the given fee config. */
|
|
1627
|
+
function wireSub(
|
|
1628
|
+
stripe: ReturnType<typeof makeStripe>,
|
|
1629
|
+
config: unknown = { percent: 2.9, fixed: 30 },
|
|
1630
|
+
) {
|
|
1631
|
+
stripe.subscriptions.retrieve.mockResolvedValue({
|
|
1632
|
+
id: "sub_1",
|
|
1633
|
+
metadata: {
|
|
1634
|
+
bsFeeMode: "per_invoice",
|
|
1635
|
+
bsFeeConfig: JSON.stringify(config),
|
|
1636
|
+
},
|
|
1637
|
+
});
|
|
1638
|
+
}
|
|
1639
|
+
|
|
1640
|
+
/** Wire the fresh-invoice retrieve + charge → transfer resolution. */
|
|
1641
|
+
function wireInvoiceCharge(
|
|
1642
|
+
stripe: ReturnType<typeof makeStripe>,
|
|
1643
|
+
fresh: Record<string, unknown>,
|
|
1644
|
+
) {
|
|
1645
|
+
stripe.invoices.retrieve.mockResolvedValue(fresh);
|
|
1646
|
+
stripe.invoicePayments.list.mockResolvedValue({
|
|
1647
|
+
data: [{ payment: { charge: "ch_first" } }],
|
|
1648
|
+
});
|
|
1649
|
+
stripe.charges.retrieve.mockResolvedValue({
|
|
1650
|
+
id: "ch_first",
|
|
1651
|
+
transfer: "tr_first",
|
|
1652
|
+
});
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
it("collects the first invoice's fixed/tier fee by reversing the destination transfer", async () => {
|
|
1656
|
+
const stripe = makeStripe();
|
|
1657
|
+
wireSub(stripe);
|
|
1658
|
+
wireInvoiceCharge(stripe, paidFirstInvoice());
|
|
1659
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1660
|
+
|
|
1661
|
+
await processEvent(whCtx, event("invoice.paid", paidFirstInvoice()));
|
|
1662
|
+
|
|
1663
|
+
// round(10000 * 0.029) + 30 = 320, clawed back from the auto-transfer.
|
|
1664
|
+
expect(stripe.transfers.createReversal).toHaveBeenCalledWith(
|
|
1665
|
+
"tr_first",
|
|
1666
|
+
{ amount: 320, metadata: { bsFeeFor: "in_first" } },
|
|
1667
|
+
{ idempotencyKey: "bs_infee_in_first" },
|
|
1668
|
+
);
|
|
1669
|
+
// Marked collected on the invoice (metadata is editable post-finalization).
|
|
1670
|
+
expect(stripe.invoices.update).toHaveBeenCalledWith(
|
|
1671
|
+
"in_first",
|
|
1672
|
+
expect.objectContaining({
|
|
1673
|
+
metadata: expect.objectContaining({
|
|
1674
|
+
bsFeeCollected: "1",
|
|
1675
|
+
bsFeeAmount: "320",
|
|
1676
|
+
}),
|
|
1677
|
+
}),
|
|
1678
|
+
);
|
|
1679
|
+
});
|
|
1680
|
+
|
|
1681
|
+
it("collects a tiered first-invoice fee (matching tier's percent + fixed)", async () => {
|
|
1682
|
+
const stripe = makeStripe();
|
|
1683
|
+
wireSub(stripe, {
|
|
1684
|
+
percent: 10,
|
|
1685
|
+
tiers: [
|
|
1686
|
+
{ upTo: 5000, percent: 5 },
|
|
1687
|
+
{ upTo: null, percent: 8, fixed: 100 },
|
|
1688
|
+
],
|
|
1689
|
+
});
|
|
1690
|
+
wireInvoiceCharge(stripe, paidFirstInvoice());
|
|
1691
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1692
|
+
|
|
1693
|
+
await processEvent(whCtx, event("invoice.paid", paidFirstInvoice()));
|
|
1694
|
+
|
|
1695
|
+
// amount 10000 → catch-all tier: round(10000 * 0.08) + 100 = 900
|
|
1696
|
+
expect(stripe.transfers.createReversal).toHaveBeenCalledWith(
|
|
1697
|
+
"tr_first",
|
|
1698
|
+
expect.objectContaining({ amount: 900 }),
|
|
1699
|
+
{ idempotencyKey: "bs_infee_in_first" },
|
|
1700
|
+
);
|
|
1701
|
+
});
|
|
1702
|
+
|
|
1703
|
+
it("does NOT reverse a renewal invoice that already carries application_fee_amount", async () => {
|
|
1704
|
+
const stripe = makeStripe();
|
|
1705
|
+
wireSub(stripe);
|
|
1706
|
+
// Draft-window path already set the fee on this (renewal) invoice.
|
|
1707
|
+
wireInvoiceCharge(
|
|
1708
|
+
stripe,
|
|
1709
|
+
paidFirstInvoice({ application_fee_amount: 320 }),
|
|
1710
|
+
);
|
|
1711
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1712
|
+
|
|
1713
|
+
await processEvent(whCtx, event("invoice.paid", paidFirstInvoice()));
|
|
1714
|
+
|
|
1715
|
+
expect(stripe.transfers.createReversal).not.toHaveBeenCalled();
|
|
1716
|
+
expect(stripe.invoices.update).not.toHaveBeenCalled();
|
|
1717
|
+
});
|
|
1718
|
+
|
|
1719
|
+
it("does NOT reverse a renewal invoice already marked bsFeeApplied", async () => {
|
|
1720
|
+
const stripe = makeStripe();
|
|
1721
|
+
wireSub(stripe);
|
|
1722
|
+
wireInvoiceCharge(
|
|
1723
|
+
stripe,
|
|
1724
|
+
paidFirstInvoice({ metadata: { bsFeeApplied: "1" } }),
|
|
1725
|
+
);
|
|
1726
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1727
|
+
|
|
1728
|
+
await processEvent(whCtx, event("invoice.paid", paidFirstInvoice()));
|
|
1729
|
+
|
|
1730
|
+
expect(stripe.transfers.createReversal).not.toHaveBeenCalled();
|
|
1731
|
+
});
|
|
1732
|
+
|
|
1733
|
+
it("does nothing for a subscription that is not flagged per_invoice (e.g. percent-only)", async () => {
|
|
1734
|
+
const stripe = makeStripe();
|
|
1735
|
+
stripe.subscriptions.retrieve.mockResolvedValue({
|
|
1736
|
+
id: "sub_1",
|
|
1737
|
+
metadata: {}, // percent-only fees ride application_fee_percent, not per_invoice
|
|
1738
|
+
});
|
|
1739
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1740
|
+
|
|
1741
|
+
await processEvent(whCtx, event("invoice.paid", paidFirstInvoice()));
|
|
1742
|
+
|
|
1743
|
+
expect(stripe.invoices.retrieve).not.toHaveBeenCalled();
|
|
1744
|
+
expect(stripe.transfers.createReversal).not.toHaveBeenCalled();
|
|
1745
|
+
});
|
|
1746
|
+
|
|
1747
|
+
it("is idempotent — an invoice already marked bsFeeCollected is not reversed again", async () => {
|
|
1748
|
+
const stripe = makeStripe();
|
|
1749
|
+
wireSub(stripe);
|
|
1750
|
+
wireInvoiceCharge(
|
|
1751
|
+
stripe,
|
|
1752
|
+
paidFirstInvoice({
|
|
1753
|
+
metadata: { bsFeeCollected: "1", bsFeeAmount: "320" },
|
|
1754
|
+
}),
|
|
1755
|
+
);
|
|
1756
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1757
|
+
|
|
1758
|
+
await processEvent(whCtx, event("invoice.paid", paidFirstInvoice()));
|
|
1759
|
+
|
|
1760
|
+
expect(stripe.transfers.createReversal).not.toHaveBeenCalled();
|
|
1761
|
+
expect(stripe.invoices.update).not.toHaveBeenCalled();
|
|
1762
|
+
});
|
|
1763
|
+
|
|
1764
|
+
it("skips a zero-amount invoice (100%-off / trial) before touching the subscription", async () => {
|
|
1765
|
+
const stripe = makeStripe();
|
|
1766
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1767
|
+
|
|
1768
|
+
await processEvent(
|
|
1769
|
+
whCtx,
|
|
1770
|
+
event("invoice.paid", paidFirstInvoice({ amount_paid: 0 })),
|
|
1771
|
+
);
|
|
1772
|
+
|
|
1773
|
+
expect(stripe.subscriptions.retrieve).not.toHaveBeenCalled();
|
|
1774
|
+
expect(stripe.transfers.createReversal).not.toHaveBeenCalled();
|
|
1775
|
+
});
|
|
1776
|
+
|
|
1777
|
+
it("does not send a non-finite fee from a shape-invalid config", async () => {
|
|
1778
|
+
const stripe = makeStripe();
|
|
1779
|
+
// No `percent` → computeFee returns NaN; must never reach a reversal.
|
|
1780
|
+
wireSub(stripe, { fixed: 30 });
|
|
1781
|
+
wireInvoiceCharge(stripe, paidFirstInvoice());
|
|
1782
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1783
|
+
|
|
1784
|
+
await processEvent(whCtx, event("invoice.paid", paidFirstInvoice()));
|
|
1785
|
+
|
|
1786
|
+
expect(stripe.transfers.createReversal).not.toHaveBeenCalled();
|
|
1787
|
+
});
|
|
1788
|
+
|
|
1789
|
+
it("logs and skips (no crash) when the charge has no destination transfer to reverse", async () => {
|
|
1790
|
+
const err = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
1791
|
+
const stripe = makeStripe();
|
|
1792
|
+
wireSub(stripe);
|
|
1793
|
+
stripe.invoices.retrieve.mockResolvedValue(paidFirstInvoice());
|
|
1794
|
+
stripe.invoicePayments.list.mockResolvedValue({
|
|
1795
|
+
data: [{ payment: { charge: "ch_first" } }],
|
|
1796
|
+
});
|
|
1797
|
+
stripe.charges.retrieve.mockResolvedValue({ id: "ch_first" }); // no transfer
|
|
1798
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1799
|
+
|
|
1800
|
+
await expect(
|
|
1801
|
+
processEvent(whCtx, event("invoice.paid", paidFirstInvoice())),
|
|
1802
|
+
).resolves.toBeUndefined();
|
|
1803
|
+
expect(stripe.transfers.createReversal).not.toHaveBeenCalled();
|
|
1804
|
+
expect(err).toHaveBeenCalled();
|
|
1805
|
+
});
|
|
1806
|
+
|
|
1807
|
+
it("propagates a reversal failure so the event is marked failed and Stripe retries", async () => {
|
|
1808
|
+
const stripe = makeStripe();
|
|
1809
|
+
wireSub(stripe);
|
|
1810
|
+
wireInvoiceCharge(stripe, paidFirstInvoice());
|
|
1811
|
+
stripe.transfers.createReversal.mockRejectedValue(new Error("stripe down"));
|
|
1812
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1813
|
+
|
|
1814
|
+
// A swallowed failure would 200 → event ledger "processed" (terminal) → the
|
|
1815
|
+
// first-invoice fee is lost forever. It must throw so Stripe retries.
|
|
1816
|
+
await expect(
|
|
1817
|
+
processEvent(whCtx, event("invoice.paid", paidFirstInvoice())),
|
|
1818
|
+
).rejects.toThrow("stripe down");
|
|
1819
|
+
});
|
|
1820
|
+
|
|
1821
|
+
it("[BTS-69] does NOT double-reverse when the marker write failed and bsFeeConfig was edited before a retry past the idempotency-key TTL", async () => {
|
|
1822
|
+
const stripe = makeStripe();
|
|
1823
|
+
wireSub(stripe, { percent: 2.9, fixed: 30 }); // first-delivery fee F1 = 320
|
|
1824
|
+
wireInvoiceCharge(stripe, paidFirstInvoice());
|
|
1825
|
+
|
|
1826
|
+
// Stateful reversal store: createReversal records, listReversals returns them
|
|
1827
|
+
// — modelling that a reversal PERSISTS across webhook deliveries (Stripe's
|
|
1828
|
+
// reversal ledger), independent of our bsFeeCollected invoice marker and of
|
|
1829
|
+
// Stripe's ≤24h idempotency-key window.
|
|
1830
|
+
const reversals: MockReversal[] = [];
|
|
1831
|
+
stripe.transfers.createReversal.mockImplementation(
|
|
1832
|
+
async (id: unknown, params: unknown) => {
|
|
1833
|
+
const p = params as { amount: number; metadata?: Record<string, string> };
|
|
1834
|
+
const rev: MockReversal = {
|
|
1835
|
+
id: `trr_${reversals.length + 1}`,
|
|
1836
|
+
amount: p.amount,
|
|
1837
|
+
metadata: p.metadata ?? null,
|
|
1838
|
+
_transfer: id as string,
|
|
1839
|
+
};
|
|
1840
|
+
reversals.push(rev);
|
|
1841
|
+
return rev;
|
|
1842
|
+
},
|
|
1843
|
+
);
|
|
1844
|
+
stripe.transfers.listReversals.mockImplementation(async (id: unknown) => ({
|
|
1845
|
+
data: reversals.filter((r) => r._transfer === (id as string)),
|
|
1846
|
+
}));
|
|
1847
|
+
|
|
1848
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1849
|
+
|
|
1850
|
+
// ── Delivery 1: the reversal SUCCEEDS, then the bsFeeCollected marker write
|
|
1851
|
+
// FAILS → the error propagates → the event is marked `failed`. So the money
|
|
1852
|
+
// moved (F1=320 reversed) but the invoice was never marked collected.
|
|
1853
|
+
stripe.invoices.update.mockRejectedValueOnce(new Error("marker write failed"));
|
|
1854
|
+
await expect(
|
|
1855
|
+
processEvent(whCtx, event("invoice.paid", paidFirstInvoice())),
|
|
1856
|
+
).rejects.toThrow("marker write failed");
|
|
1857
|
+
expect(stripe.transfers.createReversal).toHaveBeenCalledTimes(1);
|
|
1858
|
+
expect(reversals).toHaveLength(1);
|
|
1859
|
+
|
|
1860
|
+
// ── The platform edits bsFeeConfig before the retry, so a recompute would now
|
|
1861
|
+
// produce a DIFFERENT fee (F2=900) — the config-drift that defeats Stripe's
|
|
1862
|
+
// idempotency key once it expires.
|
|
1863
|
+
wireSub(stripe, { percent: 8, fixed: 100 });
|
|
1864
|
+
stripe.invoices.update.mockResolvedValue({}); // marker write now succeeds
|
|
1865
|
+
|
|
1866
|
+
// ── Delivery 2: retry AFTER the idempotency key expired (the mock doesn't
|
|
1867
|
+
// enforce keys, so a second createReversal WOULD land a real second reversal).
|
|
1868
|
+
// The invoice still carries NO bsFeeCollected marker (delivery 1's write
|
|
1869
|
+
// failed), so the marker guard can't save us. Only adopting the existing
|
|
1870
|
+
// bsFeeFor:in_first reversal prevents the double collection.
|
|
1871
|
+
await processEvent(whCtx, event("invoice.paid", paidFirstInvoice()));
|
|
1872
|
+
|
|
1873
|
+
// THE INVARIANT: exactly ONE reversal across both deliveries — no double
|
|
1874
|
+
// collection — and the recorded amount is the originally-reversed F1, not the
|
|
1875
|
+
// drifted F2.
|
|
1876
|
+
expect(stripe.transfers.createReversal).toHaveBeenCalledTimes(1);
|
|
1877
|
+
expect(reversals).toHaveLength(1);
|
|
1878
|
+
expect(reversals[0].amount).toBe(320);
|
|
1879
|
+
// The retry re-records the marker with the actually-reversed amount.
|
|
1880
|
+
expect(stripe.invoices.update).toHaveBeenCalledWith(
|
|
1881
|
+
"in_first",
|
|
1882
|
+
expect.objectContaining({
|
|
1883
|
+
metadata: expect.objectContaining({
|
|
1884
|
+
bsFeeCollected: "1",
|
|
1885
|
+
bsFeeAmount: "320",
|
|
1886
|
+
}),
|
|
1887
|
+
}),
|
|
1888
|
+
);
|
|
1889
|
+
});
|
|
1890
|
+
});
|
|
1891
|
+
|
|
1892
|
+
describe("processEvent — per-charge fixed/tier fee (BTS-60)", () => {
|
|
1893
|
+
/** A succeeded one-time destination-charge PI flagged for per-charge fee. */
|
|
1894
|
+
const flaggedPi = (overrides: Record<string, unknown> = {}) => ({
|
|
1895
|
+
id: "pi_fee",
|
|
1896
|
+
amount: 10000,
|
|
1897
|
+
currency: "usd",
|
|
1898
|
+
status: "succeeded",
|
|
1899
|
+
latest_charge: "ch_fee",
|
|
1900
|
+
transfer_data: { destination: "acct_seller" },
|
|
1901
|
+
metadata: {
|
|
1902
|
+
bsFeeMode: "per_charge",
|
|
1903
|
+
bsFeeConfig: JSON.stringify({ percent: 2.9, fixed: 30 }),
|
|
1904
|
+
},
|
|
1905
|
+
...overrides,
|
|
1906
|
+
});
|
|
1907
|
+
|
|
1908
|
+
/** Wire the retrieve mocks for the happy path: fresh PI + charge w/ transfer. */
|
|
1909
|
+
function wireHappyPath(
|
|
1910
|
+
stripe: ReturnType<typeof makeStripe>,
|
|
1911
|
+
pi: Record<string, unknown>,
|
|
1912
|
+
opts: { amountReceived?: number; freshMetadata?: Record<string, string> } = {},
|
|
1913
|
+
) {
|
|
1914
|
+
stripe.paymentIntents.retrieve.mockResolvedValue({
|
|
1915
|
+
...pi,
|
|
1916
|
+
amount_received: opts.amountReceived ?? (pi.amount as number),
|
|
1917
|
+
...(opts.freshMetadata ? { metadata: opts.freshMetadata } : {}),
|
|
1918
|
+
});
|
|
1919
|
+
stripe.charges.retrieve.mockResolvedValue({
|
|
1920
|
+
id: pi.latest_charge,
|
|
1921
|
+
transfer: "tr_auto",
|
|
1922
|
+
});
|
|
1923
|
+
}
|
|
1924
|
+
|
|
1925
|
+
it("collects a fixed fee by partially reversing the destination transfer", async () => {
|
|
1926
|
+
const stripe = makeStripe();
|
|
1927
|
+
const pi = flaggedPi();
|
|
1928
|
+
wireHappyPath(stripe, pi);
|
|
1929
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1930
|
+
|
|
1931
|
+
await processEvent(whCtx, event("payment_intent.succeeded", pi));
|
|
1932
|
+
|
|
1933
|
+
// round(10000 * 0.029) + 30 = 320, clawed back from the auto-transfer.
|
|
1934
|
+
expect(stripe.transfers.createReversal).toHaveBeenCalledWith(
|
|
1935
|
+
"tr_auto",
|
|
1936
|
+
expect.objectContaining({ amount: 320 }),
|
|
1937
|
+
{ idempotencyKey: "bs_pcfee_pi_fee" },
|
|
1938
|
+
);
|
|
1939
|
+
// Marked collected on the PI so retries beyond Stripe's idempotency window skip.
|
|
1940
|
+
expect(stripe.paymentIntents.update).toHaveBeenCalledWith(
|
|
1941
|
+
"pi_fee",
|
|
1942
|
+
expect.objectContaining({
|
|
1943
|
+
metadata: expect.objectContaining({
|
|
1944
|
+
bsFeeCollected: "1",
|
|
1945
|
+
bsFeeAmount: "320",
|
|
1946
|
+
}),
|
|
1947
|
+
}),
|
|
1948
|
+
);
|
|
1949
|
+
// Denormalized onto the payments row.
|
|
1950
|
+
const { path, data } = dispatchedPayload(whCtx.ctx);
|
|
1951
|
+
expect(path).toBe("betterStripe/connect/mutations/upsertPayment");
|
|
1952
|
+
expect(data.feeCollectedAmount).toBe(320);
|
|
1953
|
+
});
|
|
1954
|
+
|
|
1955
|
+
it("collects a tiered fee (matching tier's percent + fixed)", async () => {
|
|
1956
|
+
const stripe = makeStripe();
|
|
1957
|
+
const pi = flaggedPi({
|
|
1958
|
+
metadata: {
|
|
1959
|
+
bsFeeMode: "per_charge",
|
|
1960
|
+
bsFeeConfig: JSON.stringify({
|
|
1961
|
+
percent: 10,
|
|
1962
|
+
tiers: [
|
|
1963
|
+
{ upTo: 5000, percent: 5 },
|
|
1964
|
+
{ upTo: null, percent: 8, fixed: 100 },
|
|
1965
|
+
],
|
|
1966
|
+
}),
|
|
1967
|
+
},
|
|
1968
|
+
});
|
|
1969
|
+
wireHappyPath(stripe, pi);
|
|
1970
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1971
|
+
|
|
1972
|
+
await processEvent(whCtx, event("payment_intent.succeeded", pi));
|
|
1973
|
+
|
|
1974
|
+
// amount 10000 → catch-all tier: round(10000 * 0.08) + 100 = 900
|
|
1975
|
+
expect(stripe.transfers.createReversal).toHaveBeenCalledWith(
|
|
1976
|
+
"tr_auto",
|
|
1977
|
+
expect.objectContaining({ amount: 900 }),
|
|
1978
|
+
{ idempotencyKey: "bs_pcfee_pi_fee" },
|
|
1979
|
+
);
|
|
1980
|
+
});
|
|
1981
|
+
|
|
1982
|
+
it("computes the fee from the amount actually charged, not the list amount", async () => {
|
|
1983
|
+
const stripe = makeStripe();
|
|
1984
|
+
const pi = flaggedPi();
|
|
1985
|
+
// A Checkout promotion code discounted the final charge to 8000.
|
|
1986
|
+
wireHappyPath(stripe, pi, { amountReceived: 8000 });
|
|
1987
|
+
const whCtx = makeWhCtx({ stripe });
|
|
1988
|
+
|
|
1989
|
+
await processEvent(whCtx, event("payment_intent.succeeded", pi));
|
|
1990
|
+
|
|
1991
|
+
// round(8000 * 0.029) + 30 = 262 — NOT 320 from the stale event amount.
|
|
1992
|
+
expect(stripe.transfers.createReversal).toHaveBeenCalledWith(
|
|
1993
|
+
"tr_auto",
|
|
1994
|
+
expect.objectContaining({ amount: 262 }),
|
|
1995
|
+
expect.anything(),
|
|
1996
|
+
);
|
|
1997
|
+
});
|
|
1998
|
+
|
|
1999
|
+
it("is idempotent — a PI already marked bsFeeCollected is not reversed again", async () => {
|
|
2000
|
+
const stripe = makeStripe();
|
|
2001
|
+
const pi = flaggedPi();
|
|
2002
|
+
// The fresh PI (not the stale event snapshot) carries the marker.
|
|
2003
|
+
wireHappyPath(stripe, pi, {
|
|
2004
|
+
freshMetadata: {
|
|
2005
|
+
bsFeeMode: "per_charge",
|
|
2006
|
+
bsFeeConfig: JSON.stringify({ percent: 2.9, fixed: 30 }),
|
|
2007
|
+
bsFeeCollected: "1",
|
|
2008
|
+
bsFeeAmount: "320",
|
|
2009
|
+
},
|
|
2010
|
+
});
|
|
2011
|
+
const whCtx = makeWhCtx({ stripe });
|
|
2012
|
+
|
|
2013
|
+
await processEvent(whCtx, event("payment_intent.succeeded", pi));
|
|
2014
|
+
|
|
2015
|
+
expect(stripe.transfers.createReversal).not.toHaveBeenCalled();
|
|
2016
|
+
expect(stripe.paymentIntents.update).not.toHaveBeenCalled();
|
|
2017
|
+
// The payments row still reflects the previously collected fee.
|
|
2018
|
+
expect(dispatchedPayload(whCtx.ctx).data.feeCollectedAmount).toBe(320);
|
|
2019
|
+
});
|
|
2020
|
+
|
|
2021
|
+
it("does nothing for a PI without the per_charge marker", async () => {
|
|
2022
|
+
const stripe = makeStripe();
|
|
2023
|
+
const whCtx = makeWhCtx({ stripe });
|
|
2024
|
+
|
|
2025
|
+
await processEvent(
|
|
2026
|
+
whCtx,
|
|
2027
|
+
event("payment_intent.succeeded", flaggedPi({ metadata: {} })),
|
|
2028
|
+
);
|
|
2029
|
+
|
|
2030
|
+
expect(stripe.paymentIntents.retrieve).not.toHaveBeenCalled();
|
|
2031
|
+
expect(stripe.transfers.createReversal).not.toHaveBeenCalled();
|
|
2032
|
+
expect(dispatchedPayload(whCtx.ctx).data.feeCollectedAmount).toBeUndefined();
|
|
2033
|
+
});
|
|
2034
|
+
|
|
2035
|
+
it("swallows malformed bsFeeConfig without reversing or crashing", async () => {
|
|
2036
|
+
const stripe = makeStripe();
|
|
2037
|
+
const pi = flaggedPi({
|
|
2038
|
+
metadata: { bsFeeMode: "per_charge", bsFeeConfig: "not json" },
|
|
2039
|
+
});
|
|
2040
|
+
wireHappyPath(stripe, pi);
|
|
2041
|
+
const whCtx = makeWhCtx({ stripe });
|
|
2042
|
+
|
|
2043
|
+
await expect(
|
|
2044
|
+
processEvent(whCtx, event("payment_intent.succeeded", pi)),
|
|
2045
|
+
).resolves.toBeUndefined();
|
|
2046
|
+
expect(stripe.transfers.createReversal).not.toHaveBeenCalled();
|
|
2047
|
+
});
|
|
2048
|
+
|
|
2049
|
+
it("propagates a reversal failure so the event is marked failed and Stripe retries", async () => {
|
|
2050
|
+
const stripe = makeStripe();
|
|
2051
|
+
const pi = flaggedPi();
|
|
2052
|
+
wireHappyPath(stripe, pi);
|
|
2053
|
+
stripe.transfers.createReversal.mockRejectedValue(new Error("stripe down"));
|
|
2054
|
+
const whCtx = makeWhCtx({ stripe });
|
|
2055
|
+
|
|
2056
|
+
// A swallowed failure would 200 → event ledger "processed" (terminal) →
|
|
2057
|
+
// the fee is silently lost forever. Throwing marks the event "failed",
|
|
2058
|
+
// Stripe retries, and the whole case re-runs idempotently.
|
|
2059
|
+
await expect(
|
|
2060
|
+
processEvent(whCtx, event("payment_intent.succeeded", pi)),
|
|
2061
|
+
).rejects.toThrow("stripe down");
|
|
2062
|
+
|
|
2063
|
+
// The PI was never marked collected, so the retry collects the fee.
|
|
2064
|
+
expect(stripe.paymentIntents.update).not.toHaveBeenCalled();
|
|
2065
|
+
// Collection runs before the payment upsert, so nothing landed this
|
|
2066
|
+
// delivery — the retry writes the row (with the fee) in one pass instead
|
|
2067
|
+
// of leaving a partial row a second write would have to patch.
|
|
2068
|
+
expect(whCtx.ctx.runMutation).not.toHaveBeenCalled();
|
|
2069
|
+
});
|
|
2070
|
+
|
|
2071
|
+
it("rethrows a mark failure after a successful reversal; the replay converges on the same idempotency key", async () => {
|
|
2072
|
+
const stripe = makeStripe();
|
|
2073
|
+
const pi = flaggedPi();
|
|
2074
|
+
wireHappyPath(stripe, pi);
|
|
2075
|
+
stripe.paymentIntents.update.mockRejectedValueOnce(new Error("mark failed"));
|
|
2076
|
+
const whCtx = makeWhCtx({ stripe });
|
|
2077
|
+
|
|
2078
|
+
// Delivery 1: reversal succeeds, marking the PI fails → must throw (the
|
|
2079
|
+
// event goes "failed" and retries) rather than end "processed" with an
|
|
2080
|
+
// unmarked PI and no feeCollectedAmount on the payments row.
|
|
2081
|
+
await expect(
|
|
2082
|
+
processEvent(whCtx, event("payment_intent.succeeded", pi)),
|
|
2083
|
+
).rejects.toThrow("mark failed");
|
|
2084
|
+
|
|
2085
|
+
// Delivery 2 (Stripe retry): the fresh PI is still unmarked, so the
|
|
2086
|
+
// reversal is re-sent — with the IDENTICAL idempotency key and params
|
|
2087
|
+
// (inputs are immutable post-success), so Stripe replays it without a
|
|
2088
|
+
// second money movement. Then the mark and the payment upsert land.
|
|
2089
|
+
await processEvent(whCtx, event("payment_intent.succeeded", pi));
|
|
2090
|
+
|
|
2091
|
+
expect(stripe.transfers.createReversal).toHaveBeenCalledTimes(2);
|
|
2092
|
+
const [firstCall, secondCall] = stripe.transfers.createReversal.mock
|
|
2093
|
+
.calls as unknown as [unknown, unknown, unknown][];
|
|
2094
|
+
expect(secondCall).toEqual(firstCall);
|
|
2095
|
+
expect(firstCall[2]).toEqual({ idempotencyKey: "bs_pcfee_pi_fee" });
|
|
2096
|
+
expect(stripe.paymentIntents.update).toHaveBeenCalledTimes(2);
|
|
2097
|
+
const { path, data } = dispatchedPayload(whCtx.ctx);
|
|
2098
|
+
expect(path).toBe("betterStripe/connect/mutations/upsertPayment");
|
|
2099
|
+
expect(data.feeCollectedAmount).toBe(320);
|
|
2100
|
+
});
|
|
2101
|
+
|
|
2102
|
+
it("skips (without throwing) when the charge has no destination transfer", async () => {
|
|
2103
|
+
const stripe = makeStripe();
|
|
2104
|
+
const pi = flaggedPi();
|
|
2105
|
+
wireHappyPath(stripe, pi);
|
|
2106
|
+
// Anomalous config a retry can't fix — rethrowing would loop the event
|
|
2107
|
+
// "failed" forever. Logged skip instead.
|
|
2108
|
+
stripe.charges.retrieve.mockResolvedValue({ id: "ch_fee", transfer: null });
|
|
2109
|
+
const errSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
2110
|
+
const whCtx = makeWhCtx({ stripe });
|
|
2111
|
+
|
|
2112
|
+
await expect(
|
|
2113
|
+
processEvent(whCtx, event("payment_intent.succeeded", pi)),
|
|
2114
|
+
).resolves.toBeUndefined();
|
|
2115
|
+
|
|
2116
|
+
expect(errSpy).toHaveBeenCalled();
|
|
2117
|
+
expect(stripe.transfers.createReversal).not.toHaveBeenCalled();
|
|
2118
|
+
// The payment row still lands (without a fee) — the sale itself is real.
|
|
2119
|
+
expect(dispatchedPayload(whCtx.ctx).path).toBe(
|
|
2120
|
+
"betterStripe/connect/mutations/upsertPayment",
|
|
2121
|
+
);
|
|
2122
|
+
});
|
|
2123
|
+
|
|
2124
|
+
it("[regression] the per_invoice path is untouched by the per-charge consumer", async () => {
|
|
2125
|
+
const stripe = makeStripe();
|
|
2126
|
+
stripe.subscriptions.retrieve.mockResolvedValue({
|
|
2127
|
+
id: "sub_1",
|
|
2128
|
+
metadata: {
|
|
2129
|
+
bsFeeMode: "per_invoice",
|
|
2130
|
+
bsFeeConfig: JSON.stringify({ percent: 2.9, fixed: 30 }),
|
|
2131
|
+
},
|
|
2132
|
+
});
|
|
2133
|
+
const whCtx = makeWhCtx({ stripe });
|
|
2134
|
+
|
|
2135
|
+
await processEvent(
|
|
2136
|
+
whCtx,
|
|
2137
|
+
event("invoice.created", {
|
|
2138
|
+
id: "in_fee",
|
|
2139
|
+
currency: "usd",
|
|
2140
|
+
amount_due: 10000,
|
|
2141
|
+
amount_paid: 0,
|
|
2142
|
+
status: "draft",
|
|
2143
|
+
metadata: {},
|
|
2144
|
+
parent: { subscription_details: { subscription: "sub_1" } },
|
|
2145
|
+
}),
|
|
2146
|
+
);
|
|
2147
|
+
|
|
2148
|
+
// Still applied as an application_fee_amount on the draft invoice…
|
|
2149
|
+
expect(stripe.invoices.update).toHaveBeenCalledWith("in_fee", {
|
|
2150
|
+
application_fee_amount: 320,
|
|
2151
|
+
metadata: { bsFeeApplied: "1" },
|
|
2152
|
+
});
|
|
2153
|
+
// …and never via the per-charge reversal machinery.
|
|
2154
|
+
expect(stripe.transfers.createReversal).not.toHaveBeenCalled();
|
|
2155
|
+
expect(stripe.paymentIntents.retrieve).not.toHaveBeenCalled();
|
|
2156
|
+
});
|
|
2157
|
+
});
|
|
2158
|
+
|
|
2159
|
+
describe("processEvent — refunds: split clawback + fee-refund denormalization (BTS-34)", () => {
|
|
2160
|
+
/** Two split-engine ledger legs for ch_1 (original amounts, minor units). */
|
|
2161
|
+
const ledgerLegs = (
|
|
2162
|
+
overrides: { reversedStore?: number; reversedAff?: number } = {},
|
|
2163
|
+
) => [
|
|
2164
|
+
{
|
|
2165
|
+
stripeTransferId: "tr_s",
|
|
2166
|
+
amount: 7000,
|
|
2167
|
+
destinationAccountId: "acct_store",
|
|
2168
|
+
role: "store",
|
|
2169
|
+
currency: "usd",
|
|
2170
|
+
...(overrides.reversedStore !== undefined
|
|
2171
|
+
? { reversedAmount: overrides.reversedStore }
|
|
2172
|
+
: {}),
|
|
2173
|
+
},
|
|
2174
|
+
{
|
|
2175
|
+
stripeTransferId: "tr_a",
|
|
2176
|
+
amount: 1000,
|
|
2177
|
+
destinationAccountId: "acct_aff",
|
|
2178
|
+
role: "affiliate",
|
|
2179
|
+
currency: "usd",
|
|
2180
|
+
...(overrides.reversedAff !== undefined
|
|
2181
|
+
? { reversedAmount: overrides.reversedAff }
|
|
2182
|
+
: {}),
|
|
2183
|
+
},
|
|
2184
|
+
];
|
|
2185
|
+
|
|
2186
|
+
const refundEvent = (overrides: Record<string, unknown> = {}) =>
|
|
2187
|
+
event("refund.created", {
|
|
2188
|
+
id: "re_1",
|
|
2189
|
+
charge: "ch_1",
|
|
2190
|
+
payment_intent: "pi_1",
|
|
2191
|
+
amount: 5000,
|
|
2192
|
+
currency: "usd",
|
|
2193
|
+
status: "succeeded",
|
|
2194
|
+
metadata: {},
|
|
2195
|
+
...overrides,
|
|
2196
|
+
});
|
|
2197
|
+
|
|
2198
|
+
it("reverses split-sale transfers pro-rata on a succeeded partial refund", async () => {
|
|
2199
|
+
const ctx = makeCtx({ query: ledgerLegs() });
|
|
2200
|
+
const stripe = makeStripe();
|
|
2201
|
+
stripe.charges.retrieve.mockResolvedValue({
|
|
2202
|
+
id: "ch_1",
|
|
2203
|
+
amount: 10000,
|
|
2204
|
+
amount_refunded: 5000,
|
|
2205
|
+
});
|
|
2206
|
+
const whCtx = makeWhCtx({ ctx, stripe });
|
|
2207
|
+
|
|
2208
|
+
await processEvent(whCtx, refundEvent());
|
|
2209
|
+
|
|
2210
|
+
// 50% refunded → each leg reversed to 50% of its original amount.
|
|
2211
|
+
expect(stripe.transfers.createReversal).toHaveBeenCalledTimes(2);
|
|
2212
|
+
expect(stripe.transfers.createReversal).toHaveBeenCalledWith(
|
|
2213
|
+
"tr_s",
|
|
2214
|
+
{ amount: 3500 },
|
|
2215
|
+
{ idempotencyKey: "bs_rev_re_1_tr_s" },
|
|
2216
|
+
);
|
|
2217
|
+
expect(stripe.transfers.createReversal).toHaveBeenCalledWith(
|
|
2218
|
+
"tr_a",
|
|
2219
|
+
{ amount: 500 },
|
|
2220
|
+
{ idempotencyKey: "bs_rev_re_1_tr_a" },
|
|
2221
|
+
);
|
|
2222
|
+
});
|
|
2223
|
+
|
|
2224
|
+
it("reverses the full transfer amounts on a full refund", async () => {
|
|
2225
|
+
const ctx = makeCtx({ query: ledgerLegs() });
|
|
2226
|
+
const stripe = makeStripe();
|
|
2227
|
+
stripe.charges.retrieve.mockResolvedValue({
|
|
2228
|
+
id: "ch_1",
|
|
2229
|
+
amount: 10000,
|
|
2230
|
+
amount_refunded: 10000,
|
|
2231
|
+
});
|
|
2232
|
+
const whCtx = makeWhCtx({ ctx, stripe });
|
|
2233
|
+
|
|
2234
|
+
await processEvent(whCtx, refundEvent({ amount: 10000 }));
|
|
2235
|
+
|
|
2236
|
+
expect(stripe.transfers.createReversal).toHaveBeenCalledWith(
|
|
2237
|
+
"tr_s",
|
|
2238
|
+
{ amount: 7000 },
|
|
2239
|
+
{ idempotencyKey: "bs_rev_re_1_tr_s" },
|
|
2240
|
+
);
|
|
2241
|
+
expect(stripe.transfers.createReversal).toHaveBeenCalledWith(
|
|
2242
|
+
"tr_a",
|
|
2243
|
+
{ amount: 1000 },
|
|
2244
|
+
{ idempotencyKey: "bs_rev_re_1_tr_a" },
|
|
2245
|
+
);
|
|
2246
|
+
});
|
|
2247
|
+
|
|
2248
|
+
it("a second partial refund reverses only the delta up to the new cumulative target", async () => {
|
|
2249
|
+
// First refund (5000) already clawed back 3500/500 and was recorded.
|
|
2250
|
+
const ctx = makeCtx({
|
|
2251
|
+
query: ledgerLegs({ reversedStore: 3500, reversedAff: 500 }),
|
|
2252
|
+
});
|
|
2253
|
+
const stripe = makeStripe();
|
|
2254
|
+
stripe.charges.retrieve.mockResolvedValue({
|
|
2255
|
+
id: "ch_1",
|
|
2256
|
+
amount: 10000,
|
|
2257
|
+
amount_refunded: 7000, // cumulative: 5000 + this refund's 2000
|
|
2258
|
+
});
|
|
2259
|
+
const whCtx = makeWhCtx({ ctx, stripe });
|
|
2260
|
+
|
|
2261
|
+
await processEvent(whCtx, refundEvent({ id: "re_2", amount: 2000 }));
|
|
2262
|
+
|
|
2263
|
+
// Target 70% of each leg minus what's already reversed: 4900-3500, 700-500.
|
|
2264
|
+
expect(stripe.transfers.createReversal).toHaveBeenCalledWith(
|
|
2265
|
+
"tr_s",
|
|
2266
|
+
{ amount: 1400 },
|
|
2267
|
+
{ idempotencyKey: "bs_rev_re_2_tr_s" },
|
|
2268
|
+
);
|
|
2269
|
+
expect(stripe.transfers.createReversal).toHaveBeenCalledWith(
|
|
2270
|
+
"tr_a",
|
|
2271
|
+
{ amount: 200 },
|
|
2272
|
+
{ idempotencyKey: "bs_rev_re_2_tr_a" },
|
|
2273
|
+
);
|
|
2274
|
+
});
|
|
2275
|
+
|
|
2276
|
+
it("is idempotent — a redelivered refund event whose reversals are recorded reverses nothing", async () => {
|
|
2277
|
+
const ctx = makeCtx({
|
|
2278
|
+
query: ledgerLegs({ reversedStore: 3500, reversedAff: 500 }),
|
|
2279
|
+
});
|
|
2280
|
+
const stripe = makeStripe();
|
|
2281
|
+
stripe.charges.retrieve.mockResolvedValue({
|
|
2282
|
+
id: "ch_1",
|
|
2283
|
+
amount: 10000,
|
|
2284
|
+
amount_refunded: 5000,
|
|
2285
|
+
});
|
|
2286
|
+
const whCtx = makeWhCtx({ ctx, stripe });
|
|
2287
|
+
|
|
2288
|
+
await processEvent(whCtx, refundEvent());
|
|
2289
|
+
|
|
2290
|
+
// Cumulative target (50%) already met on both legs — no delta, no calls.
|
|
2291
|
+
expect(stripe.transfers.createReversal).not.toHaveBeenCalled();
|
|
2292
|
+
});
|
|
2293
|
+
|
|
2294
|
+
it("skips clawback quietly for a refund on a non-split sale", async () => {
|
|
2295
|
+
const ctx = makeCtx({ query: [] });
|
|
2296
|
+
const stripe = makeStripe();
|
|
2297
|
+
const whCtx = makeWhCtx({ ctx, stripe });
|
|
2298
|
+
|
|
2299
|
+
await processEvent(whCtx, refundEvent());
|
|
2300
|
+
|
|
2301
|
+
// No ledger legs → not a split sale (Stripe-native reverse_transfer covers
|
|
2302
|
+
// destination charges). No charge lookup, no reversals, no throw.
|
|
2303
|
+
expect(stripe.charges.retrieve).not.toHaveBeenCalled();
|
|
2304
|
+
expect(stripe.transfers.createReversal).not.toHaveBeenCalled();
|
|
2305
|
+
});
|
|
2306
|
+
|
|
2307
|
+
it("does not claw back for a pending refund", async () => {
|
|
2308
|
+
const ctx = makeCtx({ query: ledgerLegs() });
|
|
2309
|
+
const stripe = makeStripe();
|
|
2310
|
+
const whCtx = makeWhCtx({ ctx, stripe });
|
|
2311
|
+
|
|
2312
|
+
await processEvent(whCtx, refundEvent({ status: "pending" }));
|
|
2313
|
+
|
|
2314
|
+
expect(stripe.transfers.createReversal).not.toHaveBeenCalled();
|
|
2315
|
+
});
|
|
2316
|
+
|
|
2317
|
+
it("propagates a reversal failure so the event is marked failed and Stripe retries", async () => {
|
|
2318
|
+
const ctx = makeCtx({ query: ledgerLegs() });
|
|
2319
|
+
const stripe = makeStripe();
|
|
2320
|
+
stripe.charges.retrieve.mockResolvedValue({
|
|
2321
|
+
id: "ch_1",
|
|
2322
|
+
amount: 10000,
|
|
2323
|
+
amount_refunded: 5000,
|
|
2324
|
+
});
|
|
2325
|
+
stripe.transfers.createReversal.mockRejectedValue(new Error("stripe down"));
|
|
2326
|
+
const whCtx = makeWhCtx({ ctx, stripe });
|
|
2327
|
+
|
|
2328
|
+
await expect(processEvent(whCtx, refundEvent())).rejects.toThrow(
|
|
2329
|
+
"stripe down",
|
|
2330
|
+
);
|
|
2331
|
+
});
|
|
2332
|
+
|
|
2333
|
+
it("application_fee.refunded sets absolute fee totals on the linked payment", async () => {
|
|
2334
|
+
const stripe = makeStripe();
|
|
2335
|
+
stripe.charges.retrieve.mockResolvedValue({
|
|
2336
|
+
id: "ch_1",
|
|
2337
|
+
payment_intent: "pi_1",
|
|
2338
|
+
});
|
|
2339
|
+
const whCtx = makeWhCtx({ stripe });
|
|
2340
|
+
|
|
2341
|
+
const feeEvent = event("application_fee.refunded", {
|
|
2342
|
+
id: "fee_1",
|
|
2343
|
+
charge: "ch_1",
|
|
2344
|
+
amount: 320,
|
|
2345
|
+
amount_refunded: 160,
|
|
2346
|
+
currency: "usd",
|
|
2347
|
+
});
|
|
2348
|
+
await processEvent(whCtx, feeEvent);
|
|
2349
|
+
|
|
2350
|
+
const calls = whCtx.ctx.runMutation.mock.calls;
|
|
2351
|
+
const feeCall = calls.find(
|
|
2352
|
+
(c) =>
|
|
2353
|
+
c[0][TO_REF] === "betterStripe/connect/mutations/recordPaymentFeeRefund",
|
|
2354
|
+
);
|
|
2355
|
+
expect(feeCall).toBeDefined();
|
|
2356
|
+
// Absolute cumulative values straight from Stripe — never incremented, so
|
|
2357
|
+
// a redelivery (or a createRefund-triggered duplicate) can't double-count.
|
|
2358
|
+
expect(feeCall![1]).toEqual({
|
|
2359
|
+
stripePaymentIntentId: "pi_1",
|
|
2360
|
+
feeCollectedAmount: 160,
|
|
2361
|
+
feeRefundedAmount: 160,
|
|
2362
|
+
});
|
|
2363
|
+
|
|
2364
|
+
// Redelivery writes the identical absolute state.
|
|
2365
|
+
await processEvent(whCtx, feeEvent);
|
|
2366
|
+
const feeCalls = whCtx.ctx.runMutation.mock.calls.filter(
|
|
2367
|
+
(c) =>
|
|
2368
|
+
c[0][TO_REF] === "betterStripe/connect/mutations/recordPaymentFeeRefund",
|
|
2369
|
+
);
|
|
2370
|
+
expect(feeCalls).toHaveLength(2);
|
|
2371
|
+
expect(feeCalls[1][1]).toEqual(feeCalls[0][1]);
|
|
2372
|
+
});
|
|
2373
|
+
|
|
2374
|
+
it("application_fee.refunded skips quietly when the charge has no payment intent", async () => {
|
|
2375
|
+
const stripe = makeStripe();
|
|
2376
|
+
stripe.charges.retrieve.mockResolvedValue({ id: "ch_1", payment_intent: null });
|
|
2377
|
+
const whCtx = makeWhCtx({ stripe });
|
|
2378
|
+
|
|
2379
|
+
await expect(
|
|
2380
|
+
processEvent(
|
|
2381
|
+
whCtx,
|
|
2382
|
+
event("application_fee.refunded", {
|
|
2383
|
+
id: "fee_1",
|
|
2384
|
+
charge: "ch_1",
|
|
2385
|
+
amount: 320,
|
|
2386
|
+
amount_refunded: 320,
|
|
2387
|
+
}),
|
|
2388
|
+
),
|
|
2389
|
+
).resolves.toBeUndefined();
|
|
2390
|
+
expect(whCtx.ctx.runMutation).not.toHaveBeenCalled();
|
|
2391
|
+
});
|
|
2392
|
+
});
|