Created
April 22, 2021 22:10
-
-
Save dayhaysoos/77e72a647d7ee6fa276556789ed7e957 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
"use-strict"; | |
var style = { | |
base: { | |
// Add your base input styles here. For example: | |
fontSize: "16px", | |
color: "#32325d", | |
}, | |
}; | |
var elements = stripe.elements(); | |
var card = elements.create("card", { style: style }); | |
card.mount("#card-element"); | |
function stripeTokenHandler(token) { | |
// Insert the token ID into the form so it gets submitted to the server | |
var form = document.getElementById("payment-form"); | |
var hiddenInput = document.createElement("input"); | |
hiddenInput.setAttribute("type", "hidden"); | |
hiddenInput.setAttribute("name", "stripeToken"); | |
hiddenInput.setAttribute("value", token.id); | |
form.appendChild(hiddenInput); | |
// Submit the form | |
form.submit(); | |
} | |
var form = document.getElementById("payment-form"); | |
form.addEventListener("submit", function (event) { | |
event.preventDefault(); | |
stripe.createToken(card).then(function (result) { | |
if (result.error) { | |
var errorElement = document.getElementById("card-errors"); | |
errorElement.textContent = result.error.message; | |
} else { | |
stripeTokenHandler(result.token); | |
} | |
}); | |
}); | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment