简体   繁体   中英

Perl & SHA1 Query

My PHP Code

<?php
$xml="<request>
<point_of_sale_id>XXXXXXX</point_of_sale_id>
<order_id>XXXXXXX</order_id>
<amount>XXXXXXX</amount>
<description>XXXXXXX
</description>
<result_url>XXXXXXX</result_url>
<server_url>XXXXXXX</server_url>
</request>";

$parameters_xml=base64_encode($xml);
$merchant_secret_code='XXXXXXX';

$signature=base64_encode(sha1($xml.$merchant_secret_code,1));


echo "<form action=\"https://www.i-koruna.com/i-koruna/api/payment/payment-entry-point.jsf\"\n"; 
echo "method=\"POST\" accept-charset=\"utf-8\">\n"; 
echo "<input type=\"hidden\" name=\"api_version\" value=\"v2\" />\n"; 
echo "<input type=\"hidden\" name=\"parameters_xml\" value=\"$parameters_xml\" />   \n"; 
echo "<input type=\"hidden\" name=\"signature\" value=\"$signature\" />\n"; 
echo "<input type=\"hidden\" name=\"locale\"  value=\"en\" />\n"; 
echo "<input type=\"submit\" value=\"Buy\" />\n"; 
echo "</form> \n";
?>

this code is working but same reproduction of it in perl

Perl Code

     use MIME::Base64;
            use Digest::SHA qw(sha1);
            my $parameters_xml = encode_base64("<request><point_of_sale_id>".$c->{ikoruna_pos}."</point_of_sale_id><order_id>$id</order_id><amount>".$f->{amount}."</amount><description>".$c->{item_name}."</description><result_url>$c->{site_url}/?payment_complete=$id-$usr_id</result_url><server_url>$c->{site_url}/?payment_complete=$id-$usr_id</server_url></request>");
            $parameters_xml =~ s/\s+//g;
            my $merchant = 'XXXXXXXXXXXXX';
            my $signature=encode_base64(sha1($parameters_xml.$merchant), '');

            print "Content-type:text/html\n\n";
print <<END
<form action="https://www.i-koruna.com/i-koruna/api/payment/payment-entry-point.jsf" method="POST" accept-charset="utf-8">
     <input type="hidden" name="api_version" value="v2" />
     <input type="hidden" name="parameters_xml" value="$parameters_xml" /> 
     <input type="hidden" name="signature" value="$signature" />
    <input type="hidden" name="locale"  value="en" />
     <input type="submit" value="Buy" />
</form>
END

I have checked it is a problem while packing $signature am i doing this correct? Ignore the HTML part if i missed something there that is not an issue i can do it

I am getting this error from merchant "Invalid merchant signature"

In the PHP the sha hash is of the unencoded xml whilst in the perl the sha hash is of the base64 encoded xml

Corrected:

use MIME::Base64;
use Digest::SHA qw(sha1);
my $xml = <<END;
    <request>
        <point_of_sale_id>".$c->{ikoruna_pos}."</point_of_sale_id>
        <order_id>$id</order_id>
        <amount>".$f->{amount}."</amount>
        <description>".$c->{item_name}."</description>
        <result_url>$c->{site_url}/?payment_complete=$id-$usr_id</result_url>
        <server_url>$c->{site_url}/?payment_complete=$id-$usr_id</server_url>
    </request>
END
my $parameters_xml = encode_base64($xml);
$parameters_xml =~ s/\s+//g;
my $merchant = 'XXXXXXXXXXXXX';
my $signature=encode_base64(sha1($xml.$merchant), '');
print "Content-type:text/html\n\n";
print <<END;
<form action="https://www.i-koruna.com/i-koruna/api/payment/payment-entry-point.jsf" method="POST" accept-charset="utf-8">
    <input type="hidden" name="api_version" value="v2" />
    <input type="hidden" name="parameters_xml" value="$parameters_xml" /> 
    <input type="hidden" name="signature" value="$signature" />
    <input type="hidden" name="locale"  value="en" />
    <input type="submit" value="Buy" />
</form>
END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM