forked from opencollective/opencollective-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfirmOrder.js
148 lines (133 loc) · 4.18 KB
/
confirmOrder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import React from 'react';
import PropTypes from 'prop-types';
import { gql } from '@apollo/client';
import { graphql } from '@apollo/client/react/hoc';
import { get } from 'lodash';
import { withRouter } from 'next/router';
import { FormattedMessage } from 'react-intl';
import { API_V2_CONTEXT } from '../lib/graphql/helpers';
import { getStripe } from '../lib/stripe';
import AuthenticatedPage from '../components/AuthenticatedPage';
import Container from '../components/Container';
import MessageBox from '../components/MessageBox';
import { withUser } from '../components/UserProvider';
class ConfirmOrderPage extends React.Component {
static getInitialProps({ query }) {
return { id: parseInt(query.id) };
}
static propTypes = {
/** OrderId */
id: PropTypes.number.isRequired,
/** @ignore from graphql */
confirmOrder: PropTypes.func.isRequired,
/** @ignore from withUser */
loadingLoggedInUser: PropTypes.bool.isRequired,
/** @ignore from withUser */
LoggedInUser: PropTypes.object,
/** @ignore from withRouter */
router: PropTypes.object,
};
state = {
status: ConfirmOrderPage.SUBMITTING,
isRequestSent: false,
error: null,
};
componentDidMount() {
if (!this.props.loadingLoggedInUser && this.props.LoggedInUser) {
return this.triggerRequest();
}
}
componentDidUpdate() {
if (!this.state.isRequestSent && !this.props.loadingLoggedInUser && this.props.LoggedInUser) {
return this.triggerRequest();
}
}
static SUBMITTING = 1;
static ERROR = 3;
async triggerRequest() {
try {
this.setState({ isRequestSent: true });
const res = await this.props.confirmOrder({ variables: { order: { legacyId: this.props.id } } });
const orderConfirmed = res.data.confirmOrder;
if (orderConfirmed.stripeError) {
this.handleStripeError(orderConfirmed);
} else {
this.props.router.replace(
`/${orderConfirmed.order.fromAccount.slug}/admin/payment-methods?successType=payment`,
);
}
} catch (e) {
const error = get(e, 'graphQLErrors.0') || e;
this.setState({ status: ConfirmOrderPage.ERROR, error: error.message });
}
}
handleStripeError = async ({ id, stripeError: { message, account, response } }) => {
if (!response) {
this.setState({ status: ConfirmOrderPage.ERROR, error: message });
return;
}
if (response.paymentIntent) {
const stripe = await getStripe(null, account);
const result = await stripe.handleCardAction(response.paymentIntent.client_secret);
if (result.error) {
this.setState({ status: ConfirmOrderPage.ERROR, error: result.error.message });
}
if (result.paymentIntent && result.paymentIntent.status === 'requires_confirmation') {
this.triggerRequest({ id });
}
}
};
render() {
const { status, error } = this.state;
return (
<AuthenticatedPage title="Order confirmation">
<Container
display="flex"
py={[5, 6]}
px={2}
flexDirection="column"
alignItems="center"
background="linear-gradient(180deg, #EBF4FF, #FFFFFF)"
>
{status === ConfirmOrderPage.SUBMITTING && (
<MessageBox type="info" isLoading>
<FormattedMessage id="Order.Confirm.Processing" defaultMessage="Confirming your payment method…" />
</MessageBox>
)}
{status === ConfirmOrderPage.ERROR && (
<MessageBox type="error" withIcon>
{error}
</MessageBox>
)}
</Container>
</AuthenticatedPage>
);
}
}
export const confirmOrderMutation = gql`
mutation ConfirmOrder($order: OrderReferenceInput!) {
confirmOrder(order: $order) {
order {
id
status
transactions {
id
}
fromAccount {
id
slug
}
}
stripeError {
message
account
response
}
}
}
`;
const addConfirmOrderMutation = graphql(confirmOrderMutation, {
name: 'confirmOrder',
options: { context: API_V2_CONTEXT },
});
export default withUser(addConfirmOrderMutation(withRouter(ConfirmOrderPage)));