Class: FatZebra::Gateway

Inherits:
Object
  • Object
show all
Defined in:
lib/fat_zebra/gateway.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{:secure => true, :version => API_VERSION}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, token, gateway_server = GATEWAY_SERVER, options = {}) ⇒ Object

Initializes a new gateway object

Parameters:

  • merchants (String)

    username

  • merchants (String)

    token for authentication

  • the (String)

    server for the gateway, defaults to ‘gateway.fatzebra.com.au’

  • the (Hash)

    options for the gateway connection (e.g. secure, version etc)



26
27
28
29
30
31
32
33
# File 'lib/fat_zebra/gateway.rb', line 26

def initialize(username, token, gateway_server = GATEWAY_SERVER, options = {})
  self.username = username
  self.token = token
  self.gateway_server = gateway_server
  self.options = DEFAULT_OPTIONS.merge(options)

  require_field :username, :token, :gateway_server
end

Instance Attribute Details

#gateway_serverObject

Returns the value of attribute gateway_server.



3
4
5
# File 'lib/fat_zebra/gateway.rb', line 3

def gateway_server
  @gateway_server
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/fat_zebra/gateway.rb', line 3

def options
  @options
end

#tokenObject

Returns the value of attribute token.



3
4
5
# File 'lib/fat_zebra/gateway.rb', line 3

def token
  @token
end

#usernameObject

Returns the value of attribute username.



3
4
5
# File 'lib/fat_zebra/gateway.rb', line 3

def username
  @username
end

Class Method Details

.configure(config) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/fat_zebra/gateway.rb', line 8

def configure(config)
  g = Gateway.new(config.username, config.token, config.gateway || GATEWAY_SERVER)
  g.options ||= {}
  g.options[:test_mode] = config.test_mode
  g.options.merge!(config.options || {})

  g
end

Instance Method Details

#authorize(amount, card_number, card_expiry, cvv, reference, customer_ip) ⇒ Object

Public: Performs an authorization transaction against the gateway Note: Successful transactions must then be captured for funds to settle.

amount - the amount as an integer e.g. (1.00 * 100).to_i card_number - the customers credit card number card_expiry - the customers card expiry date cvv - the credit card verification value (cvv, cav, csc etc) reference - a reference for the purchase customer_ip - the customers IP address (for fraud prevention)

Returns a new FatZebra::Models::Purchase object



81
82
83
# File 'lib/fat_zebra/gateway.rb', line 81

def authorize(amount, card_number, card_expiry, cvv, reference, customer_ip)
  raise "Sorry we haven't compelted this functionality yet."
end

#capture(transaction_id, amount) ⇒ Object

Public: Captures a pre-authorized transaction

transaction_id - the authorization ID amount - the amount to capture, as an integer

Returns a new FatZebra::Models::Purchase object



91
92
93
# File 'lib/fat_zebra/gateway.rb', line 91

def capture(transaction_id, amount)
  raise "Sorry we haven't compelted this functionality yet."
end

#make_request(method, resource, data = nil) ⇒ Object

Public: Performs the HTTP(s) request and returns a response object, handing errors etc

method - the request method (:post or :get) resource - the resource for the request data - a hash of the data for the request

Returns hash of response data



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/fat_zebra/gateway.rb', line 145

def make_request(method, resource, data = nil)
  resource = get_resource(resource, method, data)

  if [:post, :put, :patch].include?(method)
    data[:test] = options[:test_mode] if options[:test_mode]
    payload = data.to_json
  else
    payload = {}
  end

  headers = options[:headers] || {}

  if method == :get
    resource.send(method, headers) do |response, request, result, &block|
      handle_response(response)
    end
  else
    # Add in test flag is test mode...
    resource.send(method, payload, headers) do |response, request, result, &block|
      handle_response(response)
    end
  end
end

#ping(nonce = SecureRandom.hex) ⇒ Object

Pings the Fat Zebra service

Parameters:

  • the (String)

    data to be echoed back

Returns:

  • true if reply is valid, false if the request times out, or otherwise



113
114
115
116
117
118
119
120
121
122
# File 'lib/fat_zebra/gateway.rb', line 113

def ping(nonce = SecureRandom.hex)  
  begin
    response = RestClient.get(build_url("ping") + "?echo=#{nonce}")
    response = JSON.parse(response)

    response["echo"] == nonce
  rescue => e
    return false
  end
end

#purchase(amount, card_data, reference, customer_ip, currency = "AUD") ⇒ Object

Performs a purchase transaction against the gateway

amount - the amount as an integer e.g. (1.00 * 100).to_i card_data - a hash of the card data (example: => “John Smith”, :number => “…”, :expiry => “…”, :cvv => “123” or => “abcdefg1”) card_holder - the card holders name card_number - the customers credit card number card_expiry - the customers card expiry date (as Date or string [mm/yyyy]) cvv - the credit card verification value (cvv, cav, csc etc) reference - a reference for the purchase customer_ip - the customers IP address (for fraud prevention) currency - the currency of the transaction, ISO 4217 code (en.wikipedia.org/wiki/ISO_4217)

Returns a new FatZebra::Models::Response (purchase) object



48
49
50
51
# File 'lib/fat_zebra/gateway.rb', line 48

def purchase(amount, card_data, reference, customer_ip, currency = "AUD")
  warn "[DEPRECATED] Gateway#purchase is deprecated, please use Purchase.create instead" unless options[:silence]
  Models::Purchase.create(amount, card_data, reference, customer_ip, currency)
end

#purchases(options = {}) ⇒ Array<Purchase>

Deprecated.

Please use Purchase.find(options) instead

Retrieves purchases specified by the options hash

- from (Date) - to (Date)

- offset (defaults to 0) - for pagination
- limit (defaults to 10) for pagination

Parameters:

  • options (Hash) (defaults to: {})

    for lookup includes:

    - id (unique purchase ID)
    - reference (merchant reference)
    

Returns:

  • (Array<Purchase>)

    array of purchases



65
66
67
68
# File 'lib/fat_zebra/gateway.rb', line 65

def purchases(options = {})
  warn "[DEPRECATED] Gateway#purchases is deprecated, please use Purchase.find instead" unless options[:silence]
  Models::Purchase.find(options)
end

#refund(transaction_id, amount, reference) ⇒ Refund

Deprecated.

Please use Refund.create or Purchase#refund instead

Refunds a transaction

Parameters:

  • the (String)

    ID of the original transaction to be refunded

  • the (Integer)

    amount to be refunded, as an integer

  • the (String)

    reference for the refund

Returns:

  • (Refund)

    refund result object



103
104
105
106
# File 'lib/fat_zebra/gateway.rb', line 103

def refund(transaction_id, amount, reference)
  warn "[DEPRECATED] Gateway#refund is deprecated, please use Refund.create or Purchase#refund instead`" unless options[:silence]
  Models::Refund.create(transaction_id, amount, reference)
end

#tokenize(card_holder, card_number, expiry, cvv) ⇒ Object

Deprecated.

Please use Card.create instead

Tokenizes a credit card

Parameters:

  • the (String)

    credit card holder name

  • the (String)

    card number

  • the (String)

    credit card expiry date (mm/yyyy)

  • the (String)

    card CVV

Returns:

  • Response



133
134
135
136
# File 'lib/fat_zebra/gateway.rb', line 133

def tokenize(card_holder, card_number, expiry, cvv)
  warn "[DEPRECATED] Gateway#tokenize is deprecated, please use Card.create instead" unless options[:silence]
  Models::Card.create(card_holder, card_number, expiry, cvv)
end