PHP / Ajax收集并传递表单数据

时间:2012-08-14 18:19:38

标签: php ajax forms

我需要使用php表单收集输入('x_amount'),然后将其发布到生成哈希的页面,然后将其发布到第三方站点。我不希望客户有两个步骤,所以可以用ajax和php完成吗?这是我到目前为止所拥有的,但它需要被ajaxified(我认为?)

          <form action="https://globalgatewaye4.firstdata.com/pay" method="POST">

        <?php
              $x_login = "xxx-xxx";  //  Take from Payment Page ID in Payment Pages interface
              $transaction_key = "xxxxxxx"; // Take from Payment Pages configuration interface
              $x_currency_code = "CAD"; // Needs to agree with the currency of the payment page
              srand(time()); // initialize random generator for x_fp_sequence
              $x_fp_sequence = rand(1000, 100000) + 123456;
              $x_fp_timestamp = time(); // needs to be in UTC. Make sure webserver produces UTC

              // The values that contribute to x_fp_hash 
              $hmac_data = $x_login . "^" . $x_fp_sequence . "^" . $x_fp_timestamp . "^" . $x_amount . "^" . $x_currency_code;
              $x_fp_hash = hash_hmac('MD5', $hmac_data, $transaction_key);

              echo ('<label>x_login</label><input name="x_login"  type="hidden" value="' . $x_login . '">' );
              echo ('<label>x_amount</label><input name="x_amount"  type="hidden" value="' . $x_amount . '">' );
              echo ('<label>x_fp_sequence</label><input name="x_fp_sequence"  type="hidden" value="' . $x_fp_sequence . '">' );
              echo ('<label>x_fp_timestamp</label><input name="x_fp_timestamp"  type="hidden" value="' . $x_fp_timestamp . '">' );
              echo ('<label>x_fp_hash</label><input name="x_fp_hash"  type="hidden" value="' . $x_fp_hash . '" size="50">' );
              echo ('<label>x_currency_code</label><input name="x_currency_code"  type="hidden" value="' . $x_currency_code . '">');
        ?>

              <input type="hidden" name="x_show_form" value="PAYMENT_FORM"/>
              <input type="hidden" name="x_test_request" value="FALSE"/>
              Enter Payment Amount:<br>
              <input type="text" name="x_amount"/>
              <input type="submit" value="Pay with Payment Pages"/>
            </form>

换句话说,我可以收集x金额,生成哈希值,然后只需点击一下提交按钮就可以将其发布给第三方吗?我怎么能做到这一点?

2 个答案:

答案 0 :(得分:1)

好的,所以我看到firstdata.com实际建议你这样做。我已经成功为你做了一些事情。

你必须使用ajax,在同一页面上发布客户输入的金额,处理它并返回表格。仅在处理数据时才形成Sumbit形式。

我为你制作了整个剧本,因为在某些方面弄清楚如何去做是令人沮丧的。

test.php的

<?php
    if(isset($_POST['amount']) && ($_POST['amount'] != '')){
        $x_amount = $_POST['amount'];
        $x_login = "xxx-xxx";  //  Take from Payment Page ID in Payment Pages interface
        $transaction_key = "xxxxxxx"; // Take from Payment Pages configuration interface
        $x_currency_code = "CAD"; // Needs to agree with the currency of the payment page
        srand(time()); // initialize random generator for x_fp_sequence
        $x_fp_sequence = rand(1000, 100000) + 123456;
        $x_fp_timestamp = time(); // needs to be in UTC. Make sure webserver produces UTC

        // The values that contribute to x_fp_hash 
        $hmac_data = $x_login . "^" . $x_fp_sequence . "^" . $x_fp_timestamp . "^" . $x_amount . "^" . $x_currency_code;
        $x_fp_hash = hash_hmac('MD5', $hmac_data, $transaction_key);

        $values = array('login' => $x_login, 'amount' => $x_amount, 'sequence' => $x_fp_sequence, 'timestamp' => $x_fp_timestamp, 'hash' => $x_fp_hash, 'currency' => $x_currency_code);
        echo json_encode($values);
        die;    
    }

?><html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>

<script type="text/javascript">
    var processed = false;
    function makeProcessed(type){
        processed = type;
    }

    function prepareForm(){
        $('#submitPayment').val('Please wait ...');

        var processPayment = $.ajax({
            type: 'POST',       
            url: "test.php",
            data: {"amount": $('#x_my_amount').val()},
            dataType: 'json',
            async: false,
            success: function(data) {
                $('#x_login').val(data.login); 
                $('#x_amount').val(data.amount); 
                $('#x_fp_sequence').val(data.sequence); 
                $('#x_fp_timestamp').val(data.timestamp); 
                $('#x_fp_hash').val(data.hash); 
                $('#x_currency_code').val(data.currency);
                if(data.hash){
                    makeProcessed(true);
                }
            }
        });
        return processed;
    };
</script>

</head>
<body>
<form action="https://globalgatewaye4.firstdata.com/pay" method="POST" id="payment" onsubmit="return prepareForm();">
      <input type="hidden" name="x_show_form" value="PAYMENT_FORM"/>
      <input type="hidden" name="x_test_request" value="FALSE"/>

      <input name="x_login" id="x_login"  type="hidden" value="">
      <input name="x_amount" id="x_amount"  type="hidden" value="">
      <input name="x_fp_sequence" id="x_fp_sequence"  type="hidden" value="">
      <input name="x_fp_timestamp" id="x_fp_timestamp"  type="hidden" value="">
      <input name="x_fp_hash" id="x_fp_hash"  type="hidden" value="" size="50">
      <input name="x_currency_code" id="x_currency_code"  type="hidden" value="">

      Enter Payment Amount:<br>
      <input type="text" name="x_my_amount" id="x_my_amount" />
      <input type="submit" id="submitPayment" value="Pay with Payment Pages" />
    </form>

</body>
</html>

答案 1 :(得分:0)

我认为你可以通过使用“x_amount”字段在blur事件上创建ajax-request来完成此操作。 如果生成哈希需要php,那么它将如下所示:

形式:

Enter Payment Amount:<br>
<input type="text" name="real_amount" id="real-amount"/>
<input type="hidden" name="x_amount" id="x-amount" />

的javascript:

$('#real-amount').on('blur', function() {
    $.ajax({
        ....
        success: function(data) {
            // returned hash should be put in x_amount field here
            $('#x-amount').val(data); 
        }
    });
});