From 0f6d1c3f79000c556650eaa07d748c54aac95ada Mon Sep 17 00:00:00 2001 From: Suejung Shin Date: Wed, 1 Jan 2025 20:48:34 -0800 Subject: [PATCH] add stripe payment element --- .env.development | 2 - .../EditPaymentMethod/EditPaymentMethod.tsx | 182 +++++++++--------- .../PaymentMethod/PaymentMethod.tsx | 40 ++-- 3 files changed, 107 insertions(+), 117 deletions(-) diff --git a/.env.development b/.env.development index c81e6b0f63..289e620838 100644 --- a/.env.development +++ b/.env.development @@ -5,5 +5,3 @@ REACT_APP_MARKETING_BASE_URL=https://about.codecov.io # REACT_APP_STRIPE_KEY= # REACT_APP_LAUNCHDARKLY= # REACT_APP_BAREMETRICS_TOKEN= -REACT_APP_STRIPE_KEY=pk_test_514SJTOGlVGuVgOrkRgh7zxp3tQ7bX4CY6pnxxw6zRZZSoDVtUUjArPKC7oXeeIbJNICTqS7H88FRfwZnWMskPKxo00bAnu2i9I -REACT_APP_STRIPE_PUBLIC_KEY=pk_test_514SJTOGlVGuVgOrkRgh7zxp3tQ7bX4CY6pnxxw6zRZZSoDVtUUjArPKC7oXeeIbJNICTqS7H88FRfwZnWMskPKxo00bAnu2i9I diff --git a/src/pages/PlanPage/subRoutes/CurrentOrgPlan/BillingDetails/EditPaymentMethod/EditPaymentMethod.tsx b/src/pages/PlanPage/subRoutes/CurrentOrgPlan/BillingDetails/EditPaymentMethod/EditPaymentMethod.tsx index 1779b66da3..c7142559c7 100644 --- a/src/pages/PlanPage/subRoutes/CurrentOrgPlan/BillingDetails/EditPaymentMethod/EditPaymentMethod.tsx +++ b/src/pages/PlanPage/subRoutes/CurrentOrgPlan/BillingDetails/EditPaymentMethod/EditPaymentMethod.tsx @@ -1,28 +1,29 @@ -import React, { useState } from 'react' -import AddressForm from '../Address/AddressForm' import { Elements, - CardElement, - IbanElement, - useStripe, + PaymentElement, useElements, + useStripe, } from '@stripe/react-stripe-js' import { loadStripe } from '@stripe/stripe-js' -import CreditCardForm from '../PaymentCard/CreditCardForm' -import { RadioTileGroup } from 'ui/RadioTileGroup' -import Icon from 'ui/Icon' +import React, { useState } from 'react' + +import Button from 'ui/Button' + +import AddressForm from '../Address/AddressForm' -// Load your Stripe public key -const stripePromise = loadStripe('your-publishable-key-here') +// TODO - fetch from API +const STRIPE_PUBLISHABLE_KEY = process.env.STRIPE_PUBLISHABLE_KEY || '' +const MANUALLY_FETCHED_CLIENT_SECRET = process.env.STRIPE_CLIENT_SECRET || '' + +const stripePromise = loadStripe(STRIPE_PUBLISHABLE_KEY) interface PaymentFormProps { clientSecret: string } -const PaymentForm: React.FC = ({ clientSecret }) => { +const PaymentForm: React.FC = () => { const stripe = useStripe() const elements = useElements() - const [paymentMethod, setPaymentMethod] = useState<'card' | 'bank'>('card') const [isSubmitting, setIsSubmitting] = useState(false) const [errorMessage, setErrorMessage] = useState(null) @@ -37,21 +38,11 @@ const PaymentForm: React.FC = ({ clientSecret }) => { return } - const paymentElement = elements.getElement( - paymentMethod === 'card' ? CardElement : IbanElement - ) - - if (!paymentElement) { - setErrorMessage('Payment element is missing.') - setIsSubmitting(false) - return - } - - // Confirm payment based on selected method const { error } = await stripe.confirmPayment({ elements, confirmParams: { - return_url: 'https://your-website.com/order-complete', // Redirect URL + // eslint-disable-next-line camelcase + return_url: 'https://codecov.io', }, }) @@ -64,73 +55,50 @@ const PaymentForm: React.FC = ({ clientSecret }) => { } return ( -
-

Choose Payment Method

- - setPaymentMethod(value)} - className="flex-row" - > - - -
- - Card -
-
-
- - -
- - Bank Account -
-
-
-
- - {/* Payment Element */} - {paymentMethod === 'card' && ( -
-

Card Details

- -
- )} - - {paymentMethod === 'bank' && ( -
-

Bank Account Details

- -
- )} +
+ +
+ + +
{errorMessage &&
{errorMessage}
} - +
) } -// Wrapper Component to provide Stripe Elements const PaymentPage: React.FC<{ clientSecret: string }> = ({ clientSecret }) => { - // if (!clientSecret) { - // return
Loading...
- // } - const options = { clientSecret, appearance: { - theme: 'stripe', + theme: 'stripe' as const, }, } @@ -145,20 +113,48 @@ interface EditablePaymentMethodProps { clientSecret: string } -const EditPaymentMethod: React.FC = ({ - clientSecret, -}) => { +const EditPaymentMethod: React.FC = () => { + const clientSecret = MANUALLY_FETCHED_CLIENT_SECRET // TODO - fetch from API + + const [activeTab, setActiveTab] = useState<'primary' | 'secondary'>('primary') + return (

Edit payment method

- - +
+ {/* Tabs for Primary and Secondary Payment Methods */} +
+ {['primary', 'secondary'].map((tab) => ( + + ))} +
+ + {/* Payment Details for the selected tab */} +
+ {activeTab === 'primary' && ( +
+ + {}} provider={''} owner={''} /> +
+ )} + {activeTab === 'secondary' && ( +
+ + {}} provider={''} owner={''} /> +
+ )} +
+
) } diff --git a/src/pages/PlanPage/subRoutes/CurrentOrgPlan/BillingDetails/PaymentMethod/PaymentMethod.tsx b/src/pages/PlanPage/subRoutes/CurrentOrgPlan/BillingDetails/PaymentMethod/PaymentMethod.tsx index 520909b30e..20da1857bf 100644 --- a/src/pages/PlanPage/subRoutes/CurrentOrgPlan/BillingDetails/PaymentMethod/PaymentMethod.tsx +++ b/src/pages/PlanPage/subRoutes/CurrentOrgPlan/BillingDetails/PaymentMethod/PaymentMethod.tsx @@ -1,11 +1,11 @@ import { z } from 'zod' import { SubscriptionDetailSchema } from 'services/account' +import Button from 'ui/Button' import { ExpandableSection } from 'ui/ExpandableSection' import AddressCard from '../Address/AddressCard' import PaymentCard from '../PaymentCard' -import Button from 'ui/Button' function PaymentMethod({ heading, @@ -25,12 +25,8 @@ function PaymentMethod({ owner: string }) { const isAdmin = true // TODO - const isCreditCard = subscriptionDetail?.defaultPaymentMethod?.card // TODO - console.log(subscriptionDetail) - - console.log(isEditMode) return (
@@ -47,14 +43,14 @@ function PaymentMethod({ ) : null}
{/* Payment method summary */} - + {/* Cardholder name */}

@@ -63,20 +59,20 @@ function PaymentMethod({

N/A

{/* Address */} - +
{!isPrimary ? (