Skip to content

Commit b1079cd

Browse files
authored
feat: Fallback to Customer Address if default payment method address doesn't exist (#3878)
1 parent ac06400 commit b1079cd

File tree

5 files changed

+81
-24
lines changed

5 files changed

+81
-24
lines changed

src/pages/PlanPage/subRoutes/CurrentOrgPlan/BillingDetails/Address/AddressCard.test.tsx

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,23 @@ const subscriptionDetail = {
4343
cancelAtPeriodEnd: false,
4444
} as z.infer<typeof SubscriptionDetailSchema>
4545

46+
const subscriptionDetailWithCustomer = {
47+
currentPeriodEnd: 1606851492,
48+
cancelAtPeriodEnd: false,
49+
defaultPaymentMethod: null,
50+
customer: {
51+
address: {
52+
line1: '456 Main St.',
53+
line2: null,
54+
city: 'San Francisco',
55+
country: 'US',
56+
state: 'CA',
57+
postalCode: '12345',
58+
},
59+
name: 'Bob Smith Jr.',
60+
},
61+
} as z.infer<typeof SubscriptionDetailSchema>
62+
4663
const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
4764
<ThemeContextProvider>{children}</ThemeContextProvider>
4865
)
@@ -97,14 +114,14 @@ describe('AddressCard', () => {
97114
})
98115
})
99116

100-
describe(`when the user doesn't have billing details`, () => {
117+
describe(`when the user doesn't have billing details or customer`, () => {
101118
it('renders an error message', () => {
102119
render(
103120
<AddressCard
104121
// @ts-expect-error weird param funkiness
105122
subscriptionDetail={{
106-
...subscriptionDetail,
107123
defaultPaymentMethod: null,
124+
customer: null,
108125
}}
109126
provider="gh"
110127
owner="codecov"
@@ -126,8 +143,8 @@ describe('AddressCard', () => {
126143
<AddressCard
127144
// @ts-expect-error weird param funkiness
128145
subscriptionDetail={{
129-
...subscriptionDetail,
130146
defaultPaymentMethod: null,
147+
customer: null,
131148
}}
132149
provider="gh"
133150
owner="codecov"
@@ -152,8 +169,8 @@ describe('AddressCard', () => {
152169
<AddressCard
153170
// @ts-expect-error weird param funkiness
154171
subscriptionDetail={{
155-
...subscriptionDetail,
156172
defaultPaymentMethod: null,
173+
customer: null,
157174
}}
158175
provider="gh"
159176
owner="codecov"
@@ -231,6 +248,23 @@ describe('AddressCard', () => {
231248
expect(screen.getByText('12345')).toBeInTheDocument()
232249
})
233250

251+
it('can render information from the customer', () => {
252+
render(
253+
<AddressCard
254+
subscriptionDetail={subscriptionDetailWithCustomer}
255+
provider="gh"
256+
owner="codecov"
257+
/>,
258+
{ wrapper }
259+
)
260+
261+
expect(screen.getByText('Full name')).toBeInTheDocument()
262+
expect(screen.getByText('Bob Smith Jr.')).toBeInTheDocument()
263+
expect(screen.getByText('Billing address')).toBeInTheDocument()
264+
expect(screen.getByText('456 Main St.')).toBeInTheDocument()
265+
expect(screen.getByText('San Francisco, CA 12345')).toBeInTheDocument()
266+
})
267+
234268
it('renders the card holder information', () => {
235269
render(
236270
<AddressCard

src/pages/PlanPage/subRoutes/CurrentOrgPlan/BillingDetails/Address/AddressCard.tsx

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useState } from 'react'
22
import { z } from 'zod'
33

44
import {
5-
BillingDetailsSchema,
5+
AddressSchema,
66
SubscriptionDetailSchema,
77
} from 'services/account/useAccountDetails'
88
import A from 'ui/A'
@@ -23,15 +23,20 @@ function AddressCard({
2323
owner,
2424
}: AddressCardProps) {
2525
const [isFormOpen, setIsFormOpen] = useState(false)
26-
const billingDetails =
27-
subscriptionDetail?.defaultPaymentMethod?.billingDetails
26+
const address =
27+
subscriptionDetail?.defaultPaymentMethod?.billingDetails?.address ||
28+
subscriptionDetail?.customer?.address
29+
30+
const name =
31+
subscriptionDetail?.defaultPaymentMethod?.billingDetails?.name ||
32+
subscriptionDetail?.customer?.name
2833

2934
return (
3035
<div className="flex flex-col gap-2 border-t p-4">
3136
{isFormOpen && (
3237
<AddressForm
33-
name={billingDetails?.name || ''}
34-
address={billingDetails?.address}
38+
name={name || ''}
39+
address={address}
3540
provider={provider}
3641
owner={owner}
3742
closeForm={() => setIsFormOpen(false)}
@@ -52,7 +57,8 @@ function AddressCard({
5257
</A>
5358
</div>
5459
<BillingInner
55-
billingDetails={billingDetails}
60+
address={address}
61+
name={name}
5662
setIsFormOpen={setIsFormOpen}
5763
/>
5864
</>
@@ -62,27 +68,22 @@ function AddressCard({
6268
}
6369

6470
interface BillingInnerProps {
65-
billingDetails?: z.infer<typeof BillingDetailsSchema>
71+
address?: z.infer<typeof AddressSchema> | null
72+
name?: string | null
6673
setIsFormOpen: (val: boolean) => void
6774
}
6875

69-
function BillingInner({ billingDetails, setIsFormOpen }: BillingInnerProps) {
70-
if (billingDetails) {
76+
function BillingInner({ address, name, setIsFormOpen }: BillingInnerProps) {
77+
if (address) {
7178
return (
7279
<div>
73-
<p>{`${billingDetails.name ?? 'N/A'}`}</p>
80+
<p>{`${name ?? 'N/A'}`}</p>
7481
<br />
7582
<h4 className="mb-2 font-semibold">Billing address</h4>
76-
<p>{`${billingDetails.address?.line1 ?? ''} ${
77-
billingDetails.address?.line2 ?? ''
78-
}`}</p>
83+
<p>{`${address?.line1 ?? ''} ${address?.line2 ?? ''}`}</p>
7984
<p>
80-
{billingDetails.address?.city
81-
? `${billingDetails.address?.city}, `
82-
: ''}
83-
{`${billingDetails.address?.state ?? ''} ${
84-
billingDetails.address?.postalCode ?? ''
85-
}`}
85+
{address?.city ? `${address?.city}, ` : ''}
86+
{`${address?.state ?? ''} ${address?.postalCode ?? ''}`}
8687
</p>
8788
</div>
8889
)

src/pages/PlanPage/subRoutes/CurrentOrgPlan/BillingDetails/PaymentCard/PaymentMethodForm.test.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ const subscriptionDetail: z.infer<typeof SubscriptionDetailSchema> = {
6363
customer: {
6464
id: 'cust_123',
6565
66+
address: null,
67+
name: null,
6668
},
6769
latestInvoice: null,
6870
taxIds: [],

src/services/account/mocks.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ export const accountDetailsObject = {
118118
expires: 1,
119119
},
120120
121+
address: {
122+
city: 'San Francisco',
123+
country: 'US',
124+
line1: '123 Main St',
125+
line2: null,
126+
postal_code: '94101',
127+
state: 'CA',
128+
},
129+
name: 'John Doe',
121130
},
122131
collection_method: null,
123132
},
@@ -219,6 +228,15 @@ export const accountDetailsParsedObj = {
219228
expires: 1,
220229
},
221230
231+
address: {
232+
city: 'San Francisco',
233+
country: 'US',
234+
line1: '123 Main St',
235+
line2: null,
236+
postalCode: '94101',
237+
state: 'CA',
238+
},
239+
name: 'John Doe',
222240
},
223241
collectionMethod: null,
224242
},

src/services/account/useAccountDetails.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ export const SubscriptionDetailSchema = z
9393
currentPeriodEnd: z.number(),
9494
customer: z
9595
.object({
96+
address: AddressSchema.nullable(),
97+
email: z.string().nullable(),
9698
id: z.string(),
9799
discount: z
98100
.object({
@@ -102,7 +104,7 @@ export const SubscriptionDetailSchema = z
102104
expires: z.number().nullable(),
103105
})
104106
.nullish(),
105-
email: z.string(),
107+
name: z.string().nullable(),
106108
})
107109
.nullable(),
108110
defaultPaymentMethod: PaymentMethodSchema.nullable(),

0 commit comments

Comments
 (0)