[英]Laravel paypal form submit validation
我在Laravel项目中使用paypal API,当用户单击Submit时,我要创建并执行Paypal付款。 问题是使用javascript通过以下方式处理paypal: my-page.blade.php:
<script src="https://www.paypalobjects.com/api/checkout.js"></script>
<script type="text/javascript">
(function ($) {
$('#submit').click(function(event) {
event.preventDefault()
$('#payments').fadeIn('slow')
var CREATE_PAYMENT_URL = '{{ route('change-company-activity-subject-paypal-create') }}'
var EXECUTE_PAYMENT_URL = '{{ route('change-company-activity-subject-paypal-execute') }}'
paypal.Button.render({
env: 'sandbox', // Or 'sandbox'
commit: true, // Show a 'Pay Now' button
payment: function() {
return paypal.request.post(CREATE_PAYMENT_URL, {_token: '{{ csrf_token() }}'}).then(function(data) {
// console.log(data)
return data.id
});
},
// onAuthorize() is called when the buyer approves the payment
onAuthorize: function(data, actions) {
console.log(data)
// Set up the data you need to pass to your server
var data = {
paymentID: data.paymentID,
payerID: data.payerID,
_token: '{{ csrf_token() }}'
};
// Make a call to your server to execute the payment
return paypal.request.post(EXECUTE_PAYMENT_URL, data)
.then(function (res) {
res = JSON.parse(res)
console.log(res)
window.alert('Payment Complete!');
});
}
}, '#paypal-button');
console.log('paypal')
})
})(jQuery)
</script>
付款完成后,在这种情况下将调用window.alert()
函数。 我已经考虑过要执行$('#form').submit()
以便在付款执行后提交表单。 我的表单具有后端验证规则,如果您的输入未通过某个验证规则,它将重定向您。 这样做的问题是可以取走用户的钱,而不能提交表格。 如何避免这个问题? 当使用Javascript显示登录表单并执行付款时,如何通过验证才能显示Paypal登录表单并执行付款?
Paypal还有两个可以使用的参数:
paypal.Button.render({
payment: function() {},
onAuthorize: function(data) {},
onError: function(err) {
// error handling
},
onCancel: function(data, actions) {
// cancel handling
},
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.