-
Notifications
You must be signed in to change notification settings - Fork 0
/
PaymentWidget.tsx
84 lines (71 loc) · 2.17 KB
/
PaymentWidget.tsx
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
import { useEffect, useState } from 'react';
import { css } from '@emotion/react';
import { Spinner, cx, spacing, center } from '@sumup/circuit-ui';
import usePaymentWidget from '../hooks/use-payment-widget';
import apiClient from '../modules/app-api-client';
type OnEventHandler = (event: { message: string }) => void;
function PaymentWidget({
onSuccess,
onError,
}: {
onSuccess: OnEventHandler;
onError: OnEventHandler;
}) {
const [isReady, setIsReady] = useState(false);
const [paymentWidget] = usePaymentWidget();
useEffect(() => {
if (!paymentWidget) {
return;
}
apiClient
.createCheckout({
paymentType: '',
})
.then((checkout: { id: string }) => {
paymentWidget.mount({
onLoad: () => setIsReady(true),
checkoutId: checkout.id,
onPaymentMethodsLoad: (payments: { eligible: { id: string }[] }) => {
return payments.eligible
.map((p) => p.id)
.filter((pId) => pId !== 'apple_pay');
},
onResponse: function (type: string, body: { status: string }) {
if (type === 'success' && body.status === 'PAID') {
onSuccess({ message: 'Thanks! 🎉' });
}
if (type === 'success' && body.status === 'FAILED') {
onError({
message:
'Something went wrong with the payment. Check your info and try again.',
});
}
if (type === 'fail') {
onError({
message:
'Something went wrong with our system. See console logs for details.',
});
}
console.log('Type', type);
console.log('Body', body);
},
showFooter: false,
});
});
}, [paymentWidget]);
return (
<>
{!isReady && (
<div css={cx(center, spacing({ top: 'giga', bottom: 'giga' }))}>
<Spinner
css={(theme: any) => css`
color: ${theme.colors.p500};
`}
/>
</div>
)}
<div id="sumup-card" hidden={!isReady} />
</>
);
}
export default PaymentWidget;