-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
150 lines (139 loc) · 5.48 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
import {
NativeEventEmitter,
NativeModules,
Platform,
Linking,
} from "react-native";
const SquarePOS = NativeModules.RNSquarePos;
let callbackUrl;
const errors = {
CANNOT_OPEN_SQUARE: "CANNOT_OPEN_SQUARE",
UNKNOWN_IOS_ERROR: "UNKNOWN_IOS_ERROR",
UNKNOWN_ANDROID_ERROR: "UNKNOWN_ANDROID_ERROR",
PAYMENT_CANCELLED: "PAYMENT_CANCELLED",
NOT_LOGGED_IN: "NOT_LOGGED_IN",
USER_NOT_ACTIVE: "USER_NOT_ACTIVE",
AMOUNT_TOO_SMALL: "AMOUNT_TOO_SMALL",
AMOUNT_TOO_LARGE: "AMOUNT_TOO_LARGE",
INVALID_TENDER_TYPE: "INVALID_TENDER_TYPE",
UNSUPPORTED_TENDER_TYPE: "UNSUPPORTED_TENDER_TYPE",
NO_NETWORK_CONNECTION: "NO_NETWORK_CONNECTION",
TRANSACTION_ALREADY_IN_PROGRESS: "TRANSACTION_ALREADY_IN_PROGRESS",
INVALID_REQUEST: "INVALID_REQUEST",
};
const iosErrors = {
payment_canceled: errors.PAYMENT_CANCELLED,
not_logged_in: errors.NOT_LOGGED_IN,
user_not_active: errors.USER_NOT_ACTIVE,
amount_too_small: errors.AMOUNT_TOO_SMALL,
amount_too_large: errors.AMOUNT_TOO_LARGE,
invalid_tender_type: errors.INVALID_TENDER_TYPE,
unsupported_tender_type: errors.UNSUPPORTED_TENDER_TYPE,
no_network_connection: errors.NO_NETWORK_CONNECTION,
};
const androidErrors = {
TRANSACTION_CANCELED: errors.PAYMENT_CANCELLED,
USER_NOT_LOGGED_IN: errors.NOT_LOGGED_IN,
USER_NOT_ACTIVATED: errors.USER_NOT_ACTIVE,
NO_NETWORK: errors.NO_NETWORK_CONNECTION,
TRANSACTION_ALREADY_IN_PROGRESS: errors.TRANSACTION_ALREADY_IN_PROGRESS,
INVALID_REQUEST: errors.INVALID_REQUEST,
};
const RNSquarePos = {
ERRORS: errors,
configure: (options) => {
SquarePOS.setApplicationId(options.applicationId);
callbackUrl = options.callbackUrl;
},
transaction: (amount, currency, options = {}) => {
let subscription = null;
return new Promise((resolve, reject) => {
if (Platform.OS === "android") {
SquarePOS.startTransaction(amount, currency, options, () => {
return reject({
errorCode: errors.CANNOT_OPEN_SQUARE,
});
});
function handleResponse(data) {
subscription.remove();
subscription = null;
if (data.errorCode) {
if (androidErrors[data.errorCode]) {
return reject({
errorCode: androidErrors[data.errorCode],
squareResponse: data,
});
} else {
return reject({
errorCode: errors.UNKNOWN_ANDROID_ERROR,
originalCode: data.errorCode,
squareResponse: data,
});
}
} else {
return resolve(data);
}
}
const eventEmitter = new NativeEventEmitter(SquarePOS);
subscription = eventEmitter.addListener(
"RNSquarePOSResponse",
handleResponse
);
} else if (Platform.OS === "ios") {
SquarePOS.startTransaction(
amount,
currency,
options,
callbackUrl,
(errorCode, errorDescription) => {
switch (errorCode) {
case 6:
reject({
errorCode: errors.CANNOT_OPEN_SQUARE,
});
break;
default:
reject({
errorCode: errors.UNKNOWN_IOS_ERROR,
errorDescription,
});
break;
}
}
);
function handleIOSResponse(event) {
const url = event.url;
subscription.remove();
subscription = null;
if (url.match(callbackUrl)) {
const data = JSON.parse(
decodeURIComponent(url.split("?data=")[1])
);
if (data.error_code) {
if (iosErrors[data.error_code]) {
return reject({
errorCode: iosErrors[data.error_code],
});
} else {
return reject({
errorCode: errors.UNKNOWN_IOS_ERROR,
originalCode: data.error_code,
});
}
} else {
return resolve({
transactionId: data.transaction_id,
clientTransactionId: data.client_transaction_id,
});
}
}
}
subscription = Linking.addEventListener(
"url",
handleIOSResponse
);
}
});
},
};
export default RNSquarePos;