php - 购物车 - 销售税更改后更新总金额 - 无需使用 AJAX 重新加载页面

标签 php ajax shopping-cart authorize.net

据我了解,为了更新页面或计算价格而不重新加载,我必须使用ajax。对吗?

我有一个购物车,其中包含小计、税金和总计。用户必须选择他们的州才能知道我们是否收税。然后,他们点击“结账”,这实际上是一个与 Authorize.net 绑定(bind)的按钮。我的客户不希望页面重新加载或刷新,因此我必须在他们选择状态后使用 Ajax 来更新页面。

Authorize.net 有一组变量,例如 $amount_var$loginID$transactionKey 等。他们使用这些变量并将它们放入“隐藏”值中以进行处理。我正在尝试更改 $amount_var 变量以等于我的总计算金额,该金额是在 ajax 访问的 php 页面中处理的。

状态下拉菜单有onchange="doStateTax()"。其中 GRANDtotalprice 是一个隐藏字段,其中包含购物车中所有商品的运行总计,ddl 是他们选择的状态。

<script type="text/javascript">
    function doStateTax(){
        var grandtotalX = $('#GRANDtotalprice').val();
        var statetaxX = $('#ddl').val();

        $.post('statetax_query.php',
        {statetaxX:statetaxX, grandtotalX:grandtotalX}, 
        function(data) {
            data = $.parseJSON(data);
            $('.products-placeholder').html(data.products);     
            $('.statetax-placeholder').html(data.statetax);     
            $('.total-placeholder').html(data.total);
            // ...
        });
        return false;
    };
</script>

这会发布到一个 PHP 页面,在该页面中检查状态是否 =“michigan”。如果是这样,他们将被征收 6% 的税。这是我进行计算的 PHP 页面:

 <?php
    if (($_POST['statetaxX'] == 'MI'))
    {
        $taxselect = .06;
        $taxselect1 = 1.06;
    }
    else
    {
        $taxselect = 0;
        $taxselect1 = 1;
    }

    $products = number_format(($_POST['grandtotalX']), 2, '.', '');
    $tax = number_format((($_POST['grandtotalX'])*($taxselect)), 2, '.', '');
    $total = number_format((($_POST['grandtotalX'])*($taxselect1)), 2, '.', '');

    $results = array(
       'products' => $products,
       'statetax' => $tax,
       'total' => $total
    );
    $json = json_encode($results);
    echo $json;
?>

在这里,我失去了对该过程如何工作的思考......我认为结果会返回到所谓的“成功回调”中,对吧?我确实看到小计、税收和总计立即发生变化,因此确实有效。由于这里的代码,它显示 a 中的数字:

$('.total-placeholder').html(data.total);

但这仅显示结果。我如何或在代码中的何处使 Authorize.net 将其 $amount_var 变量更改为我的总计?同样,他们的代码部分位于我的 php 购物车页面底部附近:

$amount_var = "youramount;
$loginID        = "loginid";
$transactionKey = "transkey";

如果我可以将成功回调注入(inject)到名为 x_amount 的隐藏字段中,那将非常容易,但事实并非如此简单。他们出于其他原因使用 $amount_var,例如设置指纹,因此我必须将他们的 $amount_var 设置为等于我自己的计算结果。我要在php页面中设置这个吗?还是JavaScript?别处?

编辑:响应代码 99 错误

几乎可以正常工作了。添加代码后,我收到响应代码 99 错误。当我选择一个不需要纳税的州时,它就起作用了。当我选择 MI(密歇根州)时,它给出响应代码 99。据我所知,这是因为这部分之间不匹配:

<input type='hidden' name='x_amount' value='<?php echo $amount; ?>' />

还有这个:

// The following lines generate the SIM fingerprint.  PHP versions 5.1.2 and
// newer have the necessary hmac function built in.  For older versions, it
// will try to use the mhash library.
if( phpversion() >= '5.1.2' )
    { $fingerprint = hash_hmac("md5", $loginID . "^" . $sequence . "^" . $timeStamp .     "^" . $amount . "^", $transactionKey); }
else 
    { $fingerprint = bin2hex(mhash(MHASH_MD5, $loginID . "^" . $sequence . "^" .         $timeStamp . "^" . $amount . "^", $transactionKey)); }
?>

因为隐藏字段中的“$amount”部分已被您的脚本替换,但上面的“$fingerprint 的$amount”部分并未被脚本更改。我认为 Authorize.net 需要指纹部分和要匹配的隐藏字段。这让我回到如何将此 $amount 变量设置为我们新的合并总数。

最佳答案

删除了一堆用于瞄准方法的 B/C 内容

如果需要,请查看我的回答历史记录

编辑

有一个名为x_tax的字段

此行之后

$('.statetax-placeholder').html(data.statetax);

添加这个

$('input[name=x_tax]').val("Tax1<|>state tax<|>"+data.statetax);

SIM 指南第 28 页底部:http://www.authorize.net/support/SIM_guide.pdf

ADDITIONAL SHIPPING INFORMATION (Level 2 Data) Tax (x_tax) Value: The valid tax amount OR delimited tax information Format: When submitting delimited tax information, values must be delimited by a bracketed pipe <|> Notes: The tax amount charged OR when submitting this information by means of the HTML Form POST, delimited tax information including the sales tax name, description, and amount is also allowed.  tax item name<|>  tax description<|>  tax amount Format: The dollar sign ($) is not allowed when submitting delimited information. Note: The total amount of the transaction in x_amount must include this amount. Example: state tax<|>0.0625">

另一次编辑

在 php 端在 ajax 调用中生成指纹(PHP 端):

if( phpversion() >= '5.1.2' )
{
    $fingerprint = hash_hmac("md5", $loginID . "^" . $sequence . "^" . $timeStamp .     "^" . $amount . "^", $transactionKey);
}
else 
{
    $fingerprint = bin2hex(mhash(MHASH_MD5, $loginID . "^" . $sequence . "^" .         $timeStamp . "^" . $amount . "^", $transactionKey));
}

$results = array(
   'products' => $products,
   'statetax' => $tax,
   'total' => $total,
   'fingerprint' => $fingerprint // Pass the fingerprint
);
$json = json_encode($results);
echo $json;

关于成功的 Ajax:

// Tax amount
$('input[name=x_tax]').val("Tax1<|>state tax<|>"+data.statetax);
// Total amount
$('input[name=x_amount]').val(data.total);
// use the new Fingerprint
$('input[name=x_fp_hash]').val(data.fingerprint);

关于php - 购物车 - 销售税更改后更新总金额 - 无需使用 AJAX 重新加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11383260/

相关文章:

php - 使用 League/CSV 导出 CSV 不会对变音符号进行编码

php - 在 Yii2 中执行原始 SQL 查询?

javascript - CSS 输入框在 IE 中不显示

php - jquery限制输入范围

javascript - DIV 如何在聊天脚本中滚动?

php - 找不到laravel类 'Gloudemans\Shoppingcart\ShoppingcartServiceProvider'

android - 动态更改操作栏 sherlock 中的菜单项

javascript - Yii,从 Javascript 函数调用 Controller 函数

ajax - 从Spring Controller返回ajax调用的错误消息字符串的最佳实践是什么?

paypal - paypal购物车运费之一