mirror of
https://github.com/supabase/supabase.git
synced 2026-10-05 09:25:06 +03:00
* feat(ci): fix typos * [create-pull-request] automated change * Update index.tsx * Update README.fr.md * Create .misspell-fixer.ignore * Update constant.dart * [create-pull-request] automated change * Update database.tsx * [create-pull-request] automated change * [create-pull-request] automated change Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
80 lines
2.1 KiB
TypeScript
80 lines
2.1 KiB
TypeScript
import { FC, useState } from 'react'
|
|
import { Button, Modal } from 'ui'
|
|
import { useStripe, useElements, PaymentElement } from '@stripe/react-stripe-js'
|
|
import { useStore } from 'hooks'
|
|
|
|
interface Props {
|
|
returnUrl: string
|
|
onCancel: () => void
|
|
}
|
|
|
|
// Stripe docs recommend to use the new SetupIntent flow over
|
|
// manually creating and attaching payment methods via the API
|
|
// Small UX annoyance here, that the page will be refreshed
|
|
|
|
const AddPaymentMethodForm: FC<Props> = ({ returnUrl, onCancel }) => {
|
|
const { ui } = useStore()
|
|
const stripe = useStripe()
|
|
const elements = useElements()
|
|
|
|
const [isSaving, setIsSaving] = useState(false)
|
|
|
|
const handleSubmit = async (event: any) => {
|
|
event.preventDefault()
|
|
|
|
if (!stripe || !elements) {
|
|
console.error('Stripe.js has not loaded')
|
|
return
|
|
}
|
|
|
|
setIsSaving(true)
|
|
|
|
if (document !== undefined) {
|
|
// [Joshen] This is to ensure that any 3DS popup from Stripe remains clickable
|
|
document.body.classList.add('!pointer-events-auto')
|
|
}
|
|
|
|
const { error } = await stripe.confirmSetup({
|
|
elements,
|
|
confirmParams: { return_url: returnUrl },
|
|
})
|
|
|
|
if (error) {
|
|
setIsSaving(false)
|
|
ui.setNotification({
|
|
category: 'error',
|
|
message: error?.message ?? ' Failed to save card details',
|
|
})
|
|
}
|
|
|
|
if (document !== undefined) {
|
|
document.body.classList.remove('!pointer-events-auto')
|
|
}
|
|
}
|
|
|
|
return (
|
|
<form onSubmit={handleSubmit}>
|
|
<Modal.Content>
|
|
<div
|
|
className={`transition ${isSaving ? 'pointer-events-none opacity-75' : 'opacity-100'}`}
|
|
>
|
|
<PaymentElement />
|
|
</div>
|
|
</Modal.Content>
|
|
<Modal.Separator />
|
|
<Modal.Content>
|
|
<div className="flex items-center space-x-2 pt-2">
|
|
<Button block htmlType="submit" type="primary" loading={isSaving}>
|
|
Save
|
|
</Button>
|
|
<Button htmlType="button" type="default" onClick={onCancel} block>
|
|
Cancel
|
|
</Button>
|
|
</div>
|
|
</Modal.Content>
|
|
</form>
|
|
)
|
|
}
|
|
|
|
export default AddPaymentMethodForm
|