class ActiveMerchant::Billing::FatZebraGateway
Public Class Methods
Setup a new instance of the gateway.
The options hash should include :username and :token You can find your username and token at dashboard.fatzebra.com.au Under the Your Account section
ActiveMerchant::Billing::Gateway::new
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 22 def initialize(options = {}) requires!(options, :username, :token) @username = options[:username] @token = options[:token] super end
Public Instance Methods
To create a purchase on a credit card use:
purchase(money, creditcard)
To charge a tokenized card
purchase(money, "abzy87u", :cvv => "123")
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 36 def purchase(money, creditcard, options = {}) post = {} add_amount(post, money, options) add_creditcard(post, creditcard, options) post[:reference] = options[:order_id] post[:customer_ip] = options[:ip] commit(:post, 'purchases', post) end
Refund a transaction
amount - Integer - the amount to refund txn_id - String - the original transaction to be refunded reference - String - your transaction reference
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 52 def refund(money, txn_id, reference) post = {} post[:amount] = money post[:transaction_id] = txn_id post[:reference] = reference commit(:post, "refunds", post) end
Tokenize a credit card
The token is returned in the Response#authorization
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 65 def store(creditcard) post = {} add_creditcard(post, creditcard) commit(:post, "credit_cards", post) end
Private Instance Methods
Add the money details to the request
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 75 def add_amount(post, money, options) post[:amount] = money end
Add the credit card details to the request
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 80 def add_creditcard(post, creditcard, options = {}) if creditcard.respond_to?(:number) post[:card_number] = creditcard.number post[:card_expiry] = "#{creditcard.month}/#{creditcard.year}" post[:cvv] = creditcard.verification_value if creditcard.verification_value? post[:card_holder] = creditcard.name if creditcard.name elsif creditcard.is_a?(String) post[:card_token] = creditcard post[:cvv] = options[:cvv] elsif creditcard.is_a?(Hash) ActiveMerchant.deprecated "Passing the credit card as a Hash is deprecated. Use a String and put the (optional) CVV in the options hash instead." post[:card_token] = creditcard[:token] post[:cvv] = creditcard[:cvv] else raise ArgumentError.new("Unknown credit card format #{creditcard.inspect}") end end
Post the data to the gateway
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 99 def commit(method, uri, parameters=nil) response = begin parse(ssl_request(method, get_url(uri), parameters.to_json, headers)) rescue ResponseError => e return Response.new(false, "Invalid Login") if(e.response.code == "401") parse(e.response.body) end success = success_from(response) Response.new( success, message_from(response), response, :test => response["test"], :authorization => authorization_from(response, success) ) end
Build the URL based on the AM mode and the URI
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 159 def get_url(uri) base = test? ? self.test_url : self.live_url base + "/" + uri end
Builds the auth and U-A headers for the request
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 165 def headers { "Authorization" => "Basic " + Base64.strict_encode64(@username.to_s + ":" + @token.to_s).strip, "User-Agent" => "Fat Zebra v1.0/ActiveMerchant #{ActiveMerchant::VERSION}" } end
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 133 def message_from(response) if !response["errors"].empty? response["errors"].join(", ") elsif response["response"]["message"] response["response"]["message"] else "Unknown Error" end end
Parse the returned JSON, if parse errors are raised then return a detailed error.
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 144 def parse(response) begin JSON.parse(response) rescue JSON::ParserError msg = 'Invalid JSON response received from Fat Zebra. Please contact support@fatzebra.com.au if you continue to receive this message.' msg += " (The raw response returned by the API was #{response.inspect})" { "successful" => false, "response" => {}, "errors" => [msg] } end end
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 117 def success_from(response) ( response["successful"] && response["response"] && (response["response"]["successful"] || response["response"]["token"]) ) end