forked from MattFoley/react-native-paypal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (62 loc) · 2.19 KB
/
index.js
File metadata and controls
69 lines (62 loc) · 2.19 KB
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
'use strict';
let {NativeModules, Platform} = require('react-native')
let {PayPal, MFLReactNativePayPal} = NativeModules;
let constants;
if (Platform.OS === 'android') {
constants = {};
let constantNames = Object.keys(PayPal).filter(p => p == p.toUpperCase());
constantNames.forEach(c => constants[c] = PayPal[c]);
} else {
constants = {
SANDBOX: 0,
PRODUCTION: 1,
NO_NETWORK: 2,
USER_CANCELLED: 'USER_CANCELLED',
INVALID_CONFIG: 'INVALID_CONFIG',
}
Object.keys(MFLReactNativePayPal.PaymentIntent)
.forEach((key) => {
constants[`PAYMENT_INTENT_${key.toUpperCase()}`] = MFLReactNativePayPal.PaymentIntent[key];
})
}
let functions = {
paymentRequest(payPalParameters) {
return new Promise(function(resolve, reject) {
if (Platform.OS === 'android') {
PayPal.paymentRequest(payPalParameters, (confirm, payment) => {
resolve(JSON.parse(confirm), JSON.parse(payment))
}, reject);
} else {
MFLReactNativePayPal.initializePaypalEnvironment(payPalParameters.environment, payPalParameters.clientId);
MFLReactNativePayPal.preparePaymentOfAmount(
payPalParameters.price,
payPalParameters.currency,
payPalParameters.description,
payPalParameters.softDescriptor,
payPalParameters.paymentIntent || MFLReactNativePayPal.PaymentIntent.Sale,
);
MFLReactNativePayPal.prepareConfigurationForMerchant(
payPalParameters.merchantName,
payPalParameters.acceptCreditCards !== null && payPalParameters.acceptCreditCards !== undefined
? payPalParameters.acceptCreditCards
: true,
payPalParameters.defaultUserEmail,
);
MFLReactNativePayPal.presentPaymentViewControllerForPreparedPurchase((error, payload) => {
if (error) {
reject(constants.INVALID_CONFIG, error)
} else {
if (payload.status === 1) {
resolve(payload.confirmation);
} else {
reject(constants.USER_CANCELLED, payload);
}
}
});
}
});
}
};
var exported = {};
Object.assign(exported, constants, functions);
module.exports = exported;