Skip to content

feat(clerk-js,localizations,types): Pay with test card button on <AddPaymentSource /> component #5831

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

Merged
merged 1 commit into from
May 5, 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
7 changes: 7 additions & 0 deletions .changeset/tiny-wolves-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@clerk/localizations': patch
'@clerk/clerk-js': patch
'@clerk/types': patch
---

Add `Pay with test card` button on `<AddPaymentSource />` component in dev instance
4 changes: 2 additions & 2 deletions packages/clerk-js/bundlewatch.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
{ "path": "./dist/waitlist*.js", "maxSize": "1.3KB" },
{ "path": "./dist/keylessPrompt*.js", "maxSize": "6.5KB" },
{ "path": "./dist/pricingTable*.js", "maxSize": "4.02KB" },
{ "path": "./dist/checkout*.js", "maxSize": "5.75KB" },
{ "path": "./dist/paymentSources*.js", "maxSize": "8.9KB" },
{ "path": "./dist/checkout*.js", "maxSize": "5.79KB" },
{ "path": "./dist/paymentSources*.js", "maxSize": "9.06KB" },
{ "path": "./dist/up-billing-page*.js", "maxSize": "2.4KB" },
{ "path": "./dist/op-billing-page*.js", "maxSize": "2.4KB" },
{ "path": "./dist/sessionTasks*.js", "maxSize": "1KB" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ export const CheckoutComplete = ({
text={
checkout.totals.totalDueNow.amount > 0
? checkout.paymentSource
? `${capitalize(checkout.paymentSource.cardType)} ⋯ ${checkout.paymentSource.last4}`
? checkout.paymentSource.paymentMethod !== 'card'
? `${capitalize(checkout.paymentSource.paymentMethod)}`
: `${capitalize(checkout.paymentSource.cardType)} ⋯ ${checkout.paymentSource.last4}`
: '–'
: checkout.subscription?.periodStart
? formatDate(new Date(checkout.subscription.periodStart))
Expand Down
23 changes: 22 additions & 1 deletion packages/clerk-js/src/ui/components/Checkout/CheckoutForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ const CheckoutFormElements = ({
setIsSubmitting(false);
};

const onPayWithTestPaymentSourceSuccess = async () => {
try {
const newCheckout = await checkout.confirm({
gateway: 'stripe',
useTestCard: true,
...(subscriberType === 'org' ? { orgId: organization?.id } : {}),
});
onCheckoutComplete(newCheckout);
void revalidatePaymentSources();
} catch (error) {
handleError(error, [], setSubmitError);
}
};

return (
<Col
elementDescriptor={descriptors.checkoutFormElementsRoot}
Expand Down Expand Up @@ -212,6 +226,7 @@ const CheckoutFormElements = ({
<AddPaymentSource
checkout={checkout}
onSuccess={onAddPaymentSourceSuccess}
onPayWithTestPaymentSourceSuccess={onPayWithTestPaymentSourceSuccess}
// @ts-ignore TODO(@COMMERCE): needs localization
submitLabel={
checkout.totals.totalDueNow.amount > 0
Expand All @@ -225,6 +240,7 @@ const CheckoutFormElements = ({
}
submitError={submitError}
setSubmitError={setSubmitError}
showPayWithTestCardSection
/>
)}
</Col>
Expand Down Expand Up @@ -252,9 +268,14 @@ const ExistingPaymentSourceForm = ({

const options = useMemo(() => {
return paymentSources.map(source => {
const label =
source.paymentMethod !== 'card'
? `${capitalize(source.paymentMethod)}`
: `${capitalize(source.cardType)} ⋯ ${source.last4}`;

return {
value: source.id,
label: `${capitalize(source.cardType)} ⋯ ${source.last4}`,
label,
};
});
}, [paymentSources]);
Expand Down
204 changes: 148 additions & 56 deletions packages/clerk-js/src/ui/components/PaymentSources/AddPaymentSource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useEffect, useRef, useState } from 'react';

import { clerkUnsupportedEnvironmentWarning } from '../../../core/errors';
import { useEnvironment, useSubscriberTypeContext } from '../../contexts';
import { Box, descriptors, Flex, localizationKeys, Spinner, Text, useAppearance } from '../../customizables';
import { Box, Button, descriptors, Flex, localizationKeys, Spinner, Text, useAppearance } from '../../customizables';
import { Alert, Form, FormButtons, FormContainer, LineItems, withCardStateProvider } from '../../elements';
import { useFetch } from '../../hooks/useFetch';
import type { LocalizationKey } from '../../localization';
Expand All @@ -22,10 +22,21 @@ type AddPaymentSourceProps = {
submitError?: ClerkRuntimeError | ClerkAPIError | string | undefined;
setSubmitError?: (submitError: ClerkRuntimeError | ClerkAPIError | string | undefined) => void;
resetStripeElements?: () => void;
onPayWithTestPaymentSourceSuccess?: () => void;
showPayWithTestCardSection?: boolean;
};

export const AddPaymentSource = (props: AddPaymentSourceProps) => {
const { checkout, submitLabel, onSuccess, cancelAction, submitError, setSubmitError } = props;
const {
checkout,
submitLabel,
onSuccess,
cancelAction,
submitError,
setSubmitError,
onPayWithTestPaymentSourceSuccess,
showPayWithTestCardSection,
} = props;
const { __experimental_commerce } = useClerk();
const { __experimental_commerceSettings } = useEnvironment();
const { organization } = useOrganization();
Expand Down Expand Up @@ -137,6 +148,8 @@ export const AddPaymentSource = (props: AddPaymentSourceProps) => {
submitError={submitError}
setSubmitError={setSubmitError}
resetStripeElements={resetStripeElements}
onPayWithTestPaymentSourceSuccess={onPayWithTestPaymentSourceSuccess}
showPayWithTestCardSection={showPayWithTestCardSection}
/>
</Elements>
);
Expand All @@ -151,6 +164,8 @@ const AddPaymentSourceForm = withCardStateProvider(
submitError,
setSubmitError,
resetStripeElements,
onPayWithTestPaymentSourceSuccess,
showPayWithTestCardSection,
}: AddPaymentSourceProps) => {
const clerk = useClerk();
const stripe = useStripe();
Expand Down Expand Up @@ -218,68 +233,72 @@ const AddPaymentSourceForm = withCardStateProvider(
rowGap: t.space.$3,
})}
>
{clerk.instanceType === 'development' && (
<Box
sx={t => ({
background: t.colors.$neutralAlpha50,
padding: t.space.$2x5,
borderRadius: t.radii.$md,
borderWidth: t.borderWidths.$normal,
borderStyle: t.borderStyles.$solid,
borderColor: t.colors.$neutralAlpha100,
display: 'flex',
flexDirection: 'column',
rowGap: t.space.$2,
position: 'relative',
})}
>
{showPayWithTestCardSection ? (
<PayWithTestPaymentSource onCheckoutComplete={onPayWithTestPaymentSourceSuccess} />
) : (
clerk.instanceType === 'development' && (
<Box
sx={t => ({
position: 'absolute',
inset: 0,
background: `repeating-linear-gradient(-45deg,${t.colors.$warningAlpha100},${t.colors.$warningAlpha100} 6px,${t.colors.$warningAlpha150} 6px,${t.colors.$warningAlpha150} 12px)`,
maskImage: `linear-gradient(transparent 20%, black)`,
pointerEvents: 'none',
})}
/>
<Box
sx={{
background: t.colors.$neutralAlpha50,
padding: t.space.$2x5,
borderRadius: t.radii.$md,
borderWidth: t.borderWidths.$normal,
borderStyle: t.borderStyles.$solid,
borderColor: t.colors.$neutralAlpha100,
display: 'flex',
alignItems: 'baseline',
justifyContent: 'space-between',
}}
flexDirection: 'column',
rowGap: t.space.$2,
position: 'relative',
})}
>
<Text
variant='caption'
colorScheme='body'
>
Test card information
</Text>
<Text
variant='caption'
<Box
sx={t => ({
color: t.colors.$warning500,
fontWeight: t.fontWeights.$semibold,
position: 'absolute',
inset: 0,
background: `repeating-linear-gradient(-45deg,${t.colors.$warningAlpha100},${t.colors.$warningAlpha100} 6px,${t.colors.$warningAlpha150} 6px,${t.colors.$warningAlpha150} 12px)`,
maskImage: `linear-gradient(transparent 20%, black)`,
pointerEvents: 'none',
})}
/>
<Box
sx={{
display: 'flex',
alignItems: 'baseline',
justifyContent: 'space-between',
}}
>
Development mode
</Text>
<Text
variant='caption'
colorScheme='body'
>
Test card information
</Text>
<Text
variant='caption'
sx={t => ({
color: t.colors.$warning500,
fontWeight: t.fontWeights.$semibold,
})}
>
Development mode
</Text>
</Box>
<LineItems.Root>
<LineItems.Group variant='tertiary'>
<LineItems.Title title={'Card number'} />
<LineItems.Description text={'4242 4242 4242 4242'} />
</LineItems.Group>
<LineItems.Group variant='tertiary'>
<LineItems.Title title={'Expiration date'} />
<LineItems.Description text={'11/44'} />
</LineItems.Group>
<LineItems.Group variant='tertiary'>
<LineItems.Title title={'CVC, ZIP'} />
<LineItems.Description text={'Any numbers'} />
</LineItems.Group>
</LineItems.Root>
</Box>
<LineItems.Root>
<LineItems.Group variant='tertiary'>
<LineItems.Title title={'Card number'} />
<LineItems.Description text={'4242 4242 4242 4242'} />
</LineItems.Group>
<LineItems.Group variant='tertiary'>
<LineItems.Title title={'Expiration date'} />
<LineItems.Description text={'11/44'} />
</LineItems.Group>
<LineItems.Group variant='tertiary'>
<LineItems.Title title={'CVC, ZIP'} />
<LineItems.Description text={'Any numbers'} />
</LineItems.Group>
</LineItems.Root>
</Box>
)
)}
<PaymentElement
options={{
Expand Down Expand Up @@ -327,3 +346,76 @@ const AddPaymentSourceForm = withCardStateProvider(
);
},
);

const PayWithTestPaymentSource = withCardStateProvider(
({ onCheckoutComplete }: { onCheckoutComplete?: () => void }) => {
const clerk = useClerk();
const [isSubmitting, setIsSubmitting] = useState(false);

const onPaymentSourceSubmit = (e: React.FormEvent<HTMLButtonElement>) => {
e.preventDefault();
setIsSubmitting(true);

onCheckoutComplete?.();
};

if (clerk.instanceType !== 'development') {
return null;
}

return (
<Box
sx={t => ({
background: t.colors.$neutralAlpha50,
padding: t.space.$2x5,
borderRadius: t.radii.$md,
borderWidth: t.borderWidths.$normal,
borderStyle: t.borderStyles.$solid,
borderColor: t.colors.$neutralAlpha100,
display: 'flex',
flexDirection: 'column',
rowGap: t.space.$2,
position: 'relative',
})}
>
<Box
sx={t => ({
position: 'absolute',
inset: 0,
background: `repeating-linear-gradient(-45deg,${t.colors.$warningAlpha100},${t.colors.$warningAlpha100} 6px,${t.colors.$warningAlpha150} 6px,${t.colors.$warningAlpha150} 12px)`,
maskImage: `linear-gradient(transparent 20%, black)`,
pointerEvents: 'none',
})}
/>
<Flex
sx={t => ({
alignItems: 'center',
justifyContent: 'center',
flexDirection: 'column',
rowGap: t.space.$2,
})}
>
<Text
sx={t => ({
color: t.colors.$warning500,
fontWeight: t.fontWeights.$semibold,
})}
>
Development mode
</Text>
<Button
type='button'
block
variant='bordered'
localizationKey={localizationKeys(
'userProfile.__experimental_billingPage.paymentSourcesSection.payWithTestCardButton',
)}
colorScheme='secondary'
isLoading={isSubmitting}
onClick={onPaymentSourceSubmit}
/>
</Flex>
</Box>
);
},
);
1 change: 1 addition & 0 deletions packages/localizations/src/ar-SA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@ export const arSA: LocalizationResource = {
cancelButton: undefined,
formButtonPrimary__add: undefined,
formButtonPrimary__pay: undefined,
payWithTestCardButton: undefined,
removeResource: {
messageLine1: undefined,
messageLine2: undefined,
Expand Down
1 change: 1 addition & 0 deletions packages/localizations/src/be-BY.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ export const beBY: LocalizationResource = {
cancelButton: undefined,
formButtonPrimary__add: undefined,
formButtonPrimary__pay: undefined,
payWithTestCardButton: undefined,
removeResource: {
messageLine1: undefined,
messageLine2: undefined,
Expand Down
1 change: 1 addition & 0 deletions packages/localizations/src/bg-BG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,7 @@ export const bgBG: LocalizationResource = {
cancelButton: undefined,
formButtonPrimary__add: undefined,
formButtonPrimary__pay: undefined,
payWithTestCardButton: undefined,
removeResource: {
messageLine1: undefined,
messageLine2: undefined,
Expand Down
1 change: 1 addition & 0 deletions packages/localizations/src/ca-ES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ export const caES: LocalizationResource = {
cancelButton: undefined,
formButtonPrimary__add: undefined,
formButtonPrimary__pay: undefined,
payWithTestCardButton: undefined,
removeResource: {
messageLine1: undefined,
messageLine2: undefined,
Expand Down
1 change: 1 addition & 0 deletions packages/localizations/src/cs-CZ.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,7 @@ export const csCZ: LocalizationResource = {
cancelButton: undefined,
formButtonPrimary__add: undefined,
formButtonPrimary__pay: undefined,
payWithTestCardButton: undefined,
removeResource: {
messageLine1: undefined,
messageLine2: undefined,
Expand Down
1 change: 1 addition & 0 deletions packages/localizations/src/da-DK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@ export const daDK: LocalizationResource = {
cancelButton: undefined,
formButtonPrimary__add: undefined,
formButtonPrimary__pay: undefined,
payWithTestCardButton: undefined,
removeResource: {
messageLine1: undefined,
messageLine2: undefined,
Expand Down
1 change: 1 addition & 0 deletions packages/localizations/src/de-DE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ export const deDE: LocalizationResource = {
cancelButton: undefined,
formButtonPrimary__add: undefined,
formButtonPrimary__pay: undefined,
payWithTestCardButton: undefined,
removeResource: {
messageLine1: undefined,
messageLine2: undefined,
Expand Down
Loading