class Admin::HipayVirementsController < ApplicationController
  layout "admin"
  

  
  def index
    
    @hipay_virements = HipayVirement.order("created_at DESC").all
  end


  def show

    @hipay_virement = HipayVirement.find(params[:id])


    
  end
  
  def paid
    @hipay_virement = HipayVirement.find(params[:id])
    
    if !@hipay_virement.sended
    
      if Rails.env.development?
      @host_sp = "http://78.245.74.12"
      @host_hipay = "https://test-ws.hipay.com/soap/transfer?wsdl"
      #@host_hipay = "https://ws.hipay.com/soap/payment-v2?wsdl"
      elsif Rails.env.production? 
        @host_sp = "https://sideplace.com"
      @host_hipay = "https://ws.hipay.com/soap/transfer?wsdl"
  
      end
    
     
    
    
      @client = Savon.client(wsdl: @host_hipay, ssl_version: :TLSv1)

      @client.operations
    
      message = {
    
        :amount => @hipay_virement.amount_ttc,
        :recipientUsername => @hipay_virement.hipay_email,
        :publicLabel => "Virement commission Sideplace - #{@hipay_virement.token}",
        :privateLabel => "Sideplace - D:#{@hipay_virement.devise_id} - #{@hipay_virement.token}",
    
      
      }
    
      if @hipay_virement.devise.code == "USD"

        message[:wsLogin] = "10850b5beb65cbfacbd748fa231652f2" 
        message[:wsPassword] = "6df1b0ad1c55a1e511e33e7bc42ee3cc"

      
      elsif @hipay_virement.devise.code == "CAD"

        message[:wsLogin] = "4b914f9a84e773fa07aff82ed896dd9c" 
        message[:wsPassword] = "26d098a1d239dca34c9c3aaf151a8673"

    
      elsif @hipay_virement.devise.code == "GBP"

        message[:wsLogin] = "8dfff4b14fb9fd7e79b79e50e22fe242" 
        message[:wsPassword] = "7cd17cec7bbfccacfc96ff5f3710eb22"
 
      
      elsif @hipay_virement.devise.code == "CHF"

        message[:wsLogin] = "9c8bb8673a712c6bb4a2e3c89f760e98" 
        message[:wsPassword] = "cff22a090bed10e0001754fb724c7843"

      
      elsif @hipay_virement.devise.code == "AUD"

        message[:wsLogin] = "22804a107d27702252ae8833fcc98b5e" 
        message[:wsPassword] = "6b764308b87aef21a0ca7e88810d96e0"
  
      elsif @hipay_virement.devise.code == "EUR"

        message[:wsLogin] = "cb9db4d771d58fa1354747f8891c3875" 
        message[:wsPassword] = "52d67e65baa8bd436a5dcbc7f3764efc"
  
      
      end
    
    

      @response = @client.call(:direct, :message => {:parameters => message})

      @response.body
    
    
      response_elements = @response.body[:direct_response][:direct_result]
  
    
      @hipay_virement_history = @hipay_virement.hipay_virement_histories.create(
      :transaction_id => response_elements[:transaction_id].to_s,
      :amount => response_elements[:amount].to_s,
      :currency => response_elements[:currency].to_s,
      :recipient_username => response_elements[:recipient_username].to_s,
      :public_label => response_elements[:public_label].to_s,
      :private_label => response_elements[:private_label].to_s,
      :code => response_elements[:code].to_s,
      :description => response_elements[:description].to_s,
      :success => (true if response_elements[:code].to_s == "0")
      )
    
      if response_elements[:code].to_s == "0"
        @hipay_virement.sended = true
        @hipay_virement.sended_at = Time.now
        @hipay_virement.save(:validate => false)
      end
    
      
    
    end
    redirect_to :back
    
  end
  
  
  
  def valid
    @hipay_virement = HipayVirement.find(params[:id])
    
  
      @hipay_virement.tva = 0.0
  
    
    @hipay_virement.amount_ttc = @hipay_virement.amount_ht + @hipay_virement.tva
    
    @hipay_virement.confirmed = true
   
    
    @hipay_virement.save(:validate => false)
    
    #redirect_to :back
    
  end

  def edit
    @hipay_virement = HipayVirement.find(params[:id])
  end


  def update
    @hipay_virement = HipayVirement.find(params[:id])
    
    if @hipay_virement.update_attributes(params.require(:hipay_virement).permit!)

          
    else
      render :action => "edit" 
    end
    
  end

end
