@fantastic.dev/better-stripe 0.7.0 → 0.8.1
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 +29 -18
- package/README.md +123 -91
- package/dist/client/billing/checkout.d.ts +1 -1
- package/dist/client/billing/checkout.d.ts.map +1 -1
- package/dist/client/billing/checkout.js +7 -2
- package/dist/client/billing/checkout.js.map +1 -1
- package/dist/client/connect/disputes.d.ts +1 -1
- package/dist/client/connect/disputes.d.ts.map +1 -1
- package/dist/client/connect/disputes.js +12 -6
- package/dist/client/connect/disputes.js.map +1 -1
- package/dist/client/connect/paymentMethods.d.ts.map +1 -1
- package/dist/client/connect/paymentMethods.js.map +1 -1
- package/dist/client/connect/refundActor.d.ts.map +1 -1
- package/dist/client/connect/refundActor.js.map +1 -1
- package/dist/client/connect/refunds.d.ts.map +1 -1
- package/dist/client/connect/refunds.js.map +1 -1
- package/dist/client/connect/transfers.d.ts.map +1 -1
- package/dist/client/connect/transfers.js +15 -4
- package/dist/client/connect/transfers.js.map +1 -1
- package/dist/client/core/accountDefaults.d.ts +6 -0
- package/dist/client/core/accountDefaults.d.ts.map +1 -0
- package/dist/client/core/accountDefaults.js +15 -0
- package/dist/client/core/accountDefaults.js.map +1 -0
- package/dist/client/core/accounts.d.ts +1 -5
- package/dist/client/core/accounts.d.ts.map +1 -1
- package/dist/client/core/accounts.js +2 -14
- package/dist/client/core/accounts.js.map +1 -1
- package/dist/client/core/fees.d.ts.map +1 -1
- package/dist/client/core/fees.js +7 -2
- package/dist/client/core/fees.js.map +1 -1
- package/dist/client/index.d.ts +1 -1
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/products/prices.d.ts.map +1 -1
- package/dist/client/products/prices.js.map +1 -1
- package/dist/client/products/products.d.ts.map +1 -1
- package/dist/client/products/products.js +3 -1
- package/dist/client/products/products.js.map +1 -1
- package/dist/client/utils/owner.d.ts.map +1 -1
- package/dist/client/utils/owner.js +3 -1
- package/dist/client/utils/owner.js.map +1 -1
- package/dist/client/utils/stripeDashboardUrl.d.ts.map +1 -1
- package/dist/client/utils/stripeDashboardUrl.js.map +1 -1
- package/dist/client/webhooks/helpers.d.ts +10 -0
- package/dist/client/webhooks/helpers.d.ts.map +1 -1
- package/dist/client/webhooks/helpers.js +18 -0
- package/dist/client/webhooks/helpers.js.map +1 -1
- package/dist/client/webhooks/processors.d.ts.map +1 -1
- package/dist/client/webhooks/processors.js +1 -19
- package/dist/client/webhooks/processors.js.map +1 -1
- package/dist/component/connect/mutations.d.ts.map +1 -1
- package/dist/component/connect/mutations.js +4 -33
- package/dist/component/connect/mutations.js.map +1 -1
- package/dist/component/connect/schema.d.ts.map +1 -1
- package/dist/component/connect/schema.js.map +1 -1
- package/dist/component/lib/paymentSeller.d.ts +3 -0
- package/dist/component/lib/paymentSeller.d.ts.map +1 -0
- package/dist/component/lib/paymentSeller.js +10 -0
- package/dist/component/lib/paymentSeller.js.map +1 -0
- package/dist/component/lib/reversals.d.ts +10 -0
- package/dist/component/lib/reversals.d.ts.map +1 -1
- package/dist/component/lib/reversals.js +17 -0
- package/dist/component/lib/reversals.js.map +1 -1
- package/dist/react/components/ConnectProvider.d.ts +1 -1
- package/dist/react/components/ConnectProvider.d.ts.map +1 -1
- package/dist/react/components/ConnectProvider.js +18 -4
- package/dist/react/components/ConnectProvider.js.map +1 -1
- package/dist/react/components/ConnectStatusBadge.d.ts.map +1 -1
- package/dist/react/components/ConnectStatusBadge.js.map +1 -1
- package/dist/react/components/DisputeDetail.d.ts +4 -1
- package/dist/react/components/DisputeDetail.d.ts.map +1 -1
- package/dist/react/components/DisputeDetail.js +12 -6
- package/dist/react/components/DisputeDetail.js.map +1 -1
- package/dist/react/components/DisputesList.d.ts.map +1 -1
- package/dist/react/components/DisputesList.js +2 -1
- package/dist/react/components/DisputesList.js.map +1 -1
- package/dist/react/components/EmbeddedDisputes.d.ts +1 -1
- package/dist/react/components/EmbeddedDisputes.d.ts.map +1 -1
- package/dist/react/components/EmbeddedDisputes.js +1 -1
- package/dist/react/components/EmbeddedDisputes.js.map +1 -1
- package/dist/react/components/SplitBreakdown.d.ts.map +1 -1
- package/dist/react/components/SplitBreakdown.js +12 -2
- package/dist/react/components/SplitBreakdown.js.map +1 -1
- package/dist/react/hooks/useCheckoutSession.d.ts.map +1 -1
- package/dist/react/hooks/useCheckoutSession.js.map +1 -1
- package/dist/react/hooks/useConfirmPayment.d.ts.map +1 -1
- package/dist/react/hooks/useConfirmPayment.js.map +1 -1
- package/dist/react/index.d.ts +2 -1
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +3 -1
- package/dist/react/index.js.map +1 -1
- package/dist/react/workflows/Account.d.ts +67 -0
- package/dist/react/workflows/Account.d.ts.map +1 -0
- package/dist/react/workflows/Account.js +74 -0
- package/dist/react/workflows/Account.js.map +1 -0
- package/dist/react/workflows/Billing.d.ts +60 -0
- package/dist/react/workflows/Billing.d.ts.map +1 -0
- package/dist/react/workflows/Billing.js +84 -0
- package/dist/react/workflows/Billing.js.map +1 -0
- package/dist/react/workflows/BillingPlans.d.ts +45 -0
- package/dist/react/workflows/BillingPlans.d.ts.map +1 -0
- package/dist/react/workflows/BillingPlans.js +51 -0
- package/dist/react/workflows/BillingPlans.js.map +1 -0
- package/dist/react/workflows/Checkout.d.ts +46 -0
- package/dist/react/workflows/Checkout.d.ts.map +1 -0
- package/dist/react/workflows/Checkout.js +61 -0
- package/dist/react/workflows/Checkout.js.map +1 -0
- package/dist/react/workflows/CheckoutChooser.d.ts +20 -0
- package/dist/react/workflows/CheckoutChooser.d.ts.map +1 -0
- package/dist/react/workflows/CheckoutChooser.js +24 -0
- package/dist/react/workflows/CheckoutChooser.js.map +1 -0
- package/dist/react/workflows/CheckoutForms.d.ts +26 -0
- package/dist/react/workflows/CheckoutForms.d.ts.map +1 -0
- package/dist/react/workflows/CheckoutForms.js +126 -0
- package/dist/react/workflows/CheckoutForms.js.map +1 -0
- package/dist/react/workflows/ClipboardButton.d.ts +17 -0
- package/dist/react/workflows/ClipboardButton.d.ts.map +1 -0
- package/dist/react/workflows/ClipboardButton.js +35 -0
- package/dist/react/workflows/ClipboardButton.js.map +1 -0
- package/dist/react/workflows/Collection.d.ts +30 -0
- package/dist/react/workflows/Collection.d.ts.map +1 -0
- package/dist/react/workflows/Collection.js +25 -0
- package/dist/react/workflows/Collection.js.map +1 -0
- package/dist/react/workflows/Collections.d.ts +56 -0
- package/dist/react/workflows/Collections.d.ts.map +1 -0
- package/dist/react/workflows/Collections.js +111 -0
- package/dist/react/workflows/Collections.js.map +1 -0
- package/dist/react/workflows/Connect.d.ts +19 -0
- package/dist/react/workflows/Connect.d.ts.map +1 -0
- package/dist/react/workflows/Connect.js +43 -0
- package/dist/react/workflows/Connect.js.map +1 -0
- package/dist/react/workflows/Costs.d.ts +48 -0
- package/dist/react/workflows/Costs.d.ts.map +1 -0
- package/dist/react/workflows/Costs.js +66 -0
- package/dist/react/workflows/Costs.js.map +1 -0
- package/dist/react/workflows/Disputes.d.ts +36 -0
- package/dist/react/workflows/Disputes.d.ts.map +1 -0
- package/dist/react/workflows/Disputes.js +74 -0
- package/dist/react/workflows/Disputes.js.map +1 -0
- package/dist/react/workflows/Earnings.d.ts +42 -0
- package/dist/react/workflows/Earnings.d.ts.map +1 -0
- package/dist/react/workflows/Earnings.js +38 -0
- package/dist/react/workflows/Earnings.js.map +1 -0
- package/dist/react/workflows/Financials.d.ts +43 -0
- package/dist/react/workflows/Financials.d.ts.map +1 -0
- package/dist/react/workflows/Financials.js +57 -0
- package/dist/react/workflows/Financials.js.map +1 -0
- package/dist/react/workflows/IntegrationSwitch.d.ts +23 -0
- package/dist/react/workflows/IntegrationSwitch.d.ts.map +1 -0
- package/dist/react/workflows/IntegrationSwitch.js +31 -0
- package/dist/react/workflows/IntegrationSwitch.js.map +1 -0
- package/dist/react/workflows/Onboarding.d.ts +56 -0
- package/dist/react/workflows/Onboarding.d.ts.map +1 -0
- package/dist/react/workflows/Onboarding.js +86 -0
- package/dist/react/workflows/Onboarding.js.map +1 -0
- package/dist/react/workflows/Overview.d.ts +51 -0
- package/dist/react/workflows/Overview.d.ts.map +1 -0
- package/dist/react/workflows/Overview.js +63 -0
- package/dist/react/workflows/Overview.js.map +1 -0
- package/dist/react/workflows/PaymentMethods.d.ts +35 -0
- package/dist/react/workflows/PaymentMethods.d.ts.map +1 -0
- package/dist/react/workflows/PaymentMethods.js +63 -0
- package/dist/react/workflows/PaymentMethods.js.map +1 -0
- package/dist/react/workflows/PaymentReceipt.d.ts +20 -0
- package/dist/react/workflows/PaymentReceipt.d.ts.map +1 -0
- package/dist/react/workflows/PaymentReceipt.js +24 -0
- package/dist/react/workflows/PaymentReceipt.js.map +1 -0
- package/dist/react/workflows/Payouts.d.ts +48 -0
- package/dist/react/workflows/Payouts.d.ts.map +1 -0
- package/dist/react/workflows/Payouts.js +88 -0
- package/dist/react/workflows/Payouts.js.map +1 -0
- package/dist/react/workflows/Products.d.ts +92 -0
- package/dist/react/workflows/Products.d.ts.map +1 -0
- package/dist/react/workflows/Products.js +179 -0
- package/dist/react/workflows/Products.js.map +1 -0
- package/dist/react/workflows/ReferralLinks.d.ts +26 -0
- package/dist/react/workflows/ReferralLinks.d.ts.map +1 -0
- package/dist/react/workflows/ReferralLinks.js +30 -0
- package/dist/react/workflows/ReferralLinks.js.map +1 -0
- package/dist/react/workflows/Refunds.d.ts +72 -0
- package/dist/react/workflows/Refunds.d.ts.map +1 -0
- package/dist/react/workflows/Refunds.js +95 -0
- package/dist/react/workflows/Refunds.js.map +1 -0
- package/dist/react/workflows/Setup.d.ts +50 -0
- package/dist/react/workflows/Setup.d.ts.map +1 -0
- package/dist/react/workflows/Setup.js +59 -0
- package/dist/react/workflows/Setup.js.map +1 -0
- package/dist/react/workflows/Storefront.d.ts +53 -0
- package/dist/react/workflows/Storefront.d.ts.map +1 -0
- package/dist/react/workflows/Storefront.js +44 -0
- package/dist/react/workflows/Storefront.js.map +1 -0
- package/dist/react/workflows/SubscriptionOverview.d.ts +13 -0
- package/dist/react/workflows/SubscriptionOverview.d.ts.map +1 -0
- package/dist/react/workflows/SubscriptionOverview.js +26 -0
- package/dist/react/workflows/SubscriptionOverview.js.map +1 -0
- package/dist/react/workflows/Subscriptions.d.ts +92 -0
- package/dist/react/workflows/Subscriptions.d.ts.map +1 -0
- package/dist/react/workflows/Subscriptions.js +149 -0
- package/dist/react/workflows/Subscriptions.js.map +1 -0
- package/dist/react/workflows/Tasks.d.ts +40 -0
- package/dist/react/workflows/Tasks.d.ts.map +1 -0
- package/dist/react/workflows/Tasks.js +113 -0
- package/dist/react/workflows/Tasks.js.map +1 -0
- package/dist/react/workflows/checkout-options.d.ts +37 -0
- package/dist/react/workflows/checkout-options.d.ts.map +1 -0
- package/dist/react/workflows/checkout-options.js +43 -0
- package/dist/react/workflows/checkout-options.js.map +1 -0
- package/dist/react/workflows/current-subscription.d.ts +11 -0
- package/dist/react/workflows/current-subscription.d.ts.map +1 -0
- package/dist/react/workflows/current-subscription.js +14 -0
- package/dist/react/workflows/current-subscription.js.map +1 -0
- package/dist/react/workflows/filters.d.ts +17 -0
- package/dist/react/workflows/filters.d.ts.map +1 -0
- package/dist/react/workflows/filters.js +23 -0
- package/dist/react/workflows/filters.js.map +1 -0
- package/dist/react/workflows/index.d.ts +70 -0
- package/dist/react/workflows/index.d.ts.map +1 -0
- package/dist/react/workflows/index.js +38 -0
- package/dist/react/workflows/index.js.map +1 -0
- package/dist/react/workflows/useWorkflowAction.d.ts +11 -0
- package/dist/react/workflows/useWorkflowAction.d.ts.map +1 -0
- package/dist/react/workflows/useWorkflowAction.js +68 -0
- package/dist/react/workflows/useWorkflowAction.js.map +1 -0
- package/dist/react/workflows/useWorkflowResource.d.ts +9 -0
- package/dist/react/workflows/useWorkflowResource.d.ts.map +1 -0
- package/dist/react/workflows/useWorkflowResource.js +67 -0
- package/dist/react/workflows/useWorkflowResource.js.map +1 -0
- package/docs/react-workflows-audit.md +88 -0
- package/docs/react-workflows.md +234 -0
- package/package.json +19 -3
- package/src/client/billing/checkout-ui-modes.test.ts +49 -0
- package/src/client/billing/checkout.ts +8 -3
- package/src/client/billing/invoices.test.ts +12 -6
- package/src/client/connect/disputes.test.ts +15 -0
- package/src/client/connect/disputes.ts +13 -7
- package/src/client/connect/paymentMethods.test.ts +3 -1
- package/src/client/connect/paymentMethods.ts +4 -1
- package/src/client/connect/refundActor.ts +1 -2
- package/src/client/connect/refunds.test.ts +35 -10
- package/src/client/connect/refunds.ts +4 -1
- package/src/client/connect/transfers.test.ts +66 -12
- package/src/client/connect/transfers.ts +32 -8
- package/src/client/core/accountDefaults.ts +15 -0
- package/src/client/core/accounts.ts +14 -17
- package/src/client/core/config.test.ts +2 -1
- package/src/client/core/descriptors.test.ts +6 -4
- package/src/client/core/fees.test.ts +20 -28
- package/src/client/core/fees.ts +9 -2
- package/src/client/index.test.ts +22 -22
- package/src/client/index.ts +1 -1
- package/src/client/products/prices.test.ts +8 -2
- package/src/client/products/prices.ts +4 -1
- package/src/client/products/products.test.ts +61 -20
- package/src/client/products/products.ts +3 -1
- package/src/client/utils/data.test.ts +3 -1
- package/src/client/utils/owner.test.ts +4 -1
- package/src/client/utils/owner.ts +5 -1
- package/src/client/utils/stripeDashboardUrl.ts +1 -4
- package/src/client/webhooks/handler.test.ts +16 -19
- package/src/client/webhooks/helpers.test.ts +26 -13
- package/src/client/webhooks/helpers.ts +23 -0
- package/src/client/webhooks/hooks.test.ts +1 -2
- package/src/client/webhooks/processors.test.ts +18 -34
- package/src/client/webhooks/processors.ts +1 -23
- package/src/client/webhooks.test.ts +1 -2
- package/src/component/_generated/api.ts +2 -0
- package/src/component/connect/mutations.ts +5 -46
- package/src/component/connect/schema.ts +4 -3
- package/src/component/connect.test.ts +1 -5
- package/src/component/lib/paymentSeller.ts +15 -0
- package/src/component/lib/reversals.ts +30 -2
- package/src/component/subscription-scoping.test.ts +5 -1
- package/src/component/triggers.test.ts +7 -4
- package/src/react/components/BuyerBillingView.render.test.tsx +7 -2
- package/src/react/components/ConnectProvider.live.test.tsx +61 -0
- package/src/react/components/ConnectProvider.tsx +18 -4
- package/src/react/components/ConnectStatusBadge.tsx +1 -4
- package/src/react/components/DisputeDetail.render.test.tsx +5 -1
- package/src/react/components/DisputeDetail.tsx +14 -5
- package/src/react/components/DisputesList.render.test.tsx +21 -0
- package/src/react/components/DisputesList.tsx +2 -1
- package/src/react/components/EarningsSummary.render.test.tsx +2 -6
- package/src/react/components/EmbeddedDisputes.render.test.tsx +7 -5
- package/src/react/components/EmbeddedDisputes.tsx +4 -1
- package/src/react/components/EvidenceForm.render.test.tsx +3 -10
- package/src/react/components/SplitBreakdown.render.test.tsx +28 -5
- package/src/react/components/SplitBreakdown.tsx +12 -2
- package/src/react/hooks/useCheckoutSession.ts +1 -2
- package/src/react/hooks/useConfirmPayment.ts +1 -2
- package/src/react/hooks/useEarnings.test.ts +43 -8
- package/src/react/hooks/useStripeMode.test.ts +5 -8
- package/src/react/index.ts +7 -1
- package/src/react/workflows/Account.test.tsx +31 -0
- package/src/react/workflows/Account.tsx +183 -0
- package/src/react/workflows/Billing.test.tsx +45 -0
- package/src/react/workflows/Billing.tsx +213 -0
- package/src/react/workflows/BillingPlans.test.tsx +31 -0
- package/src/react/workflows/BillingPlans.tsx +130 -0
- package/src/react/workflows/Checkout.test.tsx +118 -0
- package/src/react/workflows/Checkout.tsx +135 -0
- package/src/react/workflows/CheckoutChooser.test.tsx +44 -0
- package/src/react/workflows/CheckoutChooser.tsx +77 -0
- package/src/react/workflows/CheckoutForm.test.tsx +66 -0
- package/src/react/workflows/CheckoutForms.test.tsx +123 -0
- package/src/react/workflows/CheckoutForms.tsx +247 -0
- package/src/react/workflows/ClipboardButton.test.tsx +34 -0
- package/src/react/workflows/ClipboardButton.tsx +67 -0
- package/src/react/workflows/Collection.tsx +76 -0
- package/src/react/workflows/Collections.test.tsx +86 -0
- package/src/react/workflows/Collections.tsx +213 -0
- package/src/react/workflows/Connect.test.tsx +50 -0
- package/src/react/workflows/Connect.tsx +107 -0
- package/src/react/workflows/Costs.test.tsx +41 -0
- package/src/react/workflows/Costs.tsx +133 -0
- package/src/react/workflows/Disputes.test.tsx +64 -0
- package/src/react/workflows/Disputes.tsx +173 -0
- package/src/react/workflows/Earnings.test.ts +19 -0
- package/src/react/workflows/Earnings.tsx +84 -0
- package/src/react/workflows/Financials.test.tsx +35 -0
- package/src/react/workflows/Financials.tsx +117 -0
- package/src/react/workflows/IntegrationSwitch.test.tsx +36 -0
- package/src/react/workflows/IntegrationSwitch.tsx +63 -0
- package/src/react/workflows/Onboarding.test.tsx +75 -0
- package/src/react/workflows/Onboarding.tsx +171 -0
- package/src/react/workflows/Overview.test.tsx +57 -0
- package/src/react/workflows/Overview.tsx +123 -0
- package/src/react/workflows/PaymentMethods.test.tsx +60 -0
- package/src/react/workflows/PaymentMethods.tsx +117 -0
- package/src/react/workflows/PaymentReceipt.test.tsx +24 -0
- package/src/react/workflows/PaymentReceipt.tsx +46 -0
- package/src/react/workflows/Payouts.test.tsx +112 -0
- package/src/react/workflows/Payouts.tsx +215 -0
- package/src/react/workflows/Products.test.tsx +90 -0
- package/src/react/workflows/Products.tsx +377 -0
- package/src/react/workflows/ReferralLinks.test.tsx +39 -0
- package/src/react/workflows/ReferralLinks.tsx +75 -0
- package/src/react/workflows/Refunds.test.tsx +75 -0
- package/src/react/workflows/Refunds.tsx +223 -0
- package/src/react/workflows/Setup.test.tsx +25 -0
- package/src/react/workflows/Setup.tsx +166 -0
- package/src/react/workflows/Storefront.test.tsx +82 -0
- package/src/react/workflows/Storefront.tsx +138 -0
- package/src/react/workflows/SubscriptionOverview.test.tsx +27 -0
- package/src/react/workflows/SubscriptionOverview.tsx +48 -0
- package/src/react/workflows/Subscriptions.test.tsx +61 -0
- package/src/react/workflows/Subscriptions.tsx +282 -0
- package/src/react/workflows/Tasks.test.tsx +23 -0
- package/src/react/workflows/Tasks.tsx +199 -0
- package/src/react/workflows/checkout-options.ts +42 -0
- package/src/react/workflows/current-subscription.test.ts +29 -0
- package/src/react/workflows/current-subscription.ts +25 -0
- package/src/react/workflows/filters.test.ts +12 -0
- package/src/react/workflows/filters.ts +22 -0
- package/src/react/workflows/index.ts +238 -0
- package/src/react/workflows/native-interactions.test.tsx +73 -0
- package/src/react/workflows/public-workflows.test.tsx +216 -0
- package/src/react/workflows/useWorkflowAction.test.tsx +54 -0
- package/src/react/workflows/useWorkflowAction.ts +75 -0
- package/src/react/workflows/useWorkflowResource.test.tsx +41 -0
- package/src/react/workflows/useWorkflowResource.ts +78 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useState, useId, type ReactNode } from "react";
|
|
3
|
+
import { useWorkflowAction } from "./useWorkflowAction.js";
|
|
4
|
+
import { Connect, type ConnectProps } from "./Connect.js";
|
|
5
|
+
export type RefundRequest = {
|
|
6
|
+
stripePaymentIntentId: string;
|
|
7
|
+
amountCents?: number;
|
|
8
|
+
reason: "requested_by_customer";
|
|
9
|
+
};
|
|
10
|
+
export type ReversalRequest = { sourceChargeId: string; amountCents?: number };
|
|
11
|
+
export type RefundResult = { stripeRefundId: string };
|
|
12
|
+
export type ReversalResult = {
|
|
13
|
+
reversals: { stripeTransferId: string; amount: number }[];
|
|
14
|
+
summary: { reversalCount: number; totalReversed: number };
|
|
15
|
+
};
|
|
16
|
+
export type RefundActionsProps = {
|
|
17
|
+
scopeKey: string;
|
|
18
|
+
paymentIntentId?: string;
|
|
19
|
+
sourceChargeId?: string;
|
|
20
|
+
issueRefund: (args: RefundRequest) => Promise<RefundResult>;
|
|
21
|
+
reverseTransfers?: (args: ReversalRequest) => Promise<ReversalResult>;
|
|
22
|
+
};
|
|
23
|
+
/** Currency input expressed in two decimal minor units. Rejects rounding and scientific notation. */
|
|
24
|
+
export function parseAmountCents(value: string): number | null {
|
|
25
|
+
if (!/^\d+(?:\.\d{1,2})?$/.test(value.trim())) return null;
|
|
26
|
+
const [whole, fraction = ""] = value.trim().split(".");
|
|
27
|
+
const amount = Number(whole) * 100 + Number(fraction.padEnd(2, "0"));
|
|
28
|
+
return Number.isSafeInteger(amount) && amount > 0 ? amount : null;
|
|
29
|
+
}
|
|
30
|
+
export function useRefunds(props: RefundActionsProps) {
|
|
31
|
+
const initial = {
|
|
32
|
+
scope: props.scopeKey,
|
|
33
|
+
paymentIntentId: props.paymentIntentId ?? "",
|
|
34
|
+
sourceChargeId: props.sourceChargeId ?? "",
|
|
35
|
+
refundAmount: "",
|
|
36
|
+
reversalAmount: "",
|
|
37
|
+
};
|
|
38
|
+
const [fields, setFields] = useState(initial);
|
|
39
|
+
if (fields.scope !== props.scopeKey) setFields(initial);
|
|
40
|
+
const [operationType, setOperationType] = useState<"refund" | "reversal">(
|
|
41
|
+
"refund",
|
|
42
|
+
);
|
|
43
|
+
const action = useWorkflowAction(
|
|
44
|
+
async (operation: {
|
|
45
|
+
type: "refund" | "reversal";
|
|
46
|
+
kind: "full" | "partial";
|
|
47
|
+
}) => {
|
|
48
|
+
setOperationType(operation.type);
|
|
49
|
+
const id = (
|
|
50
|
+
operation.type === "refund"
|
|
51
|
+
? fields.paymentIntentId
|
|
52
|
+
: fields.sourceChargeId
|
|
53
|
+
).trim();
|
|
54
|
+
if (!id)
|
|
55
|
+
throw new Error(
|
|
56
|
+
operation.type === "refund"
|
|
57
|
+
? "Enter a PaymentIntent id before issuing a refund."
|
|
58
|
+
: "Enter a source charge id before reversing transfers.",
|
|
59
|
+
);
|
|
60
|
+
const amount =
|
|
61
|
+
operation.kind === "partial"
|
|
62
|
+
? parseAmountCents(
|
|
63
|
+
operation.type === "refund"
|
|
64
|
+
? fields.refundAmount
|
|
65
|
+
: fields.reversalAmount,
|
|
66
|
+
)
|
|
67
|
+
: undefined;
|
|
68
|
+
if (amount === null)
|
|
69
|
+
throw new Error(
|
|
70
|
+
`Enter a positive partial ${operation.type === "refund" ? "refund" : "reversal"} amount with at most two decimal places.`,
|
|
71
|
+
);
|
|
72
|
+
if (operation.type === "refund")
|
|
73
|
+
return {
|
|
74
|
+
type: "refund" as const,
|
|
75
|
+
value: await props.issueRefund({
|
|
76
|
+
stripePaymentIntentId: id,
|
|
77
|
+
amountCents: amount,
|
|
78
|
+
reason: "requested_by_customer",
|
|
79
|
+
}),
|
|
80
|
+
};
|
|
81
|
+
if (!props.reverseTransfers)
|
|
82
|
+
throw new Error("Transfer reversals are not configured.");
|
|
83
|
+
return {
|
|
84
|
+
type: "reversal" as const,
|
|
85
|
+
value: await props.reverseTransfers({
|
|
86
|
+
sourceChargeId: id,
|
|
87
|
+
amountCents: amount,
|
|
88
|
+
}),
|
|
89
|
+
};
|
|
90
|
+
},
|
|
91
|
+
props.scopeKey,
|
|
92
|
+
);
|
|
93
|
+
const set = (
|
|
94
|
+
name:
|
|
95
|
+
"paymentIntentId" | "sourceChargeId" | "refundAmount" | "reversalAmount",
|
|
96
|
+
value: string,
|
|
97
|
+
) => setFields((previous) => ({ ...previous, [name]: value }));
|
|
98
|
+
return {
|
|
99
|
+
...fields,
|
|
100
|
+
setPaymentIntentId: (value: string) => set("paymentIntentId", value),
|
|
101
|
+
setSourceChargeId: (value: string) => set("sourceChargeId", value),
|
|
102
|
+
setRefundAmount: (value: string) => set("refundAmount", value),
|
|
103
|
+
setReversalAmount: (value: string) => set("reversalAmount", value),
|
|
104
|
+
error: action.error,
|
|
105
|
+
isPending: action.isPending,
|
|
106
|
+
busy: action.isPending ? operationType : null,
|
|
107
|
+
refundResult: action.result?.type === "refund" ? action.result.value : null,
|
|
108
|
+
reversalResult:
|
|
109
|
+
action.result?.type === "reversal" ? action.result.value : null,
|
|
110
|
+
submitRefund: (kind: "full" | "partial") =>
|
|
111
|
+
action.run({ type: "refund", kind }),
|
|
112
|
+
submitReversal: (kind: "full" | "partial") =>
|
|
113
|
+
action.run({ type: "reversal", kind }),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
export type RefundsRenderProps = ReturnType<typeof useRefunds>;
|
|
117
|
+
export type RefundsProps =
|
|
118
|
+
| (RefundActionsProps & {
|
|
119
|
+
presentation?: "headless";
|
|
120
|
+
children?: (state: RefundsRenderProps) => ReactNode;
|
|
121
|
+
})
|
|
122
|
+
| (Omit<ConnectProps, "component"> & { presentation: "embedded" });
|
|
123
|
+
export function Refunds(props: RefundsProps) {
|
|
124
|
+
return props.presentation === "embedded" ? (
|
|
125
|
+
<Connect {...props} component="refunds" />
|
|
126
|
+
) : (
|
|
127
|
+
<RefundsBody key={props.scopeKey} {...props} />
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
function RefundsBody(
|
|
131
|
+
props: RefundActionsProps & {
|
|
132
|
+
children?: (state: RefundsRenderProps) => ReactNode;
|
|
133
|
+
},
|
|
134
|
+
) {
|
|
135
|
+
const state = useRefunds(props);
|
|
136
|
+
const id = useId();
|
|
137
|
+
if (props.children) return <>{props.children(state)}</>;
|
|
138
|
+
return (
|
|
139
|
+
<section data-better-stripe="refunds" aria-busy={state.isPending}>
|
|
140
|
+
<h2>Refunds</h2>
|
|
141
|
+
{state.error && <p role="alert">{state.error}</p>}
|
|
142
|
+
<label htmlFor={`${id}-payment`}>PaymentIntent ID</label>
|
|
143
|
+
<input
|
|
144
|
+
id={`${id}-payment`}
|
|
145
|
+
value={state.paymentIntentId}
|
|
146
|
+
onChange={(event) => state.setPaymentIntentId(event.target.value)}
|
|
147
|
+
/>
|
|
148
|
+
<label htmlFor={`${id}-amount`}>Partial refund amount</label>
|
|
149
|
+
<input
|
|
150
|
+
id={`${id}-amount`}
|
|
151
|
+
type="number"
|
|
152
|
+
min="0.01"
|
|
153
|
+
step="0.01"
|
|
154
|
+
value={state.refundAmount}
|
|
155
|
+
onChange={(event) => state.setRefundAmount(event.target.value)}
|
|
156
|
+
/>
|
|
157
|
+
<button
|
|
158
|
+
type="button"
|
|
159
|
+
disabled={state.isPending}
|
|
160
|
+
onClick={() => {
|
|
161
|
+
void state.submitRefund("full");
|
|
162
|
+
}}
|
|
163
|
+
>
|
|
164
|
+
{state.isPending ? "Processing…" : "Full refund"}
|
|
165
|
+
</button>
|
|
166
|
+
<button
|
|
167
|
+
type="button"
|
|
168
|
+
disabled={state.isPending}
|
|
169
|
+
onClick={() => {
|
|
170
|
+
void state.submitRefund("partial");
|
|
171
|
+
}}
|
|
172
|
+
>
|
|
173
|
+
Partial refund
|
|
174
|
+
</button>
|
|
175
|
+
{state.refundResult && (
|
|
176
|
+
<p role="status">Refund created: {state.refundResult.stripeRefundId}</p>
|
|
177
|
+
)}
|
|
178
|
+
{props.reverseTransfers && (
|
|
179
|
+
<>
|
|
180
|
+
<h3>Transfer reversals</h3>
|
|
181
|
+
<label htmlFor={`${id}-charge`}>Source charge ID</label>
|
|
182
|
+
<input
|
|
183
|
+
id={`${id}-charge`}
|
|
184
|
+
value={state.sourceChargeId}
|
|
185
|
+
onChange={(event) => state.setSourceChargeId(event.target.value)}
|
|
186
|
+
/>
|
|
187
|
+
<label htmlFor={`${id}-reversal`}>Partial reversal amount</label>
|
|
188
|
+
<input
|
|
189
|
+
id={`${id}-reversal`}
|
|
190
|
+
type="number"
|
|
191
|
+
min="0.01"
|
|
192
|
+
step="0.01"
|
|
193
|
+
value={state.reversalAmount}
|
|
194
|
+
onChange={(event) => state.setReversalAmount(event.target.value)}
|
|
195
|
+
/>
|
|
196
|
+
<button
|
|
197
|
+
type="button"
|
|
198
|
+
disabled={state.isPending}
|
|
199
|
+
onClick={() => {
|
|
200
|
+
void state.submitReversal("full");
|
|
201
|
+
}}
|
|
202
|
+
>
|
|
203
|
+
Reverse all transfers
|
|
204
|
+
</button>
|
|
205
|
+
<button
|
|
206
|
+
type="button"
|
|
207
|
+
disabled={state.isPending}
|
|
208
|
+
onClick={() => {
|
|
209
|
+
void state.submitReversal("partial");
|
|
210
|
+
}}
|
|
211
|
+
>
|
|
212
|
+
Partial reversal
|
|
213
|
+
</button>
|
|
214
|
+
{state.reversalResult && (
|
|
215
|
+
<p role="status">
|
|
216
|
+
Reversed {state.reversalResult.summary.reversalCount} transfers.
|
|
217
|
+
</p>
|
|
218
|
+
)}
|
|
219
|
+
</>
|
|
220
|
+
)}
|
|
221
|
+
</section>
|
|
222
|
+
);
|
|
223
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { act, cleanup, renderHook, waitFor } from "@testing-library/react";
|
|
3
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
4
|
+
import { useWebhookSetup } from "./Setup.js";
|
|
5
|
+
afterEach(cleanup);
|
|
6
|
+
it("refreshes destination status after setup and keeps setup errors available for retry", async () => {
|
|
7
|
+
const load = vi
|
|
8
|
+
.fn()
|
|
9
|
+
.mockResolvedValueOnce({ configured: false })
|
|
10
|
+
.mockResolvedValueOnce({ configured: true });
|
|
11
|
+
const setup = vi.fn().mockResolvedValue({ id: "we_test" });
|
|
12
|
+
const { result } = renderHook(() =>
|
|
13
|
+
useWebhookSetup({ scopeKey: "site", load, setup }),
|
|
14
|
+
);
|
|
15
|
+
await waitFor(() =>
|
|
16
|
+
expect(result.current.status).toEqual({ configured: false }),
|
|
17
|
+
);
|
|
18
|
+
await act(async () => {
|
|
19
|
+
await result.current.configure();
|
|
20
|
+
});
|
|
21
|
+
await waitFor(() =>
|
|
22
|
+
expect(result.current.status).toEqual({ configured: true }),
|
|
23
|
+
);
|
|
24
|
+
expect(result.current.result).toEqual({ id: "we_test" });
|
|
25
|
+
});
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
import { useWorkflowAction } from "./useWorkflowAction.js";
|
|
4
|
+
import { useWorkflowResource } from "./useWorkflowResource.js";
|
|
5
|
+
export type WebhookSetupOptions<Status, Result> = {
|
|
6
|
+
scopeKey: string;
|
|
7
|
+
load: () => Promise<Status>;
|
|
8
|
+
setup: () => Promise<Result>;
|
|
9
|
+
};
|
|
10
|
+
export function useWebhookSetup<Status, Result>(
|
|
11
|
+
props: WebhookSetupOptions<Status, Result>,
|
|
12
|
+
) {
|
|
13
|
+
const status = useWorkflowResource(props.load, props.scopeKey);
|
|
14
|
+
const setup = useWorkflowAction(
|
|
15
|
+
async () => ({ value: await props.setup() }),
|
|
16
|
+
props.scopeKey,
|
|
17
|
+
);
|
|
18
|
+
return {
|
|
19
|
+
status: status.data,
|
|
20
|
+
isChecking: status.isLoading,
|
|
21
|
+
statusError: status.error,
|
|
22
|
+
checkStatus: status.refresh,
|
|
23
|
+
result: setup.result?.value,
|
|
24
|
+
isPending: setup.isPending,
|
|
25
|
+
error: setup.error,
|
|
26
|
+
configure: async () => {
|
|
27
|
+
if (await setup.run()) status.refresh();
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export function WebhookSetup<Status, Result>(
|
|
32
|
+
props: WebhookSetupOptions<Status, Result> & {
|
|
33
|
+
renderStatus?: (status: Status) => ReactNode;
|
|
34
|
+
children?: (
|
|
35
|
+
state: ReturnType<typeof useWebhookSetup<Status, Result>>,
|
|
36
|
+
) => ReactNode;
|
|
37
|
+
},
|
|
38
|
+
) {
|
|
39
|
+
const state = useWebhookSetup(props);
|
|
40
|
+
if (props.children) return <>{props.children(state)}</>;
|
|
41
|
+
return (
|
|
42
|
+
<section data-better-stripe="webhook-setup">
|
|
43
|
+
<h2>Webhook destinations</h2>
|
|
44
|
+
{(state.error || state.statusError) && (
|
|
45
|
+
<p role="alert">{state.error || state.statusError}</p>
|
|
46
|
+
)}
|
|
47
|
+
{state.isChecking ? (
|
|
48
|
+
<p role="status">Checking destinations…</p>
|
|
49
|
+
) : state.status !== undefined && props.renderStatus ? (
|
|
50
|
+
props.renderStatus(state.status)
|
|
51
|
+
) : (
|
|
52
|
+
<p>Destination status loaded.</p>
|
|
53
|
+
)}
|
|
54
|
+
<button
|
|
55
|
+
type="button"
|
|
56
|
+
disabled={state.isPending || state.isChecking}
|
|
57
|
+
onClick={() => {
|
|
58
|
+
void state.configure();
|
|
59
|
+
}}
|
|
60
|
+
>
|
|
61
|
+
{state.isPending ? "Configuring…" : "Configure webhooks"}
|
|
62
|
+
</button>
|
|
63
|
+
<button
|
|
64
|
+
type="button"
|
|
65
|
+
disabled={state.isChecking}
|
|
66
|
+
onClick={state.checkStatus}
|
|
67
|
+
>
|
|
68
|
+
Refresh status
|
|
69
|
+
</button>
|
|
70
|
+
</section>
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
export type EnvironmentCheck = {
|
|
74
|
+
id: string;
|
|
75
|
+
label: string;
|
|
76
|
+
configured: boolean;
|
|
77
|
+
};
|
|
78
|
+
export function EnvironmentStatus({
|
|
79
|
+
checks,
|
|
80
|
+
isLoading = checks === undefined,
|
|
81
|
+
error,
|
|
82
|
+
children,
|
|
83
|
+
}: {
|
|
84
|
+
checks: EnvironmentCheck[] | undefined;
|
|
85
|
+
isLoading?: boolean;
|
|
86
|
+
error?: string | null;
|
|
87
|
+
children?: (state: {
|
|
88
|
+
checks: EnvironmentCheck[];
|
|
89
|
+
allConfigured: boolean;
|
|
90
|
+
isLoading: boolean;
|
|
91
|
+
error: string | null;
|
|
92
|
+
}) => ReactNode;
|
|
93
|
+
}) {
|
|
94
|
+
const state = {
|
|
95
|
+
checks: checks ?? [],
|
|
96
|
+
allConfigured:
|
|
97
|
+
!!checks?.length && checks.every((check) => check.configured),
|
|
98
|
+
isLoading,
|
|
99
|
+
error: error ?? null,
|
|
100
|
+
};
|
|
101
|
+
if (children) return <>{children(state)}</>;
|
|
102
|
+
return (
|
|
103
|
+
<section data-better-stripe="environment-status">
|
|
104
|
+
<h2>Environment</h2>
|
|
105
|
+
{error ? (
|
|
106
|
+
<p role="alert">{error}</p>
|
|
107
|
+
) : isLoading ? (
|
|
108
|
+
<p role="status">Checking configuration…</p>
|
|
109
|
+
) : !checks?.length ? (
|
|
110
|
+
<p>No environment checks configured.</p>
|
|
111
|
+
) : (
|
|
112
|
+
<ul>
|
|
113
|
+
{checks.map((check) => (
|
|
114
|
+
<li key={check.id}>
|
|
115
|
+
{check.label}: {check.configured ? "Set" : "Not set"}
|
|
116
|
+
</li>
|
|
117
|
+
))}
|
|
118
|
+
</ul>
|
|
119
|
+
)}
|
|
120
|
+
</section>
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
export type FixtureProgressState = {
|
|
124
|
+
cursor: number;
|
|
125
|
+
total: number;
|
|
126
|
+
status: string;
|
|
127
|
+
error?: string;
|
|
128
|
+
};
|
|
129
|
+
export function FixtureProgress({
|
|
130
|
+
state,
|
|
131
|
+
children,
|
|
132
|
+
}: {
|
|
133
|
+
state: FixtureProgressState | null | undefined;
|
|
134
|
+
children?: (
|
|
135
|
+
state: FixtureProgressState & { progress: number; message: string },
|
|
136
|
+
) => ReactNode;
|
|
137
|
+
}) {
|
|
138
|
+
if (!state) return <p>No fixture run yet.</p>;
|
|
139
|
+
const result = {
|
|
140
|
+
...state,
|
|
141
|
+
progress:
|
|
142
|
+
state.total > 0
|
|
143
|
+
? Math.max(0, Math.min(1, state.cursor / state.total))
|
|
144
|
+
: 0,
|
|
145
|
+
message:
|
|
146
|
+
state.status === "running"
|
|
147
|
+
? "Fixtures are being created. Stripe events continue to update your data."
|
|
148
|
+
: state.status === "complete"
|
|
149
|
+
? "Fixtures are populated. Stripe statuses can continue updating through webhooks."
|
|
150
|
+
: "Population stopped. Resume to retry this step.",
|
|
151
|
+
};
|
|
152
|
+
if (children) return <>{children(result)}</>;
|
|
153
|
+
return (
|
|
154
|
+
<div data-better-stripe="fixture-progress">
|
|
155
|
+
<progress
|
|
156
|
+
aria-label="Sandbox fixture progress"
|
|
157
|
+
value={result.progress}
|
|
158
|
+
max={1}
|
|
159
|
+
/>
|
|
160
|
+
<p role="status">
|
|
161
|
+
{state.cursor} of {state.total} steps complete. {result.message}
|
|
162
|
+
</p>
|
|
163
|
+
{state.error && <p role="alert">{state.error}</p>}
|
|
164
|
+
</div>
|
|
165
|
+
);
|
|
166
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { cleanup, render, screen } from "@testing-library/react";
|
|
3
|
+
import userEvent from "@testing-library/user-event";
|
|
4
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
5
|
+
import { Storefront } from "./Storefront.js";
|
|
6
|
+
afterEach(cleanup);
|
|
7
|
+
it("preserves product, store, and referral attribution when selecting a price", async () => {
|
|
8
|
+
const onSelectPrice = vi.fn();
|
|
9
|
+
render(
|
|
10
|
+
<Storefront
|
|
11
|
+
stores={[
|
|
12
|
+
{
|
|
13
|
+
accountId: "acct_one",
|
|
14
|
+
name: "Independent Store",
|
|
15
|
+
products: [
|
|
16
|
+
{
|
|
17
|
+
stripeProductId: "prod_one",
|
|
18
|
+
name: "Tee",
|
|
19
|
+
prices: [
|
|
20
|
+
{
|
|
21
|
+
stripePriceId: "price_one",
|
|
22
|
+
unitAmount: 2900,
|
|
23
|
+
currency: "usd",
|
|
24
|
+
type: "one_time",
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
},
|
|
30
|
+
]}
|
|
31
|
+
referralCode="avery"
|
|
32
|
+
onSelectPrice={onSelectPrice}
|
|
33
|
+
/>,
|
|
34
|
+
);
|
|
35
|
+
await userEvent.click(screen.getByRole("button", { name: "Buy Tee" }));
|
|
36
|
+
expect(onSelectPrice).toHaveBeenCalledWith(
|
|
37
|
+
expect.objectContaining({
|
|
38
|
+
store: expect.objectContaining({ accountId: "acct_one" }),
|
|
39
|
+
price: expect.objectContaining({ stripePriceId: "price_one" }),
|
|
40
|
+
referralCode: "avery",
|
|
41
|
+
}),
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
it("does not offer inactive prices or purchases when the application disables buying", () => {
|
|
45
|
+
render(
|
|
46
|
+
<Storefront
|
|
47
|
+
canPurchase={false}
|
|
48
|
+
stores={[
|
|
49
|
+
{
|
|
50
|
+
accountId: "acct_one",
|
|
51
|
+
name: "Store",
|
|
52
|
+
products: [
|
|
53
|
+
{
|
|
54
|
+
stripeProductId: "prod_one",
|
|
55
|
+
name: "Tee",
|
|
56
|
+
prices: [
|
|
57
|
+
{
|
|
58
|
+
stripePriceId: "price_old",
|
|
59
|
+
active: false,
|
|
60
|
+
unitAmount: 100,
|
|
61
|
+
currency: "usd",
|
|
62
|
+
type: "one_time",
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
stripePriceId: "price_one",
|
|
66
|
+
unitAmount: 2900,
|
|
67
|
+
currency: "usd",
|
|
68
|
+
type: "one_time",
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
},
|
|
74
|
+
]}
|
|
75
|
+
/>,
|
|
76
|
+
);
|
|
77
|
+
expect(
|
|
78
|
+
(screen.getByRole("button", { name: "Buy Tee" }) as HTMLButtonElement)
|
|
79
|
+
.disabled,
|
|
80
|
+
).toBe(true);
|
|
81
|
+
expect(screen.getAllByRole("button")).toHaveLength(1);
|
|
82
|
+
});
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
import { formatPriceWithInterval } from "../lib/price-helpers.js";
|
|
4
|
+
export type StorefrontPrice = {
|
|
5
|
+
stripePriceId: string;
|
|
6
|
+
unitAmount: number;
|
|
7
|
+
currency: string;
|
|
8
|
+
type: string;
|
|
9
|
+
interval?: string | null;
|
|
10
|
+
intervalCount?: number | null;
|
|
11
|
+
active?: boolean;
|
|
12
|
+
};
|
|
13
|
+
export type StorefrontProduct = {
|
|
14
|
+
stripeProductId: string;
|
|
15
|
+
name: string;
|
|
16
|
+
description?: string | null;
|
|
17
|
+
active?: boolean;
|
|
18
|
+
prices: StorefrontPrice[];
|
|
19
|
+
};
|
|
20
|
+
export type StorefrontStore = {
|
|
21
|
+
accountId: string;
|
|
22
|
+
name: string;
|
|
23
|
+
products: StorefrontProduct[];
|
|
24
|
+
};
|
|
25
|
+
export type StorefrontSelection = {
|
|
26
|
+
store: StorefrontStore;
|
|
27
|
+
product: StorefrontProduct;
|
|
28
|
+
price: StorefrontPrice;
|
|
29
|
+
referralCode?: string;
|
|
30
|
+
};
|
|
31
|
+
export type StorefrontCard = StorefrontSelection & {
|
|
32
|
+
formattedPrice: string;
|
|
33
|
+
purchaseLabel: string;
|
|
34
|
+
accessibilityLabel: string;
|
|
35
|
+
checkoutUrl?: string;
|
|
36
|
+
purchase: () => void;
|
|
37
|
+
};
|
|
38
|
+
export type StorefrontRenderProps = {
|
|
39
|
+
stores: StorefrontStore[];
|
|
40
|
+
cards: StorefrontCard[];
|
|
41
|
+
isLoading: boolean;
|
|
42
|
+
isEmpty: boolean;
|
|
43
|
+
canPurchase: boolean;
|
|
44
|
+
};
|
|
45
|
+
export type StorefrontProps = {
|
|
46
|
+
stores: StorefrontStore[] | undefined;
|
|
47
|
+
storeId?: string;
|
|
48
|
+
referralCode?: string;
|
|
49
|
+
canPurchase?: boolean;
|
|
50
|
+
getCheckoutUrl?: (selection: StorefrontSelection) => string;
|
|
51
|
+
onSelectPrice?: (selection: StorefrontSelection) => void;
|
|
52
|
+
children?: (state: StorefrontRenderProps) => ReactNode;
|
|
53
|
+
};
|
|
54
|
+
export function Storefront({
|
|
55
|
+
stores,
|
|
56
|
+
storeId,
|
|
57
|
+
referralCode,
|
|
58
|
+
canPurchase = true,
|
|
59
|
+
getCheckoutUrl,
|
|
60
|
+
onSelectPrice,
|
|
61
|
+
children,
|
|
62
|
+
}: StorefrontProps) {
|
|
63
|
+
const visible =
|
|
64
|
+
stores?.filter((store) => !storeId || store.accountId === storeId) ?? [];
|
|
65
|
+
const cards: StorefrontCard[] = visible.flatMap((store) =>
|
|
66
|
+
store.products
|
|
67
|
+
.filter((product) => product.active !== false)
|
|
68
|
+
.flatMap((product) =>
|
|
69
|
+
product.prices
|
|
70
|
+
.filter((price) => price.active !== false)
|
|
71
|
+
.map((price) => {
|
|
72
|
+
const selection = { store, product, price, referralCode };
|
|
73
|
+
return {
|
|
74
|
+
...selection,
|
|
75
|
+
formattedPrice: formatPriceWithInterval({
|
|
76
|
+
...price,
|
|
77
|
+
active: true,
|
|
78
|
+
}),
|
|
79
|
+
purchaseLabel:
|
|
80
|
+
price.type === "recurring" ? "Subscribe" : "Buy now",
|
|
81
|
+
accessibilityLabel: `${price.type === "recurring" ? "Subscribe to" : "Buy"} ${product.name}`,
|
|
82
|
+
checkoutUrl: getCheckoutUrl?.(selection),
|
|
83
|
+
purchase: () => {
|
|
84
|
+
if (canPurchase) onSelectPrice?.(selection);
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}),
|
|
88
|
+
),
|
|
89
|
+
);
|
|
90
|
+
const state = {
|
|
91
|
+
stores: visible,
|
|
92
|
+
cards,
|
|
93
|
+
isLoading: stores === undefined,
|
|
94
|
+
isEmpty: stores !== undefined && !visible.length,
|
|
95
|
+
canPurchase,
|
|
96
|
+
};
|
|
97
|
+
if (children) return <>{children(state)}</>;
|
|
98
|
+
if (state.isLoading) return <p role="status">Loading stores…</p>;
|
|
99
|
+
if (state.isEmpty) return <p>No stores available yet.</p>;
|
|
100
|
+
return (
|
|
101
|
+
<div data-better-stripe="storefront">
|
|
102
|
+
{visible.map((store) => (
|
|
103
|
+
<section key={store.accountId}>
|
|
104
|
+
<h2>{store.name}</h2>
|
|
105
|
+
{!cards.some((card) => card.store.accountId === store.accountId) && (
|
|
106
|
+
<p>No products available yet.</p>
|
|
107
|
+
)}
|
|
108
|
+
{cards
|
|
109
|
+
.filter((card) => card.store.accountId === store.accountId)
|
|
110
|
+
.map((card) => (
|
|
111
|
+
<article key={card.price.stripePriceId}>
|
|
112
|
+
<h3>{card.product.name}</h3>
|
|
113
|
+
<p>{card.product.description}</p>
|
|
114
|
+
<p>{card.formattedPrice}</p>
|
|
115
|
+
{card.checkoutUrl && canPurchase ? (
|
|
116
|
+
<a
|
|
117
|
+
href={card.checkoutUrl}
|
|
118
|
+
aria-label={card.accessibilityLabel}
|
|
119
|
+
>
|
|
120
|
+
{card.purchaseLabel}
|
|
121
|
+
</a>
|
|
122
|
+
) : (
|
|
123
|
+
<button
|
|
124
|
+
type="button"
|
|
125
|
+
disabled={!canPurchase || !onSelectPrice}
|
|
126
|
+
aria-label={card.accessibilityLabel}
|
|
127
|
+
onClick={card.purchase}
|
|
128
|
+
>
|
|
129
|
+
{card.purchaseLabel}
|
|
130
|
+
</button>
|
|
131
|
+
)}
|
|
132
|
+
</article>
|
|
133
|
+
))}
|
|
134
|
+
</section>
|
|
135
|
+
))}
|
|
136
|
+
</div>
|
|
137
|
+
);
|
|
138
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { cleanup, render, screen } from "@testing-library/react";
|
|
3
|
+
import { afterEach, expect, it } from "vitest";
|
|
4
|
+
import { SubscriptionOverview } from "./SubscriptionOverview.js";
|
|
5
|
+
afterEach(cleanup);
|
|
6
|
+
it("renders loading, empty, and current subscription states without app providers", () => {
|
|
7
|
+
const { rerender } = render(
|
|
8
|
+
<SubscriptionOverview subscriptions={undefined} />,
|
|
9
|
+
);
|
|
10
|
+
expect(screen.getByRole("status").textContent).toContain("Loading");
|
|
11
|
+
rerender(<SubscriptionOverview subscriptions={[]} />);
|
|
12
|
+
expect(screen.getByText("No subscription yet.")).toBeTruthy();
|
|
13
|
+
rerender(
|
|
14
|
+
<SubscriptionOverview
|
|
15
|
+
subscriptions={[
|
|
16
|
+
{
|
|
17
|
+
stripeSubscriptionId: "sub_old",
|
|
18
|
+
status: "canceled",
|
|
19
|
+
cancelAtPeriodEnd: true,
|
|
20
|
+
},
|
|
21
|
+
{ stripeSubscriptionId: "sub_new", status: "active" },
|
|
22
|
+
]}
|
|
23
|
+
/>,
|
|
24
|
+
);
|
|
25
|
+
expect(screen.getByText("sub_new")).toBeTruthy();
|
|
26
|
+
expect(screen.queryByText("sub_old")).toBeNull();
|
|
27
|
+
});
|