class DaFunk::PaymentChannel

Constants

DEFAULT_HEARBEAT

Attributes

app[RW]
current[RW]
client[RW]
handshake_request[RW]
handshake_response[RW]
host[RW]
port[RW]

Public Class Methods

alive?() click to toggle source
# File lib/da_funk/payment_channel.rb, line 122
def self.alive?
  Device::Network.connected? && self.current&.connected?
end
app=(application) click to toggle source
# File lib/da_funk/payment_channel.rb, line 39
def self.app=(application)
  if @app != application
    @app = application
    # if Context::CommunicationChannel send application name thought threads
    if self.current == Context::CommunicationChannel
      self.current.app = application
    else
      Device::System.klass = application
    end
  end
  @app
end
channel_limit_exceed?() click to toggle source
# File lib/da_funk/payment_channel.rb, line 92
def self.channel_limit_exceed?
  return true if transaction_http?
  if payment_channel_limit?
    payment_channel_limit <= Device::Setting.payment_channel_attempts.to_i
  else
    false
  end
end
check(display_message = true) click to toggle source
# File lib/da_funk/payment_channel.rb, line 101
def self.check(display_message = true)
  if self.dead?
    unless self.channel_limit_exceed?
      PaymentChannel.connect(display_message)
      if self.current
        self.print_info(I18n.t(:attach_waiting), display_message)
        if message = self.current.check || self.current.handshake?
          self.print_info(I18n.t(:attach_connected), display_message)
          message
        end
      end
    end
  else
    self.current&.check
  end
end
client() click to toggle source

Backward compatibility

# File lib/da_funk/payment_channel.rb, line 12
def self.client
  self.current
end
client=(obj) click to toggle source

Backward compatibility

# File lib/da_funk/payment_channel.rb, line 17
def self.client=(obj)
  self.current = obj
end
client_clear!() click to toggle source
# File lib/da_funk/payment_channel.rb, line 160
def self.client_clear!
  self.current = nil unless self.current == Context::CommunicationChannel
end
close!() click to toggle source
# File lib/da_funk/payment_channel.rb, line 126
def self.close!
  self.current&.close
ensure
  client_clear!
end
configured?() click to toggle source
# File lib/da_funk/payment_channel.rb, line 33
def self.configured?
  DaFunk::ParamsDat.file["access_token"] &&
    DaFunk::ParamsDat.file["payment_channel_enabled"] == "1" &&
    Device::Setting.logical_number
end
connect(display_message = true) click to toggle source
# File lib/da_funk/payment_channel.rb, line 64
def self.connect(display_message = true)
  if self.dead? && self.ready?
    self.print_info(I18n.t(:attach_attaching), display_message)
    create
    self.print_info(I18n.t(:attach_authenticate), display_message)
    self.current.handshake
  else
    client_clear!
  end
  self.current
end
create() click to toggle source
# File lib/da_funk/payment_channel.rb, line 151
def self.create
  if self.current != Context::CommunicationChannel
    payment_channel_increment_attempts
    self.current = PaymentChannel.new
  else
    self.current.connect
  end
end
current=(obj) click to toggle source
# File lib/da_funk/payment_channel.rb, line 25
def self.current=(obj)
  @current = obj
end
dead?() click to toggle source
# File lib/da_funk/payment_channel.rb, line 118
def self.dead?
  ! self.alive?
end
handshake_message() click to toggle source
# File lib/da_funk/payment_channel.rb, line 52
def self.handshake_message
  {
    "token"     => DaFunk::ParamsDat.file["access_token"],
    "id"        => Device::Setting.logical_number.to_s,
    "heartbeat" => Device::Setting.heartbeat || DEFAULT_HEARBEAT
  }.to_json
end
handshake_success_message() click to toggle source
# File lib/da_funk/payment_channel.rb, line 60
def self.handshake_success_message
  {"token" => DaFunk::ParamsDat.file["access_token"]}.to_json
end
new(client = nil) click to toggle source
# File lib/da_funk/payment_channel.rb, line 164
def initialize(client = nil)
  @host   = Device::Setting.host
  @port   = (Device::Setting.apn == "gprsnac.com.br") ? 32304 : 443
  if PaymentChannel.transaction_http?
    @client = client || CwHttpSocket.new
  else
    @client = client || CwWebSocket::Client.new(@host, @port)
  end
rescue SocketError, PolarSSL::SSL::Error => e
  self.error(e)
end
payment_channel_increment_attempts() click to toggle source
# File lib/da_funk/payment_channel.rb, line 136
def self.payment_channel_increment_attempts
  number = Device::Setting.payment_channel_attempts.to_i
  date   = Device::Setting.payment_channel_date
  if date.to_s.empty?
    Device::Setting.payment_channel_set_attempts(Time.now)
  else
    year, mon, day = date.split("-")
    if day.to_i == Time.now.day
      Device::Setting.payment_channel_set_attempts(nil, number + 1)
    else
      Device::Setting.payment_channel_set_attempts(Time.now)
    end
  end
end
payment_channel_limit() click to toggle source
# File lib/da_funk/payment_channel.rb, line 80
def self.payment_channel_limit
  if DaFunk::ParamsDat.exists?
    DaFunk::ParamsDat.file["payment_channel_limit"].to_i
  else
    0
  end
end
payment_channel_limit?() click to toggle source
# File lib/da_funk/payment_channel.rb, line 76
def self.payment_channel_limit?
  DaFunk::ParamsDat.exists? && DaFunk::ParamsDat.file["payment_channel_check_limit"] == "1"
end
print_info(message, display = true) click to toggle source
ready?() click to toggle source
# File lib/da_funk/payment_channel.rb, line 29
def self.ready?
  Device::Network.connected? && self.configured?
end
transaction_http?() click to toggle source
# File lib/da_funk/payment_channel.rb, line 88
def self.transaction_http?
  DaFunk::ParamsDat.exists? && DaFunk::ParamsDat.file["transaction_http_enabled"] != "0"
end

Public Instance Methods

check() click to toggle source
# File lib/da_funk/payment_channel.rb, line 223
def check
  if Device::Network.connected? && self.connected? && self.handshake?
    self.read
  end
end
close() click to toggle source
# File lib/da_funk/payment_channel.rb, line 198
def close
  @client&.close
  @client = nil
  PaymentChannel.current = nil
end
code() click to toggle source
# File lib/da_funk/payment_channel.rb, line 176
def code
  if PaymentChannel.transaction_http? && @client
    @client.code
  end
end
connected?() click to toggle source
# File lib/da_funk/payment_channel.rb, line 204
def connected?
  self.client&.connected?
end
handshake?() click to toggle source
# File lib/da_funk/payment_channel.rb, line 208
def handshake?
  if self.client.respond_to?(:handshake?)
    self.client.handshake?
  else
    if self.connected? && @handshake_request && ! @handshake_response
      timeout = Time.now + Device::Setting.tcp_recv_timeout.to_i
      loop do
        break if @handshake_response = self.client.read
        break if Time.now > timeout || getc(200) == Device::IO::CANCEL
      end
    end
    !! @handshake_response
  end
end
read() click to toggle source
# File lib/da_funk/payment_channel.rb, line 190
def read
  begin
    @client.read
  rescue SocketError, PolarSSL::SSL::Error => e
    self.error(e)
  end
end
write(value) click to toggle source
# File lib/da_funk/payment_channel.rb, line 182
def write(value)
  if Object.const_defined?(:Cloudwalk) && value.is_a?(Cloudwalk::HttpEvent)
    @client.write(value.message)
  else
    @client.write(value)
  end
end

Private Instance Methods

error(exception) click to toggle source
# File lib/da_funk/payment_channel.rb, line 230
def error(exception)
  if Context.development?
    ContextLog.exception(exception, exception.backtrace, "PaymentChannel error")
  end
  self.close
end
handshake() click to toggle source
# File lib/da_funk/payment_channel.rb, line 237
def handshake
  if self.connected?
    if self.handshake?
      true
    else
      @handshake_request = PaymentChannel.handshake_message
      @client.write(handshake_request)
    end
  end
end