changes
This commit is contained in:
@@ -8,8 +8,9 @@
|
||||
<%- include('partials/nav') %>
|
||||
<div class="content">
|
||||
<form id="payment-form">
|
||||
Payment Here!!!<%=amount%>
|
||||
|
||||
<!-- Stripe Elements will inject the Card Element here -->
|
||||
You are about to charge $<%=payforwhat.total.toFixed(2)%> for <%=payforwhat.qty%> tickets.<br>
|
||||
|
||||
<label for="card-number-element">Card Number</label>
|
||||
<div id="card-number-element"></div>
|
||||
@@ -23,7 +24,7 @@ Payment Here!!!<%=amount%>
|
||||
<label for="card-postal-element">ZIP Code</label>
|
||||
<div id="card-postal-element"></div>
|
||||
|
||||
<button id="submit-button" type="submit">Pay <%=amount%> Now</button>
|
||||
<button id="submit-button" type="submit">Pay Now</button>
|
||||
</form>
|
||||
</div>
|
||||
<!-- Stripe.js -->
|
||||
@@ -46,54 +47,54 @@ Payment Here!!!<%=amount%>
|
||||
|
||||
const paymentForm = document.getElementById("payment-form");
|
||||
paymentForm.addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
e.preventDefault();
|
||||
|
||||
// 1. Create a PaymentMethod
|
||||
const { paymentMethod, error } = await stripe.createPaymentMethod({
|
||||
// 1. Create a PaymentMethod
|
||||
const { paymentMethod, error } = await stripe.createPaymentMethod({
|
||||
type: "card",
|
||||
card: cardNumberElement,
|
||||
});
|
||||
});
|
||||
|
||||
if (error) {
|
||||
if (error) {
|
||||
console.error(error);
|
||||
alert(error.message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Send PaymentMethod ID to server
|
||||
const response = await fetch("/charge", {
|
||||
// 2. Send PaymentMethod ID to server
|
||||
console.log("Payforwhat: ",<%-JSON.stringify(payforwhat)%>);
|
||||
const pfw = <%-JSON.stringify(payforwhat)%>;
|
||||
const response = await fetch("/charge", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ paymentMethodId: paymentMethod.id, ticket:"<%=ticket%>", amount:"<%=amount%>" }),
|
||||
});
|
||||
body: JSON.stringify({ paymentMethodId: paymentMethod.id, payforwhat: pfw }),
|
||||
});
|
||||
|
||||
const responseData = await response.json();
|
||||
const responseData = await response.json();
|
||||
|
||||
if (responseData.error) {
|
||||
if (responseData.error) {
|
||||
console.error(responseData.error);
|
||||
alert(responseData.error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (responseData.requiresAction) {
|
||||
// 3. Handle Additional Action if required (e.g., 3DS)
|
||||
const { error: confirmError } = await stripe.confirmCardPayment(
|
||||
responseData.paymentIntentClientSecret
|
||||
);
|
||||
if (responseData.requiresAction) {
|
||||
// 3. Handle Additional Action if required (e.g., 3DS)
|
||||
const { error: confirmError } = await stripe.confirmCardPayment(responseData.paymentIntentClientSecret);
|
||||
|
||||
if (confirmError) {
|
||||
if (confirmError) {
|
||||
console.error(confirmError);
|
||||
alert(confirmError.message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
alert("Payment succeeded");
|
||||
// redirect or show success message
|
||||
window.location.href = responseData.redirect_url;
|
||||
} else if (responseData.success) {
|
||||
// Payment succeeded without additional action
|
||||
alert("Payment succeeded.");
|
||||
window.location.href = responseData.redirect_url;
|
||||
alert("Payment succeeded");
|
||||
// redirect or show success message
|
||||
window.location.href = responseData.redirect_url;
|
||||
} else if (responseData.success) {
|
||||
// Payment succeeded without additional action
|
||||
alert("Payment succeeded.");
|
||||
window.location.href = responseData.redirect_url;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user