class Threoze::Paya::FormService < Threoze::Paya::ApplicationService
  def initialize(school, options={})
    @options = options
    super(school)
  end

  def call
    url = URI.parse("#{base_uri}/accountform")
    url.query = URI.encode_www_form(form_params)
    url.to_s
  end

  private
    def timestamp
      @timestamp ||= Time.now.to_i
    end

    def hash_key
      salt = "#{user_id}#{timestamp}"
      OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), user_hash_key, salt)
    end

    def form_params
      {}.tap do |props|
        props[:timestamp] = timestamp
        props[:'developer-id'] = developer_id
        props[:'hash-key'] = hash_key
        props[:'user-id'] = user_id
        props[:data] = { accountvault: data }.to_json.unpack1('H*')
      end
    end

    def data
      {}.tap do |props|
        props[:contact_id] = @options[:contact_id]
        props[:payment_method] = @options[:payment_method]
        props[:location_id] = internal_location_id
        props[:parent_send_message] = true
        props[:show_street] = true
        props[:show_zip] = true
        props[:billing_street] = @school.address
        props[:billing_zip] = @school.zip
        props[:require_zip] = true
        props[:require_street] = false
        props[:stylesheet_url] = "#{ENV['LEGACY_URL']}/paya.css"

        props[:account_vault_api_id] = timestamp.to_s
        props[:accountvault_c1] = "school_#{@school.id}"
        props[:account_holder_name] = @school.name
        props[:show_account_holder_name] = true
        props[:display_close_button] = false
      end
    end
end
