midas_client 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,265 @@
1
+ module MidasClient
2
+
3
+ class Subscription < MidasClient::Request
4
+ include EndPoints
5
+
6
+ # This method creates a subscription.
7
+ # It is a synchronous operation, using method POST
8
+ #
9
+ # Params:
10
+ # externalId: string (Transaction identification on the client application)
11
+ # externalDate: [optional] datetime from client application - YYYY-MM-DDThh:mm:SS.sssTDZ - ISO8601
12
+ # frequencyType string Is the frequency about the charge will run. Possible values: DAY/WEEK/MONTH
13
+ # frequencyInterval integer The interval that will be used.
14
+ # startDate datetime for the beginning of subscription - yyyy-MM-dd
15
+ # invoicesCount integer - number of invoices for the subscription
16
+ # amount: number (Only integer numbers, with the last two digits representing the cents.
17
+ # For example: 100 is equivalent to R$ 1,00)
18
+ # cardToken: string (created by method card_store)
19
+ # cvv: number (Security Code)
20
+ # callbackUrl string - URL to receive the result for each invoice charge
21
+ #
22
+ # Response:
23
+ # {
24
+ # "result": {
25
+ # "success": "true",
26
+ # "code": "0",
27
+ # "message": "Sucesso"
28
+ # },
29
+ # "subscriptionToken": "49ac54257eb1f92f"
30
+ # }
31
+ #
32
+ #
33
+ # Response with first payment
34
+ # {
35
+ # "result": {
36
+ # "success": "true",
37
+ # "code": "0",
38
+ # "message": "Sucesso"
39
+ # },
40
+ # "subscriptionToken": "49ac54257eb1f92f",
41
+ # "firstInvoiceResult": {
42
+ # "success": true,
43
+ # "code": "000",
44
+ # "message": "Sucesso"
45
+ # }
46
+ # }
47
+ #
48
+ #
49
+ # Error response
50
+ #{
51
+ # "result": {
52
+ # "success": "false",
53
+ # "code": "114",
54
+ # "message": "O token do cartao é obrigatório"
55
+ # }
56
+ #}
57
+ #
58
+ def self.create_subscription(*params)
59
+ # Parametros são recebidos como um array de hash, pego o 1o item do array
60
+ params = params.first
61
+
62
+ # inicializa as variáveis de retorno
63
+ result ={}
64
+
65
+ first_invoice_result = ""
66
+ subscription_token = ""
67
+
68
+ # define o método de envio da requisição
69
+ method = :post
70
+
71
+ # monta a URL de chamada da requisição
72
+ endpoint= EndPoints.get_env[:url] + EndPoints.get_env[:context] + EndPoints::Subscriptions[:create]
73
+
74
+
75
+ params[:externalDate] = (params[:externaldate].blank? ? Time.now.iso8601(1) : params[:externaldate])
76
+ params[:invoicesCount]= (params[:invoicesCount].blank? ? 12 : params[:invoicesCount])
77
+
78
+ # faz a chamada a plataforma de pagamento (MIDAS)
79
+ response = request(method, endpoint, self.login, self.password, params)
80
+
81
+ result = response[:result]
82
+ subscription_token = response[:subscriptionToken] if !response[:subscriptionToken].nil?
83
+ first_invoice_result = response[:firstInvoiceResult]
84
+
85
+ # grava o tempo de execução da operação
86
+ total_time_execution = Time.now - start_time_execution
87
+
88
+ return result, subscription_token, first_invoice_result
89
+ end
90
+
91
+ # This method cancel a subscription.
92
+ # This is a is synchronous operation.
93
+ #
94
+ # Params:
95
+ # subscriptionToken: string (Subscription unique identification generated by our
96
+ # gateway and received in the subscription's creation response)
97
+ #
98
+ # Response:
99
+ # result: {
100
+ # success: true/false
101
+ # code: "XXX"
102
+ # message: "Some message to you"
103
+ # }
104
+ def self.cancel_subscription(subscription_token)
105
+ # inicializa as variáveis de retorno
106
+ result ={}
107
+
108
+ # define o método de envio da requisição
109
+ method = :put
110
+
111
+ # monta a URL de chamada da requisição
112
+ endpoint = EndPoints.get_env[:url] + EndPoints.get_env[:context] + EndPoints::Subscriptions[:cancel].gsub("{subscriptionToken}", subscription_token)
113
+
114
+ # faz a chamada a plataforma de pagamento (MIDAS)
115
+ response = request(method, endpoint, self.login, self.password, {})
116
+
117
+ result = response[:result]
118
+
119
+ return result
120
+ end
121
+
122
+ # This method performs a query by a specific transaction.
123
+ # This is a is synchronous operation, using method GET
124
+ #
125
+ # Params:
126
+ # subscription_token: string (Transaction unique identification generated by our
127
+ # gateway and received in the subscription's create response)#
128
+ #
129
+ #
130
+ # Response:
131
+ # {
132
+ # :result=>{
133
+ # :success=>true,
134
+ # :code=>"000",
135
+ # :message=>"Sucesso"
136
+ # },
137
+ # :subscription=>{
138
+ # :externalId=>"10011",
139
+ # :frequencyType=>"MONTH",
140
+ # :frequencyInterval=>1,
141
+ # :startDate=>"2016-07-28T00:00:00.000-04",
142
+ # :invoicesCount=>12,
143
+ # :paymentMethod=>"CREDIT_CARD",
144
+ # :amount=>5000,
145
+ # :creditCard=>{
146
+ # :brand=>"VISA",
147
+ # :panLastDigits=>"1855",
148
+ # :expirationMonth=>12,
149
+ # :expirationYear=>2020,
150
+ # :holderName=>"TESTE CONSOLE",
151
+ # :customer=>{
152
+ # :documentType=>"CPF",
153
+ # :documentNumber=>"41468031520"
154
+ # }
155
+ # },
156
+ # :token=>"a2bebd13f0a43e5a",
157
+ # :status=>"CANCELLED"
158
+ # }
159
+ # }
160
+ def self.subscription_by_token(subscription_token)
161
+ # inicializa as variáveis de retorno
162
+ result ={}
163
+
164
+ # define o método de envio da requisição
165
+ method = :get
166
+
167
+ # monta a URL de chamada da requisição
168
+ endpoint = EndPoints.get_env[:url] + EndPoints.get_env[:context] + EndPoints::Subscriptions[:by_token].gsub("{subscriptionToken}", subscription_token)
169
+
170
+ # faz a chamada a plataforma de pagamento (MIDAS)
171
+ response = request(method, endpoint, self.login, self.password, {})
172
+
173
+ result = response[:result]
174
+
175
+ return result, response[:subscription]
176
+ end
177
+
178
+
179
+ # This method performs a query by a specific transaction.
180
+ # This is a is synchronous operation, using method GET
181
+ #
182
+ # Params:
183
+ # subscription_token: string (Transaction unique identification generated by our
184
+ # gateway and received in the subscription's create response)#
185
+ #
186
+ #
187
+ # Response:
188
+ #
189
+ # {
190
+ # :result=>{
191
+ # :success=>true,
192
+ # :code=>"000",
193
+ # :message=>"Sucesso"
194
+ # },
195
+ # :invoices=>[{
196
+ # :number=>1,
197
+ # :amount=>5000,
198
+ # :expirationDate=>"2016-07-28",
199
+ # :token=>"8bc3bf2ff9b08566fa8496fdce6c6f99",
200
+ # :status=>"PAID"
201
+ # }. {:number=>2,...}, {:number=>3,}]
202
+ # }
203
+ def self.subscription_invoices(subscription_token)
204
+ # inicializa as variáveis de retorno
205
+ result ={}
206
+
207
+ # define o método de envio da requisição
208
+ method = :get
209
+
210
+ # monta a URL de chamada da requisição
211
+ endpoint = EndPoints.get_env[:url] + EndPoints.get_env[:context] + EndPoints::Subscriptions[:invoices].gsub("{subscriptionToken}", subscription_token)
212
+
213
+ # faz a chamada a plataforma de pagamento (MIDAS)
214
+ response = request(method, endpoint, self.login, self.password, {})
215
+
216
+ result = response[:result]
217
+
218
+ invoices = response[:invoices]
219
+
220
+ return result, invoices
221
+ end
222
+
223
+
224
+ # This method updates the credit card information associated to the subscription.
225
+ # This is a is synchronous operation, using method PUT
226
+ #
227
+ # Params:
228
+ # subscription_token: string (Transaction unique identification generated by our
229
+ # gateway and received in the subscription's create response)#
230
+ # new_card_token: string that represents the new credit card token (created using card_store) for the subscription
231
+ #
232
+ # Response:
233
+ # {
234
+ # "result": {
235
+ # "success": true,
236
+ # "code": "0",
237
+ # "message": "Sucesso"
238
+ # }
239
+ # }
240
+ #
241
+ def self.update_subscription_card(subscription_token, new_card_token)
242
+ # inicializa as variáveis de retorno
243
+ result ={}
244
+
245
+ # define o método de envio da requisição
246
+ method = :put
247
+
248
+ # monta a URL de chamada da requisição
249
+ endpoint = EndPoints.get_env[:url] + EndPoints.get_env[:context] + EndPoints::Subscriptions[:update_card].gsub("{subscriptionToken}", subscription_token)
250
+
251
+ params = {
252
+ cardToken: new_card_token
253
+ }
254
+
255
+ # faz a chamada a plataforma de pagamento (MIDAS)
256
+ response = request(method, endpoint, self.login, self.password, params)
257
+
258
+ result = response[:result]
259
+
260
+ return result
261
+ end
262
+
263
+ end
264
+
265
+ end
@@ -0,0 +1,396 @@
1
+ module MidasClient
2
+ class Transaction < MidasClient::Request
3
+
4
+ include MidasClient::EndPoints
5
+
6
+ # This method securely stores credit card's data (pan, expiration...)
7
+ # and returns a token to be used to perform payment transactions.
8
+ #
9
+ # Params:
10
+ # externalId: number (Transaction identification on the client application)
11
+ # pan: number
12
+ # expirationMonth: number (format: MM)
13
+ # expirationYear: number (format: YYYY)
14
+ # holderName: string
15
+ # customer/documentType: string (values: {'CPF', 'CNPJ'})
16
+ # customer/documentNumber: string
17
+ #
18
+ # Response success:
19
+ # {
20
+ # "result": {
21
+ # "success":"true",
22
+ # "code":"000",
23
+ # "message":"Success"
24
+ # },
25
+ # "brand":"VISA",
26
+ # "cardToken":"2b5141a2209384e4266c8f7cbaf67a2d"
27
+ # }
28
+ #
29
+ # Response Failure
30
+ # {
31
+ # :result=>{
32
+ # :success=>false,
33
+ # :code=>"120",
34
+ # :message=>"O numero do documento eh obrigatorio"
35
+ # }
36
+ # }
37
+ #
38
+ def card_store(*params)
39
+ # Parametros são recebidos como um array de hash, pego o 1o item do array
40
+ params = params.first
41
+
42
+ # inicializa as variáveis de retorno
43
+ result ={}
44
+ card_token = nil
45
+
46
+ # define o método de envio da requisição
47
+ method = :post
48
+
49
+ # monta a URL de chamada da requisição
50
+ endpoint= EndPoints.get_env[:url] + EndPoints.get_env[:context] + EndPoints::Operations[:store]
51
+
52
+ # faz a chamada a plataforma de pagamento (MIDAS)
53
+ response = request(method, endpoint, login, password, params)
54
+
55
+ result = response[:result]
56
+ card_token = response[:cardToken] if !response[:cardToken].nil?
57
+
58
+ return result, card_token
59
+ end
60
+
61
+ # This method creates a payment transaction, already performing
62
+ # the authorization and capture in only one step.
63
+ #
64
+ # Params:
65
+ # externalId: string (Transaction identification on the client application)
66
+ # externalDate: Date and time form client application - YYYY-MM-DDThh:mm:SS.sssTDZ - ISO8601
67
+ # cardToken: string (created by method card_store)
68
+ # cvv: number (Security Code)
69
+ # amount: number (Only integer numbers, with the last two digits representing the cents.
70
+ # For example: 100 is equivalent to R$ 1,00)
71
+ # instalments: number (1 to 12)
72
+ #
73
+ # Response:
74
+ # result: {
75
+ # success: true/false
76
+ # code: "XXX"
77
+ # message: "Some message to you"
78
+ # },
79
+ # transactionToken: "65ffe2897b832cb89cb2063b74fbb143",
80
+ # nsu: "99999"
81
+ #
82
+ def synchronous_transaction(*params)
83
+ # Parametros são recebidos como um array de hash, pego o 1o item do array
84
+ params = params.first
85
+
86
+ # inicializa as variáveis de retorno
87
+ result ={}
88
+ nsu = ""
89
+ transaction_token = ""
90
+
91
+ # define o método de envio da requisição
92
+ method = :post
93
+
94
+ # monta a URL de chamada da requisição
95
+ endpoint= EndPoints.get_env[:url] + EndPoints.get_env[:context] + EndPoints::Operations[:synchronous_transaction]
96
+
97
+ # regulariza o formato da data
98
+ params[:externaldate] = params[:externaldate].blank? ? Time.now.iso8601(1) : params[:externaldate]
99
+
100
+ # faz a chamada a plataforma de pagamento (MIDAS)
101
+ response = request(method, endpoint, self.login, self.password, params)
102
+
103
+ result = response[:result]
104
+ nsu = response[:nsu] if !response[:nsu].nil?
105
+ transaction_token = response[:transactionToken] if !response[:transactionToken].nil?
106
+
107
+ return result, transaction_token, nsu
108
+ end
109
+
110
+ # This method dispatches a payment transaction creation request, already performing
111
+ # the authorization and capture in only one step, but sending the response using the
112
+ # informed callback URL, in an asynchronous away.
113
+ #
114
+ # Params:
115
+ # externalId: string (Transaction identification on the client application)
116
+ # externalDate: Date and time form client application - YYYY-MM-DDThh:mm:SS.sssTDZ - ISO8601
117
+ # cardToken: string (created by method card_store)
118
+ # cvv: number (Security Code)
119
+ # softDescriptor: string (Text to be shown on the credit card invoice entry)
120
+ # amount: number (Only integer numbers, with the last two digits representing the cents.
121
+ # For example: 100 is equivalent to R$ 1,00)
122
+ # instalments: number (1 to 12)
123
+ # callbackUrl: string (URL used to notify the client application about the transaction creation result. )
124
+ #
125
+ # # Response:
126
+ # result: {
127
+ # success: true/false
128
+ # code: "XXX"
129
+ # message: "Some message to you"
130
+ # },
131
+ # transactionToken: "65ffe2897b832cb89cb2063b74fbb143",
132
+ #
133
+ def self.asynchronous_transaction(*params)
134
+ # Parametros são recebidos como um array de hash, pego o 1o item do array
135
+ params = params.first
136
+
137
+ # inicializa as variáveis de retorno
138
+ result ={}
139
+ transaction_token = ""
140
+
141
+ # define o método de envio da requisição
142
+ method = :post
143
+
144
+ # monta a URL de chamada da requisição
145
+ endpoint= EndPoints.get_env[:url] + EndPoints.get_env[:context] + EndPoints::Operations[:asynchronous_transaction]
146
+
147
+ # regulariza o formato da data
148
+ params[:externalDate] = (params[:externaldate].blank? ? Time.now.iso8601(1) : params[:externaldate])
149
+
150
+ # faz a chamada a plataforma de pagamento (MIDAS)
151
+ response = request(method, endpoint, self.login, self.password, params)
152
+
153
+ result = response[:result]
154
+ transaction_token = response[:transactionToken] if !response[:transactionToken].nil?
155
+
156
+ return result, transaction_token
157
+ end
158
+
159
+ # This method dispatches a debit payment transaction request, that's receive an authorization URL,
160
+ # where the payer must authorize the transaction.
161
+ #
162
+ # Params:
163
+ # externalId: string (Transaction identification on the client application)
164
+ # externalDate: Date and time form client application - YYYY-MM-DDThh:mm:SS.sssTDZ - ISO8601
165
+ # pan: DebitCard Number
166
+ # expirationMonth:
167
+ # expirationYear:
168
+ # holderName:
169
+ # customer/documentType: CPF/CNPJ
170
+ # customer/documentNumber: if CPF 11 digits else CNPJ 13 digits
171
+ # cvv: number (Security Code)
172
+ # amount: number (Only integer numbers, with the last two digits representing the cents.
173
+ # For example: 100 is equivalent to R$ 1,00)
174
+ # callbackUrl: string (URL used to notify the client application about the transaction authorization result. async notification )
175
+ # redirectUrl: string (URL used to notify the client application that the transaction authorization result successful. )
176
+ # errorUrl: string (URL used to notify the client application that the transaction authorization result failed)
177
+ #
178
+ # # Response:
179
+ # result: {
180
+ # success: true/false
181
+ # code: "XXX"
182
+ # message: "Some message to you"
183
+ # },
184
+ # "authenticationUrl": "https://qasecommerce.cielo.com.br/web/index.cbmp?id=ac470d041f660e21d253a5741e59c5b4",
185
+ # "transactionToken":"6accbedb2da4f7bfbca5b5fed3669be6",
186
+ #
187
+ def self.asynchronous_debit_transaction(*params)
188
+ # Parametros são recebidos como um array de hash, pego o 1o item do array
189
+ params = params.first
190
+
191
+ # inicializa as variáveis de retorno
192
+ result ={}
193
+ transaction_token = ""
194
+
195
+ # define o método de envio da requisição
196
+ method = :post
197
+
198
+ # monta a URL de chamada da requisição
199
+ endpoint= EndPoints.get_env[:url] + EndPoints.get_env[:context] + EndPoints::Operations[:asynchronous_debit_transaction]
200
+
201
+ # regulariza formato da data
202
+ params[:externalDate] = (params[:externaldate].blank? ? Time.now.iso8601(1) : params[:externaldate])
203
+
204
+ # faz a chamada a plataforma de pagamento (MIDAS)
205
+ response = request(method, endpoint, self.login, self.password, params)
206
+
207
+ result = response[:result]
208
+ authentication_url = response[:authenticationUrl] unless response[:authenticationUrl].nil?
209
+ transaction_token = response[:transactionToken] unless response[:transactionToken].nil?
210
+
211
+ return result, authentication_url, transaction_token
212
+ end
213
+
214
+ # This method performs a payment authorization. This is a is synchronous operation.
215
+ # To confirm the payment and finish the transaction you must send a capture request
216
+ #
217
+ # Params:
218
+ # externalId: string (Transaction identification on the client application)
219
+ # externalDate: Date and time form client application - YYYY-MM-DDThh:mm:SS.sssTDZ - ISO8601
220
+ # cardToken: string (created by method card_store)
221
+ # cvv: number (Security Code)
222
+ # amount: number (Only integer numbers, with the last two digits representing the cents.
223
+ # For example: 100 is equivalent to R$ 1,00)
224
+ # instalments: number (1 to 12)
225
+ #
226
+ # Response:
227
+ # {
228
+ # result: {
229
+ # success: true/false
230
+ # code: "XXX"
231
+ # message: "Some message to you"
232
+ # }
233
+ # }
234
+ #
235
+ # Response Failure
236
+ # {
237
+ # :result=>{
238
+ # :success=>false,
239
+ # :code=>"150",
240
+ # :message=>"A data externa é obrigatória"
241
+ # }
242
+ # }
243
+ #
244
+ def self.authorize(*params)
245
+ # Parametros são recebidos como um array de hash, pego o 1o item do array
246
+ params = params.first
247
+
248
+ # inicializa as variáveis de retorno
249
+ result ={}
250
+ transaction_token = ""
251
+
252
+ # define o método de envio da requisição
253
+ method = :post
254
+
255
+ # monta a URL de chamada da requisição
256
+ endpoint= EndPoints.get_env[:url] + EndPoints.get_env[:context] + EndPoints::Operations[:authorize]
257
+
258
+ # regulariza o formato da data
259
+ params[:externalDate] = (params[:externaldate].blank? ? Time.now.iso8601(1) : params[:externaldate])
260
+
261
+ # faz a chamada a plataforma de pagamento (MIDAS)
262
+ response = request(method, endpoint, self.login, self.password, params)
263
+
264
+ result = response[:result]
265
+ nsu = response[:nsu] if !response[:nsu].nil?
266
+ transaction_token = response[:transactionToken] if !response[:transactionToken].nil?
267
+
268
+ return result, transaction_token
269
+ end
270
+
271
+ # This method performs a capture (confirmation) in a already authorized transaction.
272
+ # This is a is synchronous operation.
273
+ #
274
+ # Params:
275
+ # transactionToken: string (Transaction unique identification generated by our
276
+ # gateway and received in the transaction authorization response)
277
+ #
278
+ # Response:
279
+ # result: {
280
+ # success: true/false
281
+ # code: "XXX"
282
+ # message: "Some message to you"
283
+ # }
284
+ def self.capture(transaction_token)
285
+ # inicializa as variáveis de retorno
286
+ result ={}
287
+
288
+ # define o método de envio da requisição
289
+ method = :put
290
+
291
+ # monta a URL de chamada da requisição
292
+ endpoint = EndPoints.get_env[:url] + EndPoints.get_env[:context] + EndPoints::Operations[:confirm].gsub("{transactionToken}", transaction_token)
293
+
294
+ # faz a chamada a plataforma de pagamento (MIDAS)
295
+ response = self.request(method, endpoint, self.login, self.password, {})
296
+
297
+ result = response[:result]
298
+
299
+ return result
300
+ end
301
+
302
+ # This method performs a cancellation in a already authorized transaction.
303
+ # This is a is synchronous operation.
304
+ #
305
+ # Params:
306
+ # transactionToken: string (Transaction unique identification generated by our
307
+ # gateway and received in the transaction authorization response)
308
+ #
309
+ # Response:
310
+ # result: {
311
+ # success: true/false
312
+ # code: "XXX"
313
+ # message: "Some message to you"
314
+ # }
315
+ def self.cancellation(transaction_token)
316
+ # inicializa as variáveis de retorno
317
+ result ={}
318
+
319
+ # define o método de envio da requisição
320
+ method = :put
321
+
322
+ # monta a URL de chamada da requisição
323
+ endpoint = EndPoints.get_env[:url] + EndPoints.get_env[:context] + EndPoints::Operations[:cancel].gsub("{transactionToken}", transaction_token)
324
+
325
+ # faz a chamada a plataforma de pagamento (MIDAS)
326
+ response = self.request(method, endpoint, self.login, self.password, {})
327
+ result = response[:result]
328
+
329
+ return result
330
+ end
331
+
332
+ # This method performs a refund in a captured authorized transaction.
333
+ # This is a is synchronous operation.
334
+ #
335
+ # Params:
336
+ # transactionToken: string (Transaction unique identification generated by our
337
+ # gateway and received in the transaction authorization response)#
338
+ #
339
+ #
340
+ # Response:
341
+ # result: {
342
+ # success: true/false
343
+ # code: "XXX"
344
+ # message: "Some message to you"
345
+ # }
346
+ def self.refund(transaction_token)
347
+ # inicializa as variáveis de retorno
348
+ result ={}
349
+
350
+ # define o método de envio da requisição
351
+ method = :put
352
+
353
+ # monta a URL de chamada da requisição
354
+ endpoint = EndPoints.get_env[:url] + EndPoints.get_env[:context] + EndPoints::Operations[:refund].gsub("{transactionToken}", transaction_token)
355
+
356
+ # faz a chamada a plataforma de pagamento (MIDAS)
357
+ response = request(method, endpoint, self.login, self.password, {})
358
+
359
+ result = response[:result]
360
+
361
+ return result
362
+ end
363
+
364
+ # This method performs a query by a specific transaction.
365
+ # This is a is synchronous operation, using method GET
366
+ #
367
+ # Params:
368
+ # transactionToken: string (Transaction unique identification generated by our
369
+ # gateway and received in the transaction authorization response)#
370
+ #
371
+ #
372
+ # Response:
373
+ # result: {
374
+ # success: true/false
375
+ # code: "XXX"
376
+ # message: "Some message to you"
377
+ # }
378
+ def self.query_transaction(transaction_token)
379
+ # inicializa as variáveis de retorno
380
+ result ={}
381
+
382
+ # define o método de envio da requisição
383
+ method = :get
384
+
385
+ # monta a URL de chamada da requisição
386
+ endpoint = EndPoints.get_env[:url] + EndPoints.get_env[:context] + EndPoints::Operations[:query_by_transaction].gsub("{transactionToken}", transaction_token)
387
+
388
+ response = request(method, endpoint, login, password, params)
389
+
390
+ result = response[:result]
391
+
392
+ return result
393
+ end
394
+
395
+ end
396
+ end