Payment.utils.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import type { Appearance, CustomFontSource } from '@stripe/stripe-js'
  2. import { CUSTOM_FONT_BOOK_DATA_URL } from '@/fonts/stripe-fonts'
  3. /**
  4. * Custom font for Stripe Elements iframes.
  5. * Stripe renders inside an iframe that can't access the parent page's CSS variables or fonts,
  6. * so we pass the font explicitly (Stripe requires https:// or data:// sources).
  7. * Keep using the data URL in all environments for now, since the CDN-hosted font still has
  8. * CORS issues when Stripe loads it from inside its iframe.
  9. */
  10. const fontSrc = `url(${CUSTOM_FONT_BOOK_DATA_URL})`
  11. export const STRIPE_ELEMENT_FONTS: CustomFontSource[] = [
  12. {
  13. family: 'CustomFont',
  14. src: fontSrc,
  15. },
  16. ]
  17. export const getStripeElementsAppearanceOptions = (
  18. resolvedTheme: string | undefined
  19. ): Appearance => {
  20. return {
  21. labels: 'floating',
  22. theme: (resolvedTheme?.includes('dark') ? 'night' : 'flat') as 'night' | 'flat',
  23. variables: {
  24. fontSizeBase: '14px',
  25. colorBackground: resolvedTheme?.includes('dark')
  26. ? 'hsl(0deg 0% 14.1%)'
  27. : 'hsl(0deg 0% 95.3%)',
  28. fontFamily: 'CustomFont, Helvetica Neue, Helvetica, Arial, sans-serif',
  29. },
  30. rules: {
  31. '.TermsText': {
  32. fontSize: '12px',
  33. },
  34. '.Label--floating': {
  35. fontSize: '14px',
  36. },
  37. '.Label--resting': {
  38. fontSize: '14px',
  39. color: 'rgb(137, 137, 137)',
  40. },
  41. '.Input': {
  42. boxShadow: 'none',
  43. height: '34px',
  44. lineHeight: '16px',
  45. padding: '8px 12px',
  46. },
  47. '.AccordionItem': {
  48. boxShadow: 'none',
  49. },
  50. },
  51. }
  52. }
  53. export const getAddressElementAppearanceOptions = (
  54. resolvedTheme: string | undefined
  55. ): Appearance => {
  56. const isDark = resolvedTheme?.includes('dark')
  57. return {
  58. labels: 'above' as const,
  59. theme: (isDark ? 'night' : 'flat') as 'night' | 'flat',
  60. variables: {
  61. fontSizeBase: '14px',
  62. spacingUnit: '7px',
  63. colorPrimary: isDark ? 'hsl(0deg 0% 32%)' : 'hsl(0deg 0% 55%)',
  64. colorBackground: isDark ? 'hsl(0deg 0% 14.1%)' : 'hsl(0deg 0% 95.3%)',
  65. borderRadius: '4px',
  66. fontFamily: 'CustomFont, Helvetica Neue, Helvetica, Arial, sans-serif',
  67. },
  68. rules: {
  69. '.Input': {
  70. boxShadow: isDark ? 'none' : 'inset 0 0 0 1px hsl(0deg 0% 80%)',
  71. height: '34px',
  72. lineHeight: '16px',
  73. padding: '8px 12px',
  74. borderWidth: '1px',
  75. },
  76. },
  77. }
  78. }