0

我需要使用 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金额,生成哈希,然后将其发布给第三方吗?我怎样才能做到这一点?

4

2 回答 2

2

好的,所以我看到这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>
于 2012-08-15T07:01:08.460 回答
0

我认为您可以通过使用“x_amount”字段在模糊事件上创建 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); 
        }
    });
});
于 2012-08-14T18:42:16.750 回答