class ActiveMerchant::Billing::DigitzsGateway

Public Class Methods

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

Public Instance Methods

purchase(money, payment, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 22
def purchase(money, payment, options={})
  MultiResponse.run do |r|
    r.process { commit('auth/token', app_token_request(options)) }
    r.process { commit('payments', purchase_request(money, payment, options), options.merge({ app_token: app_token_from(r) })) }
  end
end
refund(money, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 29
def refund(money, authorization, options={})
  MultiResponse.run do |r|
    r.process { commit('auth/token', app_token_request(options)) }
    r.process { commit('payments', refund_request(money, authorization, options), options.merge({ app_token: app_token_from(r) })) }
  end
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 59
def scrub(transcript)
  transcript.
    gsub(%r((Authorization: Bearer ).+), '\1[FILTERED]').
    gsub(%r((X-Api-Key: )\w+), '\1[FILTERED]').
    gsub(%r((\"id\\\":\\\").+), '\1[FILTERED]').
    gsub(%r((\"appKey\\\":\\\").+), '\1[FILTERED]').
    gsub(%r((\"appToken\\\":\\\").+), '\1[FILTERED]').
    gsub(%r((\"code\\\":\\\")\d+), '\1[FILTERED]').
    gsub(%r((\"number\\\":\\\")\d+), '\1[FILTERED]')
end
store(payment, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 36
def store(payment, options = {})
  MultiResponse.run do |r|
    r.process { commit('auth/token', app_token_request(options)) }
    options[:app_token] = app_token_from(r)

    if options[:customer_id].present?
      customer_id = check_customer_exists(options)

      if customer_id
        r.process { add_credit_card_to_customer(payment, options) }
      else
        r.process { add_customer_with_credit_card(payment, options) }
      end
    else
      r.process { add_customer_with_credit_card(payment, options) }
    end
  end
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 55
def supports_scrubbing?
  true
end

Private Instance Methods

add_address(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 115
def add_address(post, options)
  if address = options[:billing_address] || options[:address]
    post[:data][:attributes][:billingAddress] = {
      line1: address[:address1] || '',
      line2: address[:address2] || '',
      city: address[:city] || '',
      state: address[:state] || '',
      zip: address[:zip] || '',
      country: address['country'] || 'USA'
    }
  end
end
add_credit_card_to_customer(payment, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 194
def add_credit_card_to_customer(payment, options = {})
  commit('tokens', create_token_request(payment, options), options)
end
add_customer_with_credit_card(payment, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 198
def add_customer_with_credit_card(payment, options = {})
  customer_response = commit('customers', create_customer_request(payment, options), options)
  options[:customer_id] = customer_response.authorization
  commit('tokens', create_token_request(payment, options), options)
end
add_payment(post, payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 89
def add_payment(post, payment, options)
  if payment.is_a? String
    customer_id, token = split_authorization(payment)
    post[:data][:attributes][:token] = {
      customerId: customer_id,
      tokenId: token
    }
  else
    post[:data][:attributes][:card] = {
      type: payment.brand,
      holder: payment.name,
      number: payment.number,
      expiry: expdate(payment),
      code: payment.verification_value
    }
  end
end
add_split(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 80
def add_split(post, options)
  return unless options[:payment_type] == 'card_split' || options[:payment_type] == 'token_split'

  post[:data][:attributes][:split] = {
    merchantId: options[:split_merchant_id],
    amount: amount(options[:split_amount])
  }
end
add_transaction(post, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 107
def add_transaction(post, money, options)
  post[:data][:attributes][:transaction] = {
    amount: amount(money),
    currency: (options[:currency] || currency(money)),
    invoice: options[:order_id] || generate_unique_id
  }
end
app_token_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 250
def app_token_from(response)
  response.params.try(:[], 'data').try(:[], 'attributes').try(:[], 'appToken')
end
app_token_request(options) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 128
def app_token_request(options)
  post = new_post
  post[:data][:type] = 'auth'
  post[:data][:attributes] = { appKey: @options[:app_key] }

  post
end
authorization_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 234
def authorization_from(response)
  if customer_id = response.try(:[], 'data').try(:[], 'attributes').try(:[], 'customerId')
    "#{customer_id}|#{response.try(:[], "data").try(:[], "id")}"
  else
    response.try(:[], 'data').try(:[], 'id')
  end
end
avs_result_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 242
def avs_result_from(response)
  response.try(:[], 'data').try(:[], 'attributes').try(:[], 'transaction').try(:[], 'avsResult')
end
check_customer_exists(options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 186
def check_customer_exists(options = {})
  url = (test? ? test_url : live_url)
  response = parse(ssl_get(url + "/customers/#{options[:customer_id]}", headers(options)))

  return response.try(:[], 'data').try(:[], 'customerId') if success_from(response)
  return nil
end
commit(action, parameters, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 208
def commit(action, parameters, options={})
  url = (test? ? test_url : live_url)
  response = parse(ssl_post(url + "/#{action}", parameters.to_json, headers(options)))

  Response.new(
    success_from(response),
    message_from(response),
    response,
    authorization: authorization_from(response),
    avs_result: AVSResult.new(code: avs_result_from(response)),
    cvv_result: CVVResult.new(cvv_result_from(response)),
    test: test?,
    error_code: error_code_from(response)
  )
end
create_customer_request(payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 160
def create_customer_request(payment, options)
  post = new_post
  post[:data][:type] = 'customers'
  post[:data][:attributes] = {
    merchantId: options[:merchant_id],
    name: payment.name,
    externalId: SecureRandom.hex(16)
  }

  post
end
create_token_request(payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 172
def create_token_request(payment, options)
  post = new_post
  post[:data][:type] = 'tokens'
  post[:data][:attributes] = {
    tokenType: 'card',
    customerId: options[:customer_id],
    label: 'Credit Card',
  }
  add_payment(post, payment, options)
  add_address(post, options)

  post
end
cvv_result_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 246
def cvv_result_from(response)
  response.try(:[], 'data').try(:[], 'attributes').try(:[], 'transaction').try(:[], 'codeResult')
end
determine_payment_type(payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 275
def determine_payment_type(payment, options)
  return 'cardSplit' if options[:payment_type] == 'card_split'
  return 'tokenSplit' if options[:payment_type] == 'token_split'
  return 'token' if payment.is_a? String
  'card'
end
error_code_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 264
def error_code_from(response)
  unless success_from(response)
    response['errors'].nil? ? response['message'] : response['errors'].map { |error_hash| error_hash['code'] }.join(', ')
  end
end
handle_response(response) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 282
def handle_response(response)
  case response.code.to_i
  when 200..499
    response.body
  else
    raise ResponseError.new(response)
  end
end
headers(options) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 254
def headers(options)
  headers = {
    'Content-Type' => 'application/json',
    'x-api-key' => @options[:api_key]
  }

  headers['Authorization'] = "Bearer #{options[:app_token]}" if options[:app_token]
  headers
end
message_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 228
def message_from(response)
  return response['message'] if response['message']
  return 'Success' if success_from(response)
  response['errors'].map { |error_hash| error_hash['detail'] }.join(', ')
end
new_post() click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 72
def new_post
  {
    data: {
      attributes: {}
    }
  }
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 204
def parse(body)
  JSON.parse(body)
end
purchase_request(money, payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 136
def purchase_request(money, payment, options)
  post = new_post
  post[:data][:type] = 'payments'
  post[:data][:attributes][:merchantId] = options[:merchant_id]
  post[:data][:attributes][:paymentType] = determine_payment_type(payment, options)
  add_split(post, options)
  add_payment(post, payment, options)
  add_transaction(post, money, options)
  add_address(post, options)

  post
end
refund_request(money, authorization, options) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 149
def refund_request(money, authorization, options)
  post = new_post
  post[:data][:type] = 'payments'
  post[:data][:attributes][:merchantId] = options[:merchant_id]
  post[:data][:attributes][:paymentType] = 'cardRefund'
  post[:data][:attributes][:originalTransaction] = {id: authorization}
  add_transaction(post, money, options)

  post
end
split_authorization(authorization) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 270
def split_authorization(authorization)
  customer_id, token = authorization.split('|')
  [customer_id, token]
end
success_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/digitzs.rb, line 224
def success_from(response)
  response['errors'].nil? && response['message'].nil?
end