class ActiveMerchant::Billing::QuickbooksGateway
Constants
- ENDPOINT
- FRAUD_WARNING_CODES
- OAUTH_ENDPOINTS
- STANDARD_ERROR_CODE_MAPPING
developer.intuit.com/docs/0150_payments/0300_developer_guides/error_handling
Public Class Methods
new(options = {})
click to toggle source
Calls superclass method
ActiveMerchant::Billing::Gateway::new
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 53 def initialize(options = {}) requires!(options, :consumer_key, :consumer_secret, :access_token, :token_secret, :realm) @options = options super end
Public Instance Methods
capture(money, authorization, options = {})
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 77 def capture(money, authorization, options = {}) post = {} capture_uri = "#{ENDPOINT}/#{CGI.escape(authorization)}/capture" post[:amount] = localized_amount(money, currency(money)) commit(capture_uri, post) end
purchase(money, payment, options = {})
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 59 def purchase(money, payment, options = {}) post = {} add_amount(post, money, options) add_charge_data(post, payment, options) post[:capture] = "true" commit(ENDPOINT, post) end
refund(money, authorization, options = {})
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 85 def refund(money, authorization, options = {}) post = {} post[:amount] = localized_amount(money, currency(money)) commit(refund_uri(authorization), post) end
scrub(transcript)
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 100 def scrub(transcript) transcript. gsub(%r((realm=\")\w+), '\1[FILTERED]'). gsub(%r((oauth_consumer_key=\")\w+), '\1[FILTERED]'). gsub(%r((oauth_nonce=\")\w+), '\1[FILTERED]'). gsub(%r((oauth_signature=\")[a-zA-Z%0-9]+), '\1[FILTERED]'). gsub(%r((oauth_token=\")\w+), '\1[FILTERED]'). gsub(%r((number\D+)\d{16}), '\1[FILTERED]'). gsub(%r((cvc\D+)\d{3}), '\1[FILTERED]') end
supports_scrubbing?()
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 96 def supports_scrubbing? true end
verify(credit_card, options = {})
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 92 def verify(credit_card, options = {}) authorize(1.00, credit_card, options) end
Private Instance Methods
add_address(post, options)
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 118 def add_address(post, options) return unless post[:card] && post[:card].kind_of?(Hash) card_address = {} if address = options[:billing_address] || options[:address] card_address[:streetAddress] = address[:address1] card_address[:city] = address[:city] card_address[:region] = address[:state] || address[:region] card_address[:country] = address[:country] card_address[:postalCode] = address[:zip] if address[:zip] end post[:card][:address] = card_address end
add_amount(post, money, options = {})
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 132 def add_amount(post, money, options = {}) currency = options[:currency] || currency(money) post[:amount] = localized_amount(money, currency) post[:currency] = currency.upcase end
add_charge_data(post, payment, options = {})
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 113 def add_charge_data(post, payment, options = {}) add_payment(post, payment, options) add_address(post, options) end
add_creditcard(post, creditcard, options = {})
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 142 def add_creditcard(post, creditcard, options = {}) card = {} card[:number] = creditcard.number card[:expMonth] = "%02d" % creditcard.month card[:expYear] = creditcard.year card[:cvc] = creditcard.verification_value if creditcard.verification_value? card[:name] = creditcard.name if creditcard.name card[:commercialCardCode] = options[:card_code] if options[:card_code] post[:card] = card end
add_payment(post, payment, options = {})
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 138 def add_payment(post, payment, options = {}) add_creditcard(post, payment, options) end
commit(uri, body = {}, method = :post)
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 158 def commit(uri, body = {}, method = :post) endpoint = gateway_url + uri # The QuickBooks API returns HTTP 4xx on failed transactions, which causes a # ResponseError raise, so we have to inspect the response and discern between # a legitimate HTTP error and an actual gateway transactional error. response = begin case method when :post ssl_post(endpoint, post_data(body), headers(:post, endpoint)) when :get ssl_request(:get, endpoint, nil, headers(:get, endpoint)) else raise ArgumentError, "Invalid HTTP method: #{method}. Valid methods are :post and :get" end rescue ResponseError => e extract_response_body_or_raise(e) end response_object(response) end
cvv_code_from(response)
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 236 def cvv_code_from(response) if response['errors'].present? FRAUD_WARNING_CODES.include?(response['errors'].first['code']) ? 'I' : '' else success?(response) ? 'M' : '' end end
errors_from(response)
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 254 def errors_from(response) response['errors'].present? ? STANDARD_ERROR_CODE_MAPPING[response["errors"].first["code"]] : "" end
extract_response_body_or_raise(response_error)
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 266 def extract_response_body_or_raise(response_error) begin parse(response_error.response.body) rescue JSON::ParserError raise response_error end response_error.response.body end
fraud_review_status_from(response)
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 262 def fraud_review_status_from(response) response['errors'] && FRAUD_WARNING_CODES.include?(response['errors'].first['code']) end
gateway_url()
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 194 def gateway_url test? ? test_url : live_url end
headers(method, uri)
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 202 def headers(method, uri) raise ArgumentError, "Invalid HTTP method: #{method}. Valid methods are :post and :get" unless [:post, :get].include?(method) request_uri = URI.parse(uri) # Following the guidelines from http://nouncer.com/oauth/authentication.html oauth_parameters = { oauth_nonce: generate_unique_id, oauth_timestamp: Time.now.to_i.to_s, oauth_signature_method: 'HMAC-SHA1', oauth_version: "1.0", oauth_consumer_key: @options[:consumer_key], oauth_token: @options[:access_token] } # prepare components for signature oauth_signature_base_string = [method.to_s.upcase, request_uri.to_s, oauth_parameters.to_param].map{|v| CGI.escape(v) }.join('&') oauth_signing_key = [@options[:consumer_secret], @options[:token_secret]].map{|v| CGI.escape(v)}.join('&') hmac_signature = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha1'), oauth_signing_key, oauth_signature_base_string) # append signature to required OAuth parameters oauth_parameters[:oauth_signature] = CGI.escape(Base64.encode64(hmac_signature).chomp.gsub(/\n/, '')) # prepare Authorization header string oauth_parameters = Hash[oauth_parameters.sort_by {|k, _| k}] oauth_headers = ["OAuth realm=\"#{@options[:realm]}\""] oauth_headers += oauth_parameters.map {|k, v| "#{k}=\"#{v}\""} { "Content-type" => "application/json", "Request-Id" => generate_unique_id, "Authorization" => oauth_headers.join(', ') } end
message_from(response)
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 250 def message_from(response) response['errors'].present? ? response["errors"].map {|error_hash| error_hash["message"] }.join(" ") : response['status'] end
parse(body)
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 154 def parse(body) JSON.parse(body) end
post_data(data = {})
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 198 def post_data(data = {}) data.to_json end
refund_uri(authorization)
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 275 def refund_uri(authorization) "#{ENDPOINT}/#{CGI.escape(authorization)}/refunds" end
response_object(raw_response)
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 179 def response_object(raw_response) parsed_response = parse(raw_response) Response.new( success?(parsed_response), message_from(parsed_response), parsed_response, authorization: authorization_from(parsed_response), test: test?, cvv_result: cvv_code_from(parsed_response), error_code: errors_from(parsed_response), fraud_review: fraud_review_status_from(parsed_response) ) end
success?(response)
click to toggle source
# File lib/active_merchant/billing/gateways/quickbooks.rb, line 244 def success?(response) return FRAUD_WARNING_CODES.concat(['0']).include?(response['errors'].first['code']) if response['errors'] !['DECLINED', 'CANCELLED'].include?(response['status']) end