From ae29246b32d26db95dd647c06efd6e2d2a8fa829 Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Thu, 17 Jun 2021 03:27:17 +0100 Subject: [PATCH] Add payments page --- src/pages/Payments/index.tsx | 108 +++++++++++++++++++++++++++++++++++ src/pages/Payments/styles.ts | 8 +++ 2 files changed, 116 insertions(+) create mode 100644 src/pages/Payments/index.tsx create mode 100644 src/pages/Payments/styles.ts diff --git a/src/pages/Payments/index.tsx b/src/pages/Payments/index.tsx new file mode 100644 index 0000000..4f97bb3 --- /dev/null +++ b/src/pages/Payments/index.tsx @@ -0,0 +1,108 @@ +import * as Yup from 'yup'; +import { CardElement, useStripe, useElements } from '@stripe/react-stripe-js'; +import { useFormik } from 'formik'; +import { useReactToPrint } from 'react-to-print'; +import { useHistory, useParams } from 'react-router-dom'; +import { useEffect, useState } from 'react'; +import { useLazyQuery, useMutation, useReactiveVar } from '@apollo/client'; +import { Redirect } from 'react-router'; +import { roleVar } from '../../graphql/state'; +import { Wrapper } from './styles'; +import { Box, Button, Text } from '../../components'; +import { ArrowLeft } from '../../assets'; + +const Payments = () => { + const role = useReactiveVar(roleVar); + const history = useHistory(); + const stripe = useStripe(); + const elements = useElements(); + const { id } = useParams<{ id: string }>(); + const [error, setError] = useState(''); + + const handleSubmit = async (event: React.FormEvent) => { + // Block native form submission. + event.preventDefault(); + + if (!stripe || !elements) { + // Stripe.js has not loaded yet. Make sure to disable + // form submission until Stripe.js has loaded. + return; + } + + // Get a reference to a mounted CardElement. Elements knows how + // to find your CardElement because there can only ever be one of + // each type of element. + const cardElement = elements.getElement(CardElement); + + // Use your card Element with other Stripe.js APIs + const { + error: paymentError, + paymentMethod, + } = await stripe.createPaymentMethod({ + type: 'card', + card: cardElement!, + }); + + if (paymentError) { + console.log('[error]', paymentError); + } else { + console.log('[PaymentMethod]', paymentMethod); + + const paymentRequest = stripe.paymentRequest({ + country: 'US', + currency: 'usd', + total: { + label: 'Demo total', + amount: 1000, + }, + requestPayerName: true, + requestPayerEmail: false, + }); + + console.log(paymentRequest); + + const result = await paymentRequest.canMakePayment(); + + console.log(result); + + paymentRequest.show(); + } + }; + + useEffect(() => {}, []); + + return role === 'client' ? ( + + + + + + + + ) : ( + <> + {role === 'admin' && } + {role === 'developer' || + (role === 'productOwner' && )} + + ); +}; + +export default Payments; diff --git a/src/pages/Payments/styles.ts b/src/pages/Payments/styles.ts new file mode 100644 index 0000000..57eb643 --- /dev/null +++ b/src/pages/Payments/styles.ts @@ -0,0 +1,8 @@ +import styled from 'styled-components'; + +export const Wrapper = styled.div` + .empty { + fill: ${({ theme, color }) => + color ? theme.colors[color].main : theme.colors.client.main}; + } +`;