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



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/baby-braspag/protected_credit_card.rb', line 135

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:



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/baby-braspag/protected_credit_card.rb', line 116

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/baby-braspag/protected_credit_card.rb', line 60

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



157
158
159
# File 'lib/baby-braspag/protected_credit_card.rb', line 157

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

.just_click_shop(params = {}) ⇒ Object

Raises:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/baby-braspag/protected_credit_card.rb', line 80

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

  data_for_logging = data['justClickShopRequestWS'].dup
  data_for_logging['SecurityCode'] = data_for_logging['SecurityCode'].gsub(/./, '*')
  data_for_logging['JustClickKey'] = data_for_logging['JustClickKey'].gsub(/\h/, 'X')

  Braspag.logger.info("[Braspag] #just_click_shop, data: #{data_for_logging}") if Braspag.logger

  client = savon_client(self.just_click_shop_url)
  response = client.call(:just_click_shop, :message => data)

  Braspag.logger.info("[Braspag] #just_click_shop returns: #{response}") if Braspag.logger

  response.to_hash[:just_click_shop_response][:just_click_shop_result]
end

.just_click_shop_urlObject



161
162
163
# File 'lib/baby-braspag/protected_credit_card.rb', line 161

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

.redact_just_click_key(response) ⇒ Object

Internal: Redact the JustClickKey value from a XML response.

response - the XML response string to be parsed.

Example

redact_just_click_key('<JustClickKey>{070071E9-1F73-4C85-B1E4-D8040A627DED}</JustClickKey>')
# => '<JustClickKey>{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}</JustClickKey>'

Returns a String.



179
180
181
# File 'lib/baby-braspag/protected_credit_card.rb', line 179

def self.redact_just_click_key(response)
  response.to_s.gsub(/(?<=<JustClickKey>)(.*?)(?=<\/JustClickKey>)/) { |key| key.gsub(/\h/, 'X') }
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
52
53
54
55
56
57
# 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

  data_for_logging = data['saveCreditCardRequestWS'].dup
  data_for_logging['CardNumber'] = "************%s" % data_for_logging['CardNumber'][-4..-1]

  Braspag.logger.info("[Braspag] #save_credit_card, data: #{data_for_logging}") if Braspag.logger

  client = savon_client(self.save_protected_card_url)
  response = client.call(:save_credit_card, :message => data)

  # We do not want to let any sensitive data exposed on log files.
  redacted_response = redact_just_click_key(response)

  Braspag.logger.info("[Braspag] #save_credit_card returns: #{redacted_response}") if Braspag.logger

  response.to_hash[:save_credit_card_response][:save_credit_card_result]
end

.save_protected_card_urlObject



153
154
155
# File 'lib/baby-braspag/protected_credit_card.rb', line 153

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

.savon_client(url) ⇒ Object



165
166
167
# File 'lib/baby-braspag/protected_credit_card.rb', line 165

def self.savon_client(url)
  Braspag::Connection.instance.savon_client url
end

.valid_just_click_key?(just_click_key) ⇒ Boolean

Returns:

  • (Boolean)


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

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