From f06678390a64c424da6a553ad784e50132fdc15a Mon Sep 17 00:00:00 2001 From: thiagocifani Date: Fri, 14 Mar 2014 02:30:24 -0300 Subject: [PATCH 1/2] moves business logic to services namespace. All logic related with amazon payment gateway was moved to transaction.rb and payment option conditionals to payment_options_checker. --- app/controllers/preorder_controller.rb | 25 +++------------------ app/services/payment_option_checker.rb | 23 +++++++++++++++++++ app/services/transaction.rb | 31 ++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 22 deletions(-) create mode 100644 app/services/payment_option_checker.rb create mode 100644 app/services/transaction.rb diff --git a/app/controllers/preorder_controller.rb b/app/controllers/preorder_controller.rb index 6795e6d79..7c62bf590 100644 --- a/app/controllers/preorder_controller.rb +++ b/app/controllers/preorder_controller.rb @@ -9,28 +9,9 @@ def checkout def prefill @user = User.find_or_create_by(:email => params[:email]) - - if Settings.use_payment_options - payment_option_id = params['payment_option'] - raise Exception.new("No payment option was selected") if payment_option_id.nil? - payment_option = PaymentOption.find(payment_option_id) - price = payment_option.amount - else - price = Settings.price - end - - @order = Order.prefill!(:name => Settings.product_name, :price => price, :user_id => @user.id, :payment_option => payment_option) - - # This is where all the magic happens. We create a multi-use token with Amazon, letting us charge the user's Amazon account - # Then, if they confirm the payment, Amazon POSTs us their shipping details and phone number - # From there, we save it, and voila, we got ourselves a preorder! - port = Rails.env.production? ? "" : ":3000" - callback_url = "#{request.scheme}://#{request.host}#{port}/preorder/postfill" - redirect_to AmazonFlexPay.multi_use_pipeline(@order.uuid, callback_url, - :transaction_amount => price, - :global_amount_limit => price + Settings.charge_limit, - :collect_shipping_address => "True", - :payment_reason => Settings.payment_description) + redirect_to Transaction.new(user: @user, + payment_option: params['payment_option'], + request: request).amazon_flex_play end def postfill diff --git a/app/services/payment_option_checker.rb b/app/services/payment_option_checker.rb new file mode 100644 index 000000000..e73d86d8a --- /dev/null +++ b/app/services/payment_option_checker.rb @@ -0,0 +1,23 @@ +class PaymentOptionChecker + + def initialize(payment_option_id) + @payment_option_id = payment_option_id + end + + def price + payment_option.try(:amount) || Settings.price + end + + def payment_option + if use_payment_options? + PaymentOption.find(payment_option_id) + raise Exception.new("No payment option was selected") if payment_option.nil? + end + end + + +private + def use_payment_options? + Settings.use_payment_options + end +end diff --git a/app/services/transaction.rb b/app/services/transaction.rb new file mode 100644 index 000000000..511466cf2 --- /dev/null +++ b/app/services/transaction.rb @@ -0,0 +1,31 @@ +class Transaction + + attr_reader :user, :request, :payment_option_checker, :price + def initialize(options = {}) + @payment_option_checker = PaymentOptionChecker.new(options.fetch(:payment_option)) + @price = payment_option_checker.price + @user = options.fetch(:user) + @request = options.fetch(:request) + end + + def order + Order.prefill!(:name => Settings.product_name, :price => price, + :user_id => user.id, :payment_option => payment_option_checker.payment_option) + end + + def port + Rails.env.production? ? "" : ":3000" + end + + def callback_url + "#{request.scheme}://#{request.host}#{port}/preorder/postfill" + end + + def amazon_flex_play + AmazonFlexPay.multi_use_pipeline(order.uuid, callback_url, + :transaction_amount => price, + :global_amount_limit => price + Settings.charge_limit, + :collect_shipping_address => "True", + :payment_reason => Settings.payment_description) + end +end From d2830e93d8c8caa1c6cf4cfc927b38bfe578eece Mon Sep 17 00:00:00 2001 From: thiagocifani Date: Fri, 14 Mar 2014 02:38:07 -0300 Subject: [PATCH 2/2] moves conditional to avoid raise unexpect exception --- app/services/payment_option_checker.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/services/payment_option_checker.rb b/app/services/payment_option_checker.rb index e73d86d8a..c5494c18f 100644 --- a/app/services/payment_option_checker.rb +++ b/app/services/payment_option_checker.rb @@ -5,11 +5,14 @@ def initialize(payment_option_id) end def price - payment_option.try(:amount) || Settings.price + if use_payment_options? + payment_option.try(:amount) + else + Settings.price + end end def payment_option - if use_payment_options? PaymentOption.find(payment_option_id) raise Exception.new("No payment option was selected") if payment_option.nil? end