Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump stripe deps #5342

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/routes/events/components/Stripe.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
border-radius: var(--border-radius-lg);
}

.elementsLedgend {
.elementsLegend {
text-align: center;
padding: 0 var(--spacing-md);
}
Expand Down
69 changes: 42 additions & 27 deletions app/routes/events/components/StripeElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import config from 'app/config';
import { useAppDispatch } from 'app/store/hooks';
import { useTheme } from 'app/utils/themeUtils';
import stripeStyles from './Stripe.module.css';
import type { PaymentMethod, PaymentRequest } from '@stripe/stripe-js';
import type { EventRegistrationPaymentStatus } from 'app/models';
import type {
AuthUserDetailedEvent,
Expand All @@ -32,16 +33,16 @@ type Props = {
type FormProps = Props & {
fontSize?: string;
};
type CardFormProps = FormProps & {
ledgend: string;
setError: (arg0: string) => void;

type SharedFormProps = FormProps & {
setError: (errorMessage: string) => void;
setSuccess: () => void;
setLoading: (arg0: boolean) => void;
setLoading: (loading: boolean) => void;
};
type PaymentRequestFormProps = FormProps & {
setError: (arg0: string) => void;
setSuccess: () => void;
setLoading: (arg0: boolean) => void;
type CardFormProps = SharedFormProps & {
legend: string;
};
type PaymentRequestFormProps = SharedFormProps & {
setCanPaymentRequest: (arg0: boolean) => void;
};

Expand Down Expand Up @@ -97,7 +98,13 @@ const CardForm = (props: CardFormProps) => {
const completePayment = useCallback(
async (clientSecret) => {
setPaymentStarted(false);
const card = elements.getElement(CardNumberElement);
const card = elements?.getElement(CardNumberElement);
if (!card || !stripe) {
setError(
'Teknisk feil, skjemaet har ikke blitt startet riktig. Ta kontakt med Webkom om problemet vedvarer.',
);
return;
}
const { error } = await stripe.confirmCardPayment(clientSecret, {
payment_method: {
card: card,
Expand All @@ -109,7 +116,9 @@ const CardForm = (props: CardFormProps) => {
});

if (error) {
setError(error.message);
setError(
error.message ?? 'Det skjedde en ukjent feil med betalingen din.',
);
} else {
setSuccess();
}
Expand Down Expand Up @@ -137,9 +146,7 @@ const CardForm = (props: CardFormProps) => {
onSubmit={handleSubmit}
>
<fieldset className={stripeStyles.elementsFieldset}>
<legend className={stripeStyles.elementsLedgend}>
{props.ledgend}
</legend>
<legend className={stripeStyles.elementsLegend}>{props.legend}</legend>
<label data-testid="cardnumber-input">
Kortnummer
<CardNumberElement
Expand Down Expand Up @@ -170,19 +177,24 @@ const CardForm = (props: CardFormProps) => {
};

const PaymentRequestForm = (props: PaymentRequestFormProps) => {
const [complete, setComplete] = useState(null);
const [paymentRequest, setPaymentRequest] = useState(null);
const [complete, setComplete] = useState<
((status: CompleteStatus) => void) | null
>(null);
const [paymentRequest, setPaymentRequest] = useState<PaymentRequest | null>(
null,
);
const [canMakePayment, setCanMakePayment] = useState(false);
const [paymentStarted, setPaymentStarted] = useState(false);
const [paymentMethod, setPaymentMethod] = useState(null);
const [paymentMethod, setPaymentMethod] = useState<PaymentMethod | null>(
null,
);

const stripe = useStripe();

const {
event,
paymentError,
clientSecret,
createPaymentIntent,
setError,
setSuccess,
setLoading,
Expand All @@ -191,15 +203,16 @@ const PaymentRequestForm = (props: PaymentRequestFormProps) => {

const completePayment = useCallback(
async (clientSecret) => {
if (!complete || !paymentMethod) {
if (!complete || !paymentMethod || !stripe) {
return;
}

const { error: confirmError } = await stripe.confirmPaymentIntent(
const { error: confirmError } = await stripe.confirmCardPayment(
clientSecret,
{
payment_method: paymentMethod.id,
},
{ handleActions: false },
);

if (confirmError) {
Expand All @@ -209,10 +222,13 @@ const PaymentRequestForm = (props: PaymentRequestFormProps) => {

complete('success');
setLoading(true);
const { error } = await stripe.handleCardPayment(clientSecret);
const { error } = await stripe.confirmCardPayment(clientSecret);

if (error) {
setError(error.message);
setError(
error.message ??
'Det oppsto en ukjent feil. Hvis problemet vedvarer, ta kontakt med Webkom.',
);
} else {
setSuccess();
}
Expand All @@ -228,7 +244,7 @@ const PaymentRequestForm = (props: PaymentRequestFormProps) => {
return;
}

setComplete(status);
complete(status);

if (status === 'success') {
setSuccess();
Expand All @@ -239,6 +255,7 @@ const PaymentRequestForm = (props: PaymentRequestFormProps) => {

useEffect(() => {
if (!paymentRequest && stripe && event) {
// Create a paymentRequest instance
const paymentReq = stripe.paymentRequest({
currency: 'nok',
total: {
Expand All @@ -250,16 +267,16 @@ const PaymentRequestForm = (props: PaymentRequestFormProps) => {
requestPayerPhone: true,
country: 'NO',
});
// Complete the payment
paymentReq.on('paymentmethod', async ({ paymentMethod, complete }) => {
setComplete(() => complete);
setPaymentMethod(paymentMethod);

if (clientSecret) {
completePayment(clientSecret);
} else {
createPaymentIntent();
}
});
// Render the Payment Request Button Element
paymentReq.canMakePayment().then((result) => {
setCanMakePayment(!!result);
setCanPaymentRequest(!!result);
Expand All @@ -272,7 +289,6 @@ const PaymentRequestForm = (props: PaymentRequestFormProps) => {
stripe,
event,
completePayment,
createPaymentIntent,
setCanPaymentRequest,
]);

Expand Down Expand Up @@ -312,7 +328,6 @@ const PaymentRequestForm = (props: PaymentRequestFormProps) => {
);
}
}}
paymentRequest={paymentRequest}
className={stripeStyles.PaymentRequestButton}
options={{
style: {
Expand Down Expand Up @@ -380,7 +395,7 @@ const PaymentForm = (props: FormProps) => {
setSuccess={() => setSuccess(true)}
setError={(error) => setError(error)}
setLoading={(loading) => setLoading(loading)}
ledgend={
legend={
paymentRequest
? 'Eller skriv inn kortinformasjon'
: 'Skriv inn kortinformasjon'
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
"@sentry/browser": "^7.119.1",
"@sentry/integrations": "^7.114.0",
"@sentry/node": "^7.101.1",
"@stripe/react-stripe-js": "^1.14.2",
"@stripe/stripe-js": "^1.46.0",
"@stripe/react-stripe-js": "^3.1.1",
"@stripe/stripe-js": "^5.5.0",
"@webkom/lego-editor": "^2.6.1",
"@webkom/react-meter-bar": "^2.0.0",
"@webkom/react-prepare": "^1.0.1",
Expand Down
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3996,17 +3996,17 @@
resolved "https://registry.yarnpkg.com/@storybook/theming/-/theming-8.4.2.tgz#0e385869a225040e326cfba301b6cdccd31dcb21"
integrity sha512-9j4fnu5LcV+qSs1rdwf61Bt14lms0T1LOZkHxGNcS1c1oH+cPS+sxECh2lxtni+mvOAHUlBs9pKhVZzRPdWpvg==

"@stripe/react-stripe-js@^1.14.2":
version "1.16.5"
resolved "https://registry.yarnpkg.com/@stripe/react-stripe-js/-/react-stripe-js-1.16.5.tgz#51cf862b50ca91ae6193c77a5bec889e81047f10"
integrity sha512-lVPW3IfwdacyS22pP+nBB6/GNFRRhT/4jfgAK6T2guQmtzPwJV1DogiGGaBNhiKtSY18+yS8KlHSu+PvZNclvQ==
"@stripe/react-stripe-js@^3.1.1":
version "3.1.1"
resolved "https://registry.yarnpkg.com/@stripe/react-stripe-js/-/react-stripe-js-3.1.1.tgz#78a2575683637f87c965a81cc1e0f626138f20f1"
integrity sha512-+JzYFgUivVD7koqYV7LmLlt9edDMAwKH7XhZAHFQMo7NeRC+6D2JmQGzp9tygWerzwttwFLlExGp4rAOvD6l9g==
dependencies:
prop-types "^15.7.2"

"@stripe/stripe-js@^1.46.0":
version "1.54.2"
resolved "https://registry.yarnpkg.com/@stripe/stripe-js/-/stripe-js-1.54.2.tgz#0665848e22cbda936cfd05256facdfbba121438d"
integrity sha512-R1PwtDvUfs99cAjfuQ/WpwJ3c92+DAMy9xGApjqlWQMj0FKQabUAys2swfTRNzuYAYJh7NqK2dzcYVNkKLEKUg==
"@stripe/stripe-js@^5.5.0":
version "5.6.0"
resolved "https://registry.yarnpkg.com/@stripe/stripe-js/-/stripe-js-5.6.0.tgz#cbb5b5f6110f870ca7de7e8ea3d189e9525a1019"
integrity sha512-w8CEY73X/7tw2KKlL3iOk679V9bWseE4GzNz3zlaYxcTjmcmWOathRb0emgo/QQ3eoNzmq68+2Y2gxluAv3xGw==

"@swc/helpers@^0.5.0":
version "0.5.11"
Expand Down