Class: Braspag::ProtectedCreditCard

Inherits:
PaymentMethod show all
Defined in:
lib/baby-braspag/protected_credit_card.rb

Constant Summary collapse

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"
}
SAVE_PROTECTED_CARD_URI =
"/CartaoProtegido.asmx?wsdl"
GET_PROTECTED_CARD_URI =
"/CartaoProtegido.asmx/GetCreditCard"
JUST_CLICK_SHOP_URI =
"/CartaoProtegido.asmx?wsdl"

Constants inherited from PaymentMethod

Braspag::PaymentMethod::PAYMENT_METHODS

Class Method Summary collapse

Methods inherited from PaymentMethod

check_params, normalize_params, payment_method_from_id, valid_order_id?

Class Method Details

.check_just_click_shop_params(params) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/baby-braspag/protected_credit_card.rb', line 124

def self.check_just_click_shop_params(params)
  just_click_shop_attributes = [:request_id, :customer_name, :order_id, :amount, :payment_method,
  :number_installments, :payment_type, :just_click_key, :security_code]

  just_click_shop_attributes.each do |param|
    raise IncompleteParams if params[param].nil?
  end

  raise InvalidSecurityCode if params[:security_code].to_s.size < 1 || params[:security_code].to_s.size > 4

  raise InvalidNumberInstallments if params[:number_installments].to_i < 1 || params[:number_installments].to_i > 99

end

.check_protected_card_params(params) ⇒ Object

Raises:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/baby-braspag/protected_credit_card.rb', line 105

def self.check_protected_card_params(params)
  [:request_id, :customer_name, :holder, :card_number, :expiration].each do |param|
    raise IncompleteParams if params[param].nil?
  end

  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,4})$/
  raise InvalidExpirationDate unless matches
  begin
    year = matches[2].to_i
    year = "20#{year}" if year.size == 2

    Date.new(year.to_i, matches[1].to_i)
  rescue ArgumentError
    raise InvalidExpirationDate
  end
end

.get(just_click_key) ⇒ Object

request the credit card info in Braspag PCI Compliant



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/baby-braspag/protected_credit_card.rb', line 54

def self.get(just_click_key)
  connection = Braspag::Connection.instance

  raise InvalidJustClickKey unless valid_just_click_key?(just_click_key)

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

  response = Braspag::Poster.new(self.get_protected_card_url).do_post(:get_protected_card, data)

  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

.get_protected_card_urlObject



146
147
148
# File 'lib/baby-braspag/protected_credit_card.rb', line 146

def self.get_protected_card_url
  Braspag::Connection.instance.protected_card_url + GET_PROTECTED_CARD_URI
end

.just_click_shop(params = {}) ⇒ Object

Raises:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/baby-braspag/protected_credit_card.rb', line 74

def self.just_click_shop(params = {})
  connection = Braspag::Connection.instance
  params[:merchant_id] = connection.merchant_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]]
    when :amount
      data['justClickShopRequestWS'][v] = ("%.2f" % params[k].to_f).gsub('.', '')
    else
      data['justClickShopRequestWS'][v] = params[k] || ""
    end
  end

  client = savon_client(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

.just_click_shop_urlObject



150
151
152
# File 'lib/baby-braspag/protected_credit_card.rb', line 150

def self.just_click_shop_url
  Braspag::Connection.instance.protected_card_url + JUST_CLICK_SHOP_URI
end

.save(params = {}) ⇒ Object

saves credit card in Braspag PCI Compliant



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/baby-braspag/protected_credit_card.rb', line 31

def self.save(params = {})
  connection = Braspag::Connection.instance
  params[:merchant_id] = connection.merchant_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(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

.save_protected_card_urlObject



142
143
144
# File 'lib/baby-braspag/protected_credit_card.rb', line 142

def self.save_protected_card_url
  Braspag::Connection.instance.protected_card_url + SAVE_PROTECTED_CARD_URI
end

.savon_client(url) ⇒ Object



154
155
156
157
158
# File 'lib/baby-braspag/protected_credit_card.rb', line 154

def self.savon_client url
  s = Savon::Client.new(url)
  s.http.proxy = Braspag.proxy_address if Braspag.proxy_address
  s
end

.valid_just_click_key?(just_click_key) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/baby-braspag/protected_credit_card.rb', line 138

def self.valid_just_click_key?(just_click_key)
  (just_click_key.is_a?(String) && just_click_key.size == 36)
end