class ActiveMerchant::Billing::FirstPayJsonGateway

Constants

ACTIONS
WALLET_TYPES

Public Class Methods

new(options = {}) click to toggle source

Creates a new FirstPayJsonGateway

The gateway requires two values for connection to be passed in the options hash.

Options

  • :merchant_key – FirstPay’s merchant_key (REQUIRED)

  • :processor_id – FirstPay’s processor_id or processorId (REQUIRED)

Calls superclass method ActiveMerchant::Billing::Gateway::new
# File lib/active_merchant/billing/gateways/first_pay/first_pay_json.rb, line 33
def initialize(options = {})
  requires!(options, :merchant_key, :processor_id)
  super
end

Public Instance Methods

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

  commit(:authorize, post)
end
capture(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/first_pay/first_pay_json.rb, line 56
def capture(money, authorization, options = {})
  post = {}
  add_invoice(post, money, options)
  add_reference(post, authorization)

  commit(:capture, post)
end
purchase(money, payment, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/first_pay/first_pay_json.rb, line 38
def purchase(money, payment, options = {})
  post = {}
  add_invoice(post, money, options)
  add_payment(post, payment, options)
  add_address(post, payment, options)

  commit(:purchase, post)
end
refund(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/first_pay/first_pay_json.rb, line 64
def refund(money, authorization, options = {})
  post = {}
  add_invoice(post, money, options)
  add_reference(post, authorization)

  commit(:refund, post)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/first_pay/first_pay_json.rb, line 79
def scrub(transcript)
  transcript.
    gsub(%r(("processorId\\?"\s*:\s*\\?")[^"]*)i, '\1[FILTERED]').
    gsub(%r(("merchantKey\\?"\s*:\s*\\?")[^"]*)i, '\1[FILTERED]').
    gsub(%r(("cardNumber\\?"\s*:\s*\\?")[^"]*)i, '\1[FILTERED]').
    gsub(%r(("paymentCryptogram\\?"\s*:\s*\\?")[^"]*)i, '\1[FILTERED]').
    gsub(%r(("cvv\\?"\s*:\s*\\?)[^,]*)i, '\1[FILTERED]')
end
void(authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/first_pay/first_pay_json.rb, line 72
def void(authorization, options = {})
  post = {}
  add_reference(post, authorization)

  commit(:void, post)
end

Private Instance Methods

add_address(post, creditcard, options) click to toggle source
# File lib/active_merchant/billing/gateways/first_pay/first_pay_json.rb, line 90
def add_address(post, creditcard, options)
  if address = options[:billing_address] || options[:address]
    post[:ownerName] = address[:name]
    post[:ownerStreet] = address[:address1]
    post[:ownerCity] = address[:city]
    post[:ownerState] = address[:state]
    post[:ownerZip] = address[:zip]
    post[:ownerCountry] = address[:country]
  end
end
add_invoice(post, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/first_pay/first_pay_json.rb, line 101
def add_invoice(post, money, options)
  post[:orderId] = options[:order_id]
  post[:transactionAmount] = amount(money)
end
add_payment(post, payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/first_pay/first_pay_json.rb, line 106
def add_payment(post, payment, options)
  post[:cardNumber] = payment.number
  post[:cardExpMonth] = payment.month
  post[:cardExpYear] = format(payment.year, :two_digits)
  post[:cvv] = payment.verification_value
  post[:recurring] = options[:recurring] if options[:recurring]
  post[:recurringStartDate] = options[:recurring_start_date] if options[:recurring_start_date]
  post[:recurringEndDate] = options[:recurring_end_date] if options[:recurring_end_date]

  case payment
  when NetworkTokenizationCreditCard
    post[:walletType] = WALLET_TYPES[payment.source]
    other_fields = post[:otherFields] = {}
    other_fields[:paymentCryptogram] = payment.payment_cryptogram
    other_fields[:eciIndicator] = payment.eci || '07'
  when CreditCard
    post[:cvv] = payment.verification_value
  end
end
add_reference(post, authorization) click to toggle source
# File lib/active_merchant/billing/gateways/first_pay/first_pay_json.rb, line 126
def add_reference(post, authorization)
  post[:refNumber] = authorization
end
api_request(url, data) click to toggle source
# File lib/active_merchant/billing/gateways/first_pay/first_pay_json.rb, line 147
def api_request(url, data)
  ssl_post(url, data, headers)
rescue ResponseError => e
  e.response.body
end
authorization_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/first_pay/first_pay_json.rb, line 181
def authorization_from(response)
  response.dig('data', 'referenceNumber') || ''
end
base_url() click to toggle source
# File lib/active_merchant/billing/gateways/first_pay/first_pay_json.rb, line 143
def base_url
  test? ? self.test_url : self.live_url
end
commit(action, parameters) click to toggle source
# File lib/active_merchant/billing/gateways/first_pay/first_pay_json.rb, line 130
def commit(action, parameters)
  response = parse(api_request(base_url + ACTIONS[action], post_data(parameters)))

  Response.new(
    success_from(response),
    message_from(response),
    response,
    authorization: authorization_from(response),
    error_code: error_code_from(response),
    test: test?
  )
end
error_code_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/first_pay/first_pay_json.rb, line 175
def error_code_from(response)
  return 'isError' if response['isError']

  return 'validationHasFailed' if response['validationHasFailed']
end
format_messages(messages) click to toggle source
# File lib/active_merchant/billing/gateways/first_pay/first_pay_json.rb, line 161
def format_messages(messages)
  return unless messages.present?

  messages.map { |message| message['message'] || message }.join('; ')
end
headers() click to toggle source
# File lib/active_merchant/billing/gateways/first_pay/first_pay_json.rb, line 157
def headers
  { 'Content-Type' => 'application/json' }
end
message_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/first_pay/first_pay_json.rb, line 171
def message_from(response)
  format_messages(response['errorMessages'] + response['validationFailures']) || response['data']['authResponse']
end
parse(data) click to toggle source
# File lib/active_merchant/billing/gateways/first_pay/first_pay_json.rb, line 153
def parse(data)
  JSON.parse data
end
post_data(params) click to toggle source
# File lib/active_merchant/billing/gateways/first_pay/first_pay_json.rb, line 185
def post_data(params)
  params.merge({ processorId: @options[:processor_id], merchantKey: @options[:merchant_key] }).to_json
end
success_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/first_pay/first_pay_json.rb, line 167
def success_from(response)
  response['isSuccess']
end