@simpleapps-com/augur-hooks 2.2.14 → 2.2.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/checkout/index.cjs +314 -0
- package/dist/checkout/index.cjs.map +1 -0
- package/dist/checkout/index.d.cts +246 -0
- package/dist/checkout/index.d.ts +246 -0
- package/dist/checkout/index.js +314 -0
- package/dist/checkout/index.js.map +1 -0
- package/dist/chunk-AJ2TD5BG.js +225 -0
- package/dist/chunk-AJ2TD5BG.js.map +1 -0
- package/dist/chunk-SSOW6W53.cjs +225 -0
- package/dist/chunk-SSOW6W53.cjs.map +1 -0
- package/dist/index.cjs +100 -164
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +88 -79
- package/dist/index.d.ts +88 -79
- package/dist/index.js +101 -165
- package/dist/index.js.map +1 -1
- package/dist/use-payment-flow-DF8OEQiq.d.cts +88 -0
- package/dist/use-payment-flow-DF8OEQiq.d.ts +88 -0
- package/package.json +8 -3
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// src/hooks/use-payment-flow.ts
|
|
2
|
+
import { useCallback, useEffect, useState } from "react";
|
|
3
|
+
function usePaymentFlow({
|
|
4
|
+
callbacks,
|
|
5
|
+
customerId,
|
|
6
|
+
billingAddress,
|
|
7
|
+
paymentMethod,
|
|
8
|
+
shippingAccepted
|
|
9
|
+
}) {
|
|
10
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
11
|
+
const [unified, setUnified] = useState(null);
|
|
12
|
+
const [unifiedCc, setUnifiedCc] = useState(null);
|
|
13
|
+
const [unifiedResponse, setUnifiedResponse] = useState(false);
|
|
14
|
+
const [unifiedBilling, setUnifiedBilling] = useState(false);
|
|
15
|
+
const [unifiedPayment, setUnifiedPayment] = useState(null);
|
|
16
|
+
const [capturedBin, setCapturedBin] = useState(null);
|
|
17
|
+
const [editCc, setEditCc] = useState(false);
|
|
18
|
+
const handleBinCapture = useCallback((bin) => {
|
|
19
|
+
setCapturedBin(bin);
|
|
20
|
+
}, []);
|
|
21
|
+
const postUnified = useCallback(async () => {
|
|
22
|
+
if (!customerId) return;
|
|
23
|
+
try {
|
|
24
|
+
const result = await callbacks.transactionSetup({ customerId: String(customerId) });
|
|
25
|
+
if (!result) {
|
|
26
|
+
callbacks.toast?.error?.("Unable to initialize payment. Please try again.");
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
setUnified(result);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
console.error("transactionSetup failed:", error);
|
|
32
|
+
callbacks.toast?.error?.("Unable to initialize payment. Please try again.");
|
|
33
|
+
}
|
|
34
|
+
}, [customerId, callbacks]);
|
|
35
|
+
const getCardInfo = useCallback(async () => {
|
|
36
|
+
if (!unified?.transactionSetupId) return;
|
|
37
|
+
try {
|
|
38
|
+
const result = await callbacks.cardInfo({ transactionSetupId: unified.transactionSetupId });
|
|
39
|
+
if (!result) {
|
|
40
|
+
callbacks.toast?.error?.("Unable to verify card details. Please try again.");
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
setUnifiedCc(result);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
console.error("cardInfo failed:", error);
|
|
46
|
+
callbacks.toast?.error?.("Unable to verify card details. Please try again.");
|
|
47
|
+
}
|
|
48
|
+
}, [unified, callbacks]);
|
|
49
|
+
const verifyUnified = useCallback(async () => {
|
|
50
|
+
if (!unified?.transactionSetupId || !customerId) return;
|
|
51
|
+
try {
|
|
52
|
+
const result = await callbacks.validatePayment({
|
|
53
|
+
transactionSetupId: unified.transactionSetupId,
|
|
54
|
+
customerId: String(customerId)
|
|
55
|
+
});
|
|
56
|
+
if (!result) {
|
|
57
|
+
callbacks.toast?.error?.("Payment verification failed. Please try again.");
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
setUnifiedResponse(result.valid);
|
|
61
|
+
if (result.valid) {
|
|
62
|
+
await getCardInfo();
|
|
63
|
+
}
|
|
64
|
+
} catch (error) {
|
|
65
|
+
console.error("validatePayment failed:", error);
|
|
66
|
+
callbacks.toast?.error?.("Payment verification failed. Please try again.");
|
|
67
|
+
}
|
|
68
|
+
}, [unified, customerId, getCardInfo, callbacks]);
|
|
69
|
+
const getUnifiedPayment = useCallback(async () => {
|
|
70
|
+
if (!unified?.transactionSetupId || !customerId) return;
|
|
71
|
+
try {
|
|
72
|
+
const result = await callbacks.accountQuery({
|
|
73
|
+
transactionSetupId: unified.transactionSetupId,
|
|
74
|
+
customerId: String(customerId)
|
|
75
|
+
});
|
|
76
|
+
if (!result) {
|
|
77
|
+
callbacks.toast?.error?.("Unable to retrieve payment account. Please try again.");
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const payment = capturedBin && !result.Bin ? { ...result, Bin: capturedBin } : result;
|
|
81
|
+
setUnifiedPayment(payment);
|
|
82
|
+
} catch (error) {
|
|
83
|
+
console.error("accountQuery failed:", error);
|
|
84
|
+
callbacks.toast?.error?.("Unable to retrieve payment account. Please try again.");
|
|
85
|
+
}
|
|
86
|
+
}, [unified, customerId, capturedBin, callbacks]);
|
|
87
|
+
const updateUnifiedBilling = useCallback(async () => {
|
|
88
|
+
if (!unified?.transactionSetupId || !billingAddress) return;
|
|
89
|
+
try {
|
|
90
|
+
const result = await callbacks.billingUpdate({
|
|
91
|
+
transactionSetupId: unified.transactionSetupId,
|
|
92
|
+
address1: billingAddress.address1,
|
|
93
|
+
address2: billingAddress.address2,
|
|
94
|
+
city: billingAddress.city,
|
|
95
|
+
state: billingAddress.state,
|
|
96
|
+
zip: billingAddress.postalCode
|
|
97
|
+
});
|
|
98
|
+
if (!result) {
|
|
99
|
+
callbacks.toast?.error?.("Billing update failed. Please try again.");
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
setUnifiedBilling(true);
|
|
103
|
+
void getUnifiedPayment();
|
|
104
|
+
} catch (error) {
|
|
105
|
+
console.error("billingUpdate failed:", error);
|
|
106
|
+
callbacks.toast?.error?.("Billing update failed. Please try again.");
|
|
107
|
+
}
|
|
108
|
+
}, [unified, billingAddress, getUnifiedPayment, callbacks]);
|
|
109
|
+
useEffect(() => {
|
|
110
|
+
if (isOpen && (!shippingAccepted || paymentMethod !== "credit-card")) {
|
|
111
|
+
setIsOpen(false);
|
|
112
|
+
}
|
|
113
|
+
}, [isOpen, shippingAccepted, paymentMethod]);
|
|
114
|
+
useEffect(() => {
|
|
115
|
+
if (isOpen && shippingAccepted && paymentMethod === "credit-card" && unified === null) {
|
|
116
|
+
void postUnified();
|
|
117
|
+
}
|
|
118
|
+
}, [isOpen, shippingAccepted, paymentMethod, unified, postUnified]);
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
if (!isOpen && unified !== null && shippingAccepted && paymentMethod === "credit-card") {
|
|
121
|
+
void verifyUnified();
|
|
122
|
+
}
|
|
123
|
+
}, [isOpen, unified, shippingAccepted, paymentMethod, verifyUnified]);
|
|
124
|
+
useEffect(() => {
|
|
125
|
+
if (unifiedResponse && billingAddress !== void 0 && unifiedCc?.transactionSetupId && paymentMethod === "credit-card") {
|
|
126
|
+
void updateUnifiedBilling();
|
|
127
|
+
}
|
|
128
|
+
}, [
|
|
129
|
+
unifiedResponse,
|
|
130
|
+
billingAddress,
|
|
131
|
+
unifiedCc?.transactionSetupId,
|
|
132
|
+
updateUnifiedBilling,
|
|
133
|
+
paymentMethod
|
|
134
|
+
]);
|
|
135
|
+
useEffect(() => {
|
|
136
|
+
if (editCc) {
|
|
137
|
+
setIsOpen(true);
|
|
138
|
+
setUnified(null);
|
|
139
|
+
setUnifiedCc(null);
|
|
140
|
+
setUnifiedPayment(null);
|
|
141
|
+
setUnifiedBilling(false);
|
|
142
|
+
setUnifiedResponse(false);
|
|
143
|
+
setCapturedBin(null);
|
|
144
|
+
}
|
|
145
|
+
}, [editCc]);
|
|
146
|
+
return {
|
|
147
|
+
isOpen,
|
|
148
|
+
setIsOpen,
|
|
149
|
+
editCc,
|
|
150
|
+
setEditCc,
|
|
151
|
+
unified,
|
|
152
|
+
unifiedCc,
|
|
153
|
+
unifiedResponse,
|
|
154
|
+
unifiedBilling,
|
|
155
|
+
unifiedPayment,
|
|
156
|
+
capturedBin,
|
|
157
|
+
handleBinCapture
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// src/hooks/use-order-readiness.ts
|
|
162
|
+
import { useEffect as useEffect2, useState as useState2 } from "react";
|
|
163
|
+
function determineOrderReadiness(params) {
|
|
164
|
+
if (params.customReadinessCheck) {
|
|
165
|
+
return params.customReadinessCheck(params);
|
|
166
|
+
}
|
|
167
|
+
if (!params.cartHdrUid) return false;
|
|
168
|
+
const blockers = [
|
|
169
|
+
params.isEditShippingInfo,
|
|
170
|
+
params.isEditShippingMethods,
|
|
171
|
+
params.isEditBillingInfo,
|
|
172
|
+
params.editCc,
|
|
173
|
+
params.shippingAddress === void 0,
|
|
174
|
+
params.billingAddress === void 0,
|
|
175
|
+
!params.shippingMethodSaved,
|
|
176
|
+
!params.showBillingInfoSummary,
|
|
177
|
+
params.processingOrder
|
|
178
|
+
];
|
|
179
|
+
if (blockers.some(Boolean)) return false;
|
|
180
|
+
if (params.paymentMethod === "credit-card" && (!params.unifiedPaymentAccountId || !params.unifiedBilling)) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
if (!params.isFreeShipping && params.shippingFee === void 0) {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
function useOrderReadiness(params) {
|
|
189
|
+
const [enableProcessOrderBtn, setEnableProcessOrderBtn] = useState2(false);
|
|
190
|
+
const [processingValidation, setProcessingValidation] = useState2(false);
|
|
191
|
+
useEffect2(() => {
|
|
192
|
+
setProcessingValidation(true);
|
|
193
|
+
const timeoutId = setTimeout(() => {
|
|
194
|
+
const ready = determineOrderReadiness(params);
|
|
195
|
+
setEnableProcessOrderBtn(ready);
|
|
196
|
+
setProcessingValidation(false);
|
|
197
|
+
}, 300);
|
|
198
|
+
return () => clearTimeout(timeoutId);
|
|
199
|
+
}, [
|
|
200
|
+
params.billingAddress,
|
|
201
|
+
params.shippingAddress,
|
|
202
|
+
params.isEditShippingInfo,
|
|
203
|
+
params.isEditShippingMethods,
|
|
204
|
+
params.isEditBillingInfo,
|
|
205
|
+
params.editCc,
|
|
206
|
+
params.isFreeShipping,
|
|
207
|
+
params.unifiedPaymentAccountId,
|
|
208
|
+
params.unifiedBilling,
|
|
209
|
+
params.paymentMethod,
|
|
210
|
+
params.processingOrder,
|
|
211
|
+
params.carrierId,
|
|
212
|
+
params.shippingFee,
|
|
213
|
+
params.shippingMethodSaved,
|
|
214
|
+
params.showBillingInfoSummary,
|
|
215
|
+
params.cartHdrUid
|
|
216
|
+
]);
|
|
217
|
+
return { enableProcessOrderBtn, processingValidation };
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export {
|
|
221
|
+
usePaymentFlow,
|
|
222
|
+
determineOrderReadiness,
|
|
223
|
+
useOrderReadiness
|
|
224
|
+
};
|
|
225
|
+
//# sourceMappingURL=chunk-AJ2TD5BG.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/hooks/use-payment-flow.ts","../src/hooks/use-order-readiness.ts"],"sourcesContent":["import { useCallback, useEffect, useState } from \"react\";\nimport type {\n TTransactionSetup,\n TCardInfo,\n TPaymentValidation,\n TAccountQuery,\n TBillingUpdate,\n} from \"@simpleapps-com/augur-utils\";\n\n/** Site-specific payment action callbacks injected into usePaymentFlow. */\nexport interface PaymentFlowCallbacks {\n /** Initialize a payment session. Returns transaction setup or undefined on failure. */\n transactionSetup: (params: { customerId: string }) => Promise<TTransactionSetup | undefined>;\n /** Retrieve card details after iframe entry. */\n cardInfo: (params: { transactionSetupId: string }) => Promise<TCardInfo | undefined>;\n /** Verify card is valid. */\n validatePayment: (params: {\n transactionSetupId: string;\n customerId: string;\n }) => Promise<TPaymentValidation | undefined>;\n /** Get payment account ID. */\n accountQuery: (params: {\n transactionSetupId: string;\n customerId: string;\n }) => Promise<TAccountQuery | undefined>;\n /** Send billing address to payment processor. */\n billingUpdate: (params: {\n transactionSetupId: string;\n address1: string;\n address2?: string;\n city: string;\n state: string;\n zip: string;\n }) => Promise<TBillingUpdate | undefined>;\n /** Optional toast notification callback. */\n toast?: {\n error?: (message: string) => void;\n };\n}\n\n/** Billing address shape accepted by the payment flow. */\nexport interface PaymentBillingAddress {\n address1: string;\n address2?: string;\n city: string;\n state: string;\n postalCode: string;\n}\n\ninterface UsePaymentFlowParams {\n callbacks: PaymentFlowCallbacks;\n customerId: string | number | undefined;\n billingAddress: PaymentBillingAddress | undefined;\n paymentMethod: string;\n shippingAccepted: boolean;\n}\n\n/**\n * Manages the full Element/Unified payment lifecycle:\n * transactionSetup -> cardInfo -> validatePayment -> accountQuery -> billingUpdate\n *\n * Accepts site-specific server action callbacks so the hook has no dependency\n * on any particular server action implementation.\n *\n * @example\n * ```tsx\n * import { usePaymentFlow } from \"@simpleapps-com/augur-hooks\";\n * import { transactionSetup, cardInfo, validatePayment, accountQuery, billingUpdate } from \"@/lib/actions/payments\";\n *\n * const payment = usePaymentFlow({\n * callbacks: { transactionSetup, cardInfo, validatePayment, accountQuery, billingUpdate },\n * customerId,\n * billingAddress,\n * paymentMethod: \"credit-card\",\n * shippingAccepted: true,\n * });\n * ```\n */\nexport function usePaymentFlow({\n callbacks,\n customerId,\n billingAddress,\n paymentMethod,\n shippingAccepted,\n}: UsePaymentFlowParams) {\n const [isOpen, setIsOpen] = useState(false);\n const [unified, setUnified] = useState<TTransactionSetup | null>(null);\n const [unifiedCc, setUnifiedCc] = useState<TCardInfo | null>(null);\n const [unifiedResponse, setUnifiedResponse] = useState(false);\n const [unifiedBilling, setUnifiedBilling] = useState(false);\n const [unifiedPayment, setUnifiedPayment] = useState<TAccountQuery | null>(null);\n const [capturedBin, setCapturedBin] = useState<string | null>(null);\n const [editCc, setEditCc] = useState(false);\n\n const handleBinCapture = useCallback((bin: string) => {\n setCapturedBin(bin);\n }, []);\n\n // Step 1: Initialize payment session\n const postUnified = useCallback(async () => {\n if (!customerId) return;\n try {\n const result = await callbacks.transactionSetup({ customerId: String(customerId) });\n if (!result) {\n callbacks.toast?.error?.(\"Unable to initialize payment. Please try again.\");\n return;\n }\n setUnified(result);\n } catch (error) {\n console.error(\"transactionSetup failed:\", error);\n callbacks.toast?.error?.(\"Unable to initialize payment. Please try again.\");\n }\n }, [customerId, callbacks]);\n\n // Step 2: Get card info after iframe entry\n const getCardInfo = useCallback(async () => {\n /* v8 ignore next -- defensive: caller (verifyUnified) already guards */\n if (!unified?.transactionSetupId) return;\n try {\n const result = await callbacks.cardInfo({ transactionSetupId: unified.transactionSetupId });\n if (!result) {\n callbacks.toast?.error?.(\"Unable to verify card details. Please try again.\");\n return;\n }\n setUnifiedCc(result);\n } catch (error) {\n console.error(\"cardInfo failed:\", error);\n callbacks.toast?.error?.(\"Unable to verify card details. Please try again.\");\n }\n }, [unified, callbacks]);\n\n // Step 3: Validate payment\n const verifyUnified = useCallback(async () => {\n if (!unified?.transactionSetupId || !customerId) return;\n try {\n const result = await callbacks.validatePayment({\n transactionSetupId: unified.transactionSetupId,\n customerId: String(customerId),\n });\n if (!result) {\n callbacks.toast?.error?.(\"Payment verification failed. Please try again.\");\n return;\n }\n setUnifiedResponse(result.valid);\n if (result.valid) {\n await getCardInfo();\n }\n } catch (error) {\n console.error(\"validatePayment failed:\", error);\n callbacks.toast?.error?.(\"Payment verification failed. Please try again.\");\n }\n }, [unified, customerId, getCardInfo, callbacks]);\n\n // Step 4: Get payment account info\n const getUnifiedPayment = useCallback(async () => {\n if (!unified?.transactionSetupId || !customerId) return;\n try {\n const result = await callbacks.accountQuery({\n transactionSetupId: unified.transactionSetupId,\n customerId: String(customerId),\n });\n if (!result) {\n callbacks.toast?.error?.(\"Unable to retrieve payment account. Please try again.\");\n return;\n }\n const payment = capturedBin && !result.Bin ? { ...result, Bin: capturedBin } : result;\n setUnifiedPayment(payment);\n } catch (error) {\n console.error(\"accountQuery failed:\", error);\n callbacks.toast?.error?.(\"Unable to retrieve payment account. Please try again.\");\n }\n }, [unified, customerId, capturedBin, callbacks]);\n\n // Step 5: Update billing info\n const updateUnifiedBilling = useCallback(async () => {\n /* v8 ignore next -- defensive: effect already guards billingAddress/unified */\n if (!unified?.transactionSetupId || !billingAddress) return;\n try {\n const result = await callbacks.billingUpdate({\n transactionSetupId: unified.transactionSetupId,\n address1: billingAddress.address1,\n address2: billingAddress.address2,\n city: billingAddress.city,\n state: billingAddress.state,\n zip: billingAddress.postalCode,\n });\n if (!result) {\n callbacks.toast?.error?.(\"Billing update failed. Please try again.\");\n return;\n }\n setUnifiedBilling(true);\n void getUnifiedPayment();\n } catch (error) {\n console.error(\"billingUpdate failed:\", error);\n callbacks.toast?.error?.(\"Billing update failed. Please try again.\");\n }\n }, [unified, billingAddress, getUnifiedPayment, callbacks]);\n\n // Close payment modal if preconditions are lost\n useEffect(() => {\n if (isOpen && (!shippingAccepted || paymentMethod !== \"credit-card\")) {\n setIsOpen(false);\n }\n }, [isOpen, shippingAccepted, paymentMethod]);\n\n // Initialize payment setup when modal opens with no transaction\n useEffect(() => {\n if (isOpen && shippingAccepted && paymentMethod === \"credit-card\" && unified === null) {\n void postUnified();\n }\n }, [isOpen, shippingAccepted, paymentMethod, unified, postUnified]);\n\n // Verify payment after modal closes\n useEffect(() => {\n if (!isOpen && unified !== null && shippingAccepted && paymentMethod === \"credit-card\") {\n void verifyUnified();\n }\n }, [isOpen, unified, shippingAccepted, paymentMethod, verifyUnified]);\n\n // Trigger billing update when card verified + billing address ready\n useEffect(() => {\n if (\n unifiedResponse &&\n billingAddress !== undefined &&\n unifiedCc?.transactionSetupId &&\n paymentMethod === \"credit-card\"\n ) {\n void updateUnifiedBilling();\n }\n }, [\n unifiedResponse,\n billingAddress,\n unifiedCc?.transactionSetupId,\n updateUnifiedBilling,\n paymentMethod,\n ]);\n\n // Reset all payment state when editing CC\n useEffect(() => {\n if (editCc) {\n setIsOpen(true);\n setUnified(null);\n setUnifiedCc(null);\n setUnifiedPayment(null);\n setUnifiedBilling(false);\n setUnifiedResponse(false);\n setCapturedBin(null);\n }\n }, [editCc]);\n\n return {\n isOpen,\n setIsOpen,\n editCc,\n setEditCc,\n unified,\n unifiedCc,\n unifiedResponse,\n unifiedBilling,\n unifiedPayment,\n capturedBin,\n handleBinCapture,\n };\n}\n","\"use client\";\n\nimport { useEffect, useState } from \"react\";\n\n/** Parameters for order readiness validation. */\nexport interface OrderReadinessParams {\n cartHdrUid: string | number | undefined;\n shippingAddress: unknown;\n billingAddress: unknown;\n shippingMethodSaved: boolean;\n showBillingInfoSummary: boolean;\n processingOrder: boolean;\n paymentMethod: string;\n unifiedPaymentAccountId: string | undefined;\n unifiedBilling: boolean;\n isFreeShipping: boolean;\n carrierId: string;\n shippingFee: number | undefined;\n isEditShippingInfo: boolean;\n isEditShippingMethods: boolean;\n isEditBillingInfo: boolean;\n editCc: boolean;\n /** Site-provided function to check if the order is ready. Overrides default logic when provided. */\n customReadinessCheck?: (params: OrderReadinessParams) => boolean;\n}\n\n/**\n * Pure function that determines if an order is ready to submit.\n *\n * Checks all gates: cart exists, addresses present, shipping method saved,\n * billing shown, payment complete (for credit card), shipping fee present\n * (for non-free, non-pickup orders), and no sections being edited.\n */\nexport function determineOrderReadiness(params: OrderReadinessParams): boolean {\n if (params.customReadinessCheck) {\n return params.customReadinessCheck(params);\n }\n\n if (!params.cartHdrUid) return false;\n\n // Block if any section is being edited or addresses are missing\n const blockers = [\n params.isEditShippingInfo,\n params.isEditShippingMethods,\n params.isEditBillingInfo,\n params.editCc,\n params.shippingAddress === undefined,\n params.billingAddress === undefined,\n !params.shippingMethodSaved,\n !params.showBillingInfoSummary,\n params.processingOrder,\n ];\n if (blockers.some(Boolean)) return false;\n\n // Credit card requires completed payment flow\n if (\n params.paymentMethod === \"credit-card\" &&\n (!params.unifiedPaymentAccountId || !params.unifiedBilling)\n ) {\n return false;\n }\n\n // Non-free shipping requires a fee (unless carrier is pickup/freight)\n if (!params.isFreeShipping && params.shippingFee === undefined) {\n return false;\n }\n\n return true;\n}\n\n/**\n * Debounced order readiness hook.\n *\n * Recomputes readiness 300ms after any dependency changes to avoid\n * excessive re-renders during rapid state transitions (e.g., typing\n * in address fields).\n */\nexport function useOrderReadiness(params: OrderReadinessParams) {\n const [enableProcessOrderBtn, setEnableProcessOrderBtn] = useState(false);\n const [processingValidation, setProcessingValidation] = useState(false);\n\n useEffect(() => {\n setProcessingValidation(true);\n\n const timeoutId = setTimeout(() => {\n const ready = determineOrderReadiness(params);\n setEnableProcessOrderBtn(ready);\n setProcessingValidation(false);\n }, 300);\n\n return () => clearTimeout(timeoutId);\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [\n params.billingAddress,\n params.shippingAddress,\n params.isEditShippingInfo,\n params.isEditShippingMethods,\n params.isEditBillingInfo,\n params.editCc,\n params.isFreeShipping,\n params.unifiedPaymentAccountId,\n params.unifiedBilling,\n params.paymentMethod,\n params.processingOrder,\n params.carrierId,\n params.shippingFee,\n params.shippingMethodSaved,\n params.showBillingInfoSummary,\n params.cartHdrUid,\n ]);\n\n return { enableProcessOrderBtn, processingValidation };\n}\n"],"mappings":";AAAA,SAAS,aAAa,WAAW,gBAAgB;AA8E1C,SAAS,eAAe;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAyB;AACvB,QAAM,CAAC,QAAQ,SAAS,IAAI,SAAS,KAAK;AAC1C,QAAM,CAAC,SAAS,UAAU,IAAI,SAAmC,IAAI;AACrE,QAAM,CAAC,WAAW,YAAY,IAAI,SAA2B,IAAI;AACjE,QAAM,CAAC,iBAAiB,kBAAkB,IAAI,SAAS,KAAK;AAC5D,QAAM,CAAC,gBAAgB,iBAAiB,IAAI,SAAS,KAAK;AAC1D,QAAM,CAAC,gBAAgB,iBAAiB,IAAI,SAA+B,IAAI;AAC/E,QAAM,CAAC,aAAa,cAAc,IAAI,SAAwB,IAAI;AAClE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}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/hooks/use-payment-flow.ts
|
|
2
|
+
var _react = require('react');
|
|
3
|
+
function usePaymentFlow({
|
|
4
|
+
callbacks,
|
|
5
|
+
customerId,
|
|
6
|
+
billingAddress,
|
|
7
|
+
paymentMethod,
|
|
8
|
+
shippingAccepted
|
|
9
|
+
}) {
|
|
10
|
+
const [isOpen, setIsOpen] = _react.useState.call(void 0, false);
|
|
11
|
+
const [unified, setUnified] = _react.useState.call(void 0, null);
|
|
12
|
+
const [unifiedCc, setUnifiedCc] = _react.useState.call(void 0, null);
|
|
13
|
+
const [unifiedResponse, setUnifiedResponse] = _react.useState.call(void 0, false);
|
|
14
|
+
const [unifiedBilling, setUnifiedBilling] = _react.useState.call(void 0, false);
|
|
15
|
+
const [unifiedPayment, setUnifiedPayment] = _react.useState.call(void 0, null);
|
|
16
|
+
const [capturedBin, setCapturedBin] = _react.useState.call(void 0, null);
|
|
17
|
+
const [editCc, setEditCc] = _react.useState.call(void 0, false);
|
|
18
|
+
const handleBinCapture = _react.useCallback.call(void 0, (bin) => {
|
|
19
|
+
setCapturedBin(bin);
|
|
20
|
+
}, []);
|
|
21
|
+
const postUnified = _react.useCallback.call(void 0, async () => {
|
|
22
|
+
if (!customerId) return;
|
|
23
|
+
try {
|
|
24
|
+
const result = await callbacks.transactionSetup({ customerId: String(customerId) });
|
|
25
|
+
if (!result) {
|
|
26
|
+
_optionalChain([callbacks, 'access', _ => _.toast, 'optionalAccess', _2 => _2.error, 'optionalCall', _3 => _3("Unable to initialize payment. Please try again.")]);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
setUnified(result);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
console.error("transactionSetup failed:", error);
|
|
32
|
+
_optionalChain([callbacks, 'access', _4 => _4.toast, 'optionalAccess', _5 => _5.error, 'optionalCall', _6 => _6("Unable to initialize payment. Please try again.")]);
|
|
33
|
+
}
|
|
34
|
+
}, [customerId, callbacks]);
|
|
35
|
+
const getCardInfo = _react.useCallback.call(void 0, async () => {
|
|
36
|
+
if (!_optionalChain([unified, 'optionalAccess', _7 => _7.transactionSetupId])) return;
|
|
37
|
+
try {
|
|
38
|
+
const result = await callbacks.cardInfo({ transactionSetupId: unified.transactionSetupId });
|
|
39
|
+
if (!result) {
|
|
40
|
+
_optionalChain([callbacks, 'access', _8 => _8.toast, 'optionalAccess', _9 => _9.error, 'optionalCall', _10 => _10("Unable to verify card details. Please try again.")]);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
setUnifiedCc(result);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
console.error("cardInfo failed:", error);
|
|
46
|
+
_optionalChain([callbacks, 'access', _11 => _11.toast, 'optionalAccess', _12 => _12.error, 'optionalCall', _13 => _13("Unable to verify card details. Please try again.")]);
|
|
47
|
+
}
|
|
48
|
+
}, [unified, callbacks]);
|
|
49
|
+
const verifyUnified = _react.useCallback.call(void 0, async () => {
|
|
50
|
+
if (!_optionalChain([unified, 'optionalAccess', _14 => _14.transactionSetupId]) || !customerId) return;
|
|
51
|
+
try {
|
|
52
|
+
const result = await callbacks.validatePayment({
|
|
53
|
+
transactionSetupId: unified.transactionSetupId,
|
|
54
|
+
customerId: String(customerId)
|
|
55
|
+
});
|
|
56
|
+
if (!result) {
|
|
57
|
+
_optionalChain([callbacks, 'access', _15 => _15.toast, 'optionalAccess', _16 => _16.error, 'optionalCall', _17 => _17("Payment verification failed. Please try again.")]);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
setUnifiedResponse(result.valid);
|
|
61
|
+
if (result.valid) {
|
|
62
|
+
await getCardInfo();
|
|
63
|
+
}
|
|
64
|
+
} catch (error) {
|
|
65
|
+
console.error("validatePayment failed:", error);
|
|
66
|
+
_optionalChain([callbacks, 'access', _18 => _18.toast, 'optionalAccess', _19 => _19.error, 'optionalCall', _20 => _20("Payment verification failed. Please try again.")]);
|
|
67
|
+
}
|
|
68
|
+
}, [unified, customerId, getCardInfo, callbacks]);
|
|
69
|
+
const getUnifiedPayment = _react.useCallback.call(void 0, async () => {
|
|
70
|
+
if (!_optionalChain([unified, 'optionalAccess', _21 => _21.transactionSetupId]) || !customerId) return;
|
|
71
|
+
try {
|
|
72
|
+
const result = await callbacks.accountQuery({
|
|
73
|
+
transactionSetupId: unified.transactionSetupId,
|
|
74
|
+
customerId: String(customerId)
|
|
75
|
+
});
|
|
76
|
+
if (!result) {
|
|
77
|
+
_optionalChain([callbacks, 'access', _22 => _22.toast, 'optionalAccess', _23 => _23.error, 'optionalCall', _24 => _24("Unable to retrieve payment account. Please try again.")]);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const payment = capturedBin && !result.Bin ? { ...result, Bin: capturedBin } : result;
|
|
81
|
+
setUnifiedPayment(payment);
|
|
82
|
+
} catch (error) {
|
|
83
|
+
console.error("accountQuery failed:", error);
|
|
84
|
+
_optionalChain([callbacks, 'access', _25 => _25.toast, 'optionalAccess', _26 => _26.error, 'optionalCall', _27 => _27("Unable to retrieve payment account. Please try again.")]);
|
|
85
|
+
}
|
|
86
|
+
}, [unified, customerId, capturedBin, callbacks]);
|
|
87
|
+
const updateUnifiedBilling = _react.useCallback.call(void 0, async () => {
|
|
88
|
+
if (!_optionalChain([unified, 'optionalAccess', _28 => _28.transactionSetupId]) || !billingAddress) return;
|
|
89
|
+
try {
|
|
90
|
+
const result = await callbacks.billingUpdate({
|
|
91
|
+
transactionSetupId: unified.transactionSetupId,
|
|
92
|
+
address1: billingAddress.address1,
|
|
93
|
+
address2: billingAddress.address2,
|
|
94
|
+
city: billingAddress.city,
|
|
95
|
+
state: billingAddress.state,
|
|
96
|
+
zip: billingAddress.postalCode
|
|
97
|
+
});
|
|
98
|
+
if (!result) {
|
|
99
|
+
_optionalChain([callbacks, 'access', _29 => _29.toast, 'optionalAccess', _30 => _30.error, 'optionalCall', _31 => _31("Billing update failed. Please try again.")]);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
setUnifiedBilling(true);
|
|
103
|
+
void getUnifiedPayment();
|
|
104
|
+
} catch (error) {
|
|
105
|
+
console.error("billingUpdate failed:", error);
|
|
106
|
+
_optionalChain([callbacks, 'access', _32 => _32.toast, 'optionalAccess', _33 => _33.error, 'optionalCall', _34 => _34("Billing update failed. Please try again.")]);
|
|
107
|
+
}
|
|
108
|
+
}, [unified, billingAddress, getUnifiedPayment, callbacks]);
|
|
109
|
+
_react.useEffect.call(void 0, () => {
|
|
110
|
+
if (isOpen && (!shippingAccepted || paymentMethod !== "credit-card")) {
|
|
111
|
+
setIsOpen(false);
|
|
112
|
+
}
|
|
113
|
+
}, [isOpen, shippingAccepted, paymentMethod]);
|
|
114
|
+
_react.useEffect.call(void 0, () => {
|
|
115
|
+
if (isOpen && shippingAccepted && paymentMethod === "credit-card" && unified === null) {
|
|
116
|
+
void postUnified();
|
|
117
|
+
}
|
|
118
|
+
}, [isOpen, shippingAccepted, paymentMethod, unified, postUnified]);
|
|
119
|
+
_react.useEffect.call(void 0, () => {
|
|
120
|
+
if (!isOpen && unified !== null && shippingAccepted && paymentMethod === "credit-card") {
|
|
121
|
+
void verifyUnified();
|
|
122
|
+
}
|
|
123
|
+
}, [isOpen, unified, shippingAccepted, paymentMethod, verifyUnified]);
|
|
124
|
+
_react.useEffect.call(void 0, () => {
|
|
125
|
+
if (unifiedResponse && billingAddress !== void 0 && _optionalChain([unifiedCc, 'optionalAccess', _35 => _35.transactionSetupId]) && paymentMethod === "credit-card") {
|
|
126
|
+
void updateUnifiedBilling();
|
|
127
|
+
}
|
|
128
|
+
}, [
|
|
129
|
+
unifiedResponse,
|
|
130
|
+
billingAddress,
|
|
131
|
+
_optionalChain([unifiedCc, 'optionalAccess', _36 => _36.transactionSetupId]),
|
|
132
|
+
updateUnifiedBilling,
|
|
133
|
+
paymentMethod
|
|
134
|
+
]);
|
|
135
|
+
_react.useEffect.call(void 0, () => {
|
|
136
|
+
if (editCc) {
|
|
137
|
+
setIsOpen(true);
|
|
138
|
+
setUnified(null);
|
|
139
|
+
setUnifiedCc(null);
|
|
140
|
+
setUnifiedPayment(null);
|
|
141
|
+
setUnifiedBilling(false);
|
|
142
|
+
setUnifiedResponse(false);
|
|
143
|
+
setCapturedBin(null);
|
|
144
|
+
}
|
|
145
|
+
}, [editCc]);
|
|
146
|
+
return {
|
|
147
|
+
isOpen,
|
|
148
|
+
setIsOpen,
|
|
149
|
+
editCc,
|
|
150
|
+
setEditCc,
|
|
151
|
+
unified,
|
|
152
|
+
unifiedCc,
|
|
153
|
+
unifiedResponse,
|
|
154
|
+
unifiedBilling,
|
|
155
|
+
unifiedPayment,
|
|
156
|
+
capturedBin,
|
|
157
|
+
handleBinCapture
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// src/hooks/use-order-readiness.ts
|
|
162
|
+
|
|
163
|
+
function determineOrderReadiness(params) {
|
|
164
|
+
if (params.customReadinessCheck) {
|
|
165
|
+
return params.customReadinessCheck(params);
|
|
166
|
+
}
|
|
167
|
+
if (!params.cartHdrUid) return false;
|
|
168
|
+
const blockers = [
|
|
169
|
+
params.isEditShippingInfo,
|
|
170
|
+
params.isEditShippingMethods,
|
|
171
|
+
params.isEditBillingInfo,
|
|
172
|
+
params.editCc,
|
|
173
|
+
params.shippingAddress === void 0,
|
|
174
|
+
params.billingAddress === void 0,
|
|
175
|
+
!params.shippingMethodSaved,
|
|
176
|
+
!params.showBillingInfoSummary,
|
|
177
|
+
params.processingOrder
|
|
178
|
+
];
|
|
179
|
+
if (blockers.some(Boolean)) return false;
|
|
180
|
+
if (params.paymentMethod === "credit-card" && (!params.unifiedPaymentAccountId || !params.unifiedBilling)) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
if (!params.isFreeShipping && params.shippingFee === void 0) {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
function useOrderReadiness(params) {
|
|
189
|
+
const [enableProcessOrderBtn, setEnableProcessOrderBtn] = _react.useState.call(void 0, false);
|
|
190
|
+
const [processingValidation, setProcessingValidation] = _react.useState.call(void 0, false);
|
|
191
|
+
_react.useEffect.call(void 0, () => {
|
|
192
|
+
setProcessingValidation(true);
|
|
193
|
+
const timeoutId = setTimeout(() => {
|
|
194
|
+
const ready = determineOrderReadiness(params);
|
|
195
|
+
setEnableProcessOrderBtn(ready);
|
|
196
|
+
setProcessingValidation(false);
|
|
197
|
+
}, 300);
|
|
198
|
+
return () => clearTimeout(timeoutId);
|
|
199
|
+
}, [
|
|
200
|
+
params.billingAddress,
|
|
201
|
+
params.shippingAddress,
|
|
202
|
+
params.isEditShippingInfo,
|
|
203
|
+
params.isEditShippingMethods,
|
|
204
|
+
params.isEditBillingInfo,
|
|
205
|
+
params.editCc,
|
|
206
|
+
params.isFreeShipping,
|
|
207
|
+
params.unifiedPaymentAccountId,
|
|
208
|
+
params.unifiedBilling,
|
|
209
|
+
params.paymentMethod,
|
|
210
|
+
params.processingOrder,
|
|
211
|
+
params.carrierId,
|
|
212
|
+
params.shippingFee,
|
|
213
|
+
params.shippingMethodSaved,
|
|
214
|
+
params.showBillingInfoSummary,
|
|
215
|
+
params.cartHdrUid
|
|
216
|
+
]);
|
|
217
|
+
return { enableProcessOrderBtn, processingValidation };
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
exports.usePaymentFlow = usePaymentFlow; exports.determineOrderReadiness = determineOrderReadiness; exports.useOrderReadiness = useOrderReadiness;
|
|
225
|
+
//# sourceMappingURL=chunk-SSOW6W53.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/augur-packages/augur-packages/packages/augur-hooks/dist/chunk-SSOW6W53.cjs","../src/hooks/use-payment-flow.ts","../src/hooks/use-order-readiness.ts"],"names":["useState","useEffect"],"mappings":"AAAA;ACAA,8BAAiD;AA8E1C,SAAS,cAAA,CAAe;AAAA,EAC7B,SAAA;AAAA,EACA,UAAA;AAAA,EACA,cAAA;AAAA,EACA,aAAA;AAAA,EACA;AACF,CAAA,EAAyB;AACvB,EAAA,MAAM,CAAC,MAAA,EAAQ,SAAS,EAAA,EAAI,6BAAA,KAAc,CAAA;AAC1C,EAAA,MAAM,CAAC,OAAA,EAAS,UAAU,EAAA,EAAI,6BAAA,IAAuC,CAAA;AACrE,EAAA,MAAM,CAAC,SAAA,EAAW,YAAY,EAAA,EAAI,6BAAA,IAA+B,CAAA;AACjE,EAAA,MAAM,CAAC,eAAA,EAAiB,kBAAkB,EAAA,EAAI,6BAAA,KAAc,CAAA;AAC5D,EAAA,MAAM,CAAC,cAAA,EAAgB,iBAAiB,EAAA,EAAI,6BAAA,KAAc,CAAA;AAC1D,EAAA,MAAM,CAAC,cAAA,EAAgB,iBAAiB,EAAA,EAAI,6BAAA,IAAmC,CAAA;AAC/E,EAAA,MAAM,CAAC,WAAA,EAAa,cAAc,EAAA,EAAI,6BAAA,IAA4B,CAAA;AAClE,EAAA,MAAM,CAAC,MAAA,EAAQ,SAAS,EAAA,EAAI,6BAAA,KAAc,CAAA;AAE1C,EAAA,MAAM,iBAAA,EAAmB,gCAAA,CAAa,GAAA,EAAA,GAAgB;AACpD,IAAA,cAAA,CAAe,GAAG,CAAA;AAAA,EACpB,CAAA,EAAG,CAAC,CAAC,CAAA;AAGL,EAAA,MAAM,YAAA,EAAc,gCAAA,MAAY,CAAA,EAAA,GAAY;AAC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,KAAK,CAAA;AAC9B,MAAA,uBAAA,CAAwB,KAAK,CAAA;AAAA,IAC/B,CAAA,EAAG,GAAG,CAAA;AAEN,IAAA,OAAO,CAAA,EAAA,GAAM,YAAA,CAAa,SAAS,CAAA;AAAA,EAErC,CAAA,EAAG;AAAA,IACD,MAAA,CAAO,cAAA;AAAA,IACP,MAAA,CAAO,eAAA;AAAA,IACP,MAAA,CAAO,kBAAA;AAAA,IACP,MAAA,CAAO,qBAAA;AAAA,IACP,MAAA,CAAO,iBAAA;AAAA,IACP,MAAA,CAAO,MAAA;AAAA,IACP,MAAA,CAAO,cAAA;AAAA,IACP,MAAA,CAAO,uBAAA;AAAA,IACP,MAAA,CAAO,cAAA;AAAA,IACP,MAAA,CAAO,aAAA;AAAA,IACP,MAAA,CAAO,eAAA;AAAA,IACP,MAAA,CAAO,SAAA;AAAA,IACP,MAAA,CAAO,WAAA;AAAA,IACP,MAAA,CAAO,mBAAA;AAAA,IACP,MAAA,CAAO,sBAAA;AAAA,IACP,MAAA,CAAO;AAAA,EACT,CAAC,CAAA;AAED,EAAA,OAAO,EAAE,qBAAA,EAAuB,qBAAqB,CAAA;AACvD;AF0GA;AACA;AACE;AACA;AACA;AACF,kJAAC","file":"/home/runner/work/augur-packages/augur-packages/packages/augur-hooks/dist/chunk-SSOW6W53.cjs","sourcesContent":[null,"import { useCallback, useEffect, useState } from \"react\";\nimport type {\n TTransactionSetup,\n TCardInfo,\n TPaymentValidation,\n TAccountQuery,\n TBillingUpdate,\n} from \"@simpleapps-com/augur-utils\";\n\n/** Site-specific payment action callbacks injected into usePaymentFlow. */\nexport interface PaymentFlowCallbacks {\n /** Initialize a payment session. Returns transaction setup or undefined on failure. */\n transactionSetup: (params: { customerId: string }) => Promise<TTransactionSetup | undefined>;\n /** Retrieve card details after iframe entry. */\n cardInfo: (params: { transactionSetupId: string }) => Promise<TCardInfo | undefined>;\n /** Verify card is valid. */\n validatePayment: (params: {\n transactionSetupId: string;\n customerId: string;\n }) => Promise<TPaymentValidation | undefined>;\n /** Get payment account ID. */\n accountQuery: (params: {\n transactionSetupId: string;\n customerId: string;\n }) => Promise<TAccountQuery | undefined>;\n /** Send billing address to payment processor. */\n billingUpdate: (params: {\n transactionSetupId: string;\n address1: string;\n address2?: string;\n city: string;\n state: string;\n zip: string;\n }) => Promise<TBillingUpdate | undefined>;\n /** Optional toast notification callback. */\n toast?: {\n error?: (message: string) => void;\n };\n}\n\n/** Billing address shape accepted by the payment flow. */\nexport interface PaymentBillingAddress {\n address1: string;\n address2?: string;\n city: string;\n state: string;\n postalCode: string;\n}\n\ninterface UsePaymentFlowParams {\n callbacks: PaymentFlowCallbacks;\n customerId: string | number | undefined;\n billingAddress: PaymentBillingAddress | undefined;\n paymentMethod: string;\n shippingAccepted: boolean;\n}\n\n/**\n * Manages the full Element/Unified payment lifecycle:\n * transactionSetup -> cardInfo -> validatePayment -> accountQuery -> billingUpdate\n *\n * Accepts site-specific server action callbacks so the hook has no dependency\n * on any particular server action implementation.\n *\n * @example\n * ```tsx\n * import { usePaymentFlow } from \"@simpleapps-com/augur-hooks\";\n * import { transactionSetup, cardInfo, validatePayment, accountQuery, billingUpdate } from \"@/lib/actions/payments\";\n *\n * const payment = usePaymentFlow({\n * callbacks: { transactionSetup, cardInfo, validatePayment, accountQuery, billingUpdate },\n * customerId,\n * billingAddress,\n * paymentMethod: \"credit-card\",\n * shippingAccepted: true,\n * });\n * ```\n */\nexport function usePaymentFlow({\n callbacks,\n customerId,\n billingAddress,\n paymentMethod,\n shippingAccepted,\n}: UsePaymentFlowParams) {\n const [isOpen, setIsOpen] = useState(false);\n const [unified, setUnified] = useState<TTransactionSetup | null>(null);\n const [unifiedCc, setUnifiedCc] = useState<TCardInfo | null>(null);\n const [unifiedResponse, setUnifiedResponse] = useState(false);\n const [unifiedBilling, setUnifiedBilling] = useState(false);\n const [unifiedPayment, setUnifiedPayment] = useState<TAccountQuery | null>(null);\n const [capturedBin, setCapturedBin] = useState<string | null>(null);\n const [editCc, setEditCc] = useState(false);\n\n const handleBinCapture = useCallback((bin: string) => {\n setCapturedBin(bin);\n }, []);\n\n // Step 1: Initialize payment session\n const postUnified = useCallback(async () => {\n if (!customerId) return;\n try {\n const result = await callbacks.transactionSetup({ customerId: String(customerId) });\n if (!result) {\n callbacks.toast?.error?.(\"Unable to initialize payment. Please try again.\");\n return;\n }\n setUnified(result);\n } catch (error) {\n console.error(\"transactionSetup failed:\", error);\n callbacks.toast?.error?.(\"Unable to initialize payment. Please try again.\");\n }\n }, [customerId, callbacks]);\n\n // Step 2: Get card info after iframe entry\n const getCardInfo = useCallback(async () => {\n /* v8 ignore next -- defensive: caller (verifyUnified) already guards */\n if (!unified?.transactionSetupId) return;\n try {\n const result = await callbacks.cardInfo({ transactionSetupId: unified.transactionSetupId });\n if (!result) {\n callbacks.toast?.error?.(\"Unable to verify card details. Please try again.\");\n return;\n }\n setUnifiedCc(result);\n } catch (error) {\n console.error(\"cardInfo failed:\", error);\n callbacks.toast?.error?.(\"Unable to verify card details. Please try again.\");\n }\n }, [unified, callbacks]);\n\n // Step 3: Validate payment\n const verifyUnified = useCallback(async () => {\n if (!unified?.transactionSetupId || !customerId) return;\n try {\n const result = await callbacks.validatePayment({\n transactionSetupId: unified.transactionSetupId,\n customerId: String(customerId),\n });\n if (!result) {\n callbacks.toast?.error?.(\"Payment verification failed. Please try again.\");\n return;\n }\n setUnifiedResponse(result.valid);\n if (result.valid) {\n await getCardInfo();\n }\n } catch (error) {\n console.error(\"validatePayment failed:\", error);\n callbacks.toast?.error?.(\"Payment verification failed. Please try again.\");\n }\n }, [unified, customerId, getCardInfo, callbacks]);\n\n // Step 4: Get payment account info\n const getUnifiedPayment = useCallback(async () => {\n if (!unified?.transactionSetupId || !customerId) return;\n try {\n const result = await callbacks.accountQuery({\n transactionSetupId: unified.transactionSetupId,\n customerId: String(customerId),\n });\n if (!result) {\n callbacks.toast?.error?.(\"Unable to retrieve payment account. Please try again.\");\n return;\n }\n const payment = capturedBin && !result.Bin ? { ...result, Bin: capturedBin } : result;\n setUnifiedPayment(payment);\n } catch (error) {\n console.error(\"accountQuery failed:\", error);\n callbacks.toast?.error?.(\"Unable to retrieve payment account. Please try again.\");\n }\n }, [unified, customerId, capturedBin, callbacks]);\n\n // Step 5: Update billing info\n const updateUnifiedBilling = useCallback(async () => {\n /* v8 ignore next -- defensive: effect already guards billingAddress/unified */\n if (!unified?.transactionSetupId || !billingAddress) return;\n try {\n const result = await callbacks.billingUpdate({\n transactionSetupId: unified.transactionSetupId,\n address1: billingAddress.address1,\n address2: billingAddress.address2,\n city: billingAddress.city,\n state: billingAddress.state,\n zip: billingAddress.postalCode,\n });\n if (!result) {\n callbacks.toast?.error?.(\"Billing update failed. Please try again.\");\n return;\n }\n setUnifiedBilling(true);\n void getUnifiedPayment();\n } catch (error) {\n console.error(\"billingUpdate failed:\", error);\n callbacks.toast?.error?.(\"Billing update failed. Please try again.\");\n }\n }, [unified, billingAddress, getUnifiedPayment, callbacks]);\n\n // Close payment modal if preconditions are lost\n useEffect(() => {\n if (isOpen && (!shippingAccepted || paymentMethod !== \"credit-card\")) {\n setIsOpen(false);\n }\n }, [isOpen, shippingAccepted, paymentMethod]);\n\n // Initialize payment setup when modal opens with no transaction\n useEffect(() => {\n if (isOpen && shippingAccepted && paymentMethod === \"credit-card\" && unified === null) {\n void postUnified();\n }\n }, [isOpen, shippingAccepted, paymentMethod, unified, postUnified]);\n\n // Verify payment after modal closes\n useEffect(() => {\n if (!isOpen && unified !== null && shippingAccepted && paymentMethod === \"credit-card\") {\n void verifyUnified();\n }\n }, [isOpen, unified, shippingAccepted, paymentMethod, verifyUnified]);\n\n // Trigger billing update when card verified + billing address ready\n useEffect(() => {\n if (\n unifiedResponse &&\n billingAddress !== undefined &&\n unifiedCc?.transactionSetupId &&\n paymentMethod === \"credit-card\"\n ) {\n void updateUnifiedBilling();\n }\n }, [\n unifiedResponse,\n billingAddress,\n unifiedCc?.transactionSetupId,\n updateUnifiedBilling,\n paymentMethod,\n ]);\n\n // Reset all payment state when editing CC\n useEffect(() => {\n if (editCc) {\n setIsOpen(true);\n setUnified(null);\n setUnifiedCc(null);\n setUnifiedPayment(null);\n setUnifiedBilling(false);\n setUnifiedResponse(false);\n setCapturedBin(null);\n }\n }, [editCc]);\n\n return {\n isOpen,\n setIsOpen,\n editCc,\n setEditCc,\n unified,\n unifiedCc,\n unifiedResponse,\n unifiedBilling,\n unifiedPayment,\n capturedBin,\n handleBinCapture,\n };\n}\n","\"use client\";\n\nimport { useEffect, useState } from \"react\";\n\n/** Parameters for order readiness validation. */\nexport interface OrderReadinessParams {\n cartHdrUid: string | number | undefined;\n shippingAddress: unknown;\n billingAddress: unknown;\n shippingMethodSaved: boolean;\n showBillingInfoSummary: boolean;\n processingOrder: boolean;\n paymentMethod: string;\n unifiedPaymentAccountId: string | undefined;\n unifiedBilling: boolean;\n isFreeShipping: boolean;\n carrierId: string;\n shippingFee: number | undefined;\n isEditShippingInfo: boolean;\n isEditShippingMethods: boolean;\n isEditBillingInfo: boolean;\n editCc: boolean;\n /** Site-provided function to check if the order is ready. Overrides default logic when provided. */\n customReadinessCheck?: (params: OrderReadinessParams) => boolean;\n}\n\n/**\n * Pure function that determines if an order is ready to submit.\n *\n * Checks all gates: cart exists, addresses present, shipping method saved,\n * billing shown, payment complete (for credit card), shipping fee present\n * (for non-free, non-pickup orders), and no sections being edited.\n */\nexport function determineOrderReadiness(params: OrderReadinessParams): boolean {\n if (params.customReadinessCheck) {\n return params.customReadinessCheck(params);\n }\n\n if (!params.cartHdrUid) return false;\n\n // Block if any section is being edited or addresses are missing\n const blockers = [\n params.isEditShippingInfo,\n params.isEditShippingMethods,\n params.isEditBillingInfo,\n params.editCc,\n params.shippingAddress === undefined,\n params.billingAddress === undefined,\n !params.shippingMethodSaved,\n !params.showBillingInfoSummary,\n params.processingOrder,\n ];\n if (blockers.some(Boolean)) return false;\n\n // Credit card requires completed payment flow\n if (\n params.paymentMethod === \"credit-card\" &&\n (!params.unifiedPaymentAccountId || !params.unifiedBilling)\n ) {\n return false;\n }\n\n // Non-free shipping requires a fee (unless carrier is pickup/freight)\n if (!params.isFreeShipping && params.shippingFee === undefined) {\n return false;\n }\n\n return true;\n}\n\n/**\n * Debounced order readiness hook.\n *\n * Recomputes readiness 300ms after any dependency changes to avoid\n * excessive re-renders during rapid state transitions (e.g., typing\n * in address fields).\n */\nexport function useOrderReadiness(params: OrderReadinessParams) {\n const [enableProcessOrderBtn, setEnableProcessOrderBtn] = useState(false);\n const [processingValidation, setProcessingValidation] = useState(false);\n\n useEffect(() => {\n setProcessingValidation(true);\n\n const timeoutId = setTimeout(() => {\n const ready = determineOrderReadiness(params);\n setEnableProcessOrderBtn(ready);\n setProcessingValidation(false);\n }, 300);\n\n return () => clearTimeout(timeoutId);\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [\n params.billingAddress,\n params.shippingAddress,\n params.isEditShippingInfo,\n params.isEditShippingMethods,\n params.isEditBillingInfo,\n params.editCc,\n params.isFreeShipping,\n params.unifiedPaymentAccountId,\n params.unifiedBilling,\n params.paymentMethod,\n params.processingOrder,\n params.carrierId,\n params.shippingFee,\n params.shippingMethodSaved,\n params.showBillingInfoSummary,\n params.cartHdrUid,\n ]);\n\n return { enableProcessOrderBtn, processingValidation };\n}\n"]}
|