-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
280 lines (230 loc) · 6.7 KB
/
index.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// main index.js
import {NativeModules, NativeEventEmitter} from "react-native";
const {ReactNativeStripeTerminalV2} = NativeModules;
const {
checkPermissions: _checkPermissions,
discoverReaders: _discoverReaders,
connectInternetReader: _connectInternetReader,
initialize: _initialize,
setConnectionToken: _setConnectionToken,
createPaymentIntent: _createPaymentIntent,
connectBluetoothReader: _connectBluetoothReader,
cancelDiscoverReaders: _cancelDiscoverReaders,
processPaymentIntent: _processPaymentIntent,
collectPaymentMethod: _collectPaymentMethod,
retrievePaymentIntent: _retrievePaymentIntent,
cancelReadReusableCard: _cancelReadReusableCard,
readReusableCard: _readReusableCard,
refundCharge: _refundCharge,
cancelRefundCharge: _cancelRefundCharge,
disconnectReader: _disconnectReader,
cancelInstallUpdate: _cancelInstallUpdate,
cancelCurrentAction: _cancelCurrentAction,
cancelCollectPaymentMethod: _cancelCollectPaymentMethod,
getConnectedReader: _getConnectedReader,
getPaymentIntent: _getPaymentIntent,
getPaymentStatus: _getPaymentStatus,
getConnectionStatus: _getConnectionStatus,
cancelPaymentIntent: _cancelPaymentIntent,
} = ReactNativeStripeTerminalV2;
const listeners = new NativeEventEmitter(ReactNativeStripeTerminalV2);
export const DISCOVERY_METHOD = {
BLUETOOTH_PROXIMITY: 0,
BLUETOOTH_SCAN: 1,
INTERNET: 2,
};
listeners.addListener("onRequestConnectionToken", async () => {
if (defaultOptions.fetchConnectionToken === null) {
throw new Error("FETCH_CONNECTION_INVALID", "fetchConnectionToken has to be defined inside the initialize method");
}
const secret = await defaultOptions.fetchConnectionToken();
_setConnectionToken(secret, null);
});
const defaultOptions = {
fetchConnectionToken: null,
};
/**
*
* @param options
* @param options.fetchConnectionToken
* @param options.verboseLogs
*
* @return {Promise<*>}
*/
export async function initialize(options = {}) {
const {fetchConnectionToken = null, verboseLogs = false} = options;
if (fetchConnectionToken === null) {
throw new Error("FETCH_CONNECTION_INVALID", "fetchConnectionToken has to be defined inside the initialize method");
}
defaultOptions.fetchConnectionToken = fetchConnectionToken;
return _initialize(verboseLogs);
}
export async function checkPermissions() {
return _checkPermissions();
}
/**
*
* @param options
* @param options.amount
* @param options.currency
* @param options.customer
* @param options.onBehalfOf
* @param options.receiptEmail
* @param options.description
* @param options.statementDescriptor
* @param options.applicationFeeAmount
* @param options.transferDataDestination
* @param options.stripeDescription
* @param options.statementDescriptor
* @param options.metadata
* @param options.transferGroup
*
* @return {Promise<*>}
*/
export async function createPaymentIntent(options = {}) {
const {
amount = 1000,
currency = "USD",
applicationFeeAmount,
transferDataDestination = null,
onBehalfOf = null,
description,
} = options;
if (description) {
options.stripeDescription = description;
}
if (
applicationFeeAmount &&
(transferDataDestination === null || onBehalfOf === null)
) {
return Promise.reject({error: "APPLICATION_FEE_ERROR", message: "Both need to be set"});
}
if (transferDataDestination && onBehalfOf === null || transferDataDestination === null && onBehalfOf) {
return Promise.reject({error: "ON_BEHALF_ERROR", message: "Both need to be set"});
}
return _createPaymentIntent(amount, currency, options);
}
/**
*
* @param charge
* @param amount
* @param options
* @param options.reverseTransfer
* @param options.metadata
* @param options.currency
* @param options.refundApplicationFee
*
* @return {Promise<void>}
*/
export async function refundCharge(charge, amount, options) {
_refundCharge(charge, amount, options);
}
export async function cancelRefundCharge() {
_cancelRefundCharge();
}
/**
*
*
* @param options
* @param options.customer
* @param options.metadata
*
* @return {Promise<*>}
*/
export async function readReusableCard(options = {}) {
return _readReusableCard(options);
}
export async function cancelReadReusableCard() {
return _cancelReadReusableCard();
}
export async function cancelInstallUpdate() {
return _cancelInstallUpdate();
}
export async function retrievePaymentIntent(payment) {
return _retrievePaymentIntent(payment);
}
export async function collectPaymentMethod(options) {
return _collectPaymentMethod();
}
export async function processPaymentIntent() {
const processResponse = await _processPaymentIntent();
if (processResponse.error) {
return Promise.reject(processResponse);
}
return processResponse.intent;
}
export async function connectBluetoothReader(reader, location) {
return _connectBluetoothReader(reader, location);
}
export async function disconnectReader() {
return _disconnectReader();
}
export async function cancelDiscoverReaders() {
return _cancelDiscoverReaders();
}
/**
*
* @param reader
* @param options
* @param options.initWithFailIfInUse
* @param options.allowCustomerCancel
*
* @return {Promise<*>}
*/
export async function connectInternetReader(reader, options = {}) {
const {initWithFailIfInUse = true, allowCustomerCancel = false} = options;
return _connectInternetReader(
reader,
initWithFailIfInUse,
allowCustomerCancel
);
}
/**
*
* @param params
* @param params.simulated if we are simulated
* @param params.locationId location id for targeted readers
* @param params.method {DISCOVERY_METHOD} discover method
*
* @return {Promise<*>}
*/
export async function discoverReaders(params) {
const {
method = DISCOVERY_METHOD.BLUETOOTH_SCAN,
simulated = false,
locationId = null,
} = params;
return _discoverReaders(method, locationId, simulated);
}
export async function cancelCurrentAction() {
return _cancelCurrentAction();
}
export async function cancelCollectPaymentMethod() {
return _cancelCollectPaymentMethod();
}
export async function cancelPaymentIntent() {
return _cancelPaymentIntent();
}
export async function getConnectedReader() {
const connectedReader = await _getConnectedReader();
if (connectedReader) {
return connectedReader;
}
return null;
}
export async function getPaymentIntent() {
const paymentIntent = await _getPaymentIntent();
if (paymentIntent) {
return paymentIntent;
}
return null;
}
export async function getPaymentStatus() {
return _getPaymentStatus();
}
export async function getConnectionStatus() {
return _getConnectionStatus();
}
export function registerTerminalListener(key, method) {
return listeners.addListener(key, method);
}