-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpayzeApplePay.js
207 lines (167 loc) · 5.38 KB
/
payzeApplePay.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
/**
* Init Payze Apple Pay SDK
*
* @param {string=} merchantIdentifier Merchant Identifier (merchant.io.payze...)
* @param {string=} config // configure
* @param {string=} config.amount // pay amount (0.1, 10, 2.5)
* @param {string=} config.currencyCode // default is GEL
* @param {string=} config.label // 'Payze' is default
* @param {function=} callback // callback to get the status of the payment
*
*/
function PayzeApplePay(merchantIdentifier, { amount, currencyCode, label }, callback = null) {
if (!merchantIdentifier) {
throw "merchant Identifier is required";
}
if (!amount) {
throw "amount is required";
}
if (!currencyCode) {
currencyCode = "GEL";
}
var countryCode = "GE";
if (!label) {
label = "Payze";
}
var canUseApplePay = false;
var BASE_URL = "https://payze.io/v2/api/applepay";
var headers = {
"Accept": "text/plain",
"Accept-Encoding": "gzip, deflate, br",
"Content-type": "application/json"
};
var promise = init();
console.info('Payze Apple Pay SDK initialized');
function validateMerchant(trId, preAuth) {
return fetch(`${BASE_URL}/start-payment`, {
method: "POST",
body: JSON.stringify({
transactionId: trId,
preauthorize: preAuth
}),
headers: headers
});
}
function init() {
if (!canUseApplePay && window.ApplePaySession) {
var promise = window.ApplePaySession.canMakePaymentsWithActiveCard(merchantIdentifier);
return promise;
}
return null;
}
/**
* Make Payze Apple Pay With TransactionId
*
* @param {string} trId Transaction ID.
*
*/
function makeApplePay(trId, preAuth) {
if (promise) {
promise.then(function (canMakePaymentsWallet) {
canUseApplePay = canMakePaymentsWallet;
if (!canMakePaymentsWallet) {
try {
var button = document.getElementById('apple-pay-button');
if (button) {
button.removeEventListener('click', makeApplePay, false);
button.remove();
}
}
catch (e) { }
}
if (!canUseApplePay) {
throw "can't use apple pay";
}
if (!trId) {
throw "transactionId is required";
}
if (!preAuth) {
preAuth = false;
}
var applePayToken = null;
var request = {
countryCode: countryCode,
currencyCode: currencyCode,
supportedNetworks: ['visa', 'masterCard', 'amex', 'discover'],
merchantCapabilities: ['supports3DS'],
total: { label: label, amount: amount }
}
const session = new (window).ApplePaySession(10, request);
session.onvalidatemerchant = async (event) => {
const merchantSession = validateMerchant(trId, preAuth);
merchantSession.then((response) => {
response.json().then((data) => {
applePayToken = data.data.token;
session.completeMerchantValidation(data.data);
}).catch((err) => {
console.log(err);
})
}).catch((err) => {
console.log(err);
});
};
session.onpaymentmethodselected = (event) => {
const update = {
newTotal: { label: label, amount: amount },
};
session.completePaymentMethodSelection(update);
};
session.onshippingmethodselected = (event) => {
const update = {};
session.completeShippingMethodSelection(update);
};
session.onpaymentauthorized = (event) => {
var token = event.payment.token;
var acceptApplePay = fetch(`${BASE_URL}/accept`, {
method: "POST",
body: JSON.stringify({
token: applePayToken,
payzeTransactionId: trId,
acceptRequest: {
version: token.paymentData.version,
data: token.paymentData.data,
signature: token.paymentData.signature,
ephemeralPublicKey: token.paymentData.header.ephemeralPublicKey,
publicKeyHash: token.paymentData.header.publicKeyHash,
transactionId: token.paymentData.header.transactionId,
displayName: token.paymentMethod.displayName,
network: token.paymentMethod.network,
type: token.paymentMethod.type,
transactionIdentifier: token.transactionIdentifier
}
}),
headers: headers
});
var status = (window).ApplePaySession.STATUS_FAILURE;
acceptApplePay.then((response) => {
response.json().then((data) => {
if (data.status.code == "Success") {
status = (window).ApplePaySession.STATUS_SUCCESS;
}
const result = {
"status": status
};
session.completePayment(result);
if (callback) {
callback(result);
}
})
});
};
session.oncancel = (event) => {
if (callback) {
const result = {
"status": (window).ApplePaySession.STATUS_FAILURE
};
callback(result, event);
}
}
session.begin();
});
}
}
return {
makeApplePay
};
}
module.exports.PayzeApplePay = PayzeApplePay;