2011-06-29 49 views
-1

我正在使用SIM API通過我的網站運行信用卡付款。具體Authorize.netPHP產品列表頁面要付款後的總金額

我有一個產品的產品列表數量字段和購買按鈕。

如果客戶更改數量,我需要更新帖子的數量。

這裏是我的代碼:

 <?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 =我已經嘗試金額= 3.99 *'數量',並沒有做任何事情。

謝謝。 -Ann

回答

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

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

希望能幫助你指出正確的方向。

+0

這絕對是正確的道路。 我需要做的是$總額更新金額: 你可以在這裏看到我想要做的一個示例:http://test.shared-vision.net/samplewithquantity3.php。 當您點擊購買按鈕時,付款表單上的金額需要用總金額更新。 謝謝你的幫助。 -Ann –

0

除非我想明白你想要做什麼,否則在將表單發佈到authorize.net網站之前,最好使用Javascript將數量從1更新爲用戶想要的數量。

這裏的關鍵是記住發佈表單或單擊鏈接併發出GET請求時的事件順序。

PHP是服務器端技術,因此它在您向服務器發送指令時執行。例如,您將向PHP發送指令,如查詢我的數據庫並獲取內容,並且它會爲您返回這些結果。

一旦你在瀏覽器中顯示數據PHP不能再次涉及,除非你發送另一個請求到服務器。

相比之下,JavaScript及其類庫(如JQuery)是瀏覽器工具,因此它們可以更改已知的內容。在您的情況下,您可以在發生POST事件之前根據用戶的選擇使用Javascript更改數量字段。

閱讀這些JS功能:

的onChange

的onsubmit

文件撰寫

的document.getElementById

希望我不是教你班門弄斧。

相關問題