I am building a website in webflow - most of it is no code. However I need a button on my website to have dual functionality. One is to submit a form and the other is to submit payment. I have custom code that I will paste below, However when I press submit the payment won't go through. Anyone know how I could remediate this?
I also tried redirecting to a stripe link but then I'm at a loss as to how the button can have dual functionality. Ideally after hitting the submit button, I'd like the button to submit a form then to redirect to a stripe payment link - can anyone assist?
<script>
function selectOnlyThis(id) {
for (var i = 1;i <= 3; i++){
if ("Check" + i === id && document.getElementById("Check" + i).checked === true){
document.getElementById("Check" + i).checked = true;
} else {
document.getElementById("Check" + i).checked = false;
}
}
}
$(function() {
let submitted = false;
$('#Sell-form').submit(function (e) {
if (submitted) {
return true;
}
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
const amount = $('#cc-amount')
const cardName = $('#cc-name')
const cardNumber = $('#cc-number')
const cardExpir = $('#cc-exp')
const cardCv2 = $('#cc-cv2')
const frame = document.querySelector('#frame')
frame.contentDocument.write(
`
<form action="https://www.usaepay.com/gate.php" method="POST" id="inner-form">
<input type="hidden" name="UMkey" value="API_KEY">
<input type="hidden" name="UMredirDeclined" value="https://gownshadchan.com/failure">
<input type="hidden" name="UMredirApproved" value="https://gownshadchan.com/success">
<input type="hidden" name="UMname" value="${cardName.val()}">
<input type="hidden" name="UMcard" value="${cardNumber.val()}">
<input type="hidden" name="UMexpir" value="${cardExpir.val()}">
<input type="hidden" name="UMamount" value="${amount.val()}">
<input type="hidden" name="UMcvv2" value="${cardCv2.val()}">
`
)
function success() {
cardNumber.val('****' + cardNumber.val().slice(-4))
cardExpir.remove()
cardCv2.remove()
submitted = true;
$('#Sell-form').submit()
}
function failure() {
submitted = false;
$('#submission-error-message').show()
}
frame.onload = function () {
const content = frame.contentWindow.document.body.innerHTML.trim()
if (content.includes('success')) {
success()
} else {
failure()
}
}
let doc = (frame.contentWindow || frame.contentDocument);
if (doc.document) doc = doc.document;
doc.getElementById("inner-form").submit();
})
});
</script>
First off, you really don't want those credit card details passing through your client-side code. There are a host of undesirable privacy, security and PCI compliance problems with that approach.
A better approach is to submit your form data and then redirect them to a 3rd party hosted payment page. Let them deal with the PCI compliance requirements.
To your core question, your code is close.
The way to perform multiple actions on a form submit is to intercept it, prevent default, and then post the form data directly to its handler using .post()
.
In your case that looks something like;
$('#Sell-form').submit(function (e) {
if (submitted) {
return true;
}
const form = $(this);
e.preventDefault();
$.post(
form.attr("action"),
form.serialize(),
function (data, status, xhr) {
// handle success conditions
// e.g. the redirect to the hosted payment page
}.bind(handler))
.fail(function (jqxhr, settings, ex) {
// handle failure conditions
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.