class ActiveMerchant::Billing::KomojuGateway

Constants

STANDARD_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/komoju.rb, line 22
def initialize(options = {})
  requires!(options, :login)
  super
end

Public Instance Methods

purchase(money, payment, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/komoju.rb, line 27
def purchase(money, payment, options = {})
  post = {}
  post[:amount] = amount(money)
  post[:description] = options[:description]
  add_payment_details(post, payment, options)
  post[:currency] = options[:currency] || default_currency
  post[:external_order_num] = options[:order_id] if options[:order_id]
  post[:tax] = options[:tax] if options[:tax]
  add_fraud_details(post, options)

  commit('/payments', post)
end
refund(money, identification, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/komoju.rb, line 40
def refund(money, identification, options = {})
  commit("/payments/#{identification}/refund", {})
end

Private Instance Methods

add_fraud_details(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/komoju.rb, line 61
def add_fraud_details(post, options)
  details = {}

  details[:customer_ip] = options[:ip] if options[:ip]
  details[:customer_email] = options[:email] if options[:email]
  details[:browser_language] = options[:browser_language] if options[:browser_language]
  details[:browser_user_agent] = options[:browser_user_agent] if options[:browser_user_agent]

  post[:fraud_details] = details unless details.empty?
end
add_payment_details(post, payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/komoju.rb, line 46
def add_payment_details(post, payment, options)
  details = {}

  details[:type] = 'credit_card'
  details[:number] = payment.number
  details[:month] = payment.month
  details[:year] = payment.year
  details[:verification_value] = payment.verification_value
  details[:given_name] = payment.first_name
  details[:family_name] = payment.last_name
  details[:email] = options[:email] if options[:email]

  post[:payment_details] = details
end
api_request(path, data) click to toggle source
# File lib/active_merchant/billing/gateways/komoju.rb, line 72
def api_request(path, data)
  raw_response = nil
  begin
    raw_response = ssl_post("#{url}#{path}", data, headers)
  rescue ResponseError => e
    raw_response = e.response.body
  end

  JSON.parse(raw_response)
end
commit(path, params) click to toggle source
# File lib/active_merchant/billing/gateways/komoju.rb, line 83
def commit(path, params)
  response = api_request(path, params.to_json)
  success = !response.key?('error')
  message = (success ? 'Transaction succeeded' : response['error']['message'])
  Response.new(
    success,
    message,
    response,
    test: test?,
    error_code: (success ? nil : error_code(response['error']['code'])),
    authorization: (success ? response['id'] : nil)
  )
end
error_code(code) click to toggle source
# File lib/active_merchant/billing/gateways/komoju.rb, line 97
def error_code(code)
  STANDARD_ERROR_CODE_MAPPING[code] || code
end
headers() click to toggle source
# File lib/active_merchant/billing/gateways/komoju.rb, line 105
def headers
  {
    'Authorization' => 'Basic ' + Base64.encode64(@options[:login].to_s + ':').strip,
    'Accept' => 'application/json',
    'Content-Type' => 'application/json',
    'User-Agent' => "Komoju/v1 ActiveMerchantBindings/#{ActiveMerchant::VERSION}"
  }
end
url() click to toggle source
# File lib/active_merchant/billing/gateways/komoju.rb, line 101
def url
  test? ? self.test_url : self.live_url
end