def payment_profile(user, params)
      resp = Caboose::StdClass.new('success' => true)            
      response = nil
      
      card_num = params[:credit_card][:number]
      user.card_brand = 'Amex'       if card_num.starts_with?('3')   
      user.card_brand = 'Visa'       if card_num.starts_with?('4')   
      user.card_brand = 'MasterCard' if card_num.starts_with?('5')   
      user.card_brand = 'Discover'   if card_num.starts_with?('6011')
      user.save
    
      # Make sure the customer profile exists    
      if user.authnet_customer_profile_id.nil?      
        response = self.customer_profile(user)
        if !response.success
          user.authnet_customer_profile_id = nil
          user.authnet_payment_profile_id = nil
          user.save
          resp.error = "Error creating customer profile."
        else
          user.authnet_payment_profile_id = nil
          user.save
        end
      end
    
      # Now see if the payment profile exists
      if user.authnet_customer_profile_id && user.authnet_payment_profile_id
        options = payment_profile_options(user, params)
        options[:payment_profile][:customer_payment_profile_id] = user.authnet_payment_profile_id
        response = self.gateway.update_customer_payment_profile(options)            
        if response.success?
          user.authnet_payment_profile_id = response.params['customer_payment_profile_id']        
          user.save        
          resp.success = true
        elsif response.message == 'ProfileID is invalid.'
          user.authnet_customer_profile_id = nil
          user.authnet_payment_profile_id = nil
          user.save
          resp.error = "Invalid customer profile ID. Please submit again."
        elsif response.message == "The credit card number is invalid."
          resp.error = "The credit card number is invalid."
        elsif response.message == "This transaction has been declined."                
          resp.error = "We couldn't validate the credit card information."
        elsif response.message == "A duplicate transaction has been submitted." # The user submitted the same information within two minutes                
          resp.error = "We couldn't validate the credit card information."
        else        
          puts "=================================================================="
          puts "Response from gateway.update_customer_payment_profile(#{options})"
          puts ""
          puts response.message
          puts ""
          puts response.inspect
          puts "=================================================================="
          user.authnet_payment_profile_id = nil
          user.save        
        end
      end
      
      if user.authnet_customer_profile_id && user.authnet_payment_profile_id.nil?
        options = payment_profile_options(user, params)
        response = self.gateway.create_customer_payment_profile(options)            
        if response.success?        
          user.authnet_payment_profile_id = response.params['customer_payment_profile_id']        
          user.save
          resp.success = true        
        elsif response.message == "A duplicate customer payment profile already exists."
          response = self.gateway.get_customer_profile({ :customer_profile_id => user.authnet_customer_profile_id })        
          if response.success?
            arr = response.params['profile']['payment_profiles']
            arr = [arr] if arr.is_a?(Hash)
            arr.each do |h|
              if h['payment'] && h['payment']['credit_card'] && h['payment']['credit_card']['card_number']
                x = h['payment']['credit_card']['card_number'].split(//).last(4).join("")
                y = params[:credit_card][:number].split(//).last(4).join("")            
                if x == y
                  user.authnet_payment_profile_id = h['customer_payment_profile_id']
                  user.save
                  break
                end
              end
            end
          end
        elsif response.message == "The credit card number is invalid."                
          resp.error = "The credit card number is invalid."
        elsif response.message == "This transaction has been declined."        
          resp.error = "We couldn't validate the credit card information."
        elsif response.message == "A duplicate transaction has been submitted." # The user submitted the same information within two minutes        
          resp.error = "We couldn't validate the credit card information."
        else
          puts "=================================================================="
          puts "Response from gateway.create_customer_payment_profile(#{options})"
          puts ""
          puts response.message
          puts ""
          puts response.inspect
          puts "=================================================================="
          resp.error = "A fatal error occured, the web administrator has been notified. Please try again later."
        end          
      end
      return resp
    end
def payment_profile(user, params)
      resp = Caboose::StdClass.new('success' => true)            
      response = nil
      
      card_num = params[:credit_card][:number]
      user.card_brand = 'Amex'       if card_num.starts_with?('3')   
      user.card_brand = 'Visa'       if card_num.starts_with?('4')   
      user.card_brand = 'MasterCard' if card_num.starts_with?('5')   
      user.card_brand = 'Discover'   if card_num.starts_with?('6011')
      user.save
    
      # Make sure the customer profile exists    
      if user.authnet_customer_profile_id.nil?      
        response = self.customer_profile(user)
        if !response.success
          user.authnet_customer_profile_id = nil
          user.authnet_payment_profile_id = nil
          user.save
          resp.error = "Error creating customer profile."
        else
          user.authnet_payment_profile_id = nil
          user.save
        end
      end
    
      # Now see if the payment profile exists
      if user.authnet_customer_profile_id && user.authnet_payment_profile_id
        options = payment_profile_options(user, params)
        options[:payment_profile][:customer_payment_profile_id] = user.authnet_payment_profile_id
        response = self.gateway.update_customer_payment_profile(options)            
        if response.success?
          user.authnet_payment_profile_id = response.params['customer_payment_profile_id']        
          user.save        
          resp.success = true
        elsif response.message == 'ProfileID is invalid.'
          user.authnet_customer_profile_id = nil
          user.authnet_payment_profile_id = nil
          user.save
          resp.error = "Invalid customer profile ID. Please submit again."
        elsif response.message == "The credit card number is invalid."
          resp.error = "The credit card number is invalid."
        elsif response.message == "This transaction has been declined."                
          resp.error = "We couldn't validate the credit card information."
        elsif response.message == "A duplicate transaction has been submitted." # The user submitted the same information within two minutes                
          resp.error = "We couldn't validate the credit card information."
        else        
          puts "=================================================================="
          puts "Response from gateway.update_customer_payment_profile(#{options})"
          puts ""
          puts response.message
          puts ""
          puts response.inspect
          puts "=================================================================="
          user.authnet_payment_profile_id = nil
          user.save        
        end
      end
      
      if user.authnet_customer_profile_id && user.authnet_payment_profile_id.nil?
        options = payment_profile_options(user, params)
        response = self.gateway.create_customer_payment_profile(options)            
        if response.success?        
          user.authnet_payment_profile_id = response.params['customer_payment_profile_id']        
          user.save
          resp.success = true        
        elsif response.message == "A duplicate customer payment profile already exists."
          response = self.gateway.get_customer_profile({ :customer_profile_id => user.authnet_customer_profile_id })        
          if response.success?
            arr = response.params['profile']['payment_profiles']
            arr = [arr] if arr.is_a?(Hash)
            arr.each do |h|
              if h['payment'] && h['payment']['credit_card'] && h['payment']['credit_card']['card_number']
                x = h['payment']['credit_card']['card_number'].split(//).last(4).join("")
                y = params[:credit_card][:number].split(//).last(4).join("")            
                if x == y
                  user.authnet_payment_profile_id = h['customer_payment_profile_id']
                  user.save
                  break
                end
              end
            end
          end
        elsif response.message == "The credit card number is invalid."                
          resp.error = "The credit card number is invalid."
        elsif response.message == "This transaction has been declined."        
          resp.error = "We couldn't validate the credit card information."
        elsif response.message == "A duplicate transaction has been submitted." # The user submitted the same information within two minutes        
          resp.error = "We couldn't validate the credit card information."
        else
          puts "=================================================================="
          puts "Response from gateway.create_customer_payment_profile(#{options})"
          puts ""
          puts response.message
          puts ""
          puts response.inspect
          puts "=================================================================="
          resp.error = "A fatal error occured, the web administrator has been notified. Please try again later."
        end          
      end
      return resp
    end
=============================================================================
 Create or update the authorize.net payment profile for the given user.