Php 同时提交条带和自定义表单
我有一个表格和细节嵌入其中。表单由AJAX提交。表格的一部分是条带付款表格。当我单击但提交按钮时,我想检查详细信息是否正确,但不提交费用,直到检查并提交表单的其余部分。然后可以提交条带付款 以下是我提交表单的代码(我自己编写,经过大量简化): JS: PHP: 还有条带(来自wp条带插件,但很乐意编辑它或其他东西) JS:Php 同时提交条带和自定义表单,php,jquery,ajax,forms,stripe-payments,Php,Jquery,Ajax,Forms,Stripe Payments,我有一个表格和细节嵌入其中。表单由AJAX提交。表格的一部分是条带付款表格。当我单击但提交按钮时,我想检查详细信息是否正确,但不提交费用,直到检查并提交表单的其余部分。然后可以提交条带付款 以下是我提交表单的代码(我自己编写,经过大量简化): JS: PHP: 还有条带(来自wp条带插件,但很乐意编辑它或其他东西) JS: 函数stripeResponseHandler(状态、响应){ if(response.error){ $(“.payment errors”).show().html(re
函数stripeResponseHandler(状态、响应){
if(response.error){
$(“.payment errors”).show().html(response.error.message);
}否则{
风险值表$=$(“#wp条带支付表”);
变量标记=响应['id'];
表格$追加(“”);
var newStripeForm=form$.serialize();
$.ajax({
类型:“post”,
数据类型:“json”,
url:ajaxurl,
资料来源:NewsTripForm,
成功:功能(响应){
$(“.wp条带详细信息”).prepend(响应);
}
});
}
}
$(“#wp条带付款表”)。提交(功能(事件){
event.preventDefault();
$(“.wp条带通知”).hide();
var amount=$('.total vat').val()*100;//您要收取的金额
Stripe.createToken({
名称:$('.wp条带名称').val(),
编号:$('.card number').val(),
cvc:$('.card cvc').val(),
exp_month:$('.card expiration month').val(),
exp_year:$('.card expiration year').val()
},stripeResponseHandler);
如果(付款成功){
console.log(“付款成功”);
}
return false;//防止表单使用默认操作提交
}
所以我的问题是,如何集成Stripe内容和自定义内容?我找到了答案;下面是我如何将我自己的表单信息和Stripe的支付数据结合在一起,同时使用AJAX进行处理 PHP:
\Stripe\Stripe::setApiKey("YOUR_SECRET_KEY");
function wp_stripe_charge($amount, $card, $email) {
$charge = array(
'card' => $card,
'amount' => $amount,
'currency' => 'gbp',
'receipt_email' => $email
);
$response = \Stripe\Charge::create($charge);
return $response;
}
function sendData() {
// add any variables which you passed from the form here
$total = $_POST['total-vat'];
$amount = str_replace('$', '', $total) * 100;
$card = $_POST['stripeToken'];
try {
$payment = wp_stripe_charge($amount, $card, $bEmail);
} catch(\Stripe\Error\Card $e) {
$payment = false;
$errorJSON = $e->getJsonBody();
$error = $errorJSON['error'];
}
// do a whole load of stuff to submit my own form, including checking if order was successful, etc
$return = array(); // add whatever data to this to return to the JavaScript
echo json_encode($return);
}
JavaScript:
$(".booking-form").submit(function(event) {
event.preventDefault();
// do any validation checks or other actions here
var amount = $('.total-vat').val() * 100, //amount you want to charge
name = $('.booking-name').val();
Stripe.createToken({
name: name,
number: $('.card-number').val(),
cvc: $('.card-cvc').val(),
exp_month: $('.card-expiry-month').val(),
exp_year: $('.card-expiry-year').val(),
address_line1: $(".booking-address-1").val(),
address_line2: $(".booking-address-2").val(),
address_city: $(".booking-city").val(),
address_state: $(".booking-county").val(),
address_zip: $(".booking-postcode").val()
}, stripeResponseHandler);
return false;
});
function stripeResponseHandler(status, response) {
if (response.error) {
$(".payment-errors").show().html(response.error.message);
} else {
var token = response['id'];
$(".stripe-payment-form").append("<input type='hidden' name='stripeToken' value='" + token + "' />");
formSubmission(); // if form has no errors and Stripe has verified it too, go ahead with making payment
}
}
function formSubmission() {
var data = $(form).serialize(); // plus any other data that may be held in a variable or something here
$.post(ajaxUrl, data, function(response) {
// add your response messages, actions etc here
}
});
您可以在上查看更多信息
希望这会有帮助。很高兴尝试并帮助回答任何问题!Tom你知道如何做这个伴侣吗?因为我一直在处理同一个问题!Hi@Birdy-下面回答了我的方法。希望这会有帮助!
function stripeResponseHandler(status, response) {
if (response.error) {
$(".payment-errors").show().html(response.error.message);
} else {
var form$ = $("#wp-stripe-payment-form");
var token = response['id'];
form$.append("<input type='hidden' name='stripeToken' value='" + token + "' />");
var newStripeForm = form$.serialize();
$.ajax({
type : "post",
dataType : "json",
url : ajaxurl,
data : newStripeForm,
success: function(response) {
$('.wp-stripe-details').prepend(response);
}
});
}
}
$("#wp-stripe-payment-form").submit(function(event) {
event.preventDefault();
$(".wp-stripe-notification").hide();
var amount = $('.total-vat').val() * 100; //amount you want to charge
Stripe.createToken({
name: $('.wp-stripe-name').val(),
number: $('.card-number').val(),
cvc: $('.card-cvc').val(),
exp_month: $('.card-expiry-month').val(),
exp_year: $('.card-expiry-year').val()
}, stripeResponseHandler);
if (paymentSuccessful) {
console.log("the payment was successful");
}
return false; // prevent the form from submitting with the default action
}
\Stripe\Stripe::setApiKey("YOUR_SECRET_KEY");
function wp_stripe_charge($amount, $card, $email) {
$charge = array(
'card' => $card,
'amount' => $amount,
'currency' => 'gbp',
'receipt_email' => $email
);
$response = \Stripe\Charge::create($charge);
return $response;
}
function sendData() {
// add any variables which you passed from the form here
$total = $_POST['total-vat'];
$amount = str_replace('$', '', $total) * 100;
$card = $_POST['stripeToken'];
try {
$payment = wp_stripe_charge($amount, $card, $bEmail);
} catch(\Stripe\Error\Card $e) {
$payment = false;
$errorJSON = $e->getJsonBody();
$error = $errorJSON['error'];
}
// do a whole load of stuff to submit my own form, including checking if order was successful, etc
$return = array(); // add whatever data to this to return to the JavaScript
echo json_encode($return);
}
$(".booking-form").submit(function(event) {
event.preventDefault();
// do any validation checks or other actions here
var amount = $('.total-vat').val() * 100, //amount you want to charge
name = $('.booking-name').val();
Stripe.createToken({
name: name,
number: $('.card-number').val(),
cvc: $('.card-cvc').val(),
exp_month: $('.card-expiry-month').val(),
exp_year: $('.card-expiry-year').val(),
address_line1: $(".booking-address-1").val(),
address_line2: $(".booking-address-2").val(),
address_city: $(".booking-city").val(),
address_state: $(".booking-county").val(),
address_zip: $(".booking-postcode").val()
}, stripeResponseHandler);
return false;
});
function stripeResponseHandler(status, response) {
if (response.error) {
$(".payment-errors").show().html(response.error.message);
} else {
var token = response['id'];
$(".stripe-payment-form").append("<input type='hidden' name='stripeToken' value='" + token + "' />");
formSubmission(); // if form has no errors and Stripe has verified it too, go ahead with making payment
}
}
function formSubmission() {
var data = $(form).serialize(); // plus any other data that may be held in a variable or something here
$.post(ajaxUrl, data, function(response) {
// add your response messages, actions etc here
}
});
$payment->status == "succeeded" / "failed"
$payment->id
$error['code'] == 'card_declined' / 'incorrect_cvc' / 'expired_card' / 'processing_error'