class ActiveMerchant::Billing::PayboxDirectGateway

Constants

API_VERSION

Payment API Version

CURRENCY_CODES
FAILURE_MESSAGE
SUCCESS_CODES
SUCCESS_MESSAGE
TRANSACTIONS

Transactions hash

UNAVAILABILITY_CODES

Public Class Methods

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

Public Instance Methods

authorize(money, creditcard, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/paybox_direct.rb, line 67
def authorize(money, creditcard, options = {})
  post = {}
  add_invoice(post, options)
  add_creditcard(post, creditcard)
  add_amount(post, money, options)

  commit('authorization', money, post)
end
capture(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/paybox_direct.rb, line 85
def capture(money, authorization, options = {})
  requires!(options, :order_id)
  post = {}
  add_invoice(post, options)
  add_amount(post, money, options)
  post[:numappel] = authorization[0, 10]
  post[:numtrans] = authorization[10, 10]

  commit('capture', money, post)
end
credit(money, identification, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/paybox_direct.rb, line 108
def credit(money, identification, options = {})
  ActiveMerchant.deprecated CREDIT_DEPRECATION_MESSAGE
  refund(money, identification, options)
end
purchase(money, creditcard, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/paybox_direct.rb, line 76
def purchase(money, creditcard, options = {})
  post = {}
  add_invoice(post, options)
  add_creditcard(post, creditcard)
  add_amount(post, money, options)

  commit('purchase', money, post)
end
refund(money, identification, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/paybox_direct.rb, line 113
def refund(money, identification, options = {})
  post = {}
  add_invoice(post, options)
  add_reference(post, identification)
  add_amount(post, money, options)
  commit('refund', money, post)
end
void(identification, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/paybox_direct.rb, line 96
def void(identification, options = {})
  requires!(options, :order_id, :amount)
  post ={}
  add_invoice(post, options)
  add_reference(post, identification)
  add_amount(post, options[:amount], options)
  post[:porteur] = '000000000000000'
  post[:dateval] = '0000'

  commit('void', options[:amount], post)
end

Private Instance Methods

add_amount(post, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/paybox_direct.rb, line 138
def add_amount(post, money, options)
  post[:montant] = ('0000000000' + (money ? amount(money) : ''))[-10..-1]
  post[:devise] = CURRENCY_CODES[options[:currency] || currency(money)]
end
add_creditcard(post, creditcard) click to toggle source
# File lib/active_merchant/billing/gateways/paybox_direct.rb, line 127
def add_creditcard(post, creditcard)
  post[:porteur] = creditcard.number
  post[:dateval] = expdate(creditcard)
  post[:cvv] = creditcard.verification_value if creditcard.verification_value?
end
add_invoice(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/paybox_direct.rb, line 123
def add_invoice(post, options)
  post[:reference] = options[:order_id]
end
add_reference(post, identification) click to toggle source
# File lib/active_merchant/billing/gateways/paybox_direct.rb, line 133
def add_reference(post, identification)
  post[:numappel] = identification[0, 10]
  post[:numtrans] = identification[10, 10]
end
commit(action, money = nil, parameters = nil) click to toggle source
# File lib/active_merchant/billing/gateways/paybox_direct.rb, line 152
def commit(action, money = nil, parameters = nil)
  request_data = post_data(action, parameters)
  response = parse(ssl_post(test? ? self.test_url : self.live_url, request_data))
  response = parse(ssl_post(self.live_url_backup, request_data)) if service_unavailable?(response) && !test?
  Response.new(success?(response), message_from(response), response.merge(
    :timestamp => parameters[:dateq]),
    :test => test?,
    :authorization => response[:numappel].to_s + response[:numtrans].to_s,
    :fraud_review => false,
    :sent_params => parameters.delete_if { |key, value| ['porteur', 'dateval', 'cvv'].include?(key.to_s) }
  )
end
message_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/paybox_direct.rb, line 173
def message_from(response)
  success?(response) ? SUCCESS_MESSAGE : (response[:commentaire]  || FAILURE_MESSAGE)
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/paybox_direct.rb, line 143
def parse(body)
  results = {}
  body.split(/&/).each do |pair|
    key, val = pair.split(/\=/)
    results[key.downcase.to_sym] = CGI.unescape(val) if val
  end
  results
end
post_data(action, parameters = {}) click to toggle source
# File lib/active_merchant/billing/gateways/paybox_direct.rb, line 177
def post_data(action, parameters = {})
  parameters.update(
    :version => API_VERSION,
    :type => TRANSACTIONS[action.to_sym],
    :dateq => Time.now.strftime('%d%m%Y%H%M%S'),
    :numquestion => unique_id(parameters[:order_id]),
    :site => @options[:login].to_s[0, 7],
    :rang => @options[:rang] || @options[:login].to_s[7..-1],
    :cle => @options[:password],
    :pays => '',
    :archivage => parameters[:order_id]
  )

  parameters.collect { |key, value| "#{key.to_s.upcase}=#{CGI.escape(value.to_s)}" }.join('&')
end
service_unavailable?(response) click to toggle source
# File lib/active_merchant/billing/gateways/paybox_direct.rb, line 169
def service_unavailable?(response)
  UNAVAILABILITY_CODES.include?(response[:codereponse])
end
success?(response) click to toggle source
# File lib/active_merchant/billing/gateways/paybox_direct.rb, line 165
def success?(response)
  SUCCESS_CODES.include?(response[:codereponse])
end
unique_id(seed = 0) click to toggle source
# File lib/active_merchant/billing/gateways/paybox_direct.rb, line 193
def unique_id(seed = 0)
  randkey = "#{seed}#{Time.now.usec}".to_i % 2147483647 # Max paybox value for the question number

  "0000000000#{randkey}"[-10..-1]
end