From 586d6f0b3235fc9a97091eb38b0bea508bad17f9 Mon Sep 17 00:00:00 2001 From: Ludvig Hozman Date: Thu, 12 Sep 2019 14:59:40 +0200 Subject: [PATCH] Add error/success handling for stripe --- app/routes/events/components/JoinEventForm.js | 2 +- app/routes/events/components/Stripe.css | 25 +++-- app/routes/events/components/StripeElement.js | 95 ++++++++++++++----- 3 files changed, 88 insertions(+), 34 deletions(-) diff --git a/app/routes/events/components/JoinEventForm.js b/app/routes/events/components/JoinEventForm.js index df2983898b..b7210c7034 100644 --- a/app/routes/events/components/JoinEventForm.js +++ b/app/routes/events/components/JoinEventForm.js @@ -310,7 +310,7 @@ class JoinEventForm extends Component { {registration && showStripe && (
Betaling
- Du skal betale 254,- + Du skal betale {event.price / 100},- { @@ -52,19 +62,24 @@ const createOptions = (fontSize, padding) => { } }; }; -class _SplitForm extends React.Component { - handleSubmit = ev => { +class _SplitForm extends React.Component { + state = {}; + handleSubmit = async ev => { ev.preventDefault(); - if (this.props.stripe) { - this.props - .createPaymentIntent() - .then(({ payload }) => - this.props.stripe.handleCardPayment(payload.clientSecret) - ); + const { stripe, createPaymentIntent } = this.props; + if (stripe) { + const { payload } = await createPaymentIntent(); + const { error } = await stripe.handleCardPayment(payload.clientSecret); + if (error) { + this.setState({ error }); + } else { + this.setState({ success: true }); + } } }; render() { - return ( + const { success, error } = this.state; + return !success ? (
+ {error &&
{error.message}
}
+ ) : ( +
+ {success && + `Din betaling på ${this.props.event.price / 100} kr ble godkjent.`} +
); } } -class _PaymentRequestForm extends React.Component { +class _PaymentRequestForm extends React.Component< + FormProps, + State & FormState +> { constructor(props) { super(props); - const { event, createPaymentIntent } = props; + this.state = {}; + + const { event } = props; const paymentRequest = props.stripe.paymentRequest({ currency: 'nok', @@ -113,7 +139,7 @@ class _PaymentRequestForm extends React.Component { }); paymentRequest.on('paymentmethod', async ({ paymentMethod, complete }) => { - const { stripe } = this.props; + const { stripe, createPaymentIntent } = this.props; const { payload: clientSecret } = await createPaymentIntent(); const { error: confirmError } = await stripe.confirmPaymentIntent( clientSecret, @@ -124,10 +150,13 @@ class _PaymentRequestForm extends React.Component { if (confirmError) { complete('fail'); } else { - const { error, paymentIntent } = await stripe.handleCardPayment( - clientSecret - ); complete('success'); + const { error } = await stripe.handleCardPayment(clientSecret); + if (error) { + this.setState({ error }); + } else { + this.setState({ success: true }); + } } }); @@ -142,19 +171,33 @@ class _PaymentRequestForm extends React.Component { } render() { - const { chargeStatus } = this.props; + const { success, error } = this.state; return (
{this.state.canMakePayment && ( - + <> + !success ? + <> + + {error && ( +
{error.message}
+ )} + {' '} + : +
+ {success && + `Din betaling på ${this.props.event.price / + 100} kr ble godkjent.`} +
+ )}
);