class ActiveMerchant::Billing::MicropaymentGateway

Public Class Methods

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

Public Instance Methods

authorize(amount, payment_method, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/micropayment.rb, line 29
def authorize(amount, payment_method, options={})
  post = {}
  add_invoice(post, amount, options)
  add_payment_method(post, payment_method, options)
  add_customer_data(post, options)
  add_address(post, options)
  commit('authorize', post)
end
capture(amount, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/micropayment.rb, line 38
def capture(amount, authorization, options={})
  post = {}
  add_reference(post, authorization)
  add_invoice(post, amount, options)
  commit('capture', post)
end
purchase(amount, payment_method, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/micropayment.rb, line 20
def purchase(amount, payment_method, options={})
  post = {}
  add_invoice(post, amount, options)
  add_payment_method(post, payment_method, options)
  add_customer_data(post, options)
  add_address(post, options)
  commit('purchase', post)
end
refund(amount, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/micropayment.rb, line 51
def refund(amount, authorization, options={})
  post = {}
  add_reference(post, authorization)
  add_invoice(post, amount, options)
  commit('refund', post)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/micropayment.rb, line 69
def scrub(transcript)
  transcript.
    gsub(%r((accessKey=)\w+), '\1[FILTERED]').
    gsub(%r((number=)\d+), '\1[FILTERED]').
    gsub(%r((cvc2=)\d+), '\1[FILTERED]')
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/micropayment.rb, line 65
def supports_scrubbing?
  true
end
verify(credit_card, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/micropayment.rb, line 58
def verify(credit_card, options={})
  MultiResponse.run(:use_first_response) do |r|
    r.process { authorize(250, 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/micropayment.rb, line 45
def void(authorization, options={})
  post = {}
  add_reference(post, authorization)
  commit('void', post)
end

Private Instance Methods

add_address(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/micropayment.rb, line 104
def add_address(post, options)
  address = options[:billing_address]
  return unless address

  post['params[address]'] = address[:address1] if address[:address1]
  post['params[zipcode]'] = address[:zip] if address[:zip]
  post['params[town]'] = address[:city] if address[:city]
  post['params[country]'] = address[:country] if address[:country]
end
add_customer_data(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/micropayment.rb, line 98
def add_customer_data(post, options)
  post['params[email]'] = options[:email] if options[:email]
  post['params[ip]'] = options[:ip] || '1.1.1.1'
  post['params[sendMail]'] = options[:send_mail] || 'false'
end
add_invoice(post, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/micropayment.rb, line 78
def add_invoice(post, money, options)
  if money
    post[:amount] = amount(money)
    post[:currency] = options[:currency] || currency(money)
  end
  post[:project] = options[:project] if options[:project]
  post['params[title]'] = options[:description] if options[:description]
end
add_payment_method(post, payment_method, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/micropayment.rb, line 87
def add_payment_method(post, payment_method, options={})
  post[:number] = payment_method.number
  post[:recurring] = 1 if options[:recurring] == true
  post[:cvc2] = payment_method.verification_value
  post[:expiryYear] = format(payment_method.year, :four_digits)
  post[:expiryMonth] = format(payment_method.month, :two_digits)

  post['params[firstname]'] = payment_method.first_name
  post['params[surname]'] = payment_method.last_name
end
add_reference(post, authorization) click to toggle source
# File lib/active_merchant/billing/gateways/micropayment.rb, line 114
def add_reference(post, authorization)
  session_id, transaction_id = split_authorization(authorization)
  post[:sessionId] = session_id
  post[:transactionId] = transaction_id
end
authorization_from(response, request_params) click to toggle source
# File lib/active_merchant/billing/gateways/micropayment.rb, line 177
def authorization_from(response, request_params)
  session_id = response['sessionId'] || request_params[:sessionId]
  "#{session_id}|#{response["transactionId"]}"
end
commit(action, params) click to toggle source
# File lib/active_merchant/billing/gateways/micropayment.rb, line 120
def commit(action, params)
  params[:testMode] = 1 if test?
  params[:accessKey] = @options[:access_key]
  params[:apiKey] = @options[:api_key] || 'af1fd841af792f4c50131414ff76e004'

  response = parse(ssl_post(url(action), post_data(action, params), headers))

  Response.new(
    succeeded = success_from(response),
    message_from(succeeded, response),
    response,
    authorization: authorization_from(response, params),
    avs_result: AVSResult.new(code: response['some_avs_result_key']),
    cvv_result: CVVResult.new(response['some_cvv_result_key']),
    test: test?
  )
end
headers() click to toggle source
# File lib/active_merchant/billing/gateways/micropayment.rb, line 138
def headers
  { 'Content-Type'  => 'application/x-www-form-urlencoded;charset=UTF-8' }
end
message_from(succeeded, response) click to toggle source
# File lib/active_merchant/billing/gateways/micropayment.rb, line 165
def message_from(succeeded, response)
  if succeeded
    'Succeeded'
  else
    response['errorMessage'] || response['transactionResultMessage']
  end
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/micropayment.rb, line 151
def parse(body)
  body.split(/\r?\n/).inject({}) do |acc, pair|
    key, value = pair.split('=')
    acc[key] = CGI.unescape(value)
    acc
  end
end
post_data(action, params) click to toggle source
# File lib/active_merchant/billing/gateways/micropayment.rb, line 142
def post_data(action, params)
  params.map { |k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join('&')
end
split_authorization(authorization) click to toggle source
# File lib/active_merchant/billing/gateways/micropayment.rb, line 173
def split_authorization(authorization)
  authorization.split('|')
end
success_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/micropayment.rb, line 159
def success_from(response)
  response['error'] == '0' &&
    response['transactionResultCode'] == '00' &&
    response['transactionStatus'] == 'SUCCESS'
end
url(action) click to toggle source
# File lib/active_merchant/billing/gateways/micropayment.rb, line 146
def url(action)
  action_url = test? ? test_url : live_url
  "#{action_url}?action=#{action}"
end