Class: TBK::Commerce

Inherits:
Object
  • Object
show all
Defined in:
lib/tbk/commerce.rb

Overview

Represents a commerce registered with Transbank

Constant Summary collapse

TEST_COMMERCE_KEY =
TBK.parse_key('test_commerce')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Commerce

Initializes a new commerce

Parameters:

  • attributes (Hash)

    The commerce attributes

Options Hash (attributes):

  • :id (Integer)

    The commerce ID

  • :key (String|OpenSSL::PKey::RSA)

    The commerce RSA private key

  • :test (Boolean)

    flag to set commerce in test mode

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tbk/commerce.rb', line 23

def initialize(attributes)
  @environment = (attributes[:environment] || :production).to_sym
  raise TBK::CommerceError, "Invalid commerce environment" unless [:production,:test].include? @environment

  @id = attributes[:id]
  raise TBK::CommerceError, "Missing commerce id" if self.id.nil?

  @key = case attributes[:key]
  when String
    OpenSSL::PKey::RSA.new(attributes[:key])
  when OpenSSL::PKey::RSA
    attributes[:key]
  when nil
    TEST_COMMERCE_KEY if self.test?
  end

  raise TBK::CommerceError, "Missing commerce key" if self.key.nil?
  raise TBK::CommerceError, "Commerce key must be a RSA private key" unless self.key.private?
end

Instance Attribute Details

#environmentSymbol (readonly)

Returns the commerce environment.

Returns:

  • (Symbol)

    the commerce environment



16
17
18
# File 'lib/tbk/commerce.rb', line 16

def environment
  @environment
end

#idInteger (readonly)

The registered commerce id

Returns:

  • (Integer)

    the commerce id



8
9
10
# File 'lib/tbk/commerce.rb', line 8

def id
  @id
end

#keyOpenSSL::PKey::RSA (readonly)

The commerce secret RSA key

Returns:

  • (OpenSSL::PKey::RSA)

    the commerce key



12
13
14
# File 'lib/tbk/commerce.rb', line 12

def key
  @key
end

Class Method Details

.default_commerceTBK::Commerce

Returns The default commerce.

Returns:



59
60
61
62
63
64
65
# File 'lib/tbk/commerce.rb', line 59

def self.default_commerce
  @default_commerce ||= Commerce.new({
    :id => TBK.config.commerce_id,
    :key => TBK.config.commerce_key,
    :environment => TBK.config.environment
  }) unless TBK.config.commerce_id.nil?
end

Instance Method Details

#key_bytesInteger

Returns RSA key bytes.

Returns:

  • (Integer)

    RSA key bytes



54
55
56
# File 'lib/tbk/commerce.rb', line 54

def key_bytes
  self.key.n.num_bytes
end

#production?Boolean

Returns whether or not the commerce is in production mode.

Returns:

  • (Boolean)

    whether or not the commerce is in production mode



49
50
51
# File 'lib/tbk/commerce.rb', line 49

def production?
  self.environment == :production
end

#test?Boolean

Returns whether or not the commerce is in test mode.

Returns:

  • (Boolean)

    whether or not the commerce is in test mode



44
45
46
# File 'lib/tbk/commerce.rb', line 44

def test?
  self.environment == :test
end