-
Notifications
You must be signed in to change notification settings - Fork 74
/
hooks.js
184 lines (158 loc) · 6.06 KB
/
hooks.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { useEffect, useState, useRef } from "react";
export default function createHooks(StripeTerminal) {
function useStripeTerminalState() {
const [connectionStatus, setConnectionStaus] = useState(StripeTerminal.ConnectionStatusNotConnected);
const [paymentStatus, setPaymentStatus] = useState(StripeTerminal.PaymentStatusNotReady);
const [lastReaderEvent, setLastReaderEvent] = useState(StripeTerminal.ReaderEventCardRemoved);
const [connectedReader, setConnectedReader] = useState(null);
const [readerInputOptions, setReaderInputOptions] = useState(null);
const [readerInputPrompt, setReaderInputPrompt] = useState(null);
useEffect(() => {
// Populate initial values
StripeTerminal.getConnectionStatus().then(s => setConnectionStaus(s));
StripeTerminal.getPaymentStatus().then(s => setPaymentStatus(s));
StripeTerminal.getLastReaderEvent().then(e => setLastReaderEvent(e));
StripeTerminal.getConnectedReader().then(r => setConnectedReader(r));
// Setup listeners
const listeners = [
StripeTerminal.addDidChangeConnectionStatusListener(({ status }) => {
setConnectionStaus(status);
StripeTerminal.getConnectedReader().then(r => setConnectedReader(r));
}),
StripeTerminal.addDidChangePaymentStatusListener(({ status }) => setPaymentStatus(status)),
StripeTerminal.addDidReportReaderEventListener(({ event }) => setLastReaderEvent(event)),
StripeTerminal.addDidRequestReaderInputListener(({ text }) => setReaderInputOptions(text)),
StripeTerminal.addDidRequestReaderDisplayMessageListener(({ text }) => setReaderInputPrompt(text))
];
// Cleanup: remove listeners
return () => {
listeners.forEach(l => l.remove());
};
}, []);
const cardInserted = lastReaderEvent === StripeTerminal.ReaderEventCardInserted;
return {
connectionStatus,
connectedReader,
paymentStatus,
readerInputOptions,
readerInputPrompt,
cardInserted,
clearReaderInputState: () => {
setReaderInputOptions(null);
setReaderInputPrompt(null);
}
};
}
function useStripeTerminalCreatePayment({ onSuccess, onFailure, onCapture, autoRetry, ...options }) {
const {
connectionStatus,
connectedReader,
paymentStatus,
cardInserted,
readerInputOptions,
readerInputPrompt,
clearReaderInputState
} = state = useStripeTerminalState();
const [hasCreatedPayment, setHasCreatedPayment] = useState(false);
const [isCaptured, setIsCaptured] = useState(false);
const [isCompleted, setIsCompleted] = useState(false);
const [readerError, setReaderError] = useState(null);
const [hasRetried, setHasRetried] = useState(false);
useEffect(() => {
if (paymentStatus !== StripeTerminal.PaymentStatusNotReady &&
(!hasCreatedPayment || (readerError && !hasRetried && !cardInserted))) {
setHasCreatedPayment(true);
if (readerError) {
setHasRetried(true);
}
StripeTerminal.createPayment(options)
.then(intent => {
if (onCapture) {
return onCapture(intent)
.then(onSuccess)
.catch(onFailure);
}
onSuccess(intent);
})
.catch(({ error }) => {
if (autoRetry) {
StripeTerminal.abortCreatePayment()
.then(() => {
clearReaderInputState();
setHasRetried(false);
setReaderError(error);
})
.catch(e => onFailure(e));
return;
}
onFailure(error);
})
.finally(() => setIsCompleted(true));
}
}, [paymentStatus, hasCreatedPayment, readerError, hasRetried, cardInserted]);
// Cleanup: abort if unmounted midway through payment intent creation process.
useEffect(() => {
return () => {
if (!isCompleted) {
StripeTerminal.abortCreatePayment();
}
};
}, []);
return {
...state,
readerError
};
}
const ConnectionManagerStatusConnected = 'connected';
const ConnectionManagerStatusConnecting = 'connecting';
const ConnectionManagerStatusDisconnected = 'disconnected';
const ConnectionManagerStatusScanning = 'scanning';
function useStripeTerminalConnectionManager({ service }) {
const {
connectionStatus,
connectedReader,
paymentStatus,
} = state = useStripeTerminalState();
const [managerConnectionStatus, setManagerConnectionStatus] = useState(ConnectionManagerStatusDisconnected);
const [readersAvailable, setReadersAvailable] = useState([]);
const [persistedReaderSerialNumber, setPersistedReaderSerialNumber] = useState(null);
useEffect(() => {
setManagerConnectionStatus(!!connectedReader ? ConnectionManagerStatusConnected : ConnectionManagerStatusDisconnected);
}, [connectedReader]);
useEffect(() => {
// Populate initial values
service.getPersistedReaderSerialNumber().then(s => setPersistedReaderSerialNumber(s));
// Setup listeners
const listeners = [
service.addListener('readersDiscovered', readers => setReadersAvailable(readers)),
service.addListener('readerPersisted', serialNumber => setPersistedReaderSerialNumber(serialNumber))
];
// Cleanup: remove listeners
return () => {
listeners.forEach(l => l.remove());
};
}, []);
return {
...state,
managerConnectionStatus,
readersAvailable,
persistedReaderSerialNumber,
connectReader: (serialNumber) => {
setManagerConnectionStatus(ConnectionManagerStatusConnecting);
service.connect(serialNumber);
},
discoverReaders: () => {
setManagerConnectionStatus(ConnectionManagerStatusScanning);
service.discover();
},
disconnectReader: () => {
service.disconnect();
}
};
}
return {
useStripeTerminalState,
useStripeTerminalCreatePayment,
useStripeTerminalConnectionManager
};
}