开发者

How do I intercept a form's values before sending it?

I currently have a large form that gets sent to our payment authorizer (done so by action="paymentautherizerURL" ), however I am not getting all of the information I require back from them when I go to store the transaction in my DB.

I either need to intercept the form data before it submits so that I can store it in the session (we are using PHP / jQuery), or I have also tried sending it to an intermediary scriptlet that grabs the information I need, and then using jQuery's $.post() to re-build and send off the data to the authorizer.

the second approach does no开发者_运维技巧t seem to work, however, at least to the best of my efforts. I'm not sure that a $.post properly emulates the form's send action, or at least I have not done it right.

<?php
session_start();

$post = $_POST;

//gets all of the information that beanstream does not return to approved.php, but is still required to make
//a legitimate database entry. gets from the POST and stores in the session array for approved.PHP to access

$_SESSION['approvedArray']['billAddress'] = $_POST['ordAddress1'];
$_SESSION['approvedArray']['billProvince'] = $_POST['ordProvince'];
$_SESSION['approvedArray']['billCountry'] = $_POST['ordCountry'];
$_SESSION['approvedArray']['billPostalCode'] = $_POST['ordPostalCode'];
$_SESSION['approvedArray']['billCity'] = $_POST['ordCity'];

$_SESSION['approvedArray']['shipAddress'] = $_POST['shipAddress1'];
$_SESSION['approvedArray']['shipPostal'] = $_POST['shipPostalCode'];
$_SESSION['approvedArray']['shipCity'] = $_POST['shipCity'];
$_SESSION['approvedArray']['shipProvince'] = $_POST['shipProvince'];
$_SESSION['approvedArray']['shipCountry'] = $_POST['shipCountry'];

session_write_close();
//the javascript below will send what is required to beanstream as though it were sent from the form

<script type='text/javascript'>
$.post(, {
    <?php
    //rebuild the POST such that "name: value, " except the last name/value will not be followed by a comma
    $keys = array_keys($_POST);
    for($i = 0; $i < count($_POST); $i++) {
        $currentKey = $keys[$i];
        $currentPost = $_POST[i];
        echo $currentKey . ": " . $currentPost;
        if ($i < (count($_POST) - 1)) {
            echo ", ";
        }
    }
    ?>
});
</script> 

?>

normally, the transaction authorizer will re-direct the user to one of 3 pages (approved, declined, error), and our website does the job from there. however, it's currently stuck at this page, which makes me think it's not sending off properly.

i'm open to all forms of criticism, approaches and ideas. thanks very much in advance, and if any other information is needed, please let me know!


How about changing the form tag to include an onSubmit attribute:

<form action="notmal_action.whatever" onSubmit="return save_data_function()">

Where the save_data_function reads the values from the form and sends it to a script on your server to save in a database (or where ever). I use hidden iframes to make this request hidden from the user...

<script>
function save_data_function() {
$('#iframe_id').attr('src', 'data_saving_script.extension?data_1=' + $('form_data_1').val().serialize() + '&data_2=' + $('form_data_2').val().serialize());
}
</script>

You can set a timeout if the data isn't being passed quick enough to the "data_saving_script.extension" file.


Since your existing example already has a dependency on javascript, you could move to saving the data with AJAX and then using a traditional submit to do the "real" POST to the payment gateway.

Assuming that your form has id="foo", you could do something like this:

<script>
$('form#foo').submit(function(event, doRealSubmit) {
    // this executes on the second pass
    if (doRealSubmit) {
        // returning true gets browser to do a real submit
        return true; 
    }

    // this executes on the first pass
    $.ajax({
        url: '/url/to/post/to/your/server',
        type: 'POST',
        // this serializes the form data in "application/x-www-form-urlencoded"
        data: $(this).serialize(),
        success: function(data) {
            // trigger 'submit' event again, but pass the doRealSubmit flag
            $('form#foo').trigger('submit', [true]);
        }
    });

    // returning false prevents browser from processing the real submit
    return false;
});
</script>


instead of action="paymentautherizerURL" you should send it to your own page:

<form action='process.php' method='post'>

now in your process.php you can work with the data (validation, filtering ..)

and when you are done you can send the data to the right place using cURL

With curl you can send post data and wait for the response to decide which page to show.


No need to put the data in sessions, on submit of form called a validate function in which done all the validation and then post the data to ur process.php using ajex , then it will retain on that page...


Don't store the data in the session - it's the wrong place to keep transactional data.

Post the stuff to a script you host, write only the stuff you need to keep to your database, generate an order reference for it,

then....

if you are using a payment processor (e.g. Paypal)

redirect to a second script passing the order reference in the URL. On the second script put in a form with hidden fields cotaining only the details required by your payment processor and some javascript to submiot the form automatically and a message to the user like 'Connecting to Paypal...'

If you are using merchant services to authorize the payment

before you generate output from the landing script, send the details to your authorizer using (e.g.) curl and parse the response, record the response against the order in the database and output a suitable message to the customer via the web page

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜