Skip to content
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

chore: Cleanup payment related components #3692

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function PaymentCard({ accountDetails, provider, owner }) {
variant="primary"
onClick={() => setIsFormOpen(true)}
>
Set card
Set payment method
</Button>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { MemoryRouter } from 'react-router-dom'

import { ThemeContextProvider } from 'shared/ThemeContext'
import { Plans } from 'shared/utils/billing'
Expand Down Expand Up @@ -57,6 +58,7 @@ const subscriptionDetail = {

const accountDetails = {
subscriptionDetail,
activatedUserCount: 1,
}

const usBankSubscriptionDetail = {
Expand All @@ -75,7 +77,9 @@ const usBankSubscriptionDetail = {

const wrapper = ({ children }) => (
<QueryClientProvider client={queryClient}>
<ThemeContextProvider>{children}</ThemeContextProvider>
<MemoryRouter>
<ThemeContextProvider>{children}</ThemeContextProvider>
</MemoryRouter>
</QueryClientProvider>
)

Expand Down Expand Up @@ -383,7 +387,13 @@ describe('PaymentCard', () => {

await user.click(screen.getByTestId('edit-payment-method'))

expect(screen.getByText(randomError)).toBeInTheDocument()
expect(
screen.getByText((content) =>
content.includes(
"There's been an error. Please try refreshing your browser"
)
)
).toBeInTheDocument()
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,15 @@ const mocks = {
useUpdatePaymentMethod: vi.fn(),
}

vi.mock('services/account/useUpdatePaymentMethod', () => ({
useUpdatePaymentMethod: () => mocks.useUpdatePaymentMethod(),
}))
vi.mock('services/account/useUpdatePaymentMethod', async () => {
const original = await vi.importActual(
'services/account/useUpdatePaymentMethod'
)
return {
...original,
useUpdatePaymentMethod: () => mocks.useUpdatePaymentMethod(),
}
})

afterEach(() => {
vi.clearAllMocks()
Expand Down Expand Up @@ -214,7 +220,13 @@ describe('PaymentMethodForm', () => {

await user.click(screen.getByTestId('save-payment-method'))

expect(screen.getByText(randomError)).toBeInTheDocument()
expect(
screen.getByText((content) =>
content.includes(
"There's been an error. Please try refreshing your browser"
)
)
).toBeInTheDocument()
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import cs from 'classnames'
import { z } from 'zod'

import { AccountDetailsSchema, BillingDetailsSchema } from 'services/account'
import { useUpdatePaymentMethod } from 'services/account/useUpdatePaymentMethod'
import {
MissingAddressError,
MissingEmailError,
MissingNameError,
useUpdatePaymentMethod,
} from 'services/account/useUpdatePaymentMethod'
import { Provider } from 'shared/api/helpers'
import A from 'ui/A'
import Button from 'ui/Button'

interface PaymentMethodFormProps {
Expand Down Expand Up @@ -81,7 +87,7 @@ const PaymentMethodForm = ({
}}
/>
<p className="mt-1 text-ds-primary-red">
{showError && error?.message}
{showError ? getErrorMessage(error) : null}
</p>
<div className="mb-8 mt-4 flex gap-1">
<Button
Expand Down Expand Up @@ -150,4 +156,27 @@ export const getName = (
)
}

export const getErrorMessage = (error: Error): JSX.Element => {
switch (error.message) {
case MissingNameError:
return <span>Missing name, please edit Full Name</span>
case MissingEmailError:
return <span>Missing email, please edit Email</span>
case MissingAddressError:
return <span>Missing address, please edit Address</span>
default:
return (
<span>
There&apos;s been an error. Please try refreshing your browser, if
this error persists please{' '}
{/* @ts-expect-error ignore until we can convert A component to ts */}
<A to={{ pageName: 'support' }} variant="link">
contact support
</A>
.
</span>
)
}
}

export default PaymentMethodForm
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ describe('CurrentOrgPlan', () => {
)
expect(paymentFailed).toBeInTheDocument()
const contactSupport = await screen.findByText(
'Please try a different card or contact support at [email protected].'
'Please try a different payment method or contact support at [email protected].'
)
expect(contactSupport).toBeInTheDocument()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ const DelinquentAlert = () => {
<Alert variant={'error'}>
<Alert.Title>Your most recent payment failed</Alert.Title>
<Alert.Description>
Please try a different card or contact support at [email protected].
Please try a different payment method or contact support at
[email protected].
</Alert.Description>
</Alert>
<br />
Expand Down
5 changes: 5 additions & 0 deletions src/services/account/useUpdatePaymentMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,8 @@ export function useUpdatePaymentMethod({
},
})
}

// Errors from Stripe api confirmSetup() - unfortunately seems to just be by text, no error codes
export const MissingNameError = `You specified "never" for fields.billing_details.name when creating the payment Element, but did not pass confirmParams.payment_method_data.billing_details.name when calling stripe.confirmSetup(). If you opt out of collecting data via the payment Element using the fields option, the data must be passed in when calling stripe.confirmSetup().`
export const MissingEmailError = `You specified "never" for fields.billing_details.email when creating the payment Element, but did not pass confirmParams.payment_method_data.billing_details.email when calling stripe.confirmSetup(). If you opt out of collecting data via the payment Element using the fields option, the data must be passed in when calling stripe.confirmSetup().`
export const MissingAddressError = `You specified "never" for fields.billing_details.address when creating the payment Element, but did not pass confirmParams.payment_method_data.billing_details.address when calling stripe.confirmSetup(). If you opt out of collecting data via the payment Element using the fields option, the data must be passed in when calling stripe.confirmSetup().`