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


PHP/Ajax collect and pass form data

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

          <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金额,生成哈希值,然后只需单击提交按钮即可将其发布给第三方吗? 我怎样才能做到这一点?

好的,所以我看到firstdata.com实际上建议你这样做。我设法为你做了一些东西。

您必须使用 ajax,将客户端输入的金额发布到同一页面,对其进行处理并返回表单。仅在处理数据时形成总和位形式。

我为你制作了整个脚本,因为在某些时候弄清楚如何做到这一点会变得令人沮丧。

测试.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>

我认为您可以通过使用"x_amount"字段在模糊事件上创建 ajax 请求来做到这一点。如果生成哈希需要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); 
        }
    });
});