-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfront.js
72 lines (63 loc) · 2.07 KB
/
front.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
/* Stripe setup */
const stripe = Stripe(STRIPE_KEY);
let amount = parseFloat(price_el.innerText) * 100;
// ELEMENTS
const elements = stripe.elements({
mode: 'payment',
amount,
currency: 'eur'
});
const expressCheckout = elements.create('expressCheckout');
expressCheckout.mount(express_checkout_el);
// EVENTS
expressCheckout.on('click', event => {
const new_amount = parseFloat(price_el.innerText) * 100;
if (new_amount !== amount) {
amount = new_amount;
elements.update({ amount });
}
event.resolve();
});
// CONFIRM PAYMENT WITH CLIENT DETAILS FROM APPLE PAY
expressCheckout.on('confirm', async event => {
try {
// Confirm Payment and retrieve client details from Apple Pay
const result=await stripe.confirmPayment({
elements,
confirmParams:{
return_url: 'https://eshop.teticharitou.com/checkout',
}
});
if(result.error){
alert(result.error.message);
return;
}
// Extract client details from Apple Pay
const{payment_intent,payment_method}=result;
const clientDetails=payment_method.billing_details;
// Send client details to your backend endpoint
await api('/client/update', {
client_first_name: clientDetails.name.split(' ')[0] || '',
client_last_name: clientDetails.name.split(' ')[1] || '',
client_telephone: clientDetails.phone || '',
client_email: clientDetails.email || '',
client_country_id: get_country_id(clientDetails.address.country || 'el'),
client_city: clientDetails.address.city || '',
client_postal_code: clientDetails.address.postal_code || '',
client_address: clientDetails.address.line1 || ''
});
// Check if purchase can proceed
const can_proceed=await api('/client/payment/check');
if(!can_proceed){
alert("Payment cannot proceed. Please complete all required fields.");
return;
}
// Complete the purchase
const response=await api('/buy/stripe',{payment_intent});
if(response)
location.href='/order/'+response.order.id;
} catch (error) {
console.error("Error in payment confirmation:", error);
alert("Payment failed. Please try again.");
}
});