From b79a3cd13d993363a00509abc26981e76714133a Mon Sep 17 00:00:00 2001 From: Loan Laux Date: Fri, 30 Jul 2021 15:47:34 +0200 Subject: [PATCH] use Stripe.js payment input fields Signed-off-by: Loan Laux --- .../PaymentMethodView/PaymentMethodView.tsx | 13 ++++++++++--- components/checkout/withStripeElements.tsx | 19 +++++++++++++++++++ components/common/Layout/Layout.tsx | 4 ++-- next.config.js | 1 + package.json | 2 ++ yarn.lock | 14 +++++++++++++- 6 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 components/checkout/withStripeElements.tsx diff --git a/components/checkout/PaymentMethodView/PaymentMethodView.tsx b/components/checkout/PaymentMethodView/PaymentMethodView.tsx index a5f6f4b51..3b03c475c 100644 --- a/components/checkout/PaymentMethodView/PaymentMethodView.tsx +++ b/components/checkout/PaymentMethodView/PaymentMethodView.tsx @@ -1,5 +1,12 @@ import { FC } from 'react' import cn from 'classnames' +import { + CardNumberElement, + CardExpiryElement, + CardCvcElement, + useStripe, + useElements +} from '@stripe/react-stripe-js' import { Button, Text } from '@components/ui' import { useUI } from '@components/ui/context' import s from './PaymentMethodView.module.css' @@ -20,15 +27,15 @@ const PaymentMethodView: FC = () => {
- +
- +
- +

diff --git a/components/checkout/withStripeElements.tsx b/components/checkout/withStripeElements.tsx new file mode 100644 index 000000000..5bb78fcf1 --- /dev/null +++ b/components/checkout/withStripeElements.tsx @@ -0,0 +1,19 @@ +import React, { Component } from 'react' +import { loadStripe } from '@stripe/stripe-js' +import { Elements } from '@stripe/react-stripe-js' + +export default function withStripeElements(WrappedComponent) { + const stripe = loadStripe(process.env.STRIPE_PUBLIC_API_KEY) + + return class extends Component { + static displayName = "withStripeElements" + + render() { + return ( + + + + ); + } + }; +} diff --git a/components/common/Layout/Layout.tsx b/components/common/Layout/Layout.tsx index ff6d72aaf..736ce3ce6 100644 --- a/components/common/Layout/Layout.tsx +++ b/components/common/Layout/Layout.tsx @@ -13,9 +13,9 @@ import { useAcceptCookies } from '@lib/hooks/useAcceptCookies' import { Sidebar, Button, Modal, LoadingDots } from '@components/ui' import PaymentMethodView from '@components/checkout/PaymentMethodView' import CheckoutSidebarView from '@components/checkout/CheckoutSidebarView' - import LoginView from '@components/auth/LoginView' import s from './Layout.module.css' +import withStripeElements from '@components/checkout/withStripeElements' const Loading = () => (
@@ -123,4 +123,4 @@ const Layout: FC = ({ ) } -export default Layout +export default withStripeElements(Layout) diff --git a/next.config.js b/next.config.js index 18845d95a..dd576aba5 100644 --- a/next.config.js +++ b/next.config.js @@ -14,6 +14,7 @@ const isVendure = provider === 'vendure' module.exports = withCommerceConfig({ env: { REACTION_API_DOMAIN: process.env.REACTION_API_DOMAIN, + STRIPE_PUBLIC_API_KEY: process.env.STRIPE_PUBLIC_API_KEY, }, commerce, i18n: { diff --git a/package.json b/package.json index 74b8b0eda..c31a8798a 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,8 @@ }, "dependencies": { "@react-spring/web": "^9.2.1", + "@stripe/react-stripe-js": "^1.4.1", + "@stripe/stripe-js": "^1.16.0", "@vercel/fetch": "^6.1.0", "autoprefixer": "^10.2.6", "body-scroll-lock": "^3.1.5", diff --git a/yarn.lock b/yarn.lock index 62b7698cd..b5e304464 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1012,6 +1012,18 @@ resolved "https://registry.yarnpkg.com/@sindresorhus/is/-/is-0.14.0.tgz#9fb3a3cf3132328151f353de4632e01e52102bea" integrity sha512-9NET910DNaIPngYnLLPeg+Ogzqsi9uM4mSboU5y6p8S5DzMTVEsJZrawi+BoDNUVBa2DhJqQYUFvMDfgU062LQ== +"@stripe/react-stripe-js@^1.4.1": + version "1.4.1" + resolved "https://registry.yarnpkg.com/@stripe/react-stripe-js/-/react-stripe-js-1.4.1.tgz#884d59286fff00ba77389b32c045516f65d7a340" + integrity sha512-FjcVrhf72+9fUL3Lz3xi02ni9tzH1A1x6elXlr6tvBDgSD55oPJuodoP8eC7xTnBIKq0olF5uJvgtkJyDCdzjA== + dependencies: + prop-types "^15.7.2" + +"@stripe/stripe-js@^1.16.0": + version "1.16.0" + resolved "https://registry.yarnpkg.com/@stripe/stripe-js/-/stripe-js-1.16.0.tgz#73bce24fb7f47d719caa6b151e58e49b4167d463" + integrity sha512-ZSHbiwTrISoaTbpercmYGuY7QTg7HxfFyNgbJBaYbwHWbzMhpEdGTsmMpaBXIU6iiqwEEDaIyD8O6yJ+H5DWCg== + "@szmarczak/http-timer@^1.1.2": version "1.1.2" resolved "https://registry.yarnpkg.com/@szmarczak/http-timer/-/http-timer-1.1.2.tgz#b1665e2c461a2cd92f4c1bbf50d5454de0d4b421" @@ -4880,7 +4892,7 @@ promise@^7.1.1: dependencies: asap "~2.0.3" -prop-types@15.7.2: +prop-types@15.7.2, prop-types@^15.7.2: version "15.7.2" resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.7.2.tgz#52c41e75b8c87e72b9d9360e0206b99dcbffa6c5" integrity sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ==