Class: FatZebra::Gateway
- Inherits:
-
Object
- Object
- FatZebra::Gateway
- Defined in:
- lib/fat_zebra/gateway.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{:secure => true, :version => API_VERSION}
Instance Attribute Summary collapse
-
#gateway_server ⇒ Object
Returns the value of attribute gateway_server.
-
#options ⇒ Object
Returns the value of attribute options.
-
#token ⇒ Object
Returns the value of attribute token.
-
#username ⇒ Object
Returns the value of attribute username.
Class Method Summary collapse
Instance Method Summary collapse
-
#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.
-
#capture(transaction_id, amount) ⇒ Object
Public: Captures a pre-authorized transaction.
-
#initialize(username, token, gateway_server = GATEWAY_SERVER, options = {}) ⇒ Object
constructor
Initializes a new gateway object.
-
#make_request(method, resource, data = nil) ⇒ Object
Public: Performs the HTTP(s) request and returns a response object, handing errors etc.
-
#ping(nonce = SecureRandom.hex) ⇒ Object
Pings the Fat Zebra service.
-
#proxy ⇒ String
Get the proxy set for RestClient.
-
#proxy=(val) ⇒ Object
Set the proxy for RestClient.
-
#purchase(amount, card_data, reference, customer_ip, currency = "AUD") ⇒ Object
Performs a purchase transaction against the gateway.
-
#purchases(options = {}) ⇒ Array<Purchase>
deprecated
Deprecated.
Please use Purchase.find(options) instead
-
#refund(transaction_id, amount, reference) ⇒ Refund
deprecated
Deprecated.
Please use Refund.create or Purchase#refund instead
-
#tokenize(card_holder, card_number, expiry, cvv) ⇒ Object
deprecated
Deprecated.
Please use Card.create instead
Constructor Details
#initialize(username, token, gateway_server = GATEWAY_SERVER, options = {}) ⇒ Object
Initializes a new gateway object
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, = {}) self.username = username self.token = token self.gateway_server = gateway_server self. = DEFAULT_OPTIONS.merge() self.proxy = self.[:proxy] require_field :username, :token, :gateway_server end |
Instance Attribute Details
#gateway_server ⇒ Object
Returns the value of attribute gateway_server.
3 4 5 |
# File 'lib/fat_zebra/gateway.rb', line 3 def gateway_server @gateway_server end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/fat_zebra/gateway.rb', line 3 def @options end |
#token ⇒ Object
Returns the value of attribute token.
3 4 5 |
# File 'lib/fat_zebra/gateway.rb', line 3 def token @token end |
#username ⇒ Object
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. ||= {} g.[:test_mode] = config.test_mode g..merge!(config. || {}) g.proxy = g.[: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 (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] = [:test_mode] if [:test_mode] payload = data.to_json else payload = {} end headers = [: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
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 |
#proxy ⇒ String
Get 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
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 [:silence] Models::Purchase.create(amount, card_data, reference, customer_ip, currency) end |
#purchases(options = {}) ⇒ Array<Purchase>
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
83 84 85 86 |
# File 'lib/fat_zebra/gateway.rb', line 83 def purchases( = {}) warn "[DEPRECATED] Gateway#purchases is deprecated, please use Purchase.find instead" unless [:silence] Models::Purchase.find() end |
#refund(transaction_id, amount, reference) ⇒ Refund
Please use Refund.create or Purchase#refund instead
Refunds a transaction
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 [:silence] Models::Refund.create(transaction_id, amount, reference) end |
#tokenize(card_holder, card_number, expiry, cvv) ⇒ Object
Please use Card.create instead
Tokenizes a credit card
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 [:silence] Models::Card.create(card_holder, card_number, expiry, cvv) end |