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.
-
#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
26 27 28 29 30 31 32 33 |
# File 'lib/fat_zebra/gateway.rb', line 26 def initialize(username, token, gateway_server = GATEWAY_SERVER, = {}) self.username = username self.token = token self.gateway_server = gateway_server self. = DEFAULT_OPTIONS.merge() 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 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 |
# 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 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 (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 method == :post 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
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 [: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
65 66 67 68 |
# File 'lib/fat_zebra/gateway.rb', line 65 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
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 [: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
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 [:silence] Models::Card.create(card_holder, card_number, expiry, cvv) end |