Skip to content

Commit 51361dc

Browse files
committed
Fix Stripe loading
1 parent 4c33dfb commit 51361dc

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed
Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import React from 'react';
2+
import Helmet from 'react-helmet';
23
import { StripeProvider, Elements } from 'react-stripe-elements';
34
import { STRIPE_API_KEY } from '@codesandbox/common/lib/utils/config';
5+
import { useScript } from 'app/hooks';
46
import { CheckoutForm } from './CheckoutForm';
57
import { Container } from './elements';
68

@@ -14,6 +16,8 @@ interface ISubscribeFormProps {
1416
hasCoupon?: boolean;
1517
}
1618

19+
const context = window as any;
20+
1721
export const SubscribeForm: React.FC<ISubscribeFormProps> = ({
1822
name,
1923
subscribe,
@@ -22,20 +26,33 @@ export const SubscribeForm: React.FC<ISubscribeFormProps> = ({
2226
isLoading = false,
2327
error,
2428
hasCoupon,
25-
}) => (
26-
<Container>
27-
<StripeProvider apiKey={STRIPE_API_KEY}>
28-
<Elements>
29-
<CheckoutForm
30-
buttonName={buttonName}
31-
loadingText={loadingText}
32-
subscribe={subscribe}
33-
name={name}
34-
isLoading={isLoading}
35-
error={error}
36-
hasCoupon={hasCoupon}
37-
/>
38-
</Elements>
39-
</StripeProvider>
40-
</Container>
41-
);
29+
}) => {
30+
const [stripe, setStripe] = React.useState(null);
31+
const [loaded] = useScript('https://js.stripe.com/v3/');
32+
33+
React.useEffect(() => {
34+
if (context.Stripe) {
35+
setStripe(context.Stripe(STRIPE_API_KEY));
36+
}
37+
}, [loaded]);
38+
39+
return (
40+
<>
41+
<Container>
42+
<StripeProvider stripe={stripe}>
43+
<Elements>
44+
<CheckoutForm
45+
buttonName={buttonName}
46+
loadingText={loadingText}
47+
subscribe={subscribe}
48+
name={name}
49+
isLoading={isLoading}
50+
error={error}
51+
hasCoupon={hasCoupon}
52+
/>
53+
</Elements>
54+
</StripeProvider>
55+
</Container>
56+
</>
57+
);
58+
};

packages/app/src/app/pages/Patron/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class Patron extends React.Component {
1919
<MaxWidth>
2020
<Helmet>
2121
<title>Patron - CodeSandbox</title>
22-
<script async src="https://js.stripe.com/v3/" />
2322
</Helmet>
2423
<Margin vertical={1.5} horizontal={1.5}>
2524
<Navigation title="Become a Patron" />

packages/app/src/app/pages/common/Modals/PreferencesModal/PaymentInfo/index.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from 'react';
2-
import Helmet from 'react-helmet';
32
import { inject, observer } from 'app/componentConnectors';
43
import { SubscribeForm } from 'app/components/SubscribeForm';
54
import { Card } from './Card';
@@ -50,9 +49,6 @@ class PaymentInfoComponent extends React.Component<Props> {
5049
const { preferences } = this.props.store;
5150
return (
5251
<Container>
53-
<Helmet>
54-
<script async src="https://js.stripe.com/v3/" />
55-
</Helmet>
5652
<Title>Payment Info</Title>
5753
{preferences.isLoadingPaymentDetails ? (
5854
<div>Loading payment details...</div>

0 commit comments

Comments
 (0)