-
Notifications
You must be signed in to change notification settings - Fork 685
/
paymentMethods.js
121 lines (106 loc) · 3.68 KB
/
paymentMethods.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
import React from 'react';
import { shape, string, bool, func } from 'prop-types';
import { useIntl } from 'react-intl';
import { usePaymentMethods } from '@magento/peregrine/lib/talons/CheckoutPage/PaymentInformation/usePaymentMethods';
import { useStyle } from '../../../classify';
import RadioGroup from '@magento/venia-ui/lib/components/RadioGroup';
import Radio from '@magento/venia-ui/lib/components/RadioGroup/radio';
import defaultClasses from './paymentMethods.module.css';
import payments from './paymentMethodCollection';
const PaymentMethods = props => {
const {
classes: propClasses,
onPaymentError,
onPaymentSuccess,
resetShouldSubmit,
shouldSubmit
} = props;
const { formatMessage } = useIntl();
const classes = useStyle(defaultClasses, propClasses);
const talonProps = usePaymentMethods({});
const {
availablePaymentMethods,
currentSelectedPaymentMethod,
handlePaymentMethodSelection,
initialSelectedMethod,
isLoading
} = talonProps;
if (isLoading) {
return null;
}
const radios = availablePaymentMethods
.map(({ code, title }) => {
// If we don't have an implementation for a method type, ignore it.
if (!Object.keys(payments).includes(code)) {
return;
}
const id = `paymentMethod--${code}`;
const isSelected = currentSelectedPaymentMethod === code;
const PaymentMethodComponent = payments[code];
const renderedComponent = isSelected ? (
<PaymentMethodComponent
onPaymentSuccess={onPaymentSuccess}
onPaymentError={onPaymentError}
resetShouldSubmit={resetShouldSubmit}
shouldSubmit={shouldSubmit}
/>
) : null;
return (
<div key={code} className={classes.payment_method}>
<Radio
id={id}
label={title}
value={code}
classes={{
label: classes.radio_label
}}
checked={isSelected}
onChange={handlePaymentMethodSelection}
/>
{renderedComponent}
</div>
);
})
.filter(paymentMethod => !!paymentMethod);
const noPaymentMethodMessage = !radios.length ? (
<div className={classes.payment_errors}>
<span>
{formatMessage({
id: 'checkoutPage.paymentLoadingError',
defaultMessage: 'There was an error loading payments.'
})}
</span>
<span>
{formatMessage({
id: 'checkoutPage.refreshOrTryAgainLater',
defaultMessage: 'Please refresh or try again later.'
})}
</span>
</div>
) : null;
return (
<div className={classes.root}>
<RadioGroup
classes={{ root: classes.radio_group }}
field="selectedPaymentMethod"
initialValue={initialSelectedMethod}
>
{radios}
</RadioGroup>
{noPaymentMethodMessage}
</div>
);
};
export default PaymentMethods;
PaymentMethods.propTypes = {
classes: shape({
root: string,
payment_method: string,
radio_label: string
}),
onPaymentSuccess: func,
onPaymentError: func,
resetShouldSubmit: func,
selectedPaymentMethod: string,
shouldSubmit: bool
};