Permalink
Browse files

Merge pull request #1 from brazilian-payment/master

Merge From Baby
  • Loading branch information...
renatoelias committed Oct 8, 2012
2 parents 9913d34 + b0b8e57 commit 050d8b5ec8dda8e9892d729158ce43973e816e68
View
@@ -2,7 +2,7 @@ nbproject
pkg
doc
tags
-*.swp
+*.sw*
.bundle
.rvmrc
Gemfile.lock
View
@@ -2,22 +2,25 @@
require 'httpi'
require 'nokogiri'
require 'json'
+require 'savon'
require "rbraspag/version"
require 'rbraspag/connection'
require 'rbraspag/payment_method'
require 'rbraspag/crypto/jar_webservice'
require 'rbraspag/crypto/webservice'
require 'rbraspag/bill'
+require 'rbraspag/poster'
require 'rbraspag/credit_card'
+require 'rbraspag/protected_credit_card'
require 'rbraspag/eft'
require 'rbraspag/errors'
require 'rbraspag/utils'
require 'rbraspag/order'
module Braspag
- def self.logger=(logger)
- @logger = logger
+ def self.logger=(value)
+ @logger = value
end
def self.logger
@@ -31,4 +34,12 @@ def self.config_file_path=(path)
def self.config_file_path
@config_path || 'config/braspag.yml'
end
+
+ def self.proxy_address=(value)
+ @proxy_address = value
+ end
+
+ def self.proxy_address
+ @proxy_address
+ end
end
View
@@ -4,13 +4,16 @@ class Connection
PRODUCTION_URL = "https://transaction.pagador.com.br"
HOMOLOGATION_URL = "https://homologacao.pagador.com.br"
+
+ PROTECTED_CARD_PRODUCTION_URL = "https://cartaoprotegido.braspag.com.br/Services"
+ PROTECTED_CARD_HOMOLOGATION_URL = "https://homologacao.braspag.com.br/services/testenvironment"
- attr_reader :braspag_url, :merchant_id, :crypto_url, :crypto_key, :options, :environment
+ attr_reader :braspag_url, :protected_card_url, :merchant_id, :crypto_url, :crypto_key, :options, :environment
def initialize
- raise InvalidEnv if ENV["RACK_ENV"].nil? || ENV["RACK_ENV"].empty?
+ raise InvalidEnv if ENV["BRASPAG_ENV"].nil? || ENV["BRASPAG_ENV"].empty?
- @options = YAML.load_file(Braspag.config_file_path)[ ENV['RACK_ENV'] ]
+ @options = YAML.load_file(Braspag.config_file_path)[ ENV['BRASPAG_ENV'] ]
@merchant_id = @options['merchant_id']
raise InvalidMerchantId unless @merchant_id =~ /\{[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}\}/i
@@ -20,6 +23,7 @@ def initialize
@environment = @options["environment"] == 'production' ? 'production' : 'homologation'
@braspag_url = self.production? ? PRODUCTION_URL : HOMOLOGATION_URL
+ @protected_card_url = self.production? ? PROTECTED_CARD_PRODUCTION_URL : PROTECTED_CARD_HOMOLOGATION_URL
end
def production?
@@ -1,37 +1,6 @@
module Braspag
class CreditCard < PaymentMethod
- PAYMENT_METHODS = {
- # BRASIL
- :amex_2p => 18, # American Express 2 Party
- :cielo_noauth_visa => 71, # Cielo webservice captura automática sem autenticação - Visa
- :cielo_preauth_visa => 73, # Cielo webservice preauth sem autenticação - Visa
- :cielo_noauth_mastercard => 120, # Cielo webservice captura automática sem autenticação - Mastercard
- :cielo_preauth_mastercard => 122, # Cielo webservice preauth sem autenticação - Mastercard
- :cielo_noauth_elo => 126, # Cielo webservice captura automática sem autenticação - ELO
- :cielo_noauth_diners => 130, # Cielo webservice captura automática sem autenticação - Diners
- :redecard => 20, # Redecard Mastercard/Diners/Visa
- :redecard_preauth => 42, # Redecard preauth Mastercard/Diners/Visa
- :cielo_sitef => 57, # Cielo SITEF
- :hipercard_sitef => 62, # Hipercard SITEF
- :hipercard_moip => 90, # Hipercard MOIP
- :oi_paggo => 55, # OiPaggo
- :amex_sitef => 58, # Amex SITEF
- :aura_dtef => 37, # Aura DTEF
- :redecard_sitef => 44, # Redecard SITEF - Mastercard/Diners
- # MÉXICO
- :mex_amex_2p => 45, # American Express 2 Party
- :mex_banorte_visa => 50, # Banorte Visa
- :mex_banorte_diners => 52, # Banorte Diners
- :mex_banorte_mastercard => 53, # Banorte Mastercard
- # COLÔMBIA
- :col_visa => 63, # Visa
- :col_amex => 65, # Amex
- :col_diners => 66, # Diners
- # INTERNACIONAL
- :paypal_express => 35 # PayPal Express Checkout
- }
-
MAPPING = {
:merchant_id => "merchantId",
:order => 'order',
@@ -50,7 +19,6 @@ class CreditCard < PaymentMethod
AUTHORIZE_URI = "/webservices/pagador/Pagador.asmx/Authorize"
CAPTURE_URI = "/webservices/pagador/Pagador.asmx/Capture"
CANCELLATION_URI = "/webservices/pagador/Pagador.asmx/VoidTransaction"
-
PRODUCTION_INFO_URI = "/webservices/pagador/pedido.asmx/GetDadosCartao"
HOMOLOGATION_INFO_URI = "/pagador/webservice/pedido.asmx/GetDadosCartao"
@@ -64,21 +32,16 @@ def self.authorize(params = {})
MAPPING.each do |k, v|
case k
when :payment_method
- data[v] = PAYMENT_METHODS[params[:payment_method]]
+ data[v] = Braspag::Connection.instance.homologation? ? PAYMENT_METHODS[:braspag] : PAYMENT_METHODS[params[:payment_method]]
when :amount
data[v] = Utils.convert_decimal_to_string(params[:amount])
else
data[v] = params[k] || ""
end
end
- request = ::HTTPI::Request.new self.authorize_url
- request.body = data
+ response = Braspag::Poster.new(self.authorize_url).do_post(:authorize, data)
- ::Braspag.logger.info("[Braspag] #authorize: #{self.authorize_url}, data: #{data.inspect}")
-
- response = ::HTTPI.post request
- ::Braspag.logger.info("[Braspag] #authorize return: #{response.body.inspect}")
Utils::convert_to_map(response.body, {
:amount => nil,
:number => "authorisationNumber",
@@ -100,10 +63,8 @@ def self.capture(order_id)
MAPPING[:merchant_id] => merchant_id
}
- request = ::HTTPI::Request.new(self.capture_url)
- request.body = data
+ response = Braspag::Poster.new(self.capture_url).do_post(:capture, data)
- response = ::HTTPI.post(request)
Utils::convert_to_map(response.body, {
:amount => nil,
:number => "authorisationNumber",
@@ -125,10 +86,8 @@ def self.void(order_id)
MAPPING[:merchant_id] => merchant_id
}
- request = ::HTTPI::Request.new(self.cancellation_url)
- request.body = data
+ response = Braspag::Poster.new(self.cancellation_url).do_post(:void, data)
- response = ::HTTPI.post(request)
Utils::convert_to_map(response.body, {
:amount => nil,
:number => "authorisationNumber",
@@ -144,10 +103,8 @@ def self.info(order_id)
raise InvalidOrderId unless self.valid_order_id?(order_id)
- request = ::HTTPI::Request.new(self.info_url)
- request.body = {:loja => connection.merchant_id, :numeroPedido => order_id.to_s}
-
- response = ::HTTPI.post(request)
+ data = {:loja => connection.merchant_id, :numeroPedido => order_id.to_s}
+ response = Braspag::Poster.new(self.info_url).do_post(:info_credit_card, data)
response = Utils::convert_to_map(response.body, {
:checking_number => "NumeroComprovante",
@@ -170,7 +127,7 @@ def self.check_params(params)
raise InvalidHolder if params[:holder].to_s.size < 1 || params[:holder].to_s.size > 100
- matches = params[:expiration].to_s.match /^(\d{2})\/(\d{2}|\d{4})$/
+ matches = params[:expiration].to_s.match /^(\d{2})\/(\d{2,4})$/
raise InvalidExpirationDate unless matches
begin
year = matches[2].to_i
@@ -186,6 +143,24 @@ def self.check_params(params)
raise InvalidNumberPayments if params[:number_payments].to_i < 1 || params[:number_payments].to_i > 99
end
+ # <b>DEPRECATED:</b> Please use <tt>ProtectedCreditCard.save</tt> instead.
+ def self.save(params)
+ warn "[DEPRECATION] `CreditCard.save` is deprecated. Please use `ProtectedCreditCard.save` instead."
+ ProtectedCreditCard.save(params)
+ end
+
+ # <b>DEPRECATED:</b> Please use <tt>ProtectedCreditCard.get</tt> instead.
+ def self.get(just_click_key)
+ warn "[DEPRECATION] `CreditCard.get` is deprecated. Please use `ProtectedCreditCard.get` instead."
+ ProtectedCreditCard.get(just_click_key)
+ end
+
+ # <b>DEPRECATED:</b> Please use <tt>ProtectedCreditCard.just_click_shop</tt> instead.
+ def self.just_click_shop(params = {})
+ warn "[DEPRECATION] `CreditCard.just_click_shop` is deprecated. Please use `ProtectedCreditCard.just_click_shop` instead."
+ ProtectedCreditCard.just_click_shop(params)
+ end
+
def self.info_url
connection = Braspag::Connection.instance
connection.braspag_url + (connection.production? ? PRODUCTION_INFO_URI : HOMOLOGATION_INFO_URI)
View
@@ -23,6 +23,8 @@ class InvalidExpirationDate < Exception ; end
class InvalidSecurityCode < Exception ; end
class InvalidType < Exception ; end
class InvalidNumberPayments < Exception ; end
+ class InvalidNumberInstallments < Exception ; end
+ class InvalidJustClickKey < Exception ; end
class UnknownError < Exception ; end
class Connection
@@ -1,5 +1,38 @@
module Braspag
class PaymentMethod
+ PAYMENT_METHODS = {
+ # BRASIL
+ :amex_2p => 18, # American Express 2 Party
+ :cielo_noauth_visa => 71, # Cielo webservice captura automática sem autenticação - Visa
+ :cielo_preauth_visa => 73, # Cielo webservice preauth sem autenticação - Visa
+ :cielo_noauth_mastercard => 120, # Cielo webservice captura automática sem autenticação - Mastercard
+ :cielo_preauth_mastercard => 122, # Cielo webservice preauth sem autenticação - Mastercard
+ :cielo_noauth_elo => 126, # Cielo webservice captura automática sem autenticação - ELO
+ :cielo_noauth_diners => 130, # Cielo webservice captura automática sem autenticação - Diners
+ :redecard => 20, # Redecard Mastercard/Diners/Visa
+ :redecard_preauth => 42, # Redecard preauth Mastercard/Diners/Visa
+ :cielo_sitef => 57, # Cielo SITEF
+ :hipercard_sitef => 62, # Hipercard SITEF
+ :hipercard_moip => 90, # Hipercard MOIP
+ :oi_paggo => 55, # OiPaggo
+ :amex_sitef => 58, # Amex SITEF
+ :aura_dtef => 37, # Aura DTEF
+ :redecard_sitef => 44, # Redecard SITEF - Mastercard/Diners
+ # MÉXICO
+ :mex_amex_2p => 45, # American Express 2 Party
+ :mex_banorte_visa => 50, # Banorte Visa
+ :mex_banorte_diners => 52, # Banorte Diners
+ :mex_banorte_mastercard => 53, # Banorte Mastercard
+ # COLÔMBIA
+ :col_visa => 63, # Visa
+ :col_amex => 65, # Amex
+ :col_diners => 66, # Diners
+ # INTERNACIONAL
+ :paypal_express => 35, # PayPal Express Checkout
+ # HOMOLOGATION
+ :braspag => 997
+ }
+
def self.payment_method_from_id(code)
self::PAYMENT_METHODS.invert.values_at(code).first
end
View
@@ -0,0 +1,36 @@
+module Braspag
+ class Poster
+ def initialize(url)
+ @request = ::HTTPI::Request.new(url)
+ end
+
+ def do_post(method, data)
+ @request.body = data
+ @request.proxy = Braspag.proxy_address if Braspag.proxy_address
+
+ with_logger(method) do
+ ::HTTPI.post @request
+ end
+ end
+
+ private
+
+ def with_logger(method)
+ if Braspag::logger
+ Braspag::logger.info("[Braspag] ##{method}: #{@request.url}, data: #{mask_data(@request.body).inspect}")
+ response = yield
+ Braspag::logger.info("[Braspag] ##{method} returns: #{response.body.inspect}")
+ else
+ response = yield
+ end
+ response
+ end
+
+ def mask_data(data)
+ copy_data = data.dup
+ copy_data['cardNumber'] = "************%s" % copy_data['cardNumber'][-4..-1] if copy_data['cardNumber']
+ copy_data['securityCode'] = "***" if copy_data['securityCode']
+ copy_data
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 050d8b5

Please sign in to comment.