cbraspag 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/RELEASES.md ADDED
@@ -0,0 +1,20 @@
1
+ # RELEASES
2
+
3
+ ## For next release
4
+
5
+ d2b9a7a Voltando a usar o HTTPI
6
+
7
+ ## 0.1.1 - 09/01/2012
8
+
9
+ 9ffbcbd adicionando novos códigos de métodos de pagamento
10
+
11
+
12
+ ## 0.1.0 - 09/01/2012
13
+
14
+ 5cf697e adicionando Gemfile.lock ao .gitignore
15
+ a6b1f54 removendo Gemfile.lock; modificando verificação do argumento has_interest
16
+ a663e0a baixando a versão mínima requerida do nokogiri
17
+ 628d108 refatorando testes e implementação
18
+ 087549a moving Connection exceptions to errors.rb; tests for Utils class
19
+ 9a727eb bump up version
20
+ 6e5b1c3 refatorando para suportar o ambiente de produção da Braspag
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/cbraspag.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "cbraspag/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "cbraspag"
7
+ s.version = Braspag::VERSION
8
+ s.authors = ["CodeMiner42", "Renato Elias"]
9
+ s.email = %w[contato@codeminer42.com renato.elias@gmail.com]
10
+ s.homepage = "http://github.com/codeminer42/braspag"
11
+ s.summary = "Braspag: brazilian gateway, agnostic, support all features in braspag"
12
+ s.description = "Braspag: brazilian gateway, agnostic, support all features in braspag"
13
+
14
+ s.rubyforge_project = "nowarning"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency 'active_attr', '>= 0.6'
22
+ s.add_dependency 'httpi', '>= 0.9.6'
23
+ s.add_dependency 'json', '>= 1.6.1'
24
+ s.add_dependency 'nokogiri', '>= 1.4.7'
25
+ s.add_dependency 'savon', '>= 0.9.9'
26
+
27
+ s.add_development_dependency "rake"
28
+ s.add_development_dependency "rspec"
29
+ s.add_development_dependency "pry"
30
+ end
data/lib/cbraspag.rb ADDED
@@ -0,0 +1,93 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+ require 'active_attr'
4
+ require 'httpi'
5
+ require 'json'
6
+ require 'nokogiri'
7
+ require 'savon'
8
+ require 'bigdecimal'
9
+
10
+ require "cbraspag/version"
11
+ require 'cbraspag/core/converter'
12
+ require 'cbraspag/core/connection'
13
+ require 'cbraspag/core/poster'
14
+ require 'cbraspag/core/response'
15
+ require 'cbraspag/core/customer'
16
+ require 'cbraspag/core/order'
17
+
18
+ require 'cbraspag/crypto/no_crypto'
19
+ require 'cbraspag/crypto/jar_webservice'
20
+ require 'cbraspag/crypto/webservice'
21
+
22
+ require 'cbraspag/payment/billet'
23
+ require 'cbraspag/payment/credit_card'
24
+ require 'cbraspag/payment/eft'
25
+
26
+
27
+ module Braspag
28
+
29
+ INTEREST = {
30
+ :no => 0,
31
+ :merchant => 1,
32
+ :customer => 2,
33
+ :merchant_iata => 3,
34
+ :customer_iata => 4,
35
+ :no_iata => 5
36
+ }
37
+
38
+ STATUS_PAYMENT = {
39
+ :starting => 1,
40
+ :close => 2,
41
+ :paid => 3,
42
+ :cancelled => 4
43
+ }
44
+
45
+ PAYMENT_METHOD = {
46
+ #BILLET
47
+ :billet_bradesco => 6, #Boleto Bradesco
48
+ :billet_cef => 7, #Boleto Caixa Economica Federal
49
+ :billet_hsbc => 8, #Boleto HSBC
50
+ :billet_banco_do_brasil => 9, #Boleto Banco do Brasil
51
+ :billet_santader => 10, #Boleto Banco Santader
52
+ :billet_citibank => 13, #Boleto Citibank
53
+ :billet_itau => 14, #Boleto Itau
54
+ :billet_unibanco => 26, #Boleto Unibanco
55
+ #EFT
56
+ :eft_bradesco => 11, #EFT Bradesco
57
+ :eft_itau => 12, #EFT Itau Shopline
58
+ :eft_banco_do_brasil => 15, #EFT Banco do Brasil
59
+ :eft_banco_real => 16, #EFT Banco Real
60
+ :eft_banrisul => 30, #EFT Banrisul
61
+ :eft_unibanco => 31, #EFT Unibanco
62
+ #CARDS - BRASIL
63
+ :amex_2p => 18, # American Express 2 Party
64
+ :cielo_noauth_visa => 71, # Cielo webservice captura automática sem autenticação - Visa
65
+ :cielo_preauth_visa => 73, # Cielo webservice preauth sem autenticação - Visa
66
+ :cielo_noauth_mastercard => 120, # Cielo webservice captura automática sem autenticação - Mastercard
67
+ :cielo_preauth_mastercard => 122, # Cielo webservice preauth sem autenticação - Mastercard
68
+ :cielo_noauth_elo => 126, # Cielo webservice captura automática sem autenticação - ELO
69
+ :cielo_noauth_diners => 130, # Cielo webservice captura automática sem autenticação - Diners
70
+ :redecard => 20, # Redecard Mastercard/Diners/Visa
71
+ :redecard_preauth => 42, # Redecard preauth Mastercard/Diners/Visa
72
+ :cielo_sitef => 57, # Cielo SITEF
73
+ :hipercard_sitef => 62, # Hipercard SITEF
74
+ :hipercard_moip => 90, # Hipercard MOIP
75
+ :oi_paggo => 55, # OiPaggo
76
+ :amex_sitef => 58, # Amex SITEF
77
+ :aura_dtef => 37, # Aura DTEF
78
+ :redecard_sitef => 44, # Redecard SITEF - Mastercard/Diners
79
+ #CARDS - MEXICO
80
+ :mex_amex_2p => 45, # American Express 2 Party
81
+ :mex_banorte_visa => 50, # Banorte Visa
82
+ :mex_banorte_diners => 52, # Banorte Diners
83
+ :mex_banorte_mastercard => 53, # Banorte Mastercard
84
+ #CARDS - COLOMBIA
85
+ :col_visa => 63, # Visa
86
+ :col_amex => 65, # Amex
87
+ :col_diners => 66, # Diners
88
+ # INTERNACIONAL
89
+ :paypal_express => 35, # PayPal Express Checkout
90
+ # HOMOLOGATION
91
+ :braspag => 997
92
+ }
93
+ end
@@ -0,0 +1,96 @@
1
+ module Braspag
2
+ class Connection
3
+
4
+ class InvalidMerchantId < Exception ; end
5
+ class InvalidEnvironment < Exception ; end
6
+
7
+ PRODUCTION_URL = "https://transaction.pagador.com.br"
8
+ HOMOLOGATION_URL = "https://homologacao.pagador.com.br"
9
+ PROTECTED_CARD_PRODUCTION_URL = "https://cartaoprotegido.braspag.com.br/Services"
10
+ PROTECTED_CARD_HOMOLOGATION_URL = "https://homologacao.braspag.com.br/services/testenvironment"
11
+
12
+ attr_reader :merchant_id, :env, :logger, :proxy_address
13
+
14
+ def initialize(params = {})
15
+ merchant_id = params[:merchant_id]
16
+ env = params[:environment]
17
+ raise InvalidMerchantId unless merchant_id =~ /\{[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}\}/i
18
+ raise InvalidEnvironment unless (env == :homologation || env == :production)
19
+
20
+ @merchant_id = merchant_id
21
+ @env = env
22
+ @logger = params[:logger]
23
+ @proxy_address = params[:proxy_address]
24
+ end
25
+
26
+ def production?
27
+ @env == :production
28
+ end
29
+
30
+ def homologation?
31
+ @env == :homologation
32
+ end
33
+
34
+ def url_for(method_name)
35
+ braspag_url = production? ? PRODUCTION_URL : HOMOLOGATION_URL
36
+ protected_card_url = production? ? PROTECTED_CARD_PRODUCTION_URL : PROTECTED_CARD_HOMOLOGATION_URL
37
+
38
+ braspag_info_url = if production?
39
+ braspag_url + "/webservices/pagador/pedido.asmx"
40
+ else
41
+ braspag_url + "/pagador/webservice/pedido.asmx"
42
+ end
43
+
44
+ case method_name
45
+ when :authorize
46
+ braspag_url + "/webservices/pagador/Pagador.asmx/Authorize"
47
+ when :void
48
+ braspag_url + "/webservices/pagador/Pagador.asmx/VoidTransaction"
49
+ when :capture
50
+ braspag_url + "/webservices/pagador/Pagador.asmx/Capture"
51
+ when :generate_billet
52
+ braspag_url + "/webservices/pagador/Boleto.asmx/CreateBoleto"
53
+ when :generate_eft
54
+ braspag_url + "/pagador/passthru.asp"
55
+ when :info_billet
56
+ braspag_info_url + "/GetDadosBoleto"
57
+ when :info_card
58
+ braspag_info_url + "/GetDadosCartao"
59
+ when :info
60
+ braspag_info_url + "/GetDadosPedido"
61
+ when :encrypt
62
+ braspag_url + "/BraspagGeneralService/BraspagGeneralService.asmx"
63
+ when :archive_card
64
+ protected_card_url + "/CartaoProtegido.asmx?wsdl"
65
+ when :get_card
66
+ protected_card_url + "/CartaoProtegido.asmx/GetCreditCard"
67
+ when :recurrency
68
+ protected_card_url + "/CartaoProtegido.asmx?wsdl"
69
+ end
70
+ end
71
+
72
+ def convert_to(method)
73
+ {:merchant_id => self.merchant_id}
74
+ end
75
+
76
+ def post(method, *args)
77
+ data = convert_to(method)
78
+ args.each do |field|
79
+ data.merge!(field.convert_to(method))
80
+ end
81
+
82
+ data = Converter::to(method, data)
83
+
84
+ response = Braspag::Poster.new(self, self.url_for(method))
85
+ .do_post(method, data)
86
+
87
+ response = Converter::from(method, response.body)
88
+
89
+ args.each do |field|
90
+ field.populate!(method, response)
91
+ end
92
+
93
+ response
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,160 @@
1
+ module Braspag
2
+ class Converter
3
+ def self.decimal_to_string(value)
4
+ ("%.2f" % value.to_f).gsub('.', ',')
5
+ end
6
+
7
+ def self.string_to_decimal(value)
8
+ BigDecimal.new(value.to_s.gsub(".",""))
9
+ end
10
+
11
+ def self.to_map(document, map = {})
12
+ document = Nokogiri::XML(document)
13
+
14
+ map.each do |key, value|
15
+ if value.is_a?(String) || value.nil?
16
+ value = key if value.nil?
17
+
18
+ new_value = document.search(value).first
19
+
20
+ if new_value.nil?
21
+ map[key] = nil
22
+ else
23
+ new_value = new_value.content.to_s
24
+ map[key] = new_value unless new_value == ""
25
+ map[key] = nil if new_value == ""
26
+ end
27
+
28
+ elsif value.is_a?(Proc)
29
+ map[key] = value.call(document)
30
+ end
31
+ end
32
+
33
+ map
34
+ end
35
+
36
+ def self.payment_method_name?(code)
37
+ Braspag::PAYMENT_METHOD.key(code.to_s.to_i)
38
+ end
39
+
40
+ def self.status_name?(code)
41
+ Braspag::STATUS_PAYMENT.key(code.to_s.to_i)
42
+ end
43
+
44
+ def self.to_hash(format, params)
45
+ data = {}
46
+ format.each do |k, v|
47
+ case k
48
+ when :amount
49
+ data[v] = self.decimal_to_string(params[:amount])
50
+ else
51
+ data[v] = params[k] || ""
52
+ end
53
+ end
54
+ data
55
+ end
56
+
57
+ def self.to(method, data)
58
+ self.send("to_#{method}", data)
59
+ end
60
+
61
+ def self.to_authorize(params)
62
+ self.to_hash({
63
+ :merchant_id => "merchantId",
64
+ :order_id => "orderId",
65
+ :customer_name => "customerName",
66
+ :amount => "amount",
67
+ :payment_method => "paymentMethod",
68
+ :holder => "holder",
69
+ :card_number => "cardNumber",
70
+ :expiration => "expiration",
71
+ :security_code => "securityCode",
72
+ :number_payments => "numberPayments",
73
+ :type => "typePayment",
74
+ }, params)
75
+ end
76
+
77
+ def self.to_capture(params)
78
+ self.to_hash({
79
+ :merchant_id => "merchantId",
80
+ :order_id => "orderId"
81
+ }, params)
82
+ end
83
+
84
+ def self.to_void(params)
85
+ self.to_hash({
86
+ :merchant_id => "merchantId",
87
+ :order_id => "orderId"
88
+ }, params)
89
+ end
90
+
91
+ def self.to_generate_billet(params)
92
+ self.to_hash({
93
+ :merchant_id => "merchantId",
94
+ :order_id => "orderId",
95
+ :customer_name => "customerName",
96
+ :customer_id => "customerIdNumber",
97
+ :amount => "amount",
98
+ :payment_method => "paymentMethod",
99
+ :number => "boletoNumber",
100
+ :instructions => "instructions",
101
+ :expiration_date => "expirationDate",
102
+ :emails => "emails"
103
+ }, params)
104
+ end
105
+
106
+ def self.from(method, data)
107
+ self.send("from_#{method}", data)
108
+ end
109
+
110
+ def self.from_authorize(data)
111
+ to_map(data, {
112
+ :amount => nil,
113
+ :number => "authorisationNumber",
114
+ :message => 'message',
115
+ :return_code => 'returnCode',
116
+ :status => 'status',
117
+ :transaction_id => "transactionId"
118
+ })
119
+ end
120
+
121
+ def self.from_capture(data)
122
+ to_map(data, {
123
+ :amount => nil,
124
+ :message => 'message',
125
+ :return_code => 'returnCode',
126
+ :status => 'status',
127
+ :transaction_id => "transactionId"
128
+ })
129
+ end
130
+
131
+ def self.from_void(data)
132
+ to_map(data, {
133
+ :order_id => "orderId",
134
+ :amount => nil,
135
+ :message => 'message',
136
+ :return_code => 'returnCode',
137
+ :status => 'status',
138
+ :transaction_id => "transactionId"
139
+ })
140
+ end
141
+
142
+ def self.from_generate_billet(data)
143
+ to_map(data, {
144
+ :url => nil,
145
+ :amount => nil,
146
+ :number => "boletoNumber",
147
+ :expiration_date => Proc.new { |document|
148
+ begin
149
+ Date.parse(document.search("expirationDate").first.to_s)
150
+ rescue
151
+ nil
152
+ end
153
+ },
154
+ :return_code => "returnCode",
155
+ :status => nil,
156
+ :message => nil
157
+ })
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,36 @@
1
+ module Braspag
2
+ class Customer
3
+ include ::ActiveAttr::Model
4
+
5
+ attr_accessor :name, :document, :email
6
+
7
+ [:purchase, :generate, :authorize, :archive, :recurrency ].each do |check_on|
8
+ validates :name, :length => {:minimum => 1, :maximum => 100, :on => check_on }
9
+ validates :email, :length => {:minimum => 1, :maximum => 255, :on => check_on, :allow_blank => true}
10
+ validates :document, :length => {:minimum => 11, :maximum => 18, :on => check_on, :allow_blank => true}
11
+ end
12
+
13
+ def convert_to(method)
14
+ self.send("to_#{method}")
15
+ end
16
+
17
+ def to_authorize
18
+ {
19
+ :customer_name => self.name.to_s
20
+ }
21
+ end
22
+
23
+ def to_generate_billet
24
+ {
25
+ :customer_name => self.name.to_s,
26
+ :customer_id => self.document.to_s,
27
+ :emails => self.email.to_s
28
+ }
29
+
30
+ end
31
+
32
+ def populate!(method)
33
+
34
+ end
35
+ end
36
+ end