Note that there are some explanatory texts on larger screens.

plurals
  1. POShopping Cart - update total amount after sales tax change - without reloading page using AJAX
    primarykey
    data
    text
    <p>From what I understand, in order to update a page or calculate a price without it reloading, I have to use ajax. Correct?</p> <p>I have a shopping cart, that gives subtotal, tax, and grand total. User has to select their state in order to know whether we charge for tax or not. Then, they click "checkout", which is actually a button tied to Authorize.net. My client doesn't want pages to reload or refresh so I have to use Ajax to update the page once they choose their state.</p> <p>Authorize.net has a set of variables such as <code>$amount_var</code>, <code>$loginID</code>, <code>$transactionKey</code>, etc. They use those variables and place them into their "hidden" values to be sent off for processing. I am attempting to change the <code>$amount_var</code> variable to equal my total calculated amount, which was processed in the php page accessed by ajax.</p> <p>The state dropdown menu has <code>onchange="doStateTax()"</code>. Where <code>GRANDtotalprice</code> is a hidden field with a running total of all the items in the cart, and <code>ddl</code> is whichever state they chose.</p> <pre><code>&lt;script type="text/javascript"&gt; 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; }; &lt;/script&gt; </code></pre> <p>This posts to a PHP page where it checks to see if the state = "michigan". If so, they get charged 6% tax. Here is my PHP page that does the calculations:</p> <pre><code> &lt;?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' =&gt; $products, 'statetax' =&gt; $tax, 'total' =&gt; $total ); $json = json_encode($results); echo $json; ?&gt; </code></pre> <p>Here's where I lose thought on how the process works... I thought that the results go back in what is called a "success callback" right? I do see the subtotal, tax, and total changing instantly so that DOES work. It displays the numbers in a because of this code here:</p> <pre><code>$('.total-placeholder').html(data.total); </code></pre> <p>But this only DISPLAYS the results. How or where in my code do I cause Authorize.net to change their <code>$amount_var</code> variable into my total? Again, their part of code has this located on my php shopping cart near the bottom of the page:</p> <pre><code>$amount_var = "youramount; $loginID = "loginid"; $transactionKey = "transkey"; </code></pre> <p>It would be super easy if I could just inject my success callback into their hidden field called <code>x_amount</code> but it isn't that easy. They use the <code>$amount_var</code> for other reasons such as setting a fingerprint so I must set their <code>$amount_var</code> to equal my own calculations. Do I set this in the php page? Or the javascript? Elsewhere? </p> <p><strong>EDIT: Response Code 99 error</strong></p> <p>Almost have it working. After adding your code, I get a response code 99 error. When I select a state that does NOT require tax, it works. When I select MI (michigan) it gives Response Code 99. From what I've found this is because there is a mismatch between this part:</p> <pre><code>&lt;input type='hidden' name='x_amount' value='&lt;?php echo $amount; ?&gt;' /&gt; </code></pre> <p>and this: </p> <pre><code>// 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() &gt;= '5.1.2' ) { $fingerprint = hash_hmac("md5", $loginID . "^" . $sequence . "^" . $timeStamp . "^" . $amount . "^", $transactionKey); } else { $fingerprint = bin2hex(mhash(MHASH_MD5, $loginID . "^" . $sequence . "^" . $timeStamp . "^" . $amount . "^", $transactionKey)); } ?&gt; </code></pre> <p>because of the "$amount" part in the hidden field is replaced by your script yet the "$amount part of the $fingerprint above isn't altered by the script. I think Authorize.net requires the fingerprint part AND the hidden field to match. Which brings me back to how to set this $amount variable to be our new combined total.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload