Class: MundipaggApi

Inherits:
Object
  • Object
show all
Defined in:
lib/mundipagg/MundipaggApi.rb

Constant Summary collapse

@@SERVICE_URL_PRODUCTION =

URL de production

'https://transactionv2.mundipaggone.com/Sale/'
@@SERVICE_URL_STAGING =

URL de homologation

'https://stagingv2.mundipaggone.com/Sale/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment = :staging, merchantKey) ⇒ MundipaggApi

Returns a new instance of MundipaggApi.



9
10
11
12
13
# File 'lib/mundipagg/MundipaggApi.rb', line 9

def initialize(environment=:staging, merchantKey)
  @serviceEnvironment = environment
  @merchantKey = merchantKey
  @@SERVICE_HEADERS = {:MerchantKey => "#{@merchantKey}", :Accept => 'application/json', :"Content-Type" => 'application/json'}
end

Instance Attribute Details

#merchantKeyObject (readonly)

Returns the value of attribute merchantKey.



7
8
9
# File 'lib/mundipagg/MundipaggApi.rb', line 7

def merchantKey
  @merchantKey
end

#serviceEnvironmentObject (readonly)

Returns the value of attribute serviceEnvironment.



5
6
7
# File 'lib/mundipagg/MundipaggApi.rb', line 5

def serviceEnvironment
  @serviceEnvironment
end

Instance Method Details

#Cancel(cancelSaleRequest) ⇒ Object

eh uma forma de desfazer uma transação com cartao de credito mesmo a transacao sendo capturada



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/mundipagg/MundipaggApi.rb', line 239

def Cancel(cancelSaleRequest)
  saleHash = cancelSaleRequest.to_json
  saleHash['CreditCardTransactionCollection'] = []

  begin
    if cancelSaleRequest.CreditCardTransactionCollection != nil
      cancelSaleRequest.CreditCardTransactionCollection.each do |creditCard|
        c = creditCard.to_json
        saleHash['CreditCardTransactionCollection'] << c
      end
    end
  rescue Exception => e
    puts e.message
  end
  if @serviceEnvironment == :staging
    url = @@SERVICE_URL_STAGING + '/Cancel'
  elsif @serviceEnvironment == :production
    url = @@SERVICE_URL_PRODUCTION + '/Cancel'
  end
  postRequest(saleHash.to_json, url)
end

#Capture(captureRequest) ⇒ Object

confirmacao de uma transacao de cartao de credito que ja fora autorizada



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/mundipagg/MundipaggApi.rb', line 262

def Capture(captureRequest)
  saleHash = captureRequest.to_json
  saleHash['CreditCardTransactionCollection'] = []

  begin
    if captureRequest.CreditCardTransactionCollection != nil
      captureRequest.CreditCardTransactionCollection.each do |creditCard|
        c = creditCard.to_json
        saleHash['CreditCardTransactionCollection'] << c
      end
    end
  rescue Exception => e
    puts e.message
  end
  if @serviceEnvironment == :staging
    url = @@SERVICE_URL_STAGING + '/Capture'
  elsif @serviceEnvironment == :production
    url = @@SERVICE_URL_PRODUCTION + '/Capture'
  end
  postRequest(saleHash.to_json, url)
end

#CreateSale(createSaleRequest) ⇒ Object

criar uma transacao na plataforma One utilizando um ou mais meios de pagamento



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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/mundipagg/MundipaggApi.rb', line 46

def CreateSale(createSaleRequest)

  saleHash = createSaleRequest.to_json

  saleHash['BoletoTransactionCollection'] = []

  saleHash['CreditCardTransactionCollection'] = []

  saleHash['ShoppingCartCollection'] = []

  begin
    # transforma a colecao de boleto em json
    if createSaleRequest.BoletoTransactionCollection.any? == false || createSaleRequest.BoletoTransactionCollection.nil?
      saleHash['BoletoTransactionCollection'] = nil

    else
      createSaleRequest.BoletoTransactionCollection.each_with_index do |boleto, index|
        b = boleto.to_json
        saleHash['BoletoTransactionCollection'] << b

        if boleto.Options.to_json.any?
          boleto_options = boleto.Options.to_json
          saleHash['BoletoTransactionCollection'][index]['Options'] = boleto_options
        else
          saleHash['BoletoTransactionCollection'][index]['Options'] = nil
        end

        if boleto.BillingAddress.to_json.any?
          boleto_billing_address = boleto.BillingAddress.to_json
          saleHash['BoletoTransactionCollection'][index]['BillingAddress'] = boleto_billing_address
        else
          saleHash['BoletoTransactionCollection'][index]['BillingAddress'] = nil
        end
      end
    end

    # transforma a colecao de cartao de credito em json
    if createSaleRequest.CreditCardTransactionCollection.any? == false || createSaleRequest.CreditCardTransactionCollection.nil?
      saleHash['CreditCardTransactionCollection'] = nil
    else
      createSaleRequest.CreditCardTransactionCollection.each_with_index do |creditCard, index|
        c = creditCard.to_json
        saleHash['CreditCardTransactionCollection'] << c

        if creditCard.Options.to_json.any?
          credit_card_options = creditCard.Options.to_json
          saleHash['CreditCardTransactionCollection'][index]['Options'] = credit_card_options
        else
          saleHash['CreditCardTransactionCollection'][index]['Options'] = nil
        end

        if creditCard.Recurrency.to_json.any?
          credit_card_recurrency = creditCard.Recurrency.to_json
          saleHash['CreditCardTransactionCollection'][index]['Recurrency'] = credit_card_recurrency
        else
          saleHash['CreditCardTransactionCollection'][index]['Recurrency'] = nil
        end

        if creditCard.CreditCard.to_json.any?
          credit_card_item = creditCard.CreditCard.to_json
          saleHash['CreditCardTransactionCollection'][index]['CreditCard'] = credit_card_item

          if creditCard.CreditCard.BillingAddress.to_json.any?
            credit_card_billing_address = creditCard.CreditCard.BillingAddress.to_json
            saleHash['CreditCardTransactionCollection'][index]['CreditCard']['BillingAddress'] = credit_card_billing_address
          else
            saleHash['CreditCardTransactionCollection'][index]['CreditCard']['BillingAddress'] = nil
          end

        else
          saleHash['CreditCardTransactionCollection'][index]['CreditCard'] = nil
        end
      end
    end

    # transforma a colecao de shoppingcart em json
    if createSaleRequest.ShoppingCartCollection.any? == false || createSaleRequest.ShoppingCartCollection.nil?
      saleHash['ShoppingCartCollection'] = nil
    else
      createSaleRequest.ShoppingCartCollection.each_with_index do |shoppingCart, index|
        s = shoppingCart.to_json
        saleHash['ShoppingCartCollection'] << s

        if shoppingCart.DeliveryAddress.to_json.any?
          delivery_address = shoppingCart.DeliveryAddress.to_json
          saleHash['ShoppingCartCollection'][index]['DeliveryAddress'] = delivery_address
        else
          saleHash['ShoppingCartCollection'][index]['DeliveryAddress'] = nil
        end

        if shoppingCart.ShoppingCartItemCollection.any?
          shoppingCart.ShoppingCartItemCollection.each do |cartItem|
            item = cartItem.to_json
            saleHash['ShoppingCartCollection'][index]['ShoppingCartItemCollection'] << item
          end
        else
          saleHash['ShoppingCartCollection'][index]['ShoppingCartItemCollection'] = nil
        end
      end
    end

    # transforma objeto options em json
    if createSaleRequest.Options.to_json.any?
      o = createSaleRequest.Options.to_json
      saleHash['Options'] = o
    else
      saleHash['Options'] = nil
    end

    # transforma objeto order em json
    if createSaleRequest.Order.to_json.any?
      order = createSaleRequest.Order.to_json
      saleHash['Order'] = order
    else
      saleHash['Order'] = nil
    end

    # transforma objeto merchant em json
    if createSaleRequest.Merchant.to_json.any?
      merchant = createSaleRequest.Merchant.to_json
      saleHash['Merchant'] = merchant
    else
      saleHash['Merchant'] = nil
    end

    # transforma objeto request data em json
    if createSaleRequest.RequestData.to_json.any?
      request_data = createSaleRequest.RequestData.to_json
      saleHash['RequestData'] = request_data
    else
      saleHash['RequestData'] = nil
    end

    # transforma o objeto Buyer em json
    if createSaleRequest.Buyer.to_json.any? && createSaleRequest.Buyer.AddressCollection.any?
      b = createSaleRequest.Buyer.to_json
      saleHash['Buyer'] = b

      if createSaleRequest.Buyer.AddressCollection.any?
        saleHash['Buyer']['AddressCollection'] = []
        createSaleRequest.Buyer.AddressCollection.each do |address|
          a = address.to_json
          saleHash['Buyer']['AddressCollection'] << a
        end
      else
        saleHash['Buyer']['AddressCollection'] = nil
      end
    else
      saleHash['Buyer'] = nil
    end
  rescue Exception => e
    puts e.message
  end

  if @serviceEnvironment == :staging
    url = @@SERVICE_URL_STAGING
  elsif @serviceEnvironment == :production
    url = @@SERVICE_URL_PRODUCTION
  end
  postRequest(saleHash.to_json, url)
end

#ParseXmlToNotification(xml) ⇒ Object

faz um parse do xml de post notificaton



285
286
287
288
289
290
291
292
293
# File 'lib/mundipagg/MundipaggApi.rb', line 285

def ParseXmlToNotification(xml)
  begin
    response = PostNotification.ParseNotification(xml)
  rescue Exception => err
    return err.response
  end

  return response
end

#postRequest(payload, url) ⇒ Object



326
327
328
329
330
331
332
333
334
335
# File 'lib/mundipagg/MundipaggApi.rb', line 326

def postRequest(payload, url)
  response = nil
  begin
    response = RestClient.post(url, payload, headers=@@SERVICE_HEADERS)
  rescue RestClient::ExceptionWithResponse => err
    return err.response #err.response
  end
  json_response = JSON.load response
  json_response
end

#Query(querySaleRequestEnum, key) ⇒ Object

permite que o integrador adicione uma busca por transacoes utilizando alguns criterios



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mundipagg/MundipaggApi.rb', line 22

def Query(querySaleRequestEnum, key)
  # try, tenta fazer o request
  begin

    # se for homologacao faz a chamada por aqui
    if @serviceEnvironment == :staging
      response = RestClient.get @@SERVICE_URL_STAGING + '/Query/' + querySaleRequestEnum + '=' + key, headers=@@SERVICE_HEADERS

      # se for producao, faz a chamada por aqui
    elsif @serviceEnvironment == :production
      response = RestClient.get @@SERVICE_URL_PRODUCTION + '/Query/' + querySaleRequestEnum + '=' + key, headers=@@SERVICE_HEADERS
    end

      # se der algum erro, trata aqui
  rescue RestClient::ExceptionWithResponse => err
    return err.response
  end

  # se n�o houver erros, trata o json e retorna o objeto
  querySaleResponse = JSON.load response
  querySaleResponse
end

#Retry(retrySaleRequest) ⇒ Object

permite forcar a retentativa manualmente de uma transacao (podendo ser tambem uma recorrencia) nao autorizada



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/mundipagg/MundipaggApi.rb', line 209

def Retry(retrySaleRequest)
  saleHash = retrySaleRequest.to_json
  saleHash['RetrySaleCreditCardTransactionCollection'] = []

  begin
    if retrySaleRequest.RetrySaleCreditCardTransactionCollection != nil
      retrySaleRequest.RetrySaleCreditCardTransactionCollection.each do |retrySale|
        r = retrySale.to_json
        saleHash['RetrySaleCreditCardTransactionCollection'] << r

        if retrySaleRequest.Options.to_json.any?
          retry_options = retrySaleRequest.Options.to_json
          saleHash['Options'] = retry_options
        else
          saleHash['Options'] = nil
        end
      end
    end
  rescue Exception => e
    puts e.message
  end
  if @serviceEnvironment == :staging
    url = @@SERVICE_URL_STAGING + '/Retry'
  elsif @serviceEnvironment == :production
    url = @@SERVICE_URL_PRODUCTION + '/Retry'
  end
  postRequest(saleHash.to_json, url)
end

#TransactionReportFile(date) ⇒ Object

faz uma requisicao e retorna uma string com o transaction report file



296
297
298
299
300
301
302
303
# File 'lib/mundipagg/MundipaggApi.rb', line 296

def TransactionReportFile(date)
  begin
    response = RestClient.get('https://api.mundipaggone.com/TransactionReportFile/GetStream?fileDate=' + date.strftime("%Y%m%d"), headers={:MerchantKey => "#{@merchantKey}"})
  rescue RestClient::ExceptionWithResponse => err
    return err.response
  end
  return response
end

#TransactionReportFileDownloader(date, file_name, path) ⇒ Object



305
306
307
308
309
310
311
312
313
# File 'lib/mundipagg/MundipaggApi.rb', line 305

def TransactionReportFileDownloader(date, file_name, path)
  begin
    path = path + file_name + '.txt'
    response = RestClient.get('https://api.mundipaggone.com/TransactionReportFile/GetStream?fileDate=' + date.strftime("%Y%m%d"), headers={:MerchantKey => "#{@merchantKey}"})
    File.write(path, response)
  rescue RestClient::ExceptionWithResponse => err
    return err.response
  end
end

#TransactionReportFileParser(file_to_parse) ⇒ Object

faz o parse da string recebida do transaction report file e retorna um hash



316
317
318
319
320
321
322
323
324
# File 'lib/mundipagg/MundipaggApi.rb', line 316

def TransactionReportFileParser(file_to_parse)
  transaction_report_file = TransactionReportFile.new
  begin
    response = transaction_report_file.TransactionReportFileParser(file_to_parse)
  rescue Exception=>err
    return err
  end
  return response
end