class CheckoutsController < ApplicationController
def create
ActiveRecord::Base.transaction do
!
cancel_abandoned_checkouts
return unless new_checkout_from_params
@checkout.save!
!
if params[:email_address_set_membership] && params[:email_address_set_membership][:email_address_set_id].present?
@checkout.meta(
:email_address_set_join_intention,
params[:email_address_set_membership][:email_address_set_id]
)
end
!
# Track checkout creation
mixpanel.track_event('Payment', {
source: params[:checkout][:source],
user: current_user.id,
method: params[:checkout][:service]
})
!
mixpanel.track_event('Retailer Link', {
source: params[:checkout][:source],
providerId: @checkout.ownerships.reduce([]){|pids, o| pids << o.product.provider.id }.uniq,
itemId: @checkout.item_ids,
embedded: params[:checkout][:embedded],
url: request.referer ? request.referer.encode('UTF-8', invalid: :replace, undef: :replace) : nil
})
!
payment_service = params[:checkout][:service]
current_user.meta(:most_recent_payment_service, payment_service)
key = (payment_service + '_payment_service_usage_count').to_sym
if current_user.meta(key).nil?
current_user.meta(key, 1)
else
current_user.meta(key, current_user.meta(key) + 1 )
end
!
if payment_service == 'PayLater' && @checkout.pay_later_allowed?
if @checkout.gift &&
[email protected]_address
respond_to do |format|
msg = "Provide the full billing address."
format.html do
flash[:error] = Notification.new(msg,
"You tried to purchase a gift using 'pay later' option, but no billing address was specified." +
"
Contact us for more info:
[email protected]")
redirect_to checkout_path and return
end
format.json { render json: {message: msg}, status: :unprocessable_entity and return}
end
end
!
@checkout.complete_without_payment!
if current_user.existant_ea
PurchaseMailer.delay.purchase_confirmation(current_user.existant_ea, @checkout)
end
!
respond_to_successfully_completed_checkout(@checkout, payment_service) and return
end
!
collect_mailing =
case params[:checkout][:collect_mailing]
when 'true' then true
when 'false' then false
else false
end
!
allow_guest_checkout =
case params[:checkout][:pp_mode]
when 'cc' then true
when 'account' then false
else true
end
!
invoice_items = nil
!
invoice_items = @checkout.ownerships.inject([]) do |a, e|
i = e.product_item
if i.custom_invoice_items
a += i.custom_invoice_items
else
a << { name: i.product.description, amount: i.price * 100,
description: i.description }
end
a
end
if invoice_items.map{|x| x[:amount] }.sum != @checkout.amount * 100
invoice_items = nil
end
!
begin
payment_creation_options = {
user_id: current_user.id, checkout_id: @checkout.id,
amount: @checkout.amount, service: payment_service,
collect_mailing: collect_mailing,
allow_guest_checkout: allow_guest_checkout
}
payment_creation_options[:invoice_items] = invoice_items if invoice_items
payment_creation_options[:pp_page_style] =
params[:checkout][:pp_page_style] if params[:checkout][:pp_page_style]
!
payment_creation_options[:amazon_cobranding_url] =
params[:checkout][:amazon_cobranding_url] if params[:checkout][:amazon_cobranding_url]
!
@payment = Payment.create( payment_creation_options )
rescue ActiveResource::ResourceInvalid, StandardError
notify_airbrake($!)
flash[:error] = Notification.new(UX::PAYMENTS[:creation_failed], UX::PAYMENTS[:help], false)
respond_to do |format|
format.html { redirect_to checkout_path and return }
format.json { render json: {message: UX::PAYMENTS[:creation_failed]}, status: 422 and return }
end
else
@checkout.update_attributes({ payment_id: @payment.id }, without_protection: true)
!
respond_to do |format|
format.html { redirect_to( @payment.get(:authorization_url)['value'] ) and return }
format.json { render json: { payment_id: @payment.id } and return }
end
end
!
end # ActiveRecord::Base.transaction
end
end
Your code looks like this
(this is a single
controller action)