-
Notifications
You must be signed in to change notification settings - Fork 1
/
payment.js
227 lines (191 loc) · 5.92 KB
/
payment.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
const paymentForm = document.getElementById("payment_form");
const confirmForm = document.getElementById("confirm_form");
const submitButton = paymentForm.querySelector('button[type="submit"]');
const cardInputContainer = document.getElementById("card_input");
const cardInputError = document.getElementById("card_input_error");
// Show/hide full screen loader
const setLoading = (isLoading) => {
if (isLoading) {
document.body.classList.add("loading");
submitButton.disabled = true;
} else {
document.body.classList.remove("loading");
submitButton.disabled = false;
}
};
// Pass paymentId and paymentToken to confirm payment using monei.js
// This will automatically open a 3D secure confirmation popup if needed
// As an alternative you can redirect your customer to payment.nextAction.redirectUrl on the server
const moneiTokenHandler = async (paymentToken, cardholderName) => {
setLoading(true);
const params = {
paymentId: window.paymentId,
paymentToken
};
// Send a cardholder name for the card payments
if (cardholderName) {
params.paymentMethod = {
card: {cardholderName}
};
}
try {
const result = await monei.confirmPayment(params);
// At this moment you can show a customer the payment result
// But you should always rely on the result passed to the callback endpoint on your server
// to update the order status
console.log(result);
if (result.nextAction && result.nextAction.mustRedirect) {
window.location.assign(result.nextAction.redirectUrl);
} else {
// Redirect your customer to the receipt page to check the payment status (server-side)
window.location.assign(`/receipt?id=${window.paymentId}`);
}
} catch (error) {
console.log(error);
window.location.assign(`/receipt?id=${window.paymentId}`);
}
};
const bizumButton = document.getElementById("bizum");
if (bizumButton) {
// Initialize Bizum payment button
const bizumButton = monei.Bizum({
paymentId: window.paymentId,
// You can specify UI component language
language: "en",
// Specify button styles
style: {
height: 42
},
onLoad(isSupported) {
if (!isSupported) {
// Hide payment request button if it is not supported
bizumButton.classList.add("d-none");
}
},
// Specify a callback when payment is submitted
onSubmit(result) {
console.log(result);
if (result.token) {
setLoading(true);
moneiTokenHandler(result.token);
}
},
onError(error) {
console.log(error);
}
});
bizumButton.render("#bizum");
}
// Initialize PayPal payment button
const paypalButton = monei.PayPal({
paymentId: window.paymentId,
// You can specify UI component language
language: "en",
// Specify button styles
style: {
height: 42
},
// Specify a callback when payment is submitted
onSubmit(result) {
console.log(result);
if (result.token) {
// Pass the payment token to monei.js to confirm payment
moneiTokenHandler(result.token);
}
},
onError(error) {
console.log(error);
}
});
// Render PayPal button into the container div
paypalButton.render("#paypal");
// Initialize Payment Request button
// This component will render either Apple Pay or Google Pay button
// depending on the browser and operation system
const paymentRequestButton = monei.PaymentRequest({
paymentId: window.paymentId,
// You can specify UI component language
language: "en",
// Specify button styles
style: {
height: 42
},
onLoad(isSupported) {
if (!isSupported) {
// Hide payment request button if it is not supported
document.getElementById("payment_request").classList.add("d-none");
}
},
// Specify a callback when payment is submitted
onSubmit(result) {
console.log(result);
if (result.token) {
// Pass the payment token to monei.js to confirm payment
moneiTokenHandler(result.token);
}
},
onError(error) {
console.log(error);
}
});
// Render Payment Request button into the container div
paymentRequestButton.render("#payment_request");
// Initialize Card Input component
const cardInput = monei.CardInput({
paymentId: window.paymentId,
// You can specify UI component language
language: "en",
onLoad: () => {
// Enable submit button when component is loaded
submitButton.disabled = false;
},
onFocus: () => {
cardInputContainer.classList.add("is-focused");
},
onBlur: () => {
cardInputContainer.classList.remove("is-focused");
},
onEnter: () => {
// Submit form on enter key inside the card input
submitButton.click();
},
onChange: (props) => {
// Provide real time validation errors
if (props.isTouched) {
if (props.error) {
cardInputContainer.classList.add("is-invalid");
cardInputContainer.classList.remove("is-valid");
cardInputError.innerText = props.error;
} else {
cardInputContainer.classList.remove("is-invalid");
cardInputContainer.classList.add("is-valid");
cardInputError.innerText = "";
}
}
}
});
// Render card input into a container div
cardInput.render(cardInputContainer);
// Generate Card Input payment token when payment form is submitted
paymentForm.addEventListener("submit", async (e) => {
e.preventDefault();
e.stopPropagation();
// Check if form is valid
const isValid = paymentForm.checkValidity();
paymentForm.classList.add("was-validated");
if (!isValid) return;
setLoading(true);
try {
// Generate a payment token from the Card Input
const {token} = await monei.createToken(cardInput);
if (!token) {
return setLoading(false);
}
const cardholderName = paymentForm.querySelector('input[name="cardholder"]').value;
// Pass payment token and cardholder name to monei.js to confirm payment
await moneiTokenHandler(token, cardholderName);
} catch (error) {
console.log(error);
setLoading(false);
}
});