Skip to content

Commit

Permalink
Redo payment request flow
Browse files Browse the repository at this point in the history
  • Loading branch information
LudvigHz committed Apr 21, 2020
1 parent 8a5ac3c commit 9dfebcc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 34 deletions.
5 changes: 3 additions & 2 deletions app/routes/events/components/JoinEventForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ const PaymentForm = ({
}) => (
<div style={{ width: '100%' }}>
<div className={styles.joinHeader}>Betaling</div>
<div className={styles.eventPrice}>
Du skal betale {(event.price / 100).toFixed(2).replace('.', ',')} kr
<div className={styles.eventPrice} title="Special price for you my friend!">
Du skal betale{' '}
<b>{(event.price / 100).toFixed(2).replace('.', ',')} kr</b>
</div>
<PaymentRequestForm
createPaymentIntent={createPaymentIntent}
Expand Down
75 changes: 43 additions & 32 deletions app/routes/events/components/StripeElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ type CardFormState = {
paymentStarted: boolean
};

type State = {
type PaymentRequestFormState = {
paymentRequest?: Object,
canMakePayment?: boolean
canMakePayment?: boolean,
paymentMethodId?: string,
complete?: string => void
};

const StripeElementStyle = {
Expand Down Expand Up @@ -155,7 +157,6 @@ class CardForm extends React.Component<CardFormProps, CardFormState> {
CVC
<CardCVCElement
className={stripeStyles.StripeElement}
onReady={() => this.setState({})}
{...StripeElementStyle}
/>
</label>
Expand All @@ -168,7 +169,7 @@ class CardForm extends React.Component<CardFormProps, CardFormState> {

class PaymentRequestForm extends React.Component<
PaymentRequestFormProps,
State
PaymentRequestFormState
> {
constructor(props) {
super(props);
Expand All @@ -187,31 +188,12 @@ class PaymentRequestForm extends React.Component<
country: 'NO'
});

paymentRequest.on('paymentmethod', async ({ paymentMethod, complete }) => {
const { stripe, clientSecret } = this.props;
if (!stripe || !clientSecret) {
complete('fail');
paymentRequest.on('paymentmethod', ({ paymentMethod, complete }) => {
this.setState({ complete, paymentMethodId: paymentMethod.id });
if (this.props.clientSecret) {
this.completePayment(this.props.clientSecret);
} else {
complete('success');

const { error: confirmError } = await stripe.confirmPaymentIntent(
clientSecret,
{
payment_method: paymentMethod.id
}
);
if (confirmError) {
this.props.setError(confirmError);
return;
}
this.props.setLoading(true);
const { error } = await stripe.handleCardPayment(clientSecret);
if (error) {
this.props.setError(error);
} else {
this.props.setSuccess();
}
this.props.setLoading(false);
this.props.createPaymentIntent();
}
});

Expand All @@ -227,18 +209,47 @@ class PaymentRequestForm extends React.Component<
};
}

onClick = e => {
if (!this.props.clientSecret) {
this.props.createPaymentIntent();
componentDidUpdate(prevProps) {
if (!prevProps.clientSecret && this.props.clientSecret) {
this.completePayment(this.props.clientSecret);
}
}

completePayment = async clientSecret => {
const { stripe } = this.props;
const { paymentMethodId, complete } = this.state;

if (!complete || !paymentMethodId) {
return;
}

const { error: confirmError } = await stripe.confirmPaymentIntent(
clientSecret,
{
payment_method: paymentMethodId
}
);
if (confirmError) {
complete('fail');
return;
}
complete('success');
this.props.setLoading(true);

const { error } = await stripe.handleCardPayment(clientSecret);
if (error) {
this.props.setError(error);
} else {
this.props.setSuccess();
}
this.props.setLoading(false);
};

render() {
return (
<div style={{ flex: 1 }}>
{this.state.canMakePayment && (
<PaymentRequestButtonElement
onClick={e => this.onClick(e)}
paymentRequest={this.state.paymentRequest}
className={stripeStyles.PaymentRequestButton}
style={{
Expand Down

0 comments on commit 9dfebcc

Please sign in to comment.