-
Notifications
You must be signed in to change notification settings - Fork 38
fix(checkout-widgets): Ensure error view is shown when sale processing fails #2805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b8d24af
505e188
d22ee5e
66c91ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,35 @@ export type ConfigError = { | |
| data?: Record<string, unknown>; | ||
| }; | ||
|
|
||
| /** | ||
| * Validates the order quote response before use. Ensures: | ||
| * - Currencies are non-empty (empty usually indicates wrong project config or endpoint). | ||
| * - Products are non-empty. | ||
| * - Every sale item has a matching product in the quote (no missing productIds). | ||
| */ | ||
| function validateOrderQuote( | ||
| config: OrderQuote, | ||
| items: SaleItem[], | ||
| ): { valid: true } | { valid: false; reason: string } { | ||
| if (!config.currencies?.length) { | ||
| return { valid: false, reason: 'Quote returned no currencies' }; | ||
| } | ||
|
|
||
| const productIds = Object.keys(config.products || {}); | ||
| if (productIds.length === 0) { | ||
| return { valid: false, reason: 'Quote returned no products' }; | ||
| } | ||
|
|
||
| const missing = items.filter((item) => !config.products![item.productId]); | ||
| if (missing.length > 0) { | ||
| return { | ||
| valid: false, | ||
| reason: `Quote missing products for: ${missing.map((m) => m.productId).join(', ')}`, | ||
| }; | ||
| } | ||
| return { valid: true }; | ||
| } | ||
|
|
||
| export const useQuoteOrder = ({ | ||
| items, | ||
| environment, | ||
|
|
@@ -53,6 +82,13 @@ export const useQuoteOrder = ({ | |
| }); | ||
| }; | ||
|
|
||
| const setQuoteValidationError = (reason: string) => { | ||
| setOrderQuoteError({ | ||
| type: SaleErrorTypes.SERVICE_BREAKDOWN, | ||
| data: { reason: 'Invalid order quote response', error: reason }, | ||
| }); | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| // Set request params | ||
| if (!items?.length || !provider) return; | ||
|
|
@@ -110,14 +146,21 @@ export const useQuoteOrder = ({ | |
| await response.json(), | ||
| preferredCurrency, | ||
| ); | ||
|
|
||
| const validation = validateOrderQuote(config, items); | ||
| if (!validation.valid) { | ||
| setQuoteValidationError(validation.reason); | ||
| return; | ||
| } | ||
|
|
||
| setOrderQuote(config); | ||
| } catch (error) { | ||
| setError(errorToString(error)); | ||
| } finally { | ||
| fetching.current = false; | ||
| } | ||
| })(); | ||
| }, [environment, environmentId, queryParams]); | ||
| }, [environment, environmentId, queryParams, items]); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding items to the useEffect dependency array will re-fetch the quote whenever items changes. Was that intentional?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. The quote is based on the items, so it should be refetched if those change. |
||
|
|
||
| useEffect(() => { | ||
| // Set default currency | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really want to send this to prod?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. We don't show the error message in the UI, but the console logs will at least help developers debug the reason.