<commit_before>class CheckoutPresenter < ActivePresenter::Base
  presents :creditcard, {:bill_address => Address}, {:ship_address => Address} 
 
  alias_method :old_initialize, :initialize 
  attr_accessor :order
  
  def initialize(args = {})               
    old_initialize(args)
    default_country = Country.find_by_id Spree::Config[:default_country_id]
    bill_address.country ||= default_country
    ship_address.country ||= default_country    
    # credit card needs to use some bill_address attributes
    creditcard.address = bill_address  
    creditcard.first_name = bill_address.firstname
    creditcard.last_name = bill_address.lastname      
  end 
  
  def save              
    # TODO - do not allow save if checkout complete (double post, other user shennaingans)
    # TODO - handle this unxpected situation better (unexpected b/c we have client side validation along the way)
#    return false unless valid?           
    saved = false
    # TODO - make shipping method selectable by user
    shipping_method = ShippingMethod.first

    ActiveRecord::Base.transaction do
      # clear existing shipments (no orphans please)
      order.shipments.clear
      order.shipments.create(:address => ship_address, :shipping_method => shipping_method)
=begin --- remove when done testing
      order.complete
      # authorize the credit card and then save (authorize first before number is cleared for security purposes)
      creditcard.order = order
      creditcard.authorize(order.total)
      creditcard.save
=end      
      saved = true
    end
    saved  
  end
end<commit_msg>Assume that invalid orders are intentionally incomplete.  Stop the normal Rails validation process (NOTE: This will require iron clad javascript validation to prevent confusion.)<commit_after>class CheckoutPresenter < ActivePresenter::Base
  presents :creditcard, {:bill_address => Address}, {:ship_address => Address} 
 
  alias_method :old_initialize, :initialize 
  attr_accessor :order
  attr_accessor :final_answer
  
  def initialize(args = {})               
    old_initialize(args)
    default_country = Country.find_by_id Spree::Config[:default_country_id]
    bill_address.country ||= default_country
    ship_address.country ||= default_country    
    # credit card needs to use some bill_address attributes
    creditcard.address = bill_address  
    creditcard.first_name = bill_address.firstname
    creditcard.last_name = bill_address.lastname      
  end 
  
  def save              
    # TODO - do not allow save if checkout complete (double post, other user shennaingans)
    # Client side validation should ensure everything is valid, if its not all valid, user must not be done yet
    final_answer = valid?
    self.errors.clear

    saved = false
    # TODO - make shipping method selectable by user
    shipping_method = ShippingMethod.first

    ActiveRecord::Base.transaction do
      # clear existing shipments (no orphans please)
      order.shipments.clear
      order.shipments.create(:address => ship_address, :shipping_method => shipping_method)
      if final_answer
        order.complete
        # authorize the credit card and then save (authorize first before number is cleared for security purposes)
        creditcard.order = order
        creditcard.authorize(order.total)
        creditcard.save
      end      
      saved = true
    end
    saved  
  end
end
{'fraction_non_alphanumeric': 0.0631238990017616, 'fraction_numerical': 0.0, 'mean_word_length': 3.1347087378640777, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1348862', 'n_tokens_mistral': 912, 'n_tokens_neox': 881, 'n_words': 285}