{"version":3,"file":"billing-redirect-message-86779bcf.mjs","sources":["../../../common/resources/client/billing/checkout/stripe/use-stripe.ts","../../../common/resources/client/billing/checkout/stripe/stripe-elements-form.tsx","../../../common/resources/client/billing/billing-redirect-message.tsx"],"sourcesContent":["import {useEffect, useRef, useState} from 'react';\nimport {loadStripe, Stripe, StripeElements} from '@stripe/stripe-js';\nimport {apiClient} from '@common/http/query-client';\nimport {useSelectedLocale} from '@common/i18n/selected-locale';\nimport {useAuth} from '@common/auth/use-auth';\nimport {useIsDarkMode} from '@common/ui/themes/use-is-dark-mode';\nimport {useSettings} from '@common/core/settings/use-settings';\n\ninterface UseStripeProps {\n type: 'setupIntent' | 'subscription';\n productId?: string | number;\n priceId?: string | number;\n}\nexport function useStripe({type, productId, priceId}: UseStripeProps) {\n const {user} = useAuth();\n const isDarkMode = useIsDarkMode();\n const isInitiatedRef = useRef(false);\n const paymentElementRef = useRef(null);\n const {localeCode} = useSelectedLocale();\n const [stripe, setStripe] = useState(null);\n const [elements, setElements] = useState(null);\n const {\n branding: {site_name},\n billing: {\n stripe_public_key,\n stripe: {enable},\n },\n } = useSettings();\n\n useEffect(() => {\n if (!enable || !stripe_public_key || isInitiatedRef.current) return;\n\n Promise.all([\n // load stripe js library\n loadStripe(stripe_public_key, {\n apiVersion: '2022-08-01',\n locale: localeCode as any,\n }),\n // create partial subscription for clientSecret\n type === 'setupIntent'\n ? createSetupIntent()\n : createSubscription(productId!, priceId),\n ]).then(([stripe, {clientSecret}]) => {\n if (stripe && paymentElementRef.current) {\n const elements = stripe.elements({\n clientSecret,\n appearance: {\n theme: isDarkMode ? 'night' : 'stripe',\n },\n });\n\n // Create and mount the Payment Element\n const paymentElement = elements.create('payment', {\n business: {name: site_name},\n terms: {card: 'never'},\n fields: {\n billingDetails: {\n address: 'auto',\n },\n },\n defaultValues: {\n billingDetails: {\n email: user?.email,\n },\n },\n });\n paymentElement.mount(paymentElementRef.current);\n\n setStripe(stripe);\n setElements(elements);\n }\n });\n\n isInitiatedRef.current = true;\n }, [\n productId,\n stripe_public_key,\n enable,\n isDarkMode,\n localeCode,\n site_name,\n type,\n user?.email,\n ]);\n\n return {\n stripe,\n elements,\n paymentElementRef,\n stripeIsEnabled: stripe_public_key != null && enable,\n };\n}\n\nfunction createSetupIntent(): Promise<{clientSecret: string}> {\n return apiClient.post('billing/stripe/create-setup-intent').then(r => r.data);\n}\n\nfunction createSubscription(\n productId: number | string,\n priceId?: number | string\n): Promise<{clientSecret: string}> {\n return apiClient\n .post('billing/stripe/create-partial-subscription', {\n product_id: productId,\n price_id: priceId,\n })\n .then(r => r.data);\n}\n","import clsx from 'clsx';\nimport {Button} from '@common/ui/buttons/button';\nimport {FormEventHandler, Fragment, ReactNode, useState} from 'react';\nimport {useStripe} from '@common/billing/checkout/stripe/use-stripe';\nimport {Skeleton} from '@common/ui/skeleton/skeleton';\n\ninterface StripeElementsFormProps {\n productId?: string | number;\n priceId?: string | number;\n type: 'setupIntent' | 'subscription';\n submitLabel: ReactNode;\n returnUrl: string;\n}\nexport function StripeElementsForm({\n productId,\n priceId,\n type = 'subscription',\n submitLabel,\n returnUrl,\n}: StripeElementsFormProps) {\n const {stripe, elements, paymentElementRef, stripeIsEnabled} = useStripe({\n type,\n productId,\n priceId,\n });\n const [errorMessage, setErrorMessage] = useState(null);\n const [isSubmitting, setIsSubmitting] = useState(false);\n\n // disable upgrade button if stripe is enabled, but not loaded yet\n const stripeIsReady: boolean =\n !stripeIsEnabled || (elements != null && stripe != null);\n\n const handleSubmit: FormEventHandler = async e => {\n e.preventDefault();\n\n // stripe has not loaded yet\n if (!stripe || !elements) return;\n\n setIsSubmitting(true);\n\n try {\n const method =\n type === 'subscription' ? 'confirmPayment' : 'confirmSetup';\n const result = await stripe[method]({\n elements,\n confirmParams: {\n return_url: returnUrl,\n },\n });\n\n // don't show validation error as it will be shown already by stripe payment element\n if (result.error?.type !== 'validation_error' && result.error?.message) {\n setErrorMessage(result.error.message);\n }\n } catch {}\n\n setIsSubmitting(false);\n };\n\n return (\n
\n \n {stripeIsEnabled && }\n \n {errorMessage && !isSubmitting && (\n
{errorMessage}
\n )}\n \n {submitLabel}\n \n \n );\n}\n\nfunction StripeSkeleton() {\n return (\n \n \n \n \n \n \n );\n}\n","import {MessageDescriptor} from '../i18n/message-descriptor';\nimport {Link, To} from 'react-router-dom';\nimport {AnimatePresence, m} from 'framer-motion';\nimport {TaskAltIcon} from '../icons/material/TaskAlt';\nimport {ErrorIcon} from '../icons/material/Error';\nimport {Trans} from '../i18n/trans';\nimport {Button} from '../ui/buttons/button';\nimport {Skeleton} from '../ui/skeleton/skeleton';\nimport {opacityAnimation} from '../ui/animation/opacity-animation';\n\nexport interface BillingRedirectMessageConfig {\n message: MessageDescriptor;\n status: 'success' | 'error';\n link: To;\n buttonLabel: MessageDescriptor;\n}\n\ninterface BillingRedirectMessageProps {\n config?: BillingRedirectMessageConfig;\n}\nexport function BillingRedirectMessage({config}: BillingRedirectMessageProps) {\n return (\n \n
\n {config ? (\n \n {config.status === 'success' ? (\n \n ) : (\n \n )}\n
\n \n
\n \n \n \n \n ) : (\n \n )}\n
\n
\n );\n}\n\nfunction LoadingSkeleton() {\n return (\n \n \n \n \n \n );\n}\n"],"names":["stripe","elements"],"mappings":";;;;;;;;AAaO,SAAS,UAAU,EAAC,MAAM,WAAW,WAA0B;AAC9D,QAAA,EAAC,SAAQ;AACf,QAAM,aAAa;AACb,QAAA,iBAAiB,OAAgB,KAAK;AACtC,QAAA,oBAAoB,OAAuB,IAAI;AAC/C,QAAA,EAAC,eAAc;AACrB,QAAM,CAAC,QAAQ,SAAS,IAAI,SAAwB,IAAI;AACxD,QAAM,CAAC,UAAU,WAAW,IAAI,SAAgC,IAAI;AAC9D,QAAA;AAAA,IACJ,UAAU,EAAC,UAAS;AAAA,IACpB,SAAS;AAAA,MACP;AAAA,MACA,QAAQ,EAAC,OAAM;AAAA,IACjB;AAAA,MACE,YAAY;AAEhB,YAAU,MAAM;AACd,QAAI,CAAC,UAAU,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