-1

我正在使用 SIM API 通过我的网站运行信用卡付款。特别是 Authorize.net

我有一个产品,其中列出了数量字段和购买按钮的产品。

如果客户更改数量,我需要更新发布的金额。

这是我的代码:

        <?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>

我是 php 的初学者,所以对理解的任何帮助将不胜感激。S=我已经尝试过 amount= 3.99 * 'quantity' ,但什么也没做。

谢谢。-安

4

2 回答 2

0

我认为你应该给你的表格一个名字,并Amount: <?php echo $amount; ?>变成Amount: <span id='totalCost'>?php echo $amount; ?></span>

然后在html头中添加:

<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>

并在您的输入表单上添加一个 onChange 参数。

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

希望这有助于为您指明正确的方向。

于 2011-06-29T20:39:54.423 回答
0

除非我错过了您想要做什么,否则您最好使用 Javascript 将数量从 1 更新为用户想要的数量,然后再将表单发布到 authorize.net 网站。

这里的关键是在您发布表单或单击链接并发出 GET 请求时记住事件的顺序。

PHP 是服务器端技术,因此它在您向服务器发送指令时执行。例如,您将向 PHP 发送指令,例如查询我的数据库并获取内容,它会为您返回这些结果。

一旦您在浏览器中显示数据,PHP 就无法再次参与其中,除非您向服务器发送另一个请求。

相比之下,Javascript 及其库(如 JQuery)是浏览器工具,因此它们可以更改已知内容。在您的情况下,您可以在 POST 事件发生之前根据用户的选择使用 Javascript 告诉数量字段进行更改。

阅读这些 JS 函数:

改变

提交

文档.write

document.getelementbyid

希望我没有教你吸鸡蛋。

于 2011-06-29T19:27:40.600 回答