-1

i am using a SIM API to run credit card payments through my site. Specifically Authorize.net

I have a one product with a product listing a quantity field and a purchase button.

I need the amount that posts to be updated if the customer changes the quantity.

Here is my code:

        <?php
// This sample code requires the mhash library for PHP versions older than
// 5.1.2 - http://hmhash.sourceforge.net/

// the parameters for the payment can be configured here
// the API Login ID and Transaction Key must be replaced with valid values
$loginID        = "0000000";
$transactionKey = "00000000000000";
$amount         = "3.99";
$description    = "This is a Sample Transaction";
$label          = "Purchase"; // The is the label on the 'submit' button
$testMode       = "false";
// By default, this sample code is designed to post to our test server for
// developer accounts: https://test.authorize.net/gateway/transact.dll
// for real accounts (even in test mode), please make sure that you are
// posting to: https://secure.authorize.net/gateway/transact.dll
$url            = "https://test.authorize.net/gateway/transact.dll";

// If an amount or description were posted to this page, the defaults are overidden
if (array_key_exists("amount",$_REQUEST))
    { $amount = $_REQUEST["amount"]; }
if (array_key_exists("amount",$_REQUEST))
    { $description = $_REQUEST["description"]; }

// an invoice is generated using the date and time
$invoice    = date(YmdHis);
// a sequence number is randomly generated
$sequence   = rand(1, 1000);
// a timestamp is generated
$timeStamp  = time();

// 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)); }
?>

<!-- Print the Amount and Description to the screen. -->
Amount: <?php echo $amount; ?> <br />
Description: <?php echo $description; ?> <br />

<!-- Create the HTML form containing necessary SIM post values -->
 <FORM method='post' action='https://test.authorize.net/gateway/transact.dll' >
<!--  Additional fields can be added here as outlined in the SIM integration
 guide at: http://developer.authorize.net -->
    <input type='hidden' name='x_login' value='<?php echo $loginID; ?>' />
    <input type='hidden' name='x_amount' value='<?php echo $amount; ?>' />
    <input type='hidden' name='x_description' value='<?php echo $description; ?>' />
    <label>Quantity:</label><input type="text" name="quantity'" value="1" size="2" maxlength="3" />
    <input type='hidden' name='x_invoice_num' value='<?php echo $invoice; ?>' />
    <input type='hidden' name='x_fp_sequence' value='<?php echo $sequence; ?>' />
    <input type='hidden' name='x_fp_timestamp' value='<?php echo $timeStamp; ?>' />
    <input type='hidden' name='x_fp_hash' value='<?php echo $fingerprint; ?>' />
    <input type='hidden' name='x_test_request' value='<?php echo $testMode; ?>' />
    <input type='hidden' name='x_show_form' value='PAYMENT_FORM' />
    <input type="hidden" name="x_logo_URL" value="https://secure.authorize.net/mgraphics/logo_322583_1.jpg">
    <input type='submit' value='<?php echo $label; ?>' />
</form>

I am a beginner at php so any help understanding the would be appreciated. S=I have tried amount= 3.99 * 'quantity' and that did nothing.

Thanks. -Ann

Ann K
  • 49
  • 2
  • 9

2 Answers2

0

Unless I miss-understand what you want to do, you will be best off using Javascript to update the quantity from 1 to however many the user wants, before they POST the form to the authorize.net website.

The key here is to remember the order of events when you POST a form or click a link and make a GET request.

PHP is server-side tech, so it's executed when you send instructions to the server. For-instance, you'll send instructions to PHP like query my db and get stuff and it will return those results for you.

Once you display data in the browser PHP can't get involved again, unless you send another request to the server.

In contrast, Javascript and its libraries like JQuery are browser tools so they can change what is already known. In your case you can tell the quantity field to change using Javascript, based on the users choice, before the POST event takes place.

Read about these JS functions:

onChange

onSubmit

document.write

document.getelementbyid

Hope I'm not teaching you to suck eggs.

user783322
  • 479
  • 1
  • 8
  • 19
0

I think you should give your form a name, and make Amount: <?php echo $amount; ?> into Amount: <span id='totalCost'>?php echo $amount; ?></span>

then add this in the html head:

<script type='text/javascript>
function updateAmount(){
    var amount = document.formName.amount.value;
    var quantity = document.formName.quantity.value;
    var total = amount * quantity;
    document.getElementById("totalCost").value.write(total);
}
</script>

and add an onChange param on your input form.

<label>Quantity:</label><input type="text" name="quantity'" value="1" size="2" maxlength="3" onChange="updateAmount()"/>

Hope that helps point you in the right direction.

John Moses
  • 1,283
  • 1
  • 12
  • 18
  • This is definitely on the right track. What I need to happen is the $amount to update with the total cost: You can see a sample of what I am trying to do here: http://test.shared-vision.net/samplewithquantity3.php. When you click the purchase button the amount on the payment form needs to be updated with the total amount. Thank You for you help. -Ann – Ann K Jun 30 '11 at 14:26