class ActiveMerchant::Billing::MokaGateway

Constants

ERROR_CODE_MAPPING

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method ActiveMerchant::Billing::Gateway::new
# File lib/active_merchant/billing/gateways/moka.rb, line 48
def initialize(options = {})
  requires!(options, :dealer_code, :username, :password)
  super
end

Public Instance Methods

authorize(money, payment, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 64
def authorize(money, payment, options = {})
  post = {}
  post[:PaymentDealerRequest] = {}
  options[:pre_auth] = 1
  add_auth_purchase(post, money, payment, options)
  add_3ds_data(post, options) if options[:execute_threed]

  action = options[:execute_threed] ? 'three_ds_authorize' : 'authorize'
  commit(action, post)
end
capture(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 75
def capture(money, authorization, options = {})
  post = {}
  post[:PaymentDealerRequest] = {}
  add_payment_dealer_authentication(post)
  add_transaction_reference(post, authorization)
  add_invoice(post, money, options)

  commit('capture', post)
end
purchase(money, payment, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 53
def purchase(money, payment, options = {})
  post = {}
  post[:PaymentDealerRequest] = {}
  options[:pre_auth] = 0
  add_auth_purchase(post, money, payment, options)
  add_3ds_data(post, options) if options[:execute_threed]

  action = options[:execute_threed] ? 'three_ds_purchase' : 'purchase'
  commit(action, post)
end
refund(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 85
def refund(money, authorization, options = {})
  post = {}
  post[:PaymentDealerRequest] = {}
  add_payment_dealer_authentication(post)
  add_transaction_reference(post, authorization)
  add_void_refund_reason(post)
  add_amount(post, money)

  commit('refund', post)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 117
def scrub(transcript)
  transcript.
    gsub(%r(("CardNumber\\?":\\?")[^"]*)i, '\1[FILTERED]').
    gsub(%r(("CvcNumber\\?":\\?")[^"]*)i, '\1[FILTERED]').
    gsub(%r(("DealerCode\\?":\\?"?)[^"?]*)i, '\1[FILTERED]').
    gsub(%r(("Username\\?":\\?")[^"]*)i, '\1[FILTERED]').
    gsub(%r(("Password\\?":\\?")[^"]*)i, '\1[FILTERED]').
    gsub(%r(("CheckKey\\?":\\?")[^"]*)i, '\1[FILTERED]')
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 113
def supports_scrubbing?
  true
end
verify(credit_card, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 106
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/moka.rb, line 96
def void(authorization, options = {})
  post = {}
  post[:PaymentDealerRequest] = {}
  add_payment_dealer_authentication(post)
  add_transaction_reference(post, authorization)
  add_void_refund_reason(post)

  commit('void', post)
end

Private Instance Methods

add_3ds_data(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 139
def add_3ds_data(post, options)
  post[:PaymentDealerRequest][:ReturnHash] = 1
  post[:PaymentDealerRequest][:RedirectUrl] = options[:redirect_url] || ''
  post[:PaymentDealerRequest][:RedirectType] = options[:redirect_type] || 0
end
add_additional_auth_purchase_data(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 176
def add_additional_auth_purchase_data(post, options)
  post[:PaymentDealerRequest][:IsPreAuth] = options[:pre_auth]
  post[:PaymentDealerRequest][:Description] = options[:description] if options[:description]
  post[:PaymentDealerRequest][:InstallmentNumber] = options[:installment_number].to_i if options[:installment_number]
  post[:SubMerchantName] = options[:sub_merchant_name] if options[:sub_merchant_name]
  post[:IsPoolPayment] = options[:is_pool_payment] || 0
end
add_additional_transaction_data(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 210
def add_additional_transaction_data(post, options)
  post[:PaymentDealerRequest][:ClientIP] = options[:ip] if options[:ip]
  post[:PaymentDealerRequest][:OtherTrxCode] = options[:order_id] if options[:order_id]
end
add_amount(post, money) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 172
def add_amount(post, money)
  post[:PaymentDealerRequest][:Amount] = amount(money) || 0
end
add_auth_purchase(post, money, payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 129
def add_auth_purchase(post, money, payment, options)
  add_payment_dealer_authentication(post)
  add_invoice(post, money, options)
  add_payment(post, payment)
  add_additional_auth_purchase_data(post, options)
  add_additional_transaction_data(post, options)
  add_buyer_information(post, payment, options)
  add_basket_product(post, options[:basket_product]) if options[:basket_product]
end
add_basket_product(post, basket_options) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 195
def add_basket_product(post, basket_options)
  basket = []

  basket_options.each do |product|
    obj = {}
    obj[:ProductId] = product[:product_id] if product[:product_id]
    obj[:ProductCode] = product[:product_code] if product[:product_code]
    obj[:UnitPrice] = amount(product[:unit_price]) if product[:unit_price]
    obj[:Quantity] = product[:quantity] if product[:quantity]
    basket << obj
  end

  post[:PaymentDealerRequest][:BasketProduct] = basket
end
add_buyer_information(post, card, options) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 184
def add_buyer_information(post, card, options)
  obj = {}

  obj[:BuyerFullName] = card.name || ''
  obj[:BuyerEmail] = options[:email] if options[:email]
  obj[:BuyerAddress] = options[:billing_address][:address1] if options[:billing_address]
  obj[:BuyerGsmNumber] = options[:billing_address][:phone] if options[:billing_address]

  post[:PaymentDealerRequest][:BuyerInformation] = obj
end
add_invoice(post, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 159
def add_invoice(post, money, options)
  post[:PaymentDealerRequest][:Amount] = amount(money) || 0
  post[:PaymentDealerRequest][:Currency] = options[:currency] || 'TL'
end
add_payment(post, card) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 164
def add_payment(post, card)
  post[:PaymentDealerRequest][:CardHolderFullName] = card.name
  post[:PaymentDealerRequest][:CardNumber] = card.number
  post[:PaymentDealerRequest][:ExpMonth] = card.month.to_s.rjust(2, '0')
  post[:PaymentDealerRequest][:ExpYear] = card.year
  post[:PaymentDealerRequest][:CvcNumber] = card.verification_value || ''
end
add_payment_dealer_authentication(post) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 145
def add_payment_dealer_authentication(post)
  post[:PaymentDealerAuthentication] = {
    DealerCode: @options[:dealer_code],
    Username: @options[:username],
    Password: @options[:password],
    CheckKey: check_key
  }
end
add_transaction_reference(post, authorization) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 215
def add_transaction_reference(post, authorization)
  post[:PaymentDealerRequest][:VirtualPosOrderId] = authorization
end
add_void_refund_reason(post) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 219
def add_void_refund_reason(post)
  post[:PaymentDealerRequest][:VoidRefundReason] = 2
end
authorization_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 279
def authorization_from(response)
  response.dig('Data', 'VirtualPosOrderId')
end
check_key() click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 154
def check_key
  str = "#{@options[:dealer_code]}MK#{@options[:username]}PD#{@options[:password]}"
  Digest::SHA256.hexdigest(str)
end
commit(action, parameters) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 223
def commit(action, parameters)
  response = parse(ssl_post(url(action), post_data(parameters), request_headers))
  Response.new(
    success_from(response),
    message_from(response),
    response,
    authorization: authorization_from(response),
    test: test?,
    error_code: error_code_from(response)
  )
end
endpoint(action) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 242
def endpoint(action)
  case action
  when 'three_ds_authorize', 'three_ds_purchase'
    'DoDirectPaymentThreeD'
  when 'purchase', 'authorize'
    'DoDirectPayment'
  when 'capture'
    'DoCapture'
  when 'void'
    'DoVoid'
  when 'refund'
    'DoCreateRefundRequest'
  end
end
error_code_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 283
def error_code_from(response)
  codes = [response['ResultCode'], response.dig('Data', 'ResultCode')].flatten
  codes.reject! { |code| code.blank? || code.casecmp('success').zero? }
  codes.map { |code| ERROR_CODE_MAPPING[code] || code }.join(', ')
end
message_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 275
def message_from(response)
  response.dig('Data', 'ResultMessage').presence || response['ResultCode']
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 265
def parse(body)
  JSON.parse(body)
end
post_data(parameters = {}) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 261
def post_data(parameters = {})
  JSON.generate(parameters)
end
request_headers() click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 257
def request_headers
  { 'Content-Type' => 'application/json' }
end
success_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 269
def success_from(response)
  return response.dig('Data', 'IsSuccessful') if response.dig('Data', 'IsSuccessful').to_s.present?

  response['ResultCode']&.casecmp('success') == 0
end
url(action) click to toggle source
# File lib/active_merchant/billing/gateways/moka.rb, line 235
def url(action)
  host = (test? ? test_url : live_url)
  endpoint = endpoint(action)

  "#{host}/PaymentDealer/#{endpoint}"
end