使用重定向发送数据
send data with a redirect
我正在我的应用程序中实现卡支付,当付款被批准或拒绝时,它会重定向到 URL
重定向时我需要将变量'status''status detail''id'发送到控制器
<script>
const cardForm = mp.cardForm({
amount: "100.5",
autoMount: true,
form: {
id: "form-checkout",
cardholderName: {
id: "form-checkout__cardholderName",
placeholder: "Nombre",
},
cardholderEmail: {
id: "form-checkout__cardholderEmail",
placeholder: "E-mail",
},
cardNumber: {
id: "form-checkout__cardNumber",
placeholder: "Número de la tarjeta",
},
expirationDate: {
id: "form-checkout__expirationDate",
placeholder: "MM/YY",
},
securityCode: {
id: "form-checkout__securityCode",
placeholder: "000",
},
installments: {
id: "form-checkout__installments",
placeholder: "Cuotas",
},
identificationType: {
id: "form-checkout__identificationType",
placeholder: "Tipo de documento",
},
identificationNumber: {
id: "form-checkout__identificationNumber",
placeholder: "Número de documento",
},
issuer: {
id: "form-checkout__issuer",
placeholder: "Banco emisor",
},
},
callbacks: {
onFormMounted: error => {
if (error) return console.warn("Form Mounted handling error: ", error);
console.log("Form mounted");
},
onSubmit: event => {
event.preventDefault();
const {
paymentMethodId: payment_method_id,
issuerId: issuer_id,
cardholderEmail: email,
amount,
token,
installments,
identificationNumber,
identificationType,
} = cardForm.getCardFormData();
fetch("/process_payment", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
token,
issuer_id,
payment_method_id,
transaction_amount: Number(amount),
installments: Number(installments),
description: "Descripción del producto",
payer: {
email,
identification: {
type: identificationType,
number: identificationNumber,
},
},
}),
})
.then(response => {
return response.json();
})
.then(result => {
if(result.status == "approved"){
window.location.href = "/thanks";
}else{
window.location.href = "/rejected";
}
})
.catch(error => {
alert("Unexpected error\n"+JSON.stringify(error));
});
},
onFetching: (resource) => {
console.log("Fetching resource: ", resource);
// Animate progress bar
const progressBar = document.querySelector(".progress-bar");
progressBar.removeAttribute("value");
return () => {
progressBar.setAttribute("value", "0");
};
}
},
});
</script>
我对 js 的了解不多,到目前为止过程是正确的,但我需要重定向并传递数据
如何用 js 传递该数据?请帮忙
您可以简单地使用查询字符串将详细信息传递给 url 或直接调用 ajax
像这样
window.location.href = "/thanks?status="+result.status+"&id="+result.id;
或使用ajax
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: "{{url('/thanks')}}",
type: "POST",
data: {"id" : result.id,"status":result.status},
success: function(data){
console.log("ajax called succesfully");
}
});
您不应使用 ajax 或 GET 存储付款交易详情。这是不安全的,很容易被利用。
我想你是用approved
或accredited
等背书上的交易信息向客户提供一些商品或服务。我可以轻松地 POST 使用 ajax 或伪造 GET 请求并伪造一个您将存储在后端的交易。然后我就可以免费获得服务了。
您需要在后台验证付款。因此,您的支付提供商应该有一些网络挂钩,他们 POST 向您 API 发送交易详细信息,您只允许他们 post 向 API 发送交易详情。然后将这些详细信息存储在数据库中并完成订单。
另一种方法是加密表单详细信息并将其发送到您的后端,然后从您的后端 post 将其发送给支付提供商,他们通过后端进行响应。它必须在前端加密的原因是信用卡信息永远不应该接触您的服务器,因为这是一个很大的安全问题。加密JS一般由支付provider/merchant.
提供
尝试检查您的薪水提供者是否提供了一些替代方案,例如用于您的表单的 webhook 或加密 JS。
我正在我的应用程序中实现卡支付,当付款被批准或拒绝时,它会重定向到 URL
重定向时我需要将变量'status''status detail''id'发送到控制器
<script>
const cardForm = mp.cardForm({
amount: "100.5",
autoMount: true,
form: {
id: "form-checkout",
cardholderName: {
id: "form-checkout__cardholderName",
placeholder: "Nombre",
},
cardholderEmail: {
id: "form-checkout__cardholderEmail",
placeholder: "E-mail",
},
cardNumber: {
id: "form-checkout__cardNumber",
placeholder: "Número de la tarjeta",
},
expirationDate: {
id: "form-checkout__expirationDate",
placeholder: "MM/YY",
},
securityCode: {
id: "form-checkout__securityCode",
placeholder: "000",
},
installments: {
id: "form-checkout__installments",
placeholder: "Cuotas",
},
identificationType: {
id: "form-checkout__identificationType",
placeholder: "Tipo de documento",
},
identificationNumber: {
id: "form-checkout__identificationNumber",
placeholder: "Número de documento",
},
issuer: {
id: "form-checkout__issuer",
placeholder: "Banco emisor",
},
},
callbacks: {
onFormMounted: error => {
if (error) return console.warn("Form Mounted handling error: ", error);
console.log("Form mounted");
},
onSubmit: event => {
event.preventDefault();
const {
paymentMethodId: payment_method_id,
issuerId: issuer_id,
cardholderEmail: email,
amount,
token,
installments,
identificationNumber,
identificationType,
} = cardForm.getCardFormData();
fetch("/process_payment", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
token,
issuer_id,
payment_method_id,
transaction_amount: Number(amount),
installments: Number(installments),
description: "Descripción del producto",
payer: {
email,
identification: {
type: identificationType,
number: identificationNumber,
},
},
}),
})
.then(response => {
return response.json();
})
.then(result => {
if(result.status == "approved"){
window.location.href = "/thanks";
}else{
window.location.href = "/rejected";
}
})
.catch(error => {
alert("Unexpected error\n"+JSON.stringify(error));
});
},
onFetching: (resource) => {
console.log("Fetching resource: ", resource);
// Animate progress bar
const progressBar = document.querySelector(".progress-bar");
progressBar.removeAttribute("value");
return () => {
progressBar.setAttribute("value", "0");
};
}
},
});
</script>
我对 js 的了解不多,到目前为止过程是正确的,但我需要重定向并传递数据
如何用 js 传递该数据?请帮忙
您可以简单地使用查询字符串将详细信息传递给 url 或直接调用 ajax 像这样
window.location.href = "/thanks?status="+result.status+"&id="+result.id;
或使用ajax
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: "{{url('/thanks')}}",
type: "POST",
data: {"id" : result.id,"status":result.status},
success: function(data){
console.log("ajax called succesfully");
}
});
您不应使用 ajax 或 GET 存储付款交易详情。这是不安全的,很容易被利用。
我想你是用approved
或accredited
等背书上的交易信息向客户提供一些商品或服务。我可以轻松地 POST 使用 ajax 或伪造 GET 请求并伪造一个您将存储在后端的交易。然后我就可以免费获得服务了。
您需要在后台验证付款。因此,您的支付提供商应该有一些网络挂钩,他们 POST 向您 API 发送交易详细信息,您只允许他们 post 向 API 发送交易详情。然后将这些详细信息存储在数据库中并完成订单。
另一种方法是加密表单详细信息并将其发送到您的后端,然后从您的后端 post 将其发送给支付提供商,他们通过后端进行响应。它必须在前端加密的原因是信用卡信息永远不应该接触您的服务器,因为这是一个很大的安全问题。加密JS一般由支付provider/merchant.
提供尝试检查您的薪水提供者是否提供了一些替代方案,例如用于您的表单的 webhook 或加密 JS。