class ActiveMerchant::Billing::FlexPayGateway

Constants

STANDARD_ERROR_CODE_MAPPING

Public Class Methods

new(options={}) click to toggle source
Calls superclass method
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 34
def initialize(options={})
  requires!(options, :api_key)
  super
end

Public Instance Methods

authorize(money, payment, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 49
def authorize(money, payment, options={})
  post = {}
  add_invoice(post, money, options)
  add_payment(post, payment, options)
  add_address(post, payment, options)
  add_customer_data(post, options)

  commit('gateways/authorize', post)
end
capture(money, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 59
def capture(money, authorization, options={})
  post = {}
  post[:amount] = amount(money)
  post[:merchantTransactionId] = generate_unique_id
  commit("transactions/#{authorization}/capture", post)
end
purchase(money, payment, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 39
def purchase(money, payment, options={})
  post = {}
  add_invoice(post, money, options)
  add_payment(post, payment, options)
  add_address(post, payment, options)
  add_customer_data(post, options)

  commit('gateways/charge', post)
end
refund(money, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 66
def refund(money, authorization, options={})
  post = {}
  post[:amount] = amount(money)
  post[:merchantTransactionId] = generate_unique_id
  commit("transactions/#{authorization}/refund", post)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 90
def scrub(transcript)
  transcript.
    gsub(%r((Authorization: Basic )\w+), '\1[FILTERED]').
    gsub(%r(("creditCardNumber\\?":\\?")[^"]*)i, '\1[FILTERED]').
    gsub(%r(("cvv\\?":\\?")[^"]*)i, '\1[FILTERED]')
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 86
def supports_scrubbing?
  true
end
verify(credit_card, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 79
def verify(credit_card, options={})
  MultiResponse.run(:use_first_response) do |r|
    r.process { authorize(100, credit_card, options) }
    r.process(:ignore_result) { void(r.authorization, options) }
  end
end
void(authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 73
def void(authorization, options={})
  post = {}
  post[:merchantTransactionId] = generate_unique_id
  commit("transactions/#{authorization}/void", post)
end

Private Instance Methods

add_address(post, creditcard, options) click to toggle source
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 107
def add_address(post, creditcard, options)
  address = options[:billing_address] || options[:address] || {}
  post[:paymentMethod][:address1] = address[:address1] if address[:address1].present?
  post[:paymentMethod][:address2] = address[:address2] if address[:address2].present?
  post[:paymentMethod][:postalCode] = address[:zip]
  post[:paymentMethod][:city] = address[:city]
  post[:paymentMethod][:state] = address[:state]
  post[:paymentMethod][:country] = address[:country] if address[:country]
end
add_customer_data(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 99
def add_customer_data(post, options)
  post[:customerId] = options[:customer_id] if options[:customer_id].present?
  post[:paymentMethod][:email] = options[:email] if options[:email].present?
  post[:customerId] ||= generate_unique_id if options[:email].blank?

  add_shipping_address(post, options[:shipping_address]) if options[:shipping_address]
end
add_invoice(post, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 127
def add_invoice(post, money, options)
  post[:merchantTransactionId] = generate_unique_id
  post[:orderId] = options[:order_id] || post[:merchantTransactionId]
  post[:amount] = amount(money)
  post[:currencyCode] = (options[:currency] || currency(money))
  post[:retryCount] = options[:retry_count] || 0
  post[:referenceData] = options[:reference_data] if options[:reference_data]
end
add_payment(post, payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 136
def add_payment(post, payment, options)
  post[:paymentMethod] = {
    creditCardNumber: payment.number,
    expiryMonth: '%02d' % payment.month,
    expiryYear: payment.year,
    cvv: payment.verification_value,
    fullName: payment.name
  }
  post[:retainOnSuccess] = options[:store] ? true : false
end
add_shipping_address(post, address) click to toggle source
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 117
def add_shipping_address(post, address)
  post[:shippingAddress] = {}
  post[:shippingAddress][:address1] = address[:address1] if address[:address1].present?
  post[:shippingAddress][:address2] = address[:address2] if address[:address2].present?
  post[:shippingAddress][:postalCode] = address[:zip]
  post[:shippingAddress][:city] = address[:city]
  post[:shippingAddress][:state] = address[:state]
  post[:shippingAddress][:country] = address[:country] if address[:country]
end
authorization_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 200
def authorization_from(response)
  response['transactionId']
end
commit(uri, parameters) click to toggle source
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 164
def commit(uri, parameters)
  url = (test? ? test_url : live_url)
  headers = headers(@options[:api_key])
  response = parse(ssl_post(get_url(uri), post_data(parameters), headers))

  Response.new(
    success_from(response),
    message_from(response),
    response,
    authorization: authorization_from(response),
    avs_result: AVSResult.new(code: response['response']['avsCode']),
    cvv_result: CVVResult.new(response['response']['cvvCode']),
    test: test?,
    error_code: error_code_from(response)
  )
rescue ResponseError => e
  case e.response.code
  when '401', '405'
    return Response.new(false, e.response.message, {}, :test => test?)
  end
  raise
end
error_code_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 208
def error_code_from(response)
  unless success_from(response)
    STANDARD_ERROR_CODE_MAPPING[response['responseCode']]
  end
end
get_url(uri) click to toggle source
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 187
def get_url(uri)
  url = (test? ? test_url : live_url)
  "#{url}/#{uri}"
end
headers(api_key) click to toggle source
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 147
def headers(api_key)
  {
    'Authorization' => "Basic #{api_key}",
    'Content-Type' => 'application/json',
    'User-Agent'    => "ActiveMerchant::FlexPay/#{ActiveMerchant::FlexPay::VERSION}"
  }
end
message_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 196
def message_from(response)
  response['message']
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 155
def parse(body)
  return {} if body.blank?
  JSON.parse(body).fetch('transaction')
rescue JSON::ParserError
  message = 'Unparsable response received from FlexPay. Please contact FlexPay if you continue to receive this message.'
  message += " (The raw response returned by the API was #{body.inspect})"
  { 'message' => message }
end
post_data(parameters = {}) click to toggle source
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 204
def post_data(parameters = {})
  JSON.generate(transaction: parameters)
end
success_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/flex_pay.rb, line 192
def success_from(response)
  response['responseCode'] == '10000'
end