# -*- encoding : utf-8 -*-

class Public::DonatorTransactionsController < ApplicationController
	
	layout "public"
  
  skip_before_filter :verify_authenticity_token, :only => [:payplug_ipn]
  
  def new
    @donator_transaction = DonatorTransaction.new(:recurrent => false)
    
    @donator_transaction.particular_bill = Particular.new(:particular_type => "facturation", :skip_validation => true)
    
    transaction_item =  TransactionItem.new(:amount => 50)
    transaction_item.transaction_content = TransactionDonate.new(:donate_destination_id => 1)
    
    @donator_transaction.transaction_items << transaction_item
  end
  
  def create
    @donator_transaction = DonatorTransaction.new(params.require(:donator_transaction).permit!)
    @donator_transaction.particular_idem = true
    
    #@donator_transaction.particular_bill = Particular.new(:particular_type => "facturation", :skip_validation => true)
      
    @donator_transaction.particular_send = Particular.new(:particular_type => "expedition", :skip_validation => true)
    
    @donator_transaction.tr_payment = TrPayment.new()
    @donator_transaction.particular_bill.skip_validation = true
    #@donator_transaction.particular_bill.validate_email = true
    
    if @donator_transaction.save
      
      @donator_transaction = DonatorTransaction.find(@donator_transaction.id)
      @donator_transaction.particular_bill.skip_validation = false
      
      redirect_to particulars_public_donator_transaction_path(@donator_transaction.id)
    else
      render :action => :new
    end
    
    
  end
  
  def particulars
    @donator_transaction = DonatorTransaction.find(params[:id])
    @donator_transaction.particular_bill.skip_validation = false
  end
  
  def particulars_save
    @donator_transaction = DonatorTransaction.find(params[:id])
    @donator_transaction.particular_bill.skip_validation = false
    if @donator_transaction.update_attributes(params.require(:donator_transaction).permit!)
      redirect_to select_payment_public_donator_transaction_path(@donator_transaction.id)
    
      
      
    else
      
      render :action => :particulars
      
    end
    
  end
  
  
  def select_payment
    @donator_transaction = DonatorTransaction.find(params[:id])
    if @donator_transaction.recurrent == true
      @donator_transaction.tr_payment.tr_payment_type_id = 5
    else
      @donator_transaction.tr_payment.tr_payment_type_id = 3
    end
  end
  
  def select_payment_save
    @donator_transaction = DonatorTransaction.find(params[:id])
    @donator_transaction.particular_bill.skip_validation = false
    if @donator_transaction.update_attributes(params.require(:donator_transaction).permit!)
      if @donator_transaction.tr_payment_type and @donator_transaction.tr_payment_type_id == 3
        redirect_to paid_by_payplug_public_donator_transaction_path(@donator_transaction.id)
      else
        if @donator_transaction.tr_payment_type_id == 5
          DonateTransactionsMails.confirmation_pa(@donator_transaction).deliver
        
        elsif @donator_transaction.tr_payment_type_id == 1
          DonateTransactionsMails.confirmation_cheque(@donator_transaction).deliver
        end
        redirect_to payment_instructions_public_donator_transaction_path(@donator_transaction.id)
    
      end
      
      
      
      
    else
      
      render :action => :particulars
      
    end
    
  end
  def payment_instructions
    @donator_transaction = DonatorTransaction.find(params[:id])
  end
  
  def edit
    @donator_transaction = DonatorTransaction.find(params[:id])
    
  end
  
  def update
    @donator_transaction = DonatorTransaction.find(params[:id])
    #@donator_transaction.particular_bill.validate_email =true
    if @donator_transaction.update_attributes(params.require(:donator_transaction).permit!)
      
      
      redirect_to particulars_public_donator_transaction_path(@donator_transaction.id)
      
    else
      render :action => :edit
    end
  end
  
  def payplug_ipn
    
    
    payplug_retrieve_payement(params[:id])
    
    
    response = @response
    
    #redirect_to response["hosted_payment"]["payment_url"]
    puts response
    puts "TESt"
    puts response["metadata"]["donate_transaction_id"]
    @donator_transaction = DonatorTransaction.find(response["metadata"]["donate_transaction_id"].to_i)
    @tr_payment = @donator_transaction.tr_payment
    
    @tr_payment.tr_payment_type_id = 3
    @tr_payment.amount = response["amount"].to_i*0.1
    @tr_payment.payplug_payment_id = response["id"]
    
    if response["card"]
      @tr_payment.payplug_card_id = response["card"]["id"]
      @tr_payment.payplug_card_expiry_month = response["card"]["exp_month"]
      @tr_payment.payplug_card_expiry_year = response["card"]["exp_year"]
      @tr_payment.payplug_card_last_number = response["card"]["last4"]
      
    end
    
    if response["is_paid"] == true || response["is_paid"] == "true"
      
      @tr_payment.paid_at = Time.now
      @donator_transaction.paid = true
      
      if @donator_transaction.recurrent
        DonateTransactionsMails.confirmation_cb_rec(@donator_transaction).deliver
      else
        DonateTransactionsMails.confirmation_cb(@donator_transaction).deliver
      end
      
    else
      @donator_transaction.paid = false
    end
    
    @donator_transaction.save
    @tr_payment.save
    
    
    
    
    render :inline => "ok"
  end
  
  #card_7gtdWugb6n0RtzfjYdq8hl
  
  
  def paid_by_payplug_recurent_sample
    @donator_transaction = DonatorTransaction.find(params[:id])
    
    api_key = "sk_test_334683e60741f814b8e715b855eb8243"
    
    host = "http://78.245.74.12"
    
    @data = {
    "amount" => 8800,
    "payment_method" => "card_7gtdWugb6n0RtzfjYdq8hl",
    "currency" => "EUR",
    "customer" =>{
      "email" => "john.watson@example.net",
      "first_name" => "John",
      "last_name" =>  "Watson"
    }, 
    
    "notification_url" => "#{host}"+payplug_ipn_public_donator_transactions_path(),
    "metadata" => {
      "donate_transaction_id" => @donator_transaction.id
     }, 
    
     "force_3ds" => true
  }
    
    @c = Curl::Easy.new("https://api.payplug.com/v1/payments") do |curl| 
      curl.headers["Authorization"] = "Bearer "+api_key
      curl.headers["Content-Type"] = "application/json"
      #@datacurl.datas["amount"] = @data
      
      
      
      curl.verbose = true
    end
    
   
    
    @c.http_post(@data.to_json)
    
    response = JSON.parse(@c.body_str)
    
    #redirect_to response["hosted_payment"]["payment_url"]
    
    
    #-H "Authorization: Bearer sk_live_43b7e007298f57f7aedee32800a52301" 
    
    
    
    #render :inline => "ok"
    
  end
  
  
  
  def paid_by_payplug
    @donator_transaction = DonatorTransaction.find(params[:id])
    if @donator_transaction.paid
      redirect_to select_payment_public_donator_transaction_path(@donator_transaction.id)
    else
      
      options = {
    :amount => @donator_transaction.total,
    :email => @donator_transaction.particular_bill.email ,
    :first_name => @donator_transaction.particular_bill.firstname,
    :last_name => @donator_transaction.particular_bill.name,
    :return_url => "http://#{HOSTNAME}"+paid_by_payplug_confirm_public_donator_transaction_path(@donator_transaction.id),
    :cancel_url => "http://#{HOSTNAME}"+select_payment_public_donator_transaction_path(@donator_transaction.id),
    :notification_url  => "http://#{HOSTNAME}"+payplug_ipn_public_donator_transactions_path(),
    :donate_transaction_id => @donator_transaction.id,
    :save_card  => @donator_transaction.recurrent,
    
  }
  

    payplug_init_payement(options)
      
      response = @new_payplug_payment
    
      @donator_transaction.tr_payment.payplug_payment_id = response["id"]
      @donator_transaction.save
    
      redirect_to response["hosted_payment"]["payment_url"]
    
    
      #-H "Authorization: Bearer sk_live_43b7e007298f57f7aedee32800a52301" 
    
    
    
      #render :inline => "ok"
    end
    
  end
  
  
  def paid_by_payplug_id
    @donator_transaction = DonatorTransaction.find(params[:id])
    
    api_key = "sk_test_334683e60741f814b8e715b855eb8243"
    
    host = "http://78.245.74.12"
    
    @data = 
    
    @c = Curl::Easy.new("https://api.payplug.com/v1/payments/pay_5QUqqcwKSiKrJH5qnODfR6") do |curl| 
      curl.headers["Authorization"] = "Bearer "+api_key
      curl.headers["Content-Type"] = "application/json"
      #@datacurl.datas["amount"] = @data
      
      
      
      curl.verbose = true
    end
    
  
    
    @c.http_get()
    
    response = JSON.parse(@c.body_str)
    
    #redirect_to response["hosted_payment"]["payment_url"]
    
    
    #-H "Authorization: Bearer sk_live_43b7e007298f57f7aedee32800a52301" 
    
    
    
    #render :inline => "ok"
    
  end
  
  def paid_by_payplug_confirm
    
    
    @donator_transaction = DonatorTransaction.find(params[:id])
    
    payplug_retrieve_payement @donator_transaction.tr_payment.payplug_payment_id
    
    if @response and @response["is_paid"] == true# || response["is_paid"] == "true"
      @paid = true
    else
      redirect_to select_payment_public_donator_transaction_path(@donator_transaction.id, :paid_fail => 1) 
      
    end
    
    
  end
  
  protected
  
  
  
end