Class: Braspag::CreditCard

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

Constant Summary collapse

MAPPING =
{
  :merchant_id => "merchantId",
  :order => 'order',
  :order_id => "orderId",
  :customer_name => "customerName",
  :amount => "amount",
  :payment_method => "paymentMethod",
  :holder => "holder",
  :card_number => "cardNumber",
  :expiration => "expiration",
  :security_code => "securityCode",
  :number_payments => "numberPayments",
  :type => "typePayment"
}
AUTHORIZE_URI =
"/webservices/pagador/Pagador.asmx/Authorize"
CAPTURE_URI =
"/webservices/pagador/Pagador.asmx/Capture"
PARTIAL_CAPTURE_URI =
"/webservices/pagador/Pagador.asmx/CapturePartial"
CANCELLATION_URI =
"/webservices/pagador/Pagador.asmx/VoidTransaction"
PRODUCTION_INFO_URI =
"/webservices/pagador/pedido.asmx/GetDadosCartao"
HOMOLOGATION_INFO_URI =
"/pagador/webservice/pedido.asmx/GetDadosCartao"

Constants inherited from PaymentMethod

PaymentMethod::PAYMENT_METHODS

Class Method Summary collapse

Methods inherited from PaymentMethod

normalize_params, payment_method_from_id, valid_order_id?

Class Method Details

.authorize(params = {}) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/baby-braspag/credit_card.rb', line 26

def self.authorize(params = {})
  connection = Braspag::Connection.instance
  params[:merchant_id] = connection.merchant_id

  self.check_params(params)

  data = {}
  MAPPING.each do |k, v|
    case k
    when :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

  response = Braspag::Poster.new(self.authorize_url).do_post(:authorize, data)

  Utils::convert_to_map(response.body, {
      :amount => nil,
      :number => "authorisationNumber",
      :message => 'message',
      :return_code => 'returnCode',
      :status => 'status',
      :transaction_id => "transactionId"
    })
end

.authorize_urlObject


194
195
196
# File 'lib/baby-braspag/credit_card.rb', line 194

def self.authorize_url
  Braspag::Connection.instance.braspag_url + AUTHORIZE_URI
end

.cancellation_urlObject


206
207
208
# File 'lib/baby-braspag/credit_card.rb', line 206

def self.cancellation_url
  Braspag::Connection.instance.braspag_url + CANCELLATION_URI
end

.capture(order_id) ⇒ Object

Raises:


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

def self.capture(order_id)
  connection = Braspag::Connection.instance
  merchant_id = connection.merchant_id

  raise InvalidOrderId unless self.valid_order_id?(order_id)

  data = {
    MAPPING[:order_id] => order_id,
    MAPPING[:merchant_id] => merchant_id
  }

  response = Braspag::Poster.new(self.capture_url).do_post(:capture, data)

  Utils::convert_to_map(response.body, {
      :amount => nil,
      :number => "authorisationNumber",
      :message => 'message',
      :return_code => 'returnCode',
      :status => 'status',
      :transaction_id => "transactionId"
    })
end

.capture_urlObject


198
199
200
# File 'lib/baby-braspag/credit_card.rb', line 198

def self.capture_url
  Braspag::Connection.instance.braspag_url + CAPTURE_URI
end

.check_params(params) ⇒ Object

Raises:


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/baby-braspag/credit_card.rb', line 146

def self.check_params(params)
  super

  [:customer_name, :holder, :card_number, :expiration, :security_code, :number_payments, :type].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

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

  raise InvalidNumberPayments if params[:number_payments].to_i < 1 || params[:number_payments].to_i > 99
end

.get(just_click_key) ⇒ Object

DEPRECATED: Please use ProtectedCreditCard.get instead.


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

def self.get(just_click_key)
  warn "[DEPRECATION] `CreditCard.get` is deprecated.  Please use `ProtectedCreditCard.get` instead."
  ProtectedCreditCard.get(just_click_key)
end

.info(order_id) ⇒ Object

Raises:


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/baby-braspag/credit_card.rb', line 126

def self.info(order_id)
  connection = Braspag::Connection.instance

  raise InvalidOrderId unless self.valid_order_id?(order_id)

  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",
      :certified => "Autenticada",
      :autorization_number => "NumeroAutorizacao",
      :card_number => "NumeroCartao",
      :transaction_number => "NumeroTransacao"
    })

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

.info_urlObject


189
190
191
192
# File 'lib/baby-braspag/credit_card.rb', line 189

def self.info_url
  connection = Braspag::Connection.instance
  connection.braspag_url + (connection.production? ? PRODUCTION_INFO_URI : HOMOLOGATION_INFO_URI)
end

.just_click_shop(params = {}) ⇒ Object

DEPRECATED: Please use ProtectedCreditCard.just_click_shop instead.


184
185
186
187
# File 'lib/baby-braspag/credit_card.rb', line 184

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

.partial_capture(order_id, amount) ⇒ Object

Raises:


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/baby-braspag/credit_card.rb', line 79

def self.partial_capture(order_id, amount)
  connection = Braspag::Connection.instance
  merchant_id = connection.merchant_id

  raise InvalidOrderId unless self.valid_order_id?(order_id)

  data = {
    MAPPING[:order_id] => order_id,
    MAPPING[:merchant_id] => merchant_id,
    "captureAmount" => Utils.convert_decimal_to_string(amount)
  }

  response = Braspag::Poster.new(self.partial_capture_url).do_post(:partial_capture, data)

  Utils::convert_to_map(response.body, {
      :amount => nil,
      :number => "authorisationNumber",
      :message => 'message',
      :return_code => 'returnCode',
      :status => 'status',
      :transaction_id => "transactionId"
    })
end

.partial_capture_urlObject


202
203
204
# File 'lib/baby-braspag/credit_card.rb', line 202

def self.partial_capture_url
  Braspag::Connection.instance.braspag_url + PARTIAL_CAPTURE_URI
end

.save(params) ⇒ Object

DEPRECATED: Please use ProtectedCreditCard.save instead.


172
173
174
175
# File 'lib/baby-braspag/credit_card.rb', line 172

def self.save(params)
  warn "[DEPRECATION] `CreditCard.save` is deprecated.  Please use `ProtectedCreditCard.save` instead."
  ProtectedCreditCard.save(params)
end

.void(order_id) ⇒ Object

Raises:


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

def self.void(order_id)
  connection = Braspag::Connection.instance
  merchant_id = connection.merchant_id

  raise InvalidOrderId unless self.valid_order_id?(order_id)

  data = {
    MAPPING[:order] => order_id,
    MAPPING[:merchant_id] => merchant_id
  }

  response = Braspag::Poster.new(self.cancellation_url).do_post(:void, data)

  Utils::convert_to_map(response.body, {
      :amount => nil,
      :number => "authorisationNumber",
      :message => 'message',
      :return_code => 'returnCode',
      :status => 'status',
      :transaction_id => "transactionId"
    })
end