Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moves all transaction logic to a PORO's business class #83

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 3 additions & 22 deletions app/controllers/preorder_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions app/services/payment_option_checker.rb
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions app/services/transaction.rb
Original file line number Diff line number Diff line change
@@ -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