Perl& SHA1查询

时间:2013-01-23 20:14:07

标签: php xml perl

我的PHP代码

<?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";
?>

此代码正在运行但在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

我已经检查过这是一个问题,包装$ signature我这样做是否正确? 忽略HTML部分,如果我错过了那些不是问题我可以做到的事情

我从商家“商家签名无效”中收到此错误

1 个答案:

答案 0 :(得分:3)

在PHP中,sha哈希是未编码的xml,而在perl中,sha哈希是base64编码的xml

修正:

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