class ActiveMerchant::Billing::XpayGateway

Constants

ENDPOINTS_MAPPING
SUCCESS_MESSAGES

Public Class Methods

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

Public Instance Methods

authorize(amount, credit_card, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 42
def authorize(amount, credit_card, options = {})
  complete_order_request(:authorize, amount, credit_card, options)
end
capture(amount, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 46
def capture(amount, authorization, options = {})
  operation_request(:capture, amount, authorization, options)
end
preauth(amount, credit_card, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 34
def preauth(amount, credit_card, options = {})
  order_request(:preauth, amount, {}, credit_card, options)
end
purchase(amount, credit_card, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 38
def purchase(amount, credit_card, options = {})
  complete_order_request(:purchase, amount, credit_card, options)
end
refund(amount, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 50
def refund(amount, authorization, options = {})
  operation_request(:refund, amount, authorization, options)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 66
def scrub(transcript)
  transcript.
    gsub(%r((X-Api-Key: )(\w|-)+), '\1[FILTERED]').
    gsub(%r(("pan\\?"\s*:\s*\\?")[^"]*)i, '\1[FILTERED]').
    gsub(%r(("cvv\\?":\\?")\d+), '\1[FILTERED]')
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 62
def supports_scrubbing?
  true
end
verify(credit_card, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 54
def verify(credit_card, options = {})
  post = {}
  add_invoice(post, 0, options)
  add_customer_data(post, credit_card, options)
  add_credit_card(post, credit_card)
  commit(:verify, post, options)
end

Private Instance Methods

add_3ds_params(post, validation) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 161
def add_3ds_params(post, validation)
  post[:threeDSAuthData] = {
    authenticationValue: validation['threeDSAuthResult']['authenticationValue'],
    eci: validation['threeDSAuthResult']['eci'],
    xid: validation['threeDSAuthResult']['xid']
  }
  post[:operationId] = validation['operation']['operationId']
end
add_3ds_validation_params(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 170
def add_3ds_validation_params(post, options)
  post[:operationId] = options[:operation_id]
  post[:threeDSAuthResponse] = options[:three_ds_auth_response]
end
add_address(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 129
def add_address(post, options)
  if address = options[:billing_address] || options[:address]
    post[:order][:customerInfo][:billingAddress] = {
      name: address[:name],
      street: address[:address1],
      additionalInfo: address[:address2],
      city: address[:city],
      postCode: address[:zip],
      country: address[:country]
    }.compact
  end

  if address = options[:shipping_address]
    post[:order][:customerInfo][:shippingAddress] = {
      name: address[:name],
      street: address[:address1],
      additionalInfo: address[:address2],
      city: address[:city],
      postCode: address[:zip],
      country: address[:country]
    }.compact
  end
end
add_credit_card(post, credit_card) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 114
def add_credit_card(post, credit_card)
  post[:card] = {
    pan: credit_card.number,
    expiryDate: expdate(credit_card),
    cvv: credit_card.verification_value
  }
end
add_customer_data(post, credit_card, options) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 122
def add_customer_data(post, credit_card, options)
  post[:order][:customerInfo] = {
    cardHolderName: credit_card.name,
    cardHolderEmail: options[:email]
  }.compact
end
add_exemptions(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 157
def add_exemptions(post, options)
  post[:exemptions] = options[:exemptions] || 'NO_PREFERENCE'
end
add_invoice(post, amount, options) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 105
def add_invoice(post, amount, options)
  currency = options[:currency] || currency(amount)
  post[:order] = {
    orderId: options[:order_id],
    amount: localized_amount(amount, currency),
    currency: currency
  }.compact
end
add_recurrence(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 153
def add_recurrence(post, options)
  post[:recurrence] = { action: options[:recurrence] || 'NO_RECURRING' }
end
authorization_from(correlation_id, response = {}) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 233
def authorization_from(correlation_id, response = {})
  [correlation_id, (response['operationId'] || response.dig('operation', 'operationId'))].join('#')
end
build_request_url(action, id = nil) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 216
def build_request_url(action, id = nil)
  "#{test? ? test_url : live_url}#{ENDPOINTS_MAPPING[action.to_sym] % id}"
end
commit(action, params, options) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 179
def commit(action, params, options)
  options[:correlation_id] ||= SecureRandom.uuid
  transaction_id = transaction_id_from(params, options, action)
  raw_response =
    begin
      url = build_request_url(action, transaction_id)
      ssl_post(url, params.to_json, request_headers(options, action))
    rescue ResponseError => e
      { errors: [code: e.response.code, description: e.response.body] }.to_json
    end
  response = parse(raw_response)

  Response.new(
    success_from(action, response),
    message_from(response),
    response,
    authorization: authorization_from(options[:correlation_id], response),
    test: test?,
    error_code: error_code_from(response)
  )
end
complete_order_request(action, amount, credit_card, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 81
def complete_order_request(action, amount, credit_card, options = {})
  MultiResponse.run do |r|
    r.process { validation(options) }
    r.process { order_request(action, amount, { captureType: (action == :authorize ? 'EXPLICIT' : 'IMPLICIT') }, credit_card, options.merge!(validation: r.params)) }
  end
end
error_code_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 237
def error_code_from(response)
  response.dig('errors', 0, 'code')
end
message_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 229
def message_from(response)
  response['operationId'] || response.dig('operation', 'operationResult') || response.dig('errors', 0, 'description')
end
operation_request(action, amount, authorization, options) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 100
def operation_request(action, amount, authorization, options)
  options[:correlation_id], options[:reference] = authorization.split('#')
  commit(action, { amount: amount, currency: options[:currency] }, options)
end
order_request(action, amount, post, credit_card, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 88
def order_request(action, amount, post, credit_card, options = {})
  add_invoice(post, amount, options)
  add_credit_card(post, credit_card)
  add_customer_data(post, credit_card, options)
  add_address(post, options)
  add_recurrence(post, options) unless options[:operation_id]
  add_exemptions(post, options)
  add_3ds_params(post, options[:validation]) if options[:validation]

  commit(action, post, options)
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 175
def parse(body)
  JSON.parse(body)
end
request_headers(options, action = nil) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 201
def request_headers(options, action = nil)
  headers = { 'X-Api-Key' => @api_key, 'Content-Type' => 'application/json', 'Correlation-Id' => options[:correlation_id] }
  headers.merge!('Idempotency-Key' => options[:idempotency_key] || SecureRandom.uuid) if %i[capture refund].include?(action)
  headers
end
success_from(action, response) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 220
def success_from(action, response)
  case action
  when :capture, :refund
    response.include?('operationId') && response.include?('operationTime')
  else
    SUCCESS_MESSAGES.include?(response.dig('operation', 'operationResult'))
  end
end
transaction_id_from(params, options, action = nil) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 207
def transaction_id_from(params, options, action = nil)
  case action
  when :refund, :capture
    return options[:reference]
  else
    return params[:operation_id]
  end
end
validation(options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/xpay.rb, line 75
def validation(options = {})
  post = {}
  add_3ds_validation_params(post, options)
  commit(:validation, post, options)
end