forked from datapimp/react-native-apple-pay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApplePayManager.ios.js
90 lines (70 loc) · 1.79 KB
/
ApplePayManager.ios.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
/**
* @providesModule ApplePayManager
* @flow
*/
'use strict';
var React = require('react-native');
var {
NativeModules
} = React;
var NativeApplePayManager = NativeModules.ApplePayManager;
/**
* High-level docs for the ApplePayManager iOS API can be written here.
*/
var ApplePayManager = {
canMakePayments: NativeApplePayManager.canMakePayments,
canMakePaymentsUsingNetworks: NativeApplePayManager.canMakePaymentsUsingNetworks,
paymentRequest: function(opts, items) {
var summaryItems = JSON.parse(JSON.stringify(items));
var options = opts || {};
options.DEV = __DEV__;
// Setup total item
var totalItem = {
label: opts.merchantName,
amount: getTotal(items).toString()
};
// Add total item to items
summaryItems.push(totalItem);
// Set amounts as strings
summaryItems.forEach(function(i) {
i.amount = i.amount.toString();
});
return new Promise(function(resolve, reject) {
NativeApplePayManager.paymentRequest(options, summaryItems, function(error, token) {
if (error) {
return reject(error);
}
resolve(token);
});
});
},
success: function() {
return new Promise(function(resolve, reject) {
NativeApplePayManager.success(function(error) {
if (error) {
return reject(error);
}
resolve(true);
});
});
},
failure: function() {
return new Promise(function(resolve, reject) {
NativeApplePayManager.failure(function(error) {
if (error) {
return reject(error);
}
resolve(true);
});
});
},
getTotal: getTotal
};
function getTotal(items) {
var total = 0;
items.forEach(function(i) {
total+= i.amount;
});
return total;
}
module.exports = ApplePayManager;