lib/active_merchant/billing/gateways/fat_zebra.rb in activemerchant-1.35.1 vs lib/active_merchant/billing/gateways/fat_zebra.rb in activemerchant-1.36.0

- old
+ new

@@ -11,31 +11,30 @@ self.money_format = :cents self.supported_cardtypes = [:visa, :master, :american_express, :jcb] self.homepage_url = 'https://www.fatzebra.com.au/' self.display_name = 'Fat Zebra' - + # Setup a new instance of the gateway. # # The options hash should include :username and :token # You can find your username and token at https://dashboard.fatzebra.com.au # Under the Your Account section def initialize(options = {}) - requires!(options, :username) - requires!(options, :token) + requires!(options, :username, :token) @username = options[:username] @token = options[:token] super end # To create a purchase on a credit card use: # - # purchase(money, creditcard , { ... }) + # purchase(money, creditcard) # # To charge a tokenized card # - # purchase(money, {:token => "abzy87u", :cvv => "123"}, { ... }}) + # purchase(money, "abzy87u", :cvv => "123") def purchase(money, creditcard, options = {}) post = {} add_amount(post, money, options) add_creditcard(post, creditcard, options) @@ -59,18 +58,21 @@ commit(:post, "refunds", post) end # Tokenize a credit card + # + # The token is returned in the Response#authorization def store(creditcard) post = {} add_creditcard(post, creditcard) commit(:post, "credit_cards", post) end private + # Add the money details to the request def add_amount(post, money, options) post[:amount] = money end @@ -79,46 +81,65 @@ 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) + 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 - post[:card_token] = creditcard[:token] - post[:cvv] = creditcard[:cvv] + raise ArgumentError.new("Unknown credit card format #{creditcard.inspect}") end end # Post the data to the gateway def commit(method, uri, parameters=nil) - raw_response = response = nil - success = false - begin - raw_response = ssl_request(method, get_url(uri), parameters.to_json, headers) - response = parse(raw_response) - success = response["successful"] && (response["response"]["successful"] || response["response"]["token"]) + response = begin + parse(ssl_request(method, get_url(uri), parameters.to_json, headers)) rescue ResponseError => e - if e.response.code == "401" - return Response.new(false, "Invalid Login") - end - - raw_response = e.response.body - response = parse(raw_response) - rescue JSON::ParserError - response = json_error(raw_response) + return Response.new(false, "Invalid Login") if(e.response.code == "401") + parse(e.response.body) end - message = response["response"]["message"] - unless response["successful"] - # There is an error, so we will show that instead - message = response["errors"].empty? ? "Unknown Error" : response["errors"].join(", ") + success = success_from(response) + Response.new( + success, + message_from(response), + response, + :test => response["test"], + :authorization => authorization_from(response, success) + ) + end + + def success_from(response) + ( + response["successful"] && + response["response"] && + (response["response"]["successful"] || response["response"]["token"]) + ) + end + + def authorization_from(response, success) + if success + (response["response"]["id"] || response["response"]["token"]) + else + nil end + end - Response.new(success, - message, - response, - :test => response.has_key?("test") ? response["test"] : false, - :authorization => response["response"]["id"] || response["response"]["token"]) + 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. def parse(response) begin @@ -144,9 +165,9 @@ def headers { "Authorization" => "Basic " + Base64.strict_encode64(@username.to_s + ":" + @token.to_s).strip, "User-Agent" => "Fat Zebra v1.0/ActiveMerchant #{ActiveMerchant::VERSION}" } - end + end end end end