@fantastic.dev/better-stripe 0.7.0 → 0.8.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 +23 -18
- package/README.md +122 -90
- 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/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/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 +1 -0
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +2 -0
- 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 +47 -0
- package/dist/react/workflows/Payouts.d.ts.map +1 -0
- package/dist/react/workflows/Payouts.js +87 -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 +66 -0
- package/docs/react-workflows.md +231 -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/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/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 +2 -8
- 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 +3 -0
- 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 +25 -0
- package/src/react/workflows/Disputes.tsx +164 -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 +82 -0
- package/src/react/workflows/Payouts.tsx +212 -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,212 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useState, type ReactNode } from "react";
|
|
3
|
+
import { Connect, type ConnectProps } from "./Connect.js";
|
|
4
|
+
import { useWorkflowAction } from "./useWorkflowAction.js";
|
|
5
|
+
import { useWorkflowResource } from "./useWorkflowResource.js";
|
|
6
|
+
import {
|
|
7
|
+
Collection,
|
|
8
|
+
type CollectionProps,
|
|
9
|
+
type WorkflowColumn,
|
|
10
|
+
} from "./Collection.js";
|
|
11
|
+
import { formatPrice } from "../lib/price-helpers.js";
|
|
12
|
+
export type PayoutRow = {
|
|
13
|
+
id: string;
|
|
14
|
+
amount: number;
|
|
15
|
+
currency: string;
|
|
16
|
+
status: string;
|
|
17
|
+
/** Unix milliseconds. */ arrivalDate: number | null;
|
|
18
|
+
created: number;
|
|
19
|
+
};
|
|
20
|
+
export type PayoutSnapshot = {
|
|
21
|
+
balances: { currency: string; available: number; pending: number }[];
|
|
22
|
+
payouts: PayoutRow[];
|
|
23
|
+
upcoming: PayoutRow[];
|
|
24
|
+
nextCursor: string | null;
|
|
25
|
+
};
|
|
26
|
+
export type PayoutsRenderProps = {
|
|
27
|
+
data: PayoutSnapshot | undefined;
|
|
28
|
+
error: string | null;
|
|
29
|
+
isLoading: boolean;
|
|
30
|
+
isPending: boolean;
|
|
31
|
+
nextPayout: PayoutRow | undefined;
|
|
32
|
+
previousPayouts: PayoutRow[];
|
|
33
|
+
refresh: () => void;
|
|
34
|
+
loadMore: () => Promise<void>;
|
|
35
|
+
};
|
|
36
|
+
export type HeadlessPayoutsProps = {
|
|
37
|
+
presentation?: "headless";
|
|
38
|
+
scopeKey: string;
|
|
39
|
+
load: (cursor?: string) => Promise<PayoutSnapshot>;
|
|
40
|
+
children?: (state: PayoutsRenderProps) => ReactNode;
|
|
41
|
+
};
|
|
42
|
+
export type PayoutsProps =
|
|
43
|
+
| HeadlessPayoutsProps
|
|
44
|
+
| (Omit<ConnectProps, "component"> & { presentation: "embedded" });
|
|
45
|
+
const upcoming = (row: PayoutRow) =>
|
|
46
|
+
row.status === "pending" || row.status === "in_transit";
|
|
47
|
+
export function Payouts(props: PayoutsProps) {
|
|
48
|
+
return props.presentation === "embedded" ? (
|
|
49
|
+
<Connect {...props} component="payouts" />
|
|
50
|
+
) : (
|
|
51
|
+
<PayoutsBody key={props.scopeKey} {...props} />
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
function PayoutsBody({ load, scopeKey, children }: HeadlessPayoutsProps) {
|
|
55
|
+
const resource = useWorkflowResource(() => load(), scopeKey);
|
|
56
|
+
const more = useWorkflowAction(
|
|
57
|
+
(cursor: string) => load(cursor),
|
|
58
|
+
`${scopeKey}:${resource.revision}`,
|
|
59
|
+
);
|
|
60
|
+
const [pages, setPages] = useState<{
|
|
61
|
+
base: PayoutSnapshot | undefined;
|
|
62
|
+
rows: PayoutRow[];
|
|
63
|
+
cursor: string | null | undefined;
|
|
64
|
+
}>({ base: undefined, rows: [], cursor: undefined });
|
|
65
|
+
if (pages.base !== resource.data)
|
|
66
|
+
setPages({ base: resource.data, rows: [], cursor: undefined });
|
|
67
|
+
const base = resource.data;
|
|
68
|
+
const nextCursor =
|
|
69
|
+
pages.cursor === undefined ? (base?.nextCursor ?? null) : pages.cursor;
|
|
70
|
+
const data = base
|
|
71
|
+
? {
|
|
72
|
+
...base,
|
|
73
|
+
nextCursor,
|
|
74
|
+
payouts: [
|
|
75
|
+
...new Map(
|
|
76
|
+
[...base.payouts, ...pages.rows].map((row) => [row.id, row]),
|
|
77
|
+
).values(),
|
|
78
|
+
],
|
|
79
|
+
}
|
|
80
|
+
: undefined;
|
|
81
|
+
const loadMore = async () => {
|
|
82
|
+
if (!nextCursor || resource.isLoading) return;
|
|
83
|
+
const next = await more.run(nextCursor);
|
|
84
|
+
if (next)
|
|
85
|
+
setPages((previous) => ({
|
|
86
|
+
...previous,
|
|
87
|
+
rows: [...previous.rows, ...next.payouts],
|
|
88
|
+
cursor: next.nextCursor,
|
|
89
|
+
}));
|
|
90
|
+
};
|
|
91
|
+
const sortedUpcoming =
|
|
92
|
+
data?.upcoming
|
|
93
|
+
.filter(upcoming)
|
|
94
|
+
.slice()
|
|
95
|
+
.sort(
|
|
96
|
+
(a, b) => (a.arrivalDate ?? Infinity) - (b.arrivalDate ?? Infinity),
|
|
97
|
+
) ?? [];
|
|
98
|
+
const state: PayoutsRenderProps = {
|
|
99
|
+
data,
|
|
100
|
+
isLoading: resource.isLoading,
|
|
101
|
+
isPending: resource.isLoading || more.isPending,
|
|
102
|
+
error: resource.error ?? more.error,
|
|
103
|
+
nextPayout: sortedUpcoming[0],
|
|
104
|
+
previousPayouts: data?.payouts.filter((row) => !upcoming(row)) ?? [],
|
|
105
|
+
refresh: resource.refresh,
|
|
106
|
+
loadMore,
|
|
107
|
+
};
|
|
108
|
+
return <>{children ? children(state) : <NativePayouts {...state} />}</>;
|
|
109
|
+
}
|
|
110
|
+
function date(value: number | null) {
|
|
111
|
+
return value === null
|
|
112
|
+
? "Not supplied by Stripe"
|
|
113
|
+
: new Date(value).toLocaleDateString(undefined, { timeZone: "UTC" });
|
|
114
|
+
}
|
|
115
|
+
export type PayoutHistoryRow = Pick<
|
|
116
|
+
PayoutRow,
|
|
117
|
+
"id" | "amount" | "currency" | "status" | "arrivalDate"
|
|
118
|
+
>;
|
|
119
|
+
export type PayoutHistoryProps<
|
|
120
|
+
Row extends PayoutHistoryRow = PayoutHistoryRow,
|
|
121
|
+
> = Omit<CollectionProps<Row>, "columns" | "getKey"> & {
|
|
122
|
+
columns?: WorkflowColumn<Row>[];
|
|
123
|
+
};
|
|
124
|
+
export function PayoutHistory<Row extends PayoutHistoryRow>(
|
|
125
|
+
props: PayoutHistoryProps<Row>,
|
|
126
|
+
) {
|
|
127
|
+
const columns: WorkflowColumn<Row>[] = [
|
|
128
|
+
{ id: "id", label: "Payout ID", cell: (row) => row.id },
|
|
129
|
+
{
|
|
130
|
+
id: "amount",
|
|
131
|
+
label: "Amount",
|
|
132
|
+
align: "end",
|
|
133
|
+
cell: (row) => formatPrice(row.amount, row.currency),
|
|
134
|
+
},
|
|
135
|
+
{ id: "status", label: "Status", cell: (row) => row.status },
|
|
136
|
+
{
|
|
137
|
+
id: "arrival",
|
|
138
|
+
label: "Arrival date",
|
|
139
|
+
cell: (row) => date(row.arrivalDate),
|
|
140
|
+
},
|
|
141
|
+
];
|
|
142
|
+
return (
|
|
143
|
+
<Collection
|
|
144
|
+
emptyLabel="No payouts yet."
|
|
145
|
+
{...props}
|
|
146
|
+
getKey={(row) => row.id}
|
|
147
|
+
columns={props.columns ?? columns}
|
|
148
|
+
/>
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
function NativePayouts({
|
|
152
|
+
data,
|
|
153
|
+
isLoading,
|
|
154
|
+
isPending,
|
|
155
|
+
error,
|
|
156
|
+
nextPayout,
|
|
157
|
+
previousPayouts,
|
|
158
|
+
refresh,
|
|
159
|
+
loadMore,
|
|
160
|
+
}: PayoutsRenderProps) {
|
|
161
|
+
return (
|
|
162
|
+
<section data-better-stripe="payouts" aria-busy={isPending}>
|
|
163
|
+
<h2>Payouts</h2>
|
|
164
|
+
<button type="button" disabled={isPending} onClick={refresh}>
|
|
165
|
+
{isPending ? "Refreshing…" : "Refresh"}
|
|
166
|
+
</button>
|
|
167
|
+
{error && <p role="alert">{error}</p>}
|
|
168
|
+
{isLoading && !data && <p role="status">Loading payouts…</p>}
|
|
169
|
+
{data && (
|
|
170
|
+
<>
|
|
171
|
+
<dl>
|
|
172
|
+
{data.balances.map((balance) => (
|
|
173
|
+
<div key={balance.currency}>
|
|
174
|
+
<dt>Available</dt>
|
|
175
|
+
<dd>{formatPrice(balance.available, balance.currency)}</dd>
|
|
176
|
+
<dt>Pending</dt>
|
|
177
|
+
<dd>{formatPrice(balance.pending, balance.currency)}</dd>
|
|
178
|
+
</div>
|
|
179
|
+
))}
|
|
180
|
+
<dt>Next payout</dt>
|
|
181
|
+
<dd>
|
|
182
|
+
{nextPayout
|
|
183
|
+
? `${formatPrice(nextPayout.amount, nextPayout.currency)} · ${date(nextPayout.arrivalDate)}`
|
|
184
|
+
: "Not scheduled"}
|
|
185
|
+
</dd>
|
|
186
|
+
</dl>
|
|
187
|
+
<h3>Upcoming payouts</h3>
|
|
188
|
+
<PayoutHistory
|
|
189
|
+
rows={data.upcoming}
|
|
190
|
+
emptyLabel="No upcoming payouts scheduled."
|
|
191
|
+
/>
|
|
192
|
+
<h3>Previous payouts</h3>
|
|
193
|
+
<PayoutHistory
|
|
194
|
+
rows={previousPayouts}
|
|
195
|
+
emptyLabel="No previous payouts yet."
|
|
196
|
+
/>
|
|
197
|
+
{data.nextCursor && (
|
|
198
|
+
<button
|
|
199
|
+
type="button"
|
|
200
|
+
disabled={isPending}
|
|
201
|
+
onClick={() => {
|
|
202
|
+
void loadMore();
|
|
203
|
+
}}
|
|
204
|
+
>
|
|
205
|
+
{isPending ? "Loading…" : "Load more payouts"}
|
|
206
|
+
</button>
|
|
207
|
+
)}
|
|
208
|
+
</>
|
|
209
|
+
)}
|
|
210
|
+
</section>
|
|
211
|
+
);
|
|
212
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { act, cleanup, renderHook } from "@testing-library/react";
|
|
3
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
4
|
+
import { useProductManager } from "./Products.js";
|
|
5
|
+
afterEach(cleanup);
|
|
6
|
+
it("validates product names and price minor units before mutation", async () => {
|
|
7
|
+
const createProduct = vi.fn().mockResolvedValue("prod_test");
|
|
8
|
+
const createPrice = vi.fn().mockResolvedValue("price_test");
|
|
9
|
+
const { result } = renderHook(() =>
|
|
10
|
+
useProductManager({ scopeKey: "seller", createProduct, createPrice }),
|
|
11
|
+
);
|
|
12
|
+
await act(async () => {
|
|
13
|
+
await result.current.handleCreateProduct();
|
|
14
|
+
});
|
|
15
|
+
expect(createProduct).not.toHaveBeenCalled();
|
|
16
|
+
act(() =>
|
|
17
|
+
result.current.setNewProduct({
|
|
18
|
+
name: " Classic Tee ",
|
|
19
|
+
description: "Cotton",
|
|
20
|
+
}),
|
|
21
|
+
);
|
|
22
|
+
await act(async () => {
|
|
23
|
+
await result.current.handleCreateProduct();
|
|
24
|
+
});
|
|
25
|
+
expect(createProduct).toHaveBeenCalledWith({
|
|
26
|
+
name: "Classic Tee",
|
|
27
|
+
description: "Cotton",
|
|
28
|
+
});
|
|
29
|
+
act(() =>
|
|
30
|
+
result.current.setNewPrice({
|
|
31
|
+
amount: "12.50",
|
|
32
|
+
currency: "usd",
|
|
33
|
+
interval: "one_time",
|
|
34
|
+
}),
|
|
35
|
+
);
|
|
36
|
+
await act(async () => {
|
|
37
|
+
await result.current.handleAddPrice("prod_test");
|
|
38
|
+
});
|
|
39
|
+
expect(createPrice).not.toHaveBeenCalled();
|
|
40
|
+
act(() =>
|
|
41
|
+
result.current.setNewPrice({
|
|
42
|
+
amount: "2900",
|
|
43
|
+
currency: "usd",
|
|
44
|
+
interval: "one_time",
|
|
45
|
+
}),
|
|
46
|
+
);
|
|
47
|
+
await act(async () => {
|
|
48
|
+
await result.current.handleAddPrice("prod_test");
|
|
49
|
+
});
|
|
50
|
+
expect(createPrice).toHaveBeenCalledWith({
|
|
51
|
+
stripeProductId: "prod_test",
|
|
52
|
+
unitAmount: 2900,
|
|
53
|
+
currency: "usd",
|
|
54
|
+
type: "one_time",
|
|
55
|
+
interval: undefined,
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("keeps the active product operation when a second operation is attempted", async () => {
|
|
60
|
+
let finish!: () => void;
|
|
61
|
+
const deactivateProduct = vi.fn(
|
|
62
|
+
() =>
|
|
63
|
+
new Promise<void>((resolve) => {
|
|
64
|
+
finish = resolve;
|
|
65
|
+
}),
|
|
66
|
+
);
|
|
67
|
+
const deactivatePrice = vi.fn();
|
|
68
|
+
const { result } = renderHook(() =>
|
|
69
|
+
useProductManager({
|
|
70
|
+
scopeKey: "seller",
|
|
71
|
+
createProduct: async () => null,
|
|
72
|
+
createPrice: async () => null,
|
|
73
|
+
deactivateProduct,
|
|
74
|
+
deactivatePrice,
|
|
75
|
+
}),
|
|
76
|
+
);
|
|
77
|
+
let pending!: Promise<unknown>;
|
|
78
|
+
act(() => {
|
|
79
|
+
pending = result.current.handleDeactivate("prod_one");
|
|
80
|
+
});
|
|
81
|
+
await act(async () => {
|
|
82
|
+
await result.current.handleDeactivatePrice("price_one");
|
|
83
|
+
});
|
|
84
|
+
expect(result.current.deactivatingId).toBe("prod_one");
|
|
85
|
+
expect(deactivatePrice).not.toHaveBeenCalled();
|
|
86
|
+
await act(async () => {
|
|
87
|
+
finish();
|
|
88
|
+
await pending;
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useId, useState, type ReactNode } from "react";
|
|
3
|
+
import { useWorkflowAction } from "./useWorkflowAction.js";
|
|
4
|
+
import { PriceCard, type PriceCardPrice } from "../components/PriceCard.js";
|
|
5
|
+
export type ProductInput = { name: string; description?: string };
|
|
6
|
+
export type PriceInput = {
|
|
7
|
+
stripeProductId: string;
|
|
8
|
+
unitAmount: number;
|
|
9
|
+
currency: string;
|
|
10
|
+
type: "one_time" | "recurring";
|
|
11
|
+
interval?: "month" | "year" | "week" | "day";
|
|
12
|
+
};
|
|
13
|
+
export type ProductActions = {
|
|
14
|
+
scopeKey: string;
|
|
15
|
+
createProduct: (args: ProductInput) => Promise<unknown>;
|
|
16
|
+
createPrice: (args: PriceInput) => Promise<unknown>;
|
|
17
|
+
updateProduct?: (
|
|
18
|
+
args: ProductInput & { stripeProductId: string },
|
|
19
|
+
) => Promise<unknown>;
|
|
20
|
+
deactivateProduct?: (args: { stripeProductId: string }) => Promise<unknown>;
|
|
21
|
+
deactivatePrice?: (args: { stripePriceId: string }) => Promise<unknown>;
|
|
22
|
+
};
|
|
23
|
+
export type ProductEdit = {
|
|
24
|
+
stripeProductId: string;
|
|
25
|
+
name: string;
|
|
26
|
+
description: string;
|
|
27
|
+
};
|
|
28
|
+
export function useProductManager(props: ProductActions) {
|
|
29
|
+
const initial = {
|
|
30
|
+
scope: props.scopeKey,
|
|
31
|
+
expandedProduct: null as string | null,
|
|
32
|
+
showCreateDialog: false,
|
|
33
|
+
showAddPriceFor: null as string | null,
|
|
34
|
+
newProduct: { name: "", description: "" },
|
|
35
|
+
newPrice: { amount: "", currency: "usd", interval: "month" },
|
|
36
|
+
editingProduct: null as ProductEdit | null,
|
|
37
|
+
};
|
|
38
|
+
const [fields, setFields] = useState(initial);
|
|
39
|
+
if (fields.scope !== props.scopeKey) setFields(initial);
|
|
40
|
+
type Command = {
|
|
41
|
+
type: "create" | "price" | "edit" | "archiveProduct" | "archivePrice";
|
|
42
|
+
id?: string;
|
|
43
|
+
};
|
|
44
|
+
const [command, setCommand] = useState<Command | null>(null);
|
|
45
|
+
const action = useWorkflowAction(async (command: Command) => {
|
|
46
|
+
setCommand(command);
|
|
47
|
+
switch (command.type) {
|
|
48
|
+
case "create": {
|
|
49
|
+
if (!fields.newProduct.name.trim())
|
|
50
|
+
throw new Error("Enter a product name.");
|
|
51
|
+
await props.createProduct({
|
|
52
|
+
name: fields.newProduct.name.trim(),
|
|
53
|
+
description: fields.newProduct.description.trim() || undefined,
|
|
54
|
+
});
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
case "price": {
|
|
58
|
+
const unitAmount = Number(fields.newPrice.amount);
|
|
59
|
+
if (
|
|
60
|
+
!/^\d+$/.test(fields.newPrice.amount) ||
|
|
61
|
+
!Number.isSafeInteger(unitAmount) ||
|
|
62
|
+
unitAmount <= 0
|
|
63
|
+
)
|
|
64
|
+
throw new Error("Enter a positive integer price in minor units.");
|
|
65
|
+
if (!/^[a-zA-Z]{3}$/.test(fields.newPrice.currency))
|
|
66
|
+
throw new Error("Enter a three-letter currency code.");
|
|
67
|
+
const interval = fields.newPrice.interval;
|
|
68
|
+
if (!["one_time", "month", "year", "week", "day"].includes(interval))
|
|
69
|
+
throw new Error("Choose a supported price interval.");
|
|
70
|
+
await props.createPrice({
|
|
71
|
+
stripeProductId: command.id!,
|
|
72
|
+
unitAmount,
|
|
73
|
+
currency: fields.newPrice.currency.toLowerCase(),
|
|
74
|
+
type: interval === "one_time" ? "one_time" : "recurring",
|
|
75
|
+
interval:
|
|
76
|
+
interval === "one_time"
|
|
77
|
+
? undefined
|
|
78
|
+
: (interval as PriceInput["interval"]),
|
|
79
|
+
});
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
case "edit": {
|
|
83
|
+
if (!props.updateProduct || !fields.editingProduct)
|
|
84
|
+
throw new Error("Product editing is not configured.");
|
|
85
|
+
if (!fields.editingProduct.name.trim())
|
|
86
|
+
throw new Error("Enter a product name.");
|
|
87
|
+
await props.updateProduct({
|
|
88
|
+
stripeProductId: fields.editingProduct.stripeProductId,
|
|
89
|
+
name: fields.editingProduct.name.trim(),
|
|
90
|
+
description: fields.editingProduct.description.trim() || undefined,
|
|
91
|
+
});
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
case "archiveProduct":
|
|
95
|
+
if (!props.deactivateProduct)
|
|
96
|
+
throw new Error("Product archiving is not configured.");
|
|
97
|
+
else await props.deactivateProduct({ stripeProductId: command.id! });
|
|
98
|
+
break;
|
|
99
|
+
case "archivePrice":
|
|
100
|
+
if (!props.deactivatePrice)
|
|
101
|
+
throw new Error("Price archiving is not configured.");
|
|
102
|
+
else await props.deactivatePrice({ stripePriceId: command.id! });
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
return true;
|
|
106
|
+
}, props.scopeKey);
|
|
107
|
+
const run = action.run;
|
|
108
|
+
const set = <K extends keyof typeof initial>(
|
|
109
|
+
key: K,
|
|
110
|
+
value: (typeof initial)[K],
|
|
111
|
+
) => setFields((previous) => ({ ...previous, [key]: value }));
|
|
112
|
+
return {
|
|
113
|
+
...fields,
|
|
114
|
+
error: action.error,
|
|
115
|
+
clearError: action.reset,
|
|
116
|
+
isPending: action.isPending,
|
|
117
|
+
isCreatingProduct: action.isPending && command?.type === "create",
|
|
118
|
+
isCreatingPrice: action.isPending && command?.type === "price",
|
|
119
|
+
isSavingEdit: action.isPending && command?.type === "edit",
|
|
120
|
+
deactivatingId:
|
|
121
|
+
action.isPending && command?.type === "archiveProduct"
|
|
122
|
+
? command.id
|
|
123
|
+
: null,
|
|
124
|
+
deactivatingPriceId:
|
|
125
|
+
action.isPending && command?.type === "archivePrice" ? command.id : null,
|
|
126
|
+
setExpandedProduct: (value: string | null) => set("expandedProduct", value),
|
|
127
|
+
setShowCreateDialog: (value: boolean) => set("showCreateDialog", value),
|
|
128
|
+
setShowAddPriceFor: (value: string | null) => set("showAddPriceFor", value),
|
|
129
|
+
setNewProduct: (value: typeof initial.newProduct) =>
|
|
130
|
+
set("newProduct", value),
|
|
131
|
+
setNewPrice: (value: typeof initial.newPrice) => set("newPrice", value),
|
|
132
|
+
setEditingProduct: (value: ProductEdit | null) =>
|
|
133
|
+
set("editingProduct", value),
|
|
134
|
+
handleCreateProduct: async () => {
|
|
135
|
+
if (await run({ type: "create" }))
|
|
136
|
+
setFields((previous) => ({
|
|
137
|
+
...previous,
|
|
138
|
+
newProduct: initial.newProduct,
|
|
139
|
+
showCreateDialog: false,
|
|
140
|
+
}));
|
|
141
|
+
},
|
|
142
|
+
handleAddPrice: async (id: string) => {
|
|
143
|
+
if (await run({ type: "price", id }))
|
|
144
|
+
setFields((previous) => ({
|
|
145
|
+
...previous,
|
|
146
|
+
newPrice: initial.newPrice,
|
|
147
|
+
showAddPriceFor: null,
|
|
148
|
+
}));
|
|
149
|
+
},
|
|
150
|
+
handleSaveEdit: async () => {
|
|
151
|
+
if (await run({ type: "edit" })) set("editingProduct", null);
|
|
152
|
+
},
|
|
153
|
+
handleDeactivate: (id: string) => run({ type: "archiveProduct", id }),
|
|
154
|
+
handleDeactivatePrice: (id: string) => run({ type: "archivePrice", id }),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
export type CatalogProduct = {
|
|
158
|
+
stripeProductId: string;
|
|
159
|
+
name: string;
|
|
160
|
+
description?: string;
|
|
161
|
+
active: boolean;
|
|
162
|
+
prices: PriceCardPrice[];
|
|
163
|
+
};
|
|
164
|
+
export type ProductsRenderProps = ReturnType<typeof useProductManager> & {
|
|
165
|
+
products: CatalogProduct[];
|
|
166
|
+
isLoading: boolean;
|
|
167
|
+
};
|
|
168
|
+
export type ProductsProps = ProductActions & {
|
|
169
|
+
products: CatalogProduct[] | undefined;
|
|
170
|
+
children?: (state: ProductsRenderProps) => ReactNode;
|
|
171
|
+
};
|
|
172
|
+
/** Complete catalog creation/editing/archiving controller, with optional custom rendering. Amount inputs use minor units. */
|
|
173
|
+
export function Products(props: ProductsProps) {
|
|
174
|
+
return <ProductsBody key={props.scopeKey} {...props} />;
|
|
175
|
+
}
|
|
176
|
+
function ProductsBody(props: ProductsProps) {
|
|
177
|
+
const state = useProductManager(props);
|
|
178
|
+
const id = useId();
|
|
179
|
+
const result = {
|
|
180
|
+
...state,
|
|
181
|
+
products: props.products ?? [],
|
|
182
|
+
isLoading: props.products === undefined,
|
|
183
|
+
};
|
|
184
|
+
if (props.children) return <>{props.children(result)}</>;
|
|
185
|
+
return (
|
|
186
|
+
<section data-better-stripe="products" aria-busy={state.isPending}>
|
|
187
|
+
<h2>Products</h2>
|
|
188
|
+
{state.error && <p role="alert">{state.error}</p>}
|
|
189
|
+
<form
|
|
190
|
+
onSubmit={(event) => {
|
|
191
|
+
event.preventDefault();
|
|
192
|
+
void state.handleCreateProduct();
|
|
193
|
+
}}
|
|
194
|
+
>
|
|
195
|
+
<label htmlFor={`${id}-name`}>Product name</label>
|
|
196
|
+
<input
|
|
197
|
+
id={`${id}-name`}
|
|
198
|
+
required
|
|
199
|
+
value={state.newProduct.name}
|
|
200
|
+
onChange={(event) =>
|
|
201
|
+
state.setNewProduct({
|
|
202
|
+
...state.newProduct,
|
|
203
|
+
name: event.target.value,
|
|
204
|
+
})
|
|
205
|
+
}
|
|
206
|
+
/>
|
|
207
|
+
<label htmlFor={`${id}-description`}>Description</label>
|
|
208
|
+
<input
|
|
209
|
+
id={`${id}-description`}
|
|
210
|
+
value={state.newProduct.description}
|
|
211
|
+
onChange={(event) =>
|
|
212
|
+
state.setNewProduct({
|
|
213
|
+
...state.newProduct,
|
|
214
|
+
description: event.target.value,
|
|
215
|
+
})
|
|
216
|
+
}
|
|
217
|
+
/>
|
|
218
|
+
<button type="submit" disabled={state.isPending}>
|
|
219
|
+
{state.isCreatingProduct ? "Creating…" : "Create product"}
|
|
220
|
+
</button>
|
|
221
|
+
</form>
|
|
222
|
+
{result.isLoading ? (
|
|
223
|
+
<p role="status">Loading products…</p>
|
|
224
|
+
) : !result.products.length ? (
|
|
225
|
+
<p>No products yet.</p>
|
|
226
|
+
) : (
|
|
227
|
+
result.products.map((product) => (
|
|
228
|
+
<article key={product.stripeProductId}>
|
|
229
|
+
<h3>{product.name}</h3>
|
|
230
|
+
<p>{product.description}</p>
|
|
231
|
+
{product.prices.map((price) => (
|
|
232
|
+
<div key={price.stripePriceId}>
|
|
233
|
+
<PriceCard price={price}>
|
|
234
|
+
{({ formattedPrice }) => <p>{formattedPrice}</p>}
|
|
235
|
+
</PriceCard>
|
|
236
|
+
{props.deactivatePrice && price.active && (
|
|
237
|
+
<button
|
|
238
|
+
type="button"
|
|
239
|
+
disabled={state.isPending}
|
|
240
|
+
onClick={() => {
|
|
241
|
+
void state.handleDeactivatePrice(price.stripePriceId);
|
|
242
|
+
}}
|
|
243
|
+
>
|
|
244
|
+
Archive price
|
|
245
|
+
</button>
|
|
246
|
+
)}
|
|
247
|
+
</div>
|
|
248
|
+
))}
|
|
249
|
+
<button
|
|
250
|
+
type="button"
|
|
251
|
+
disabled={state.isPending}
|
|
252
|
+
onClick={() => state.setShowAddPriceFor(product.stripeProductId)}
|
|
253
|
+
>
|
|
254
|
+
Add price
|
|
255
|
+
</button>
|
|
256
|
+
{props.updateProduct && (
|
|
257
|
+
<button
|
|
258
|
+
type="button"
|
|
259
|
+
onClick={() =>
|
|
260
|
+
state.setEditingProduct({
|
|
261
|
+
stripeProductId: product.stripeProductId,
|
|
262
|
+
name: product.name,
|
|
263
|
+
description: product.description ?? "",
|
|
264
|
+
})
|
|
265
|
+
}
|
|
266
|
+
>
|
|
267
|
+
Edit product
|
|
268
|
+
</button>
|
|
269
|
+
)}
|
|
270
|
+
{props.deactivateProduct && (
|
|
271
|
+
<button
|
|
272
|
+
type="button"
|
|
273
|
+
disabled={state.isPending}
|
|
274
|
+
onClick={() => {
|
|
275
|
+
void state.handleDeactivate(product.stripeProductId);
|
|
276
|
+
}}
|
|
277
|
+
>
|
|
278
|
+
Archive product
|
|
279
|
+
</button>
|
|
280
|
+
)}
|
|
281
|
+
</article>
|
|
282
|
+
))
|
|
283
|
+
)}
|
|
284
|
+
{state.showAddPriceFor && (
|
|
285
|
+
<form
|
|
286
|
+
onSubmit={(event) => {
|
|
287
|
+
event.preventDefault();
|
|
288
|
+
void state.handleAddPrice(state.showAddPriceFor!);
|
|
289
|
+
}}
|
|
290
|
+
>
|
|
291
|
+
<label htmlFor={`${id}-amount`}>Price in minor units</label>
|
|
292
|
+
<input
|
|
293
|
+
id={`${id}-amount`}
|
|
294
|
+
type="number"
|
|
295
|
+
required
|
|
296
|
+
min="1"
|
|
297
|
+
step="1"
|
|
298
|
+
value={state.newPrice.amount}
|
|
299
|
+
onChange={(event) =>
|
|
300
|
+
state.setNewPrice({
|
|
301
|
+
...state.newPrice,
|
|
302
|
+
amount: event.target.value,
|
|
303
|
+
})
|
|
304
|
+
}
|
|
305
|
+
/>
|
|
306
|
+
<label htmlFor={`${id}-currency`}>Currency</label>
|
|
307
|
+
<input
|
|
308
|
+
id={`${id}-currency`}
|
|
309
|
+
required
|
|
310
|
+
minLength={3}
|
|
311
|
+
maxLength={3}
|
|
312
|
+
value={state.newPrice.currency}
|
|
313
|
+
onChange={(event) =>
|
|
314
|
+
state.setNewPrice({
|
|
315
|
+
...state.newPrice,
|
|
316
|
+
currency: event.target.value,
|
|
317
|
+
})
|
|
318
|
+
}
|
|
319
|
+
/>
|
|
320
|
+
<label htmlFor={`${id}-interval`}>Interval</label>
|
|
321
|
+
<select
|
|
322
|
+
id={`${id}-interval`}
|
|
323
|
+
value={state.newPrice.interval}
|
|
324
|
+
onChange={(event) =>
|
|
325
|
+
state.setNewPrice({
|
|
326
|
+
...state.newPrice,
|
|
327
|
+
interval: event.target.value,
|
|
328
|
+
})
|
|
329
|
+
}
|
|
330
|
+
>
|
|
331
|
+
{["one_time", "month", "year", "week", "day"].map((value) => (
|
|
332
|
+
<option key={value}>{value}</option>
|
|
333
|
+
))}
|
|
334
|
+
</select>
|
|
335
|
+
<button type="submit" disabled={state.isPending}>
|
|
336
|
+
{state.isCreatingPrice ? "Creating…" : "Create price"}
|
|
337
|
+
</button>
|
|
338
|
+
</form>
|
|
339
|
+
)}
|
|
340
|
+
{state.editingProduct && (
|
|
341
|
+
<form
|
|
342
|
+
onSubmit={(event) => {
|
|
343
|
+
event.preventDefault();
|
|
344
|
+
void state.handleSaveEdit();
|
|
345
|
+
}}
|
|
346
|
+
>
|
|
347
|
+
<label htmlFor={`${id}-edit-name`}>Product name</label>
|
|
348
|
+
<input
|
|
349
|
+
id={`${id}-edit-name`}
|
|
350
|
+
required
|
|
351
|
+
value={state.editingProduct.name}
|
|
352
|
+
onChange={(event) =>
|
|
353
|
+
state.setEditingProduct({
|
|
354
|
+
...state.editingProduct!,
|
|
355
|
+
name: event.target.value,
|
|
356
|
+
})
|
|
357
|
+
}
|
|
358
|
+
/>
|
|
359
|
+
<label htmlFor={`${id}-edit-description`}>Description</label>
|
|
360
|
+
<input
|
|
361
|
+
id={`${id}-edit-description`}
|
|
362
|
+
value={state.editingProduct.description}
|
|
363
|
+
onChange={(event) =>
|
|
364
|
+
state.setEditingProduct({
|
|
365
|
+
...state.editingProduct!,
|
|
366
|
+
description: event.target.value,
|
|
367
|
+
})
|
|
368
|
+
}
|
|
369
|
+
/>
|
|
370
|
+
<button type="submit" disabled={state.isPending}>
|
|
371
|
+
{state.isSavingEdit ? "Saving…" : "Save product"}
|
|
372
|
+
</button>
|
|
373
|
+
</form>
|
|
374
|
+
)}
|
|
375
|
+
</section>
|
|
376
|
+
);
|
|
377
|
+
}
|