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)



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

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)
	self.proxy = self.options[:proxy]

	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
16
# 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.proxy = g.options[:proxy]

	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



99
100
101
# File 'lib/fat_zebra/gateway.rb', line 99

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



109
110
111
# File 'lib/fat_zebra/gateway.rb', line 109

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



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/fat_zebra/gateway.rb', line 163

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



131
132
133
134
135
136
137
138
139
140
# File 'lib/fat_zebra/gateway.rb', line 131

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

#proxyString

Get the proxy set for RestClient

Returns:

  • (String)

    the proxy set for RestClient



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

def proxy
	RestClient.proxy
end

#proxy=(val) ⇒ Object

Set the proxy for RestClient

Parameters:

  • val (String)

    the proxy

Returns:

  • Nil



42
43
44
# File 'lib/fat_zebra/gateway.rb', line 42

def proxy=(val)
	RestClient.proxy = val
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



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

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



83
84
85
86
# File 'lib/fat_zebra/gateway.rb', line 83

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



121
122
123
124
# File 'lib/fat_zebra/gateway.rb', line 121

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



151
152
153
154
# File 'lib/fat_zebra/gateway.rb', line 151

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