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..c5494c18f --- /dev/null +++ b/app/services/payment_option_checker.rb @@ -0,0 +1,26 @@ +class PaymentOptionChecker + + def initialize(payment_option_id) + @payment_option_id = payment_option_id + end + + def price + if use_payment_options? + payment_option.try(:amount) + else + Settings.price + end + end + + def payment_option + 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