使用WorldPay验证付款金额

本文关键字:金额 付款 验证 WorldPay 使用 | 更新日期: 2023-09-27 18:04:56

我们使用WorldPay来处理分级会员系统的付款,其付款金额取决于所选择的会员级别。

付款通过一个表单post从一些隐藏字段传递给WorldPay,包括:

<input type="hidden" name="amount" value="295.00" />

本质上,表单是通过POST提交给WorldPay的,用户按照一些步骤来处理他们的支付。完成后,用户将被重定向到指定的确认页面。

这似乎是WorldPay接受付款的典型方式。这里有一个明显的问题,隐藏字段的值很容易被任何具有HTML基础知识的人篡改。表单直接发送到WorldPay,因此我们没有PostBack来根据会员级别验证金额。

当付款通知从WorldPay返回给我们时,我们可以选择在确认页面之前通过处理程序路由回调来验证付款金额;但是,我希望避免这样的情况:用户提交了篡改的表单,支付了不正确的金额,却没有收到会员资格,然后必须联系公司以退还他们的钱。

在处理付款之前,我们如何验证提交的金额是否正确?

我突然想到,我们有一个额外的问题,即,即使我们验证表单post服务器端,也没有什么可以阻止恶意用户直接欺骗表单post到WorldPay。

使用WorldPay验证付款金额

这确实是一个漏洞,可以使用签名轻松解决。看看这个链接:

http://culttt.com/2012/07/25/integrating-worldpay-into-a-database-driven-website/

这个方法应该在帮助页上更好的推广,太可惜了

我能想到的一个解决方案是,捕获form标签的submit:

<form id="myForm" onsubmit="return validatePayment();">

,然后创建JavaScript文件,看起来像这样:

var isValidAmount = false;
function validatePayment() {
    if (isValidAmount) { return true; }
    // in here you want to issue an AJAX call back to your server
    // with the appropriate information ... I would recommend using
    // jQuery and so it might look something like this:
    $.ajax( {
        type: "POST",
        url: url,
        data: { amount: $("#amount").val(), someotherfield: somevalue },
        success: function(data, textStatus, jqXHR) {
            // set the flag so that it can succeed the next time through
            isValidAmount = true;
            // resubmit the form ... it will reenter this function but leave
            // immediately returning true so the submit will actually occur
            $("myForm").submit();
        },
    });
    // this will keep the form from actually submitting the first time
    return false;
}