0

I am not aware of PHP language, I have this code in PHP, it is getting form value from previous page, checking secret ket etc and passing to another page with auto submitting the form. It is working fine, but I want to add an additional field "signed_date_time" that is $date_utc =gmdate("n/j/Y H:i:s A"); and pass it with this form. But receiving error on the next page.

<?php

define ('HMAC_SHA256', 'sha256');
define ('SECRET_KEY', '123456');

function sign ($params) {
  return signData(buildDataToSign($params), SECRET_KEY);
}

function signData($data, $secretKey) {
    return base64_encode(hash_hmac('sha256', $data, $secretKey, true));
}

function buildDataToSign($params) {
        $signedFieldNames = explode(",",$params["signed_field_names"]);
        foreach ($signedFieldNames as $field) {
           $dataToSign[] = $field . "=" . $params[$field];
        }
        return commaSeparate($dataToSign);
}

function commaSeparate ($dataToSign) {
    return implode(",",$dataToSign);
}

?>

<form  method="POST" name="frm" id="frm" action="https://" >

<?php
    foreach($_REQUEST as $name => $value) {
        $params[$name] = $value;
    }      
?>

<?PHP
  $date_utc =gmdate("n/j/Y H:i:s A");

  foreach($params as $name => $value) {
  echo "<input type=\"hidden\" id=\"" . $name . "\" name=\"" . $name . "\" value=\"" . $value . "\"/>\n";
        }
echo "<input type=\"hidden\" id=\"signature\" name=\"signature\" value=\"" . sign($params) . "\"/>\n";

?>

<input type="hidden" name="signed_date_time" value="<?php echo $date_utc; ?>">

</form>
Babar
  • 75
  • 1
  • 7
  • 1
    _"But receiving error on the next page."_ - which error? – El_Vanja Jun 02 '21 at 08:44
  • actually, it goes to the payment processor website, and they don't give details of the errors. – Babar Jun 02 '21 at 09:51
  • 1
    Then you need to direct this question to them. How can we know what data format the payment processor expects and what might have gone wrong? – El_Vanja Jun 02 '21 at 09:53
  • on error checking, it seems like the problem is with the date format. The system is accepting date like 2021-06-02T14:59:04Z and $date_utc is passing like 6/2/2021 3:02:32 PM. Any idea how to convert it into an acceptable format. – Babar Jun 02 '21 at 15:03
  • 1
    https://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php – El_Vanja Jun 02 '21 at 15:08
  • Thank you, I am now using echo gmdate("Y-m-d\TH:i:s\Z"); for UTC date. The format is now correct like 2021-06-03T14:59:04Z But still facing an error. The above code seems like adding Sign function to each field to verify the secret key. How to do this to the signed_date_time field. I tried this but does not work. – Babar Jun 03 '21 at 08:59

0 Answers0