class ActiveMerchant::Billing::NetpayGateway

NETPAY Gateway

Support for NETPAY's HTTP Connector payment gateway in Mexico.

The gateway sends requests as HTTP POST and receives the response details in the HTTP header, making the process really rather simple.

Support for calls to the authorize and capture methods have been included as per the Netpay manuals, however, your millage may vary with these methods. At the time of writing (January 2013) they were not fully supported by the production or test gateways. This situation is expected to change within a few weeks/months.

Purchases can be cancelled (`#void`) only within 24 hours of the transaction. After this, a refund should be performed instead.

In addition to the regular ActiveMerchant transaction options, NETPAY also supports a `:mode` parameter. This allows testing to be performed in production and force specific results.

* 'P' - Production
* 'A' - Approved
* 'D' - Declined
* 'R' - Random (Approved or Declined)
* 'T' - Test

For example:

response = @gateway.purchase(1000, card, :mode => 'D')
response.success  # false

Constants

CURRENCY_CODES
RESPONSE_KEYS

The header keys that we will provide in the response params hash

Public Class Methods

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

Public Instance Methods

authorize(money, creditcard, options = {}) click to toggle source

Send an authorization request

# File lib/active_merchant/billing/gateways/netpay.rb, line 66
def authorize(money, creditcard, options = {})
  post = {}
  add_invoice(post, options)
  add_creditcard(post, creditcard)
  add_customer_data(post, options)
  add_amount(post, money, options)

  commit('PreAuth', post, options)
end
capture(money, authorization, options = {}) click to toggle source

Capture an authorization

# File lib/active_merchant/billing/gateways/netpay.rb, line 77
def capture(money, authorization, options = {})
  post = {}
  add_order_id(post, order_id_from(authorization))
  add_amount(post, money, options)

  commit('PostAuth', post, options)
end
purchase(money, creditcard, options = {}) click to toggle source

Make a purchase.

# File lib/active_merchant/billing/gateways/netpay.rb, line 97
def purchase(money, creditcard, options = {})
  post = {}
  add_invoice(post, options)
  add_creditcard(post, creditcard)
  add_customer_data(post, options)
  add_amount(post, money, options)

  commit('Auth', post, options)
end
refund(money, authorization, options = {}) click to toggle source

Perform a Credit transaction.

# File lib/active_merchant/billing/gateways/netpay.rb, line 108
def refund(money, authorization, options = {})
  post = {}
  add_order_id(post, order_id_from(authorization))
  add_amount(post, money, options)

  commit('Credit', post, options)
end
void(authorization, options = {}) click to toggle source

Cancel an auth/purchase within first 24 hours

# File lib/active_merchant/billing/gateways/netpay.rb, line 86
def void(authorization, options = {})
  post = {}
  order_id, amount, currency = split_authorization(authorization)
  add_order_id(post, order_id)
  post['Total'] = (options[:amount] || amount)
  post['CurrencyCode'] = currency

  commit('Refund', post, options)
end

Private Instance Methods

add_action(post, action, options) click to toggle source
# File lib/active_merchant/billing/gateways/netpay.rb, line 124
def add_action(post, action, options)
  post['ResourceName'] = action
  post['ContentType']  = 'Transaction'
  post['Mode']         = options[:mode] || 'P'
end
add_amount(post, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/netpay.rb, line 134
def add_amount(post, money, options)
  post['Total'] = amount(money)
  post['CurrencyCode'] = currency_code(options[:currency] || currency(money))
end
add_creditcard(post, creditcard) click to toggle source
# File lib/active_merchant/billing/gateways/netpay.rb, line 147
def add_creditcard(post, creditcard)
  post['CardNumber']   = creditcard.number
  post['ExpDate']      = expdate(creditcard)
  post['CustomerName'] = creditcard.name
  post['CVV2']         = creditcard.verification_value unless creditcard.verification_value.nil?
end
add_customer_data(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/netpay.rb, line 139
def add_customer_data(post, options)
  post['IPAddress'] = options[:ip] unless options[:ip].blank?
end
add_invoice(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/netpay.rb, line 143
def add_invoice(post, options)
  post['Comments'] = options[:description] if options[:description]
end
add_login_data(post) click to toggle source
# File lib/active_merchant/billing/gateways/netpay.rb, line 118
def add_login_data(post)
  post['StoreId']     = @options[:store_id]
  post['UserName']    = @options[:login]
  post['Password']    = @options[:password]
end
add_order_id(post, order_id) click to toggle source
# File lib/active_merchant/billing/gateways/netpay.rb, line 130
def add_order_id(post, order_id)
  post['OrderId'] = order_id
end
build_authorization(request_params, response_params) click to toggle source
# File lib/active_merchant/billing/gateways/netpay.rb, line 154
def build_authorization(request_params, response_params)
  [response_params['OrderId'], request_params['Total'], request_params['CurrencyCode']].join('|')
end
commit(action, parameters, options) click to toggle source
# File lib/active_merchant/billing/gateways/netpay.rb, line 189
def commit(action, parameters, options)
  add_login_data(parameters)
  add_action(parameters, action, options)

  post = parameters.collect { |key, value| "#{key}=#{CGI.escape(value.to_s)}" }.join('&')
  parse(ssl_post(url, post), parameters)
end
currency_code(currency) click to toggle source
# File lib/active_merchant/billing/gateways/netpay.rb, line 216
def currency_code(currency)
  return currency if currency =~ /^\d+$/
  CURRENCY_CODES[currency]
end
expdate(credit_card) click to toggle source
# File lib/active_merchant/billing/gateways/netpay.rb, line 167
def expdate(credit_card)
  year  = sprintf('%.4i', credit_card.year)
  month = sprintf('%.2i', credit_card.month)

  "#{month}/#{year[-2..-1]}"
end
handle_response(response) click to toggle source

Override the regular handle response so we can access the headers

# File lib/active_merchant/billing/gateways/netpay.rb, line 198
def handle_response(response)
  case response.code.to_i
  when 200...300
    response
  else
    raise ResponseError.new(response)
  end
end
order_id_from(authorization) click to toggle source
# File lib/active_merchant/billing/gateways/netpay.rb, line 163
def order_id_from(authorization)
  split_authorization(authorization).first
end
params_from_response(response) click to toggle source

Return a hash containing all the useful, or informative values from netpay

# File lib/active_merchant/billing/gateways/netpay.rb, line 208
def params_from_response(response)
  params = {}
  RESPONSE_KEYS.each do |k|
    params[k] = response[k] unless response[k].to_s.empty?
  end
  params
end
parse(response, request_params) click to toggle source
# File lib/active_merchant/billing/gateways/netpay.rb, line 178
def parse(response, request_params)
  response_params = params_from_response(response)

  success = (response_params['ResponseCode'] == '00')
  message = response_params['ResponseText'] || response_params['ResponseMsg']
  options = @options.merge(:test => test?,
                           :authorization => build_authorization(request_params, response_params))

  Response.new(success, message, response_params, options)
end
split_authorization(authorization) click to toggle source
# File lib/active_merchant/billing/gateways/netpay.rb, line 158
def split_authorization(authorization)
  order_id, amount, currency = authorization.split('|')
  [order_id, amount, currency]
end
url() click to toggle source
# File lib/active_merchant/billing/gateways/netpay.rb, line 174
def url
  test? ? test_url : live_url
end