Class: Braspag::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/cbraspag/core/order.rb,
lib/cbraspag/payment/eft.rb,
lib/cbraspag/payment/billet.rb,
lib/cbraspag/core/connection.rb,
lib/cbraspag/payment/credit_card.rb,
lib/cbraspag/payment/recurrency_credit_card.rb

Defined Under Namespace

Classes: InvalidEnvironment, InvalidMerchantId

Constant Summary collapse

MAPPING_EFT =
{
  :merchant_id => "Id_Loja",
  :order_id => "VENDAID",
  :customer_name => "nome",
  :customer_id => "CPF",
  :amount => "VALOR",
  :payment_method => "CODPAGAMENTO",
  :installments => "PARCELAS",
  :has_interest => "TIPOPARCELADO"
}
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"
PROTECTED_CARD_MAPPING =
{
  :request_id => "RequestId",
  :merchant_id => "MerchantKey",
  :customer_name => "CustomerName",
  :holder => "CardHolder",
  :card_number => "CardNumber",
  :expiration => "CardExpiration"
}
JUST_CLICK_MAPPING =
{
  :request_id => "RequestId",
  :merchant_id => "MerchantKey",
  :customer_name => "CustomerName",
  :order_id => "OrderId",
  :amount => "Amount",
  :payment_method => "PaymentMethod",
  :number_installments => "NumberInstallments",
  :payment_type => "PaymentType",
  :just_click_key => "JustClickKey",
  :security_code => "SecurityCode"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Connection

Returns a new instance of Connection.

Raises:



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cbraspag/core/connection.rb', line 14

def initialize(params = {})
  merchant_id = params[:merchant_id]
  env = params[:environment]
  raise InvalidMerchantId unless merchant_id =~ /\{[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}\}/i
  raise InvalidEnvironment unless (env == :homologation || env == :production)
  
  @merchant_id = merchant_id
  @env = env
  @logger = params[:logger]
  @proxy_address = params[:proxy_address]
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



12
13
14
# File 'lib/cbraspag/core/connection.rb', line 12

def env
  @env
end

#loggerObject (readonly)

Returns the value of attribute logger.



12
13
14
# File 'lib/cbraspag/core/connection.rb', line 12

def logger
  @logger
end

#merchant_idObject (readonly)

Returns the value of attribute merchant_id.



12
13
14
# File 'lib/cbraspag/core/connection.rb', line 12

def merchant_id
  @merchant_id
end

#proxy_addressObject (readonly)

Returns the value of attribute proxy_address.



12
13
14
# File 'lib/cbraspag/core/connection.rb', line 12

def proxy_address
  @proxy_address
end

Class Method Details

.generate_eft(order, eft) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cbraspag/payment/eft.rb', line 16

def self.generate_eft(order, eft)
  
  params[:merchant_id] = connection.merchant_id


  data = {}

  MAPPING.each do |k, v|
    case k
    when :payment_method
      data[v] = PAYMENT_METHODS[params[:payment_method]]
    when :amount
      data[v] = Utils.convert_decimal_to_string(params[:amount])
    else
      data[v] = params[k] || ""
    end
  end

  html = "<form id=\"form_tef_#{params[:order_id]}\" name=\"form_tef_#{params[:order_id]}\" action=\"#{self.action_url}\" method=\"post\">"

  if crypto_strategy.nil?
    data.each do |key, value|
      html << "<input type=\"text\" name=\"#{key}\" value=\"#{value}\" />"
    end
  else
    data.delete("Id_Loja")
    html << "<input type=\"text\" name=\"Id_Loja\" value=\"#{params[:merchant_id]}\" />"
    html << "<input type=\"text\" name=\"crypt\" value=\"#{crypto_strategy.encrypt(data)}\" />"
  end

  html << "</form><script type=\"text/javascript\" charset=\"utf-8\">document.forms[\"form_tef_#{params[:order_id]}\"].submit();</script>"

  html
end

Instance Method Details

#archive(credit_card, customer, request_id) ⇒ Object

saves credit card in Braspag PCI Compliant



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cbraspag/payment/recurrency_credit_card.rb', line 27

def archive(credit_card, customer, request_id)

  self.check_protected_card_params(params)

  data = { 'saveCreditCardRequestWS' => {} }

  PROTECTED_CARD_MAPPING.each do |k, v|
    data['saveCreditCardRequestWS'][v] = params[k] || ""
  end


 client = Savon::Client.new(self.save_protected_card_url)
 response = client.request(:web, :save_credit_card) do
   soap.body = data
 end

  response.to_hash[:save_credit_card_response][:save_credit_card_result]

end

#authorize(order, credit_card) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/cbraspag/payment/credit_card.rb', line 9

def authorize(order, credit_card)
  response = self.post(:authorize, order, credit_card)
  
  status = (response[:status] == "0" || response[:status] == "1")

  ActiveMerchant::Billing::Response.new(status,
               response[:message],
               response,
               :test => homologation?,
               :authorization => response[:number])
end

#capture(order) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cbraspag/payment/credit_card.rb', line 21

def capture(order)
  response = self.post(:capture, order)
  
  status = (response[:status] == "0")
  
  ActiveMerchant::Billing::Response.new(status,
               response[:message],
               response,
               :test => homologation?,
               :authorization => response[:number])
end

#convert(method_name, direction, args) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cbraspag/core/connection.rb', line 84

def convert(method_name, direction, args)
  target = case method_name
  when :authorize, :void, :capture, :archive_card, :get_card, :recurrency
    CreditCard
  when :generate_billet
    Billet
  when :generate_eft
    EFT
  when :info_billet, :info_credit_card, :info
    Order
  when :encrypt
    Crypto::Webservice
  end
  
  target.send("#{direction}_#{method_name}", *([self] + args))
end

#generate_billet(order, billet) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/cbraspag/payment/billet.rb', line 5

def generate_billet(order, billet)
  response = self.post(:generate_billet, order, billet)
  status = (response[:status] == "0")

  ActiveMerchant::Billing::Response.new(status,
               response[:message],
               response,
               :test => homologation?,
               :authorization => response[:number])
end

#get(order) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cbraspag/core/order.rb', line 3

def get(order)
  response = self.post(:info, order)
  
  if (  response.size == 0 || 
        !response.fetch(:error_code, nil).nil? || 
        response.fetch(:status, nil).nil?
     )
    return ActiveMerchant::Billing::Response.new(false,
                 response.fetch(:error_message, ''),
                 response,
                 :test => homologation?)
  end
  
  case order.payment_method_type?
  when :billet
    self.post(:info_billet, order)
  when :credit_card
    self.post(:info_credit_card, order)
  end
  
  ActiveMerchant::Billing::Response.new(true,
               'OK',
               response,
               :test => homologation?)
end

#get_recurrency(credit_card) ⇒ Object

request the credit card info in Braspag PCI Compliant

Raises:

  • (InvalidJustClickKey)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cbraspag/payment/recurrency_credit_card.rb', line 48

def get_recurrency(credit_card)

  raise InvalidJustClickKey unless valid_just_click_key?(just_click_key)

  data = { 'getCreditCardRequestWS' => {:loja => connection.merchant_id, :justClickKey => just_click_key} }

  request = ::HTTPI::Request.new(self.get_protected_card_url)
  request.body = { 'getCreditCardRequestWS' => {:loja => connection.merchant_id, :justClickKey => just_click_key} }

  response = ::HTTPI.post(request)

  response = Utils::convert_to_map(response.body, {
      :holder => "CardHolder",
      :card_number => "CardNumber",
      :expiration => "CardExpiration",
      :masked_card_number => "MaskedCardNumber"
    })

  raise UnknownError if response[:card_number].nil?
  response
end

#homologation?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/cbraspag/core/connection.rb', line 30

def homologation?
  @env == :homologation
end

#post(method_name, *args) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cbraspag/core/connection.rb', line 72

def post(method_name, *args)
  response = Braspag::Poster.new(
    self, 
    self.url_for(method_name)
  ).do_post(
    method_name, 
    self.convert(method_name, :to, args)
  )
  
  self.convert(method_name, :from, args + [response] )
end

#production?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/cbraspag/core/connection.rb', line 26

def production?
  @env == :production
end

#purchase(order, credit_card) ⇒ Object



3
4
5
6
7
# File 'lib/cbraspag/payment/credit_card.rb', line 3

def purchase(order, credit_card)
  resp = self.authorize(order, credit_card)
  resp = self.capture(order) if resp.success?
  resp
end

#recurrency(order, credit_card, request_id) ⇒ Object

Raises:

  • (InvalidOrderId)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cbraspag/payment/recurrency_credit_card.rb', line 70

def recurrency(order, credit_card, request_id)

  self.check_just_click_shop_params(params)

  order_id = params[:order_id]
  raise InvalidOrderId unless self.valid_order_id?(order_id)

  data = { 'justClickShopRequestWS' => {} }

  JUST_CLICK_MAPPING.each do |k, v|
    case k
    when :payment_method
      data['justClickShopRequestWS'][v] = Braspag::Connection.instance.homologation? ? PAYMENT_METHODS[:braspag] : PAYMENT_METHODS[params[:payment_method]]
    else
      data['justClickShopRequestWS'][v] = params[k] || ""
    end
  end

  client = Savon::Client.new(self.just_click_shop_url)
  response = client.request(:web, :just_click_shop) do
    soap.body = data
  end

  response.to_hash[:just_click_shop_response][:just_click_shop_result]

end

#url_for(method_name) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cbraspag/core/connection.rb', line 34

def url_for(method_name)
  braspag_url = production? ? PRODUCTION_URL : HOMOLOGATION_URL
  protected_card_url = production? ? PROTECTED_CARD_PRODUCTION_URL : PROTECTED_CARD_HOMOLOGATION_URL

  braspag_info_url = if production?
    braspag_url + "/webservices/pagador/pedido.asmx"
  else
    braspag_url + "/pagador/webservice/pedido.asmx"
  end
  
  case method_name
  when :authorize
    braspag_url + "/webservices/pagador/Pagador.asmx/Authorize"
  when :void
    braspag_url + "/webservices/pagador/Pagador.asmx/VoidTransaction"
  when :capture
    braspag_url + "/webservices/pagador/Pagador.asmx/Capture"
  when :generate_billet
    braspag_url + "/webservices/pagador/Boleto.asmx/CreateBoleto"
  when :generate_eft
    braspag_url + "/pagador/passthru.asp"
  when :info_billet
    braspag_info_url + "/GetDadosBoleto"
  when :info_credit_card
    braspag_info_url + "/GetDadosCartao"
  when :info
    braspag_info_url + "/GetDadosPedido"
  when :encrypt
    braspag_url + "/BraspagGeneralService/BraspagGeneralService.asmx"
  when :archive_card
    protected_card_url + "/CartaoProtegido.asmx?wsdl"
  when :get_card
    protected_card_url + "/CartaoProtegido.asmx/GetCreditCard"
  when :recurrency
    protected_card_url + "/CartaoProtegido.asmx?wsdl"
  end
end

#void(order, partial = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/cbraspag/payment/credit_card.rb', line 33

def void(order, partial=nil)
  response = self.post(:void, order)
  
  status = (response[:status] == "0")
  
  ActiveMerchant::Billing::Response.new(status,
               response[:message],
               response,
               :test => homologation?)
end