dimanche 1 mars 2015

Interrupt form post, Execute jquery $.post to

I am trying a different tact on a problem I am trying to solve. I have a form where I gather demographic information and a payment amount. I then need to calculate a hash, sequence, and timestamp in a way that is not visible to browser, then update hidden form values with these calculated fields and then submit to payment processor.


What I am trying currently is to have the main page with my form with the input and hidden fields, when submit is clicked, a jquery function runs, does an ajax post to a calculation php which uses an input of amount, which is part of the hash. The calculation php calculates the items then passes a json array back.


I then want to parse the return, update the hidden form values, then submit.


I will attach my code below, but right now, I am interrupting the post, passing the value to the calculation php and building the array. At that point, my process stops, it seems I am not returned to main page to update values and post.


What have I done incorrectly? Also, if you have a better solution, I would be interested to hear. Thanks


Main Page:



<!DOCTYPE html>
<html>
<head>
<script src="http://ift.tt/1xDNnh9"></script>
<script>
$(document).ready(

function () {

$('#Waterform').submit(

function(event) {

//var txt = $("x_amount").val();


var formData = { xamount : $('#x_amount').val() };
$.ajax({
type: 'POST',
url: "calc_fp_etc_anet.php",
data: formData,
success: function(result,stat,xmlRegObj){


for (var key in result)
{
if (result.hasOwnProperty(key))
{

$('#returnmsg').val(result[key].type);
$('#x_fp_hash').val(result[key].fp_hash);
$('#x_fp_sequence').val(result[key].fp_sequence);
$('#x_fp_timestamp').val(result[key].fp_timestamp);
}
}

//return false;
$("#Waterform").submit();},
error: function (jqXHR, textStatus, errorThrown){
$('#returnmsg').val(textStatus);
return false;},
dataType: "json",
async:false
});

}

)

}


);
</script>
</head>
<body>


<form id="Waterform" name="adaWaterform" action = 'http://ift.tt/18ag4D4' method = 'post'>

<input type = 'hidden' id = 'x_show_form' name = 'x_show_form' value = 'PAYMENT_FORM' />
<input type = 'hidden' id = 'x_description' name = 'x_description' value = 'Online Bill Payment' />
<input type = 'hidden' id = 'x_login' name = 'x_login' value = 'xxx4NhDyy' />
<input type = 'input' id = 'x_fp_hash' name = 'x_fp_hash' value = '' />
<input type = 'input' id = 'x_fp_sequence' name = 'x_fp_sequence' value = '' />
<input type = 'input' id = 'x_fp_timestamp' name = 'x_fp_timestamp' value = '' />
<input type = 'input' id = 'returnmsg' name = 'returnmsg' value = '' />

<div id="m_divLineItems">
<h3>Service Information</h3>
<div id="ContentBodyPlaceholder_m_divTime"></div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblAccountNumber" class="InfoLabel">
Account Number</label>
<input type = 'text' id = 'x_invoice_num' name = 'x_invoice_num' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblPhone" class="InfoLabel">
Phone</label>
<input type = 'text' id = 'x_phone' name = 'x_phone' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblEMail" class="InfoLabel">
e-mail</label>
<input type = 'text' id = 'x_email' name = 'x_email' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblServiceAddress" class="InfoLabel">
Service Address</label>
<input type = 'text' id = 'x_ship_to_address' name = 'x_ship_to_address' value = '' />
</div>
<h3>Billing Information</h3>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblFirstName" class="InfoLabel">
First Name</label>
<input type = 'text' id = 'x_first_name' name = 'x_first_name' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblLastName" class="InfoLabel">
Last Name</label>
<input type = 'text' id = 'x_last_name' name = 'x_last_name' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblAddress" class="InfoLabel">
Address</label>
<input type = 'text' id = 'x_address' name = 'x_address' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblCity" class="InfoLabel">
City</label>
<input type = 'text' id = 'x_city' name = 'x_city' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblState" class="InfoLabel">
State</label>
<input type = 'text' id = 'x_state' name = 'x_state' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblZip" class="InfoLabel">
Zip</label>
<input type = 'text' id = 'x_zip' name = 'x_zip' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblAmount" class="InfoLabel">
Amount</label>&nbsp;
<input type = 'text' id = 'x_amount' name = 'x_amount' value = '' />
<input type="submit" class="submit" id="submitbutton" name="submitbutton" value="Pay Bill &gt;&gt;"/>
</div>
</div>





</form>


</body>
</html>


PHP Calculation:



<?php

require_once (dirname(__FILE__).'./config.php');

$amount = $_POST['xamount'];


$loginID = AUTHORIZENET_API_LOGIN_ID;
$transactionKey = AUTHORIZENET_TRANSACTION_KEY;


// a sequence number is randomly generated
$sequence = rand(1, 10000);
// a timestamp is generated
$timeStamp = time();


$x_fp_hash = hash_hmac("md5", $loginID . "^" . $sequence . "^" . $timeStamp . "^" . $amount . "^", $transactionKey);

$x_fp_sequence = $sequence;

$x_fp_timestamp = $timeStamp;


$result = json_encode(array("type"=>"success","fp_hash"=>"$x_fp_hash","fp_sequence"=>"$x_fp_sequence","fp_sequence"=>"$x_fp_sequence","fp_timestamp"=>"$x_fp_timestamp"));

echo $result;

die();

?>

Aucun commentaire:

Enregistrer un commentaire