Created
September 24, 2008 21:47
-
-
Save gnufied/12685 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'base64' | |
require 'openssl' | |
module PayNowWidgetUtils | |
def generate_signed_form(access_key, aws_secret_key, form_params) | |
form_params['accessKey'] = access_key | |
# lexicographically sort the form parameters | |
# and create the canonicalized string | |
str_to_sign = "" | |
form_params.keys.sort.each { |k| str_to_sign += "#{k}#{form_params[k]}" } | |
# calculate signature of the above string | |
digest = OpenSSL::Digest::Digest.new('sha1') | |
hmac = OpenSSL::HMAC.digest(digest, aws_secret_key, str_to_sign) | |
form_params['signature'] = Base64.encode64(hmac).chomp | |
# construct the form | |
signed_form =<<-STARTFORM | |
<form action="https://authorize.payments-sandbox.amazon.com/pba/paypipeline" | |
method="post"> | |
STARTFORM | |
form_params.each do |key, value| | |
next unless key and value | |
signed_form +=<<-"FORMELEM" | |
<input type="hidden" name="#{key}" value="#{value}" > | |
FORMELEM | |
end | |
signed_form +=<<-ENDFORM | |
<input type="image" | |
src="https://authorize.payments-sandbox.amazon.com/pba/images/amazonPaymentsButton.jpg" | |
border="0" > | |
</form> | |
ENDFORM | |
return signed_form | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment