creditCardForm.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. import {useEffect, useRef, useState} from 'react';
  2. import {css, useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import rubikFontPath from 'sentry/../fonts/rubik-regular.woff';
  5. import ButtonBar from 'sentry/components/buttonBar';
  6. import {Alert} from 'sentry/components/core/alert';
  7. import {Button} from 'sentry/components/core/button';
  8. import {Input} from 'sentry/components/core/input';
  9. import FieldGroup from 'sentry/components/forms/fieldGroup';
  10. import TextField from 'sentry/components/forms/fields/textField';
  11. import ExternalLink from 'sentry/components/links/externalLink';
  12. import LoadingIndicator from 'sentry/components/loadingIndicator';
  13. import {NODE_ENV} from 'sentry/constants';
  14. import {t, tct} from 'sentry/locale';
  15. import ConfigStore from 'sentry/stores/configStore';
  16. import {space} from 'sentry/styles/space';
  17. import {loadStripe} from 'getsentry/utils/stripe';
  18. export type SubmitData = {
  19. /**
  20. * The card element used to collect the credit card.
  21. */
  22. cardElement: stripe.elements.Element;
  23. /**
  24. * To be called when the stripe operation is complete.
  25. * When called it re-enables the form buttons.
  26. */
  27. onComplete: () => void;
  28. /**
  29. * Stripe client instance used.
  30. */
  31. stripe: stripe.Stripe;
  32. /**
  33. * Validation errors from fields contained in this form.
  34. * If not-empty submission should not continue.
  35. */
  36. validationErrors: string[];
  37. };
  38. type Props = {
  39. /**
  40. * Handle the card form submission.
  41. */
  42. onSubmit: (data: SubmitData) => void;
  43. /**
  44. * Text for the submit button.
  45. */
  46. buttonText?: string;
  47. /**
  48. * Text for the cancel button.
  49. */
  50. cancelButtonText?: string;
  51. /**
  52. * Classname/styled component wrapper for the form.
  53. */
  54. className?: string;
  55. /**
  56. * Error message to show.
  57. */
  58. error?: string;
  59. /**
  60. * If the error message has an action that can be retried, this callback
  61. * will be invoked by the 'retry' button shown in the error message.
  62. */
  63. errorRetry?: () => void;
  64. /**
  65. * Classname for the footer buttons.
  66. */
  67. footerClassName?: string;
  68. /**
  69. * Handler for cancellation.
  70. */
  71. onCancel?: () => void;
  72. /**
  73. * The URL referrer, if any.
  74. */
  75. referrer?: string;
  76. };
  77. /**
  78. * Standalone credit card form that requires onSubmit to be handled
  79. * by the parent. This allows us to reuse the same form for both paymentintent, setupintent
  80. * and classic card flows.
  81. */
  82. function CreditCardForm({
  83. className,
  84. error,
  85. errorRetry,
  86. onCancel,
  87. onSubmit,
  88. buttonText = t('Save Changes'),
  89. cancelButtonText = t('Cancel'),
  90. footerClassName = 'form-actions',
  91. referrer,
  92. }: Props) {
  93. const theme = useTheme();
  94. const [postalCode, setPostalCode] = useState('');
  95. const [busy, setBusy] = useState(false);
  96. const [stripe, setStripe] = useState<stripe.Stripe>();
  97. const [cardElement, setCardElement] = useState<stripe.elements.Element>();
  98. const stripeMount = useRef<HTMLDivElement>(null);
  99. // XXX: Default loading to false when in test mode. The stripe elements will
  100. // never load, but we still want to test some functionality of this modal.
  101. const defaultLoadState = NODE_ENV !== 'test';
  102. const [loading, setLoading] = useState(defaultLoadState);
  103. useEffect(() => {
  104. loadStripe(Stripe => {
  105. const apiKey = ConfigStore.get('getsentry.stripePublishKey');
  106. const instance = Stripe(apiKey);
  107. setStripe(instance);
  108. });
  109. }, []);
  110. useEffect(() => {
  111. if (!stripe || !stripeMount.current) {
  112. return;
  113. }
  114. const stripeElementStyles = {
  115. base: {
  116. backgroundColor: theme.background,
  117. color: theme.textColor,
  118. fontFamily: theme.text.family,
  119. fontWeight: 400,
  120. fontSize: theme.fontSizeLarge,
  121. '::placeholder': {
  122. color: theme.gray300,
  123. },
  124. },
  125. invalid: {
  126. color: theme.red300,
  127. iconColor: theme.red300,
  128. },
  129. };
  130. const stripeElements = stripe.elements({
  131. fonts: [{family: 'Rubik', src: `url(${rubikFontPath})`, weight: '400'}],
  132. });
  133. const stripeCardElement = stripeElements.create('card', {
  134. hidePostalCode: true,
  135. style: stripeElementStyles,
  136. });
  137. stripeCardElement.mount(stripeMount.current);
  138. stripeCardElement.on('ready', () => {
  139. setLoading(false);
  140. });
  141. setCardElement(stripeCardElement);
  142. }, [stripe, theme]);
  143. function onComplete() {
  144. setBusy(false);
  145. }
  146. function handleSubmit(e: React.FormEvent) {
  147. e.preventDefault();
  148. if (busy) {
  149. return;
  150. }
  151. setBusy(true);
  152. const validationErrors: string[] = [];
  153. if (postalCode === '') {
  154. validationErrors.push(t('Postal code is required.'));
  155. }
  156. if (!stripe || !cardElement) {
  157. return;
  158. }
  159. onSubmit({stripe, cardElement, onComplete, validationErrors});
  160. }
  161. function handleCancel(e: React.MouseEvent) {
  162. e.preventDefault();
  163. if (busy) {
  164. return;
  165. }
  166. onCancel?.();
  167. }
  168. function handlePostalCodeChange(value: string) {
  169. setPostalCode(value);
  170. if (cardElement) {
  171. cardElement.update({value: {postalCode: value}});
  172. }
  173. }
  174. function handlePostalCodeBlur(value: string) {
  175. setPostalCode(value);
  176. if (cardElement) {
  177. cardElement.update({value: {postalCode: value}});
  178. }
  179. }
  180. function handleErrorRetry(event: React.MouseEvent) {
  181. event.preventDefault();
  182. errorRetry?.();
  183. }
  184. const disabled = busy || loading;
  185. return (
  186. <form
  187. className={className}
  188. action="."
  189. method="POST"
  190. id="payment-form"
  191. onSubmit={handleSubmit}
  192. >
  193. {error && (
  194. <Alert.Container>
  195. <Alert type="error">
  196. <AlertContent>
  197. {error}
  198. {errorRetry && (
  199. <Button size="sm" onClick={handleErrorRetry}>
  200. {t('Retry')}
  201. </Button>
  202. )}
  203. </AlertContent>
  204. </Alert>
  205. </Alert.Container>
  206. )}
  207. {loading && <LoadingIndicator />}
  208. {referrer?.includes('billing-failure') && (
  209. <Alert.Container>
  210. <Alert type="warning">
  211. {t('Your credit card will be charged upon update.')}
  212. </Alert>
  213. </Alert.Container>
  214. )}
  215. <CreditCardInfoWrapper isLoading={loading}>
  216. <StyledField
  217. stacked
  218. flexibleControlStateSize
  219. inline={false}
  220. label={t('Card Details')}
  221. >
  222. <FormControl>
  223. <div ref={stripeMount} />
  224. </FormControl>
  225. </StyledField>
  226. <StyledTextField
  227. required
  228. stacked
  229. flexibleControlStateSize
  230. inline={false}
  231. maxLength={12}
  232. name="postal"
  233. label={t('Postal Code')}
  234. value={postalCode}
  235. onChange={handlePostalCodeChange}
  236. onBlur={handlePostalCodeBlur}
  237. />
  238. <Info>
  239. <small>
  240. {tct('Payments are processed securely through [stripe:Stripe].', {
  241. stripe: <ExternalLink href="https://stripe.com/" />,
  242. })}
  243. </small>
  244. </Info>
  245. <div className={footerClassName}>
  246. <StyledButtonBar gap={1}>
  247. {onCancel && (
  248. <Button
  249. data-test-id="cancel"
  250. priority="default"
  251. disabled={disabled}
  252. onClick={handleCancel}
  253. >
  254. {cancelButtonText}
  255. </Button>
  256. )}
  257. <Button
  258. data-test-id="submit"
  259. type="submit"
  260. priority="primary"
  261. disabled={disabled}
  262. onClick={handleSubmit}
  263. >
  264. {buttonText}
  265. </Button>
  266. </StyledButtonBar>
  267. </div>
  268. </CreditCardInfoWrapper>
  269. </form>
  270. );
  271. }
  272. const FormControl = Input.withComponent('div');
  273. const fieldCss = css`
  274. padding-right: 0;
  275. padding-left: 0;
  276. `;
  277. const StyledField = styled(FieldGroup)`
  278. ${fieldCss};
  279. padding-top: 0;
  280. `;
  281. const StyledTextField = styled(TextField)`
  282. ${fieldCss};
  283. `;
  284. const Info = styled('p')`
  285. ${fieldCss};
  286. margin-bottom: ${space(3)};
  287. margin-top: ${space(1)};
  288. `;
  289. const CreditCardInfoWrapper = styled('div')<{isLoading?: boolean}>`
  290. ${p => p.isLoading && 'display: none'};
  291. `;
  292. const StyledButtonBar = styled(ButtonBar)`
  293. max-width: fit-content;
  294. `;
  295. const AlertContent = styled('span')`
  296. display: flex;
  297. align-items: center;
  298. justify-content: space-between;
  299. `;
  300. export default CreditCardForm;