Created
February 1, 2009 03:10
-
-
Save brandon-beacher/55775 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
class DigitalFaith::AmazonSimplePayTransactionsController < DigitalFaithController | |
before_filter :find_instance | |
def abandon | |
AmazonSimplePayTransaction.create! attributes | |
flash[:error] = "Your payment has been canceled" | |
redirect_to polymorphic_failure_path | |
end | |
def prepare | |
end | |
def return | |
AmazonSimplePayTransaction.create! attributes | |
case params[:status] | |
when "PS" | |
flash[:notice] = "Your payment was successful" | |
redirect_to polymorphic_success_path | |
when "PF" | |
flash[:error] = "Sorry — your payment failed" | |
redirect_to polymorphic_failure_path | |
when "PI" | |
flash[:notice] = "Your payment is pending" | |
redirect_to polymorphic_success_path | |
when "ME" | |
flash[:error] = "Sorry — payments are not available" | |
redirect_to polymorphic_failure_path | |
when "SE" | |
flash[:error] = "Sorry — payment processing is unavailable. Please try again later" | |
redirect_to polymorphic_failure_path | |
else | |
raise ArgumentError | |
end | |
end | |
private | |
def attributes | |
{ :amazon_simple_payable_type => params[:amazon_simple_payable_type], | |
:amazon_simple_payable_id => params[:amazon_simple_payable_id], | |
:transaction_id => params[:transactionId], | |
:reference_id => params[:referenceId], | |
:status => params[:status], | |
:error_message => params[:errorMessage], | |
:operation => params[:operation], | |
:payment_reason => params[:paymentReason], | |
:transaction_amount => params[:transactionAmount], | |
:transaction_date => params[:transactionDate], | |
:payment_method => params[:paymentMethod], | |
:recipient_name => params[:recipientName], | |
:buyer_name => params[:buyerName], | |
:recipient_email => params[:recipientEmail], | |
:buyer_email => params[:buyerEmail], | |
:signature => params[:signature] } | |
end | |
def find_instance | |
@instance = params[:amazon_simple_payable_type].constantize.find params[:amazon_simple_payable_id] | |
end | |
def polymorphic_failure_path | |
case @instance.class.to_s | |
when "Eventregistration" | |
event_path @instance.event | |
else | |
raise ArgumentError | |
end | |
end | |
def polymorphic_success_path | |
case @instance.class.to_s | |
when "Eventregistration" | |
my_event_path @instance.event | |
else | |
raise ArgumentError | |
end | |
end | |
end |
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
module DigitalFaith::AmazonSimplePayTransactionsHelper | |
def amazon_simple_pay_form(amazon_simple_payable_type, amazon_simple_payable_id, reference_id, amount) | |
params = default_params(amazon_simple_payable_type, amazon_simple_payable_id).update \ | |
"amount" => "USD #{amount}", | |
"description" => reference_id, | |
"referenceId" => reference_id | |
haml_tag "form", :action => amazon_simple_pay_url, :method => "post" do | |
params.each do |key, value| | |
haml_concat hidden_field_tag(key, value) | |
end | |
haml_concat hidden_field_tag("signature", build_signature(params)) | |
haml_concat image_submit_tag("https://authorize.payments.amazon.com/pba/images/SMPayNowWithLogo.png") | |
end | |
end | |
def amazon_simple_pay_url | |
if production_stage? | |
"https://authorize.payments.amazon.com/pba/paypipeline" | |
else | |
"https://authorize.payments-sandbox.amazon.com/pba/paypipeline" | |
end | |
end | |
def build_message(params) | |
params.keys.sort.inject("") { |string, key| string += "#{key}#{params[key]}" } | |
end | |
def build_signature(params) | |
digest = OpenSSL::Digest::Digest.new "sha1" | |
hmac = OpenSSL::HMAC.digest digest, "sekret", build_message(params) | |
Base64.encode64(hmac).chomp | |
end | |
def default_params(amazon_simple_payable_type, amazon_simple_payable_id) | |
{ "abandonUrl" => abandon_amazon_simple_pay_transaction_url(:amazon_simple_payable_type => amazon_simple_payable_type, :amazon_simple_payable_id => amazon_simple_payable_id), | |
"accessKey" => "sekret", | |
"immediateReturn" => "1", | |
"processImmediate" => "1", | |
"returnUrl" => return_amazon_simple_pay_transaction_url(:amazon_simple_payable_type => amazon_simple_payable_type, :amazon_simple_payable_id => amazon_simple_payable_id) } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment