class Threoze::Paya::TransactionService < Threoze::Paya::ApplicationService
  def initialize(school, method, options={})
    @method = method
    @options = options
    @transaction_amount = options[:transaction_amount].to_f.round(2)
    super(school)
  end

  def call
    return unless @method == :create

    JSON.parse(RestClient.post(endpoint, { transaction: data }, headers))
  rescue RestClient::Exception => e
    JSON.parse(e.response.body)
  end

  private
    def endpoint
      "#{base_uri}/transactions"
    end

    def data
      {}.tap do |props|
        props[:account_vault_id] = @options[:vault_id]
        props[:action] = ach? ? 'debit' : 'sale'
        props[:location_id] = internal_location_id
        props[:payment_method] = @options[:payment_method]
        props[:transaction_amount] = @transaction_amount.to_s
        props[:transaction_api_id] = Time.now.to_i.to_s
      end
    end

    def ach?
      @options[:payment_method] == 'ach'
    end
end
