cuenta_digital 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 85a78d3f37c6b04c0bd37b008da468dc87076cc1adf8ffb24e83c5722ccd2533
4
- data.tar.gz: 4aa62b7837bf4445e3fdf8d317060fd92948ee35d3341ecb5eafdc11f5e543fe
3
+ metadata.gz: 8b78d57edda1b829e9ea2ee6d352a0f91fd409c3c0516b460cf1dea191b7c2a7
4
+ data.tar.gz: 2dc9a771ced4160f5ae186304e520535880a41a0f750f39210acf392617caeed
5
5
  SHA512:
6
- metadata.gz: c350334de734c6556b7eeaff36924f144599c94bb47aa5a8ea37c2809c0007b173e1b80268bbc20ae7cf2b85f56d102edc1ed53d9575b210544484be98758e4d
7
- data.tar.gz: fb65094d6e8f5ea9276d571a9e1099dd3a0119cce22289ef0e640f9e3167da761d2edceee5f47a5106e931fe0c9addb39045d57539c847b405add72b4cbc426a
6
+ metadata.gz: 6162c9bd7d524991c27f0dbed6d984fa4ab16f227b0407061c8857b9b488e81fe50c88733fd3baf598bc6a249887db408ce0db9af5cf325d92fc72d170aa9558
7
+ data.tar.gz: ef26033d936568136c8445ed28057282d149cdcfcbb483b4320fab21dcfee55ce9bb35c70215e93e226a6879c5ecda3e4f48e8adf88fc4bf734f29cf6dc4562e
data/README.md CHANGED
@@ -131,16 +131,6 @@ This method return `CuentaDigital::Response` object
131
131
  - **bar_code**
132
132
  - **csv_line**
133
133
 
134
- #### Methods
135
-
136
- - [class method] `process_webhook`:
137
-
138
- ```ruby
139
- CuentaDigital::Payment.process_webhook(csv) # String csv separated by ','
140
- ```
141
-
142
- return arrays of `CuentaDigital::Payment`
143
-
144
134
  - [class method] `request`. Only get transactions completed
145
135
 
146
136
 
@@ -164,6 +154,19 @@ return arrays of `CuentaDigital::Payment`
164
154
  CuentaDigital::Payment.request(control: 'e9161bfccfba345237fb1311b890203f', date: Time.new(2019, 07, 01), opts: { wget: false, sandbox: true })
165
155
  ```
166
156
 
157
+ ### Webhook
158
+
159
+ #### Methods
160
+
161
+ - [class method] `process`:
162
+
163
+ ```ruby
164
+ CuentaDigital::Payment.process(csv) # String csv separated by ','
165
+ ```
166
+
167
+ return arrays of `CuentaDigital::Payment`
168
+
169
+
167
170
  ## Development
168
171
 
169
172
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -74,6 +74,8 @@ module CuentaDigital
74
74
  end
75
75
 
76
76
  def generate(xml: true, wget: false)
77
+ return false unless valid?
78
+
77
79
  retries = 0
78
80
  begin
79
81
  if wget
@@ -118,6 +120,8 @@ module CuentaDigital
118
120
  validate_attributes_presence
119
121
  validate_standar_values
120
122
  validate_format_values
123
+ validate_dates
124
+ valideate_lengths
121
125
  end
122
126
 
123
127
  def validate_attributes_presence
@@ -128,23 +132,67 @@ module CuentaDigital
128
132
 
129
133
  missing_attributes.uniq.each do |attr|
130
134
  @errors[attr.to_sym] = [] unless errors.key?(attr)
131
- @errors[attr.to_sym] << CuentaDigital::Exception::MissingAttributes.new(attr).message
135
+ @errors[attr.to_sym] << [
136
+ :cant_be_blank,
137
+ CuentaDigital::Exception::MissingAttributes.new(attr).message
138
+ ]
139
+ end
140
+
141
+ if @first_due_date.blank? && !@second_due_date.blank?
142
+ @errors[:first_due_date] = [] unless errors.key?(:first_due_date)
143
+ @errors[:first_due_date] << [
144
+ :cant_be_blank,
145
+ CuentaDigital::Exception::MissingAttributes.new(:first_due_date).message
146
+ ]
147
+ end
148
+
149
+ if @amount.blank? && !@second_amount.blank?
150
+ @errors[:amount] = [] unless errors.key?(:amount)
151
+ @errors[:amount] << [
152
+ :cant_be_blank,
153
+ CuentaDigital::Exception::MissingAttributes.new(:amount).message
154
+ ]
132
155
  end
133
156
  end
134
157
 
135
158
  def validate_standar_values
136
- return if CuentaDigital::CURRENCIES.keys.include?(@currency)
159
+ return if CuentaDigital::CURRENCIES.key?(@currency)
137
160
 
138
161
  @errors[:currency] = [] unless errors.key?(:currency)
139
-
140
- @errors[:currency] << CuentaDigital::Exception::InvalidValueAttribute.new("currency, Possible values #{CuentaDigital::CURRENCIES.keys}").message
162
+ @errors[:currency] << [
163
+ :unsupported_value,
164
+ CuentaDigital::Exception::InvalidValueAttribute.new("currency, Possible values #{CuentaDigital::CURRENCIES.keys}").message
165
+ ]
141
166
  end
142
167
 
143
168
  def validate_format_values
144
169
  return if @email_to.nil? || !@email_to.match(CuentaDigital::VALID_EMAIL_REGEX).nil?
145
170
 
146
171
  @errors[:email_to] = [] unless errors.key?(:email_to)
147
- @errors[:email_to] << CuentaDigital::Exception::InvalidFormat.new(attr).message
172
+ @errors[:email_to] << [
173
+ :invalid_format,
174
+ CuentaDigital::Exception::InvalidFormat.new(attr).message
175
+ ]
176
+ end
177
+
178
+ def validate_dates
179
+ return unless (@first_due_date && @second_due_date) && @second_due_date <= @first_due_date
180
+
181
+ @errors[:second_due_date] = [] unless errors.key?(:second_due_date)
182
+ @errors[:second_due_date] << [
183
+ :second_due_date_must_be_greater_than_first_due_date,
184
+ CuentaDigital::Exception::InvalidValueAttribute.new('second due date cant be greater than or equal to first due date').message
185
+ ]
186
+ end
187
+
188
+ def valideate_lengths
189
+ return if code.size <= 50
190
+
191
+ @errors[:code] = [] unless errors.key?(:code)
192
+ @errors[:code] << [
193
+ :code_must_be_greater_than_50_characters,
194
+ CuentaDigital::Exception::InvalidValueAttribute.new('code cant be greater than 50 chars').message
195
+ ]
148
196
  end
149
197
  end
150
198
  end
@@ -17,6 +17,7 @@ module CuentaDigital
17
17
  :checksum,
18
18
  :operation_event_number,
19
19
  :bar_code,
20
+ :secret,
20
21
  :csv_line
21
22
 
22
23
  def initialize(params = {})
@@ -32,6 +33,7 @@ module CuentaDigital
32
33
  @checksum = params[:checksum]
33
34
  @bar_code = params[:bar_code]
34
35
  @csv_line = params[:csv_line]
36
+ @secret = params[:secret]
35
37
  end
36
38
 
37
39
  def credit?
@@ -42,37 +44,8 @@ module CuentaDigital
42
44
  @operation_kind == 0
43
45
  end
44
46
 
45
- # Linea de operaciones:
46
- # CSV: Clase de operacion,Fecha de la operacion,Hora de la operacion,Monto,Codigo de Barras,Referencia,Medio de pago,Codigo unico de operacion,Checksum de operacion,Numero de operacion en evento
47
- # Linea final:
48
- # CSV: Valor fijo 3 indicando linea final,Fecha actual,Horario actual,Monto total de creditos en evento,Monto total de debitos en evento,Cantidad de operaciones,Checksum
49
- def self.process_webhook(csv)
50
- csv_splitted = csv.split("\n")
51
- _final_line = csv_splitted.pop
52
-
53
- csv_splitted.map do |result|
54
- args = result.split(',')
55
-
56
- params = {
57
- csv_line: result,
58
- operation_kind: args[0],
59
- payment_date: Time.parse(
60
- [
61
- args[1].insert(2, '-').insert(5, '-'),
62
- args[2].insert(2, ':').insert(5, ':')
63
- ].join(' ')
64
- ),
65
- net_amount: args[3],
66
- bar_code: args[4],
67
- reference: args[5],
68
- payment_method: args[6],
69
- operation_id: args[7],
70
- checksum: args[8],
71
- operation_event_number: args[9]
72
- }
73
-
74
- CuentaDigital::Payment.new(params)
75
- end
47
+ def secret_valid?
48
+ secret.nil? || Digest::SHA256.hexdigest(@secret) == @checksum
76
49
  end
77
50
 
78
51
  def self.uri(control:, sandbox: false, date: Time.now, from: nil, to: nil)
@@ -111,7 +84,7 @@ module CuentaDigital
111
84
  # Fecha del cobro|Horario de la operacion (HHmmss)|Monto Bruto|Monto neto recibido|Comision|Su referencia|Medio de pago usado|Codigo interno unico de operacion|Cobro numero 1 del archivo
112
85
  # Ejemplo de una linea del archivo:
113
86
  # 30122008|221500|1000.50|999.50|1.00|cliente9879|PagoFacil|23a0f1c7b636c08660abbe4f02360633-de70dbb3f907c613ddce6566667f92c6|1
114
- def self.request(control:, date: Time.now, from: nil, to: nil, opts: { wget: false, sandbox: false })
87
+ def self.collector(control:, date: Time.now, from: nil, to: nil, opts: { wget: false, sandbox: false })
115
88
  retries = 0
116
89
 
117
90
  uri_request = uri(control: control, sandbox: opts[:sandbox], date: date, from: from, to: to)
@@ -1,3 +1,3 @@
1
1
  module CuentaDigital
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CuentaDigital
4
+ # Linea de operaciones:
5
+ # CSV: Clase de operacion,Fecha de la operacion,Hora de la operacion,Monto,Codigo de Barras,Referencia,Medio de pago,Codigo unico de operacion,Checksum de operacion,Numero de operacion en evento
6
+ # Linea final:
7
+ # CSV: Valor fijo 3 indicando linea final,Fecha actual,Horario actual,Monto total de creditos en evento,Monto total de debitos en evento,Cantidad de operaciones,Checksum
8
+
9
+ # El Checksum de la operacion (firma) corresponde a una encriptacion SHA256 concatenando:
10
+ # - la clase de operacion,
11
+ # - la fecha de la operacion,
12
+ # - hora de la operacion,
13
+ # - Monto,
14
+ # - Codigo de Barras,
15
+ # - Referencia,
16
+ # - Codigo unico de operacion
17
+ # - clave de seguridad
18
+ # ash('sha256',ClaseDDMMYYYHHMMSSMontoBarraReferenciaUnicoClave),
19
+ # su finalidad es la validacion de la operacion.
20
+ class Webhook
21
+ def self.process(csv, secret = nil)
22
+ csv_splitted = csv.split("\n")
23
+ _final_line = csv_splitted.pop
24
+
25
+ csv_splitted.map do |result|
26
+ args = result.split(',')
27
+
28
+ params = {
29
+ csv_line: result,
30
+ secret: secret,
31
+ operation_kind: args[0],
32
+ payment_date: Time.parse(
33
+ [
34
+ args[1].insert(2, '-').insert(5, '-'),
35
+ args[2].insert(2, ':').insert(5, ':')
36
+ ].join(' ')
37
+ ),
38
+ net_amount: args[3],
39
+ bar_code: args[4],
40
+ reference: args[5],
41
+ payment_method: args[6],
42
+ operation_id: args[7],
43
+ checksum: args[8],
44
+ operation_event_number: args[9]
45
+ }
46
+
47
+ params[:secret] = if secret
48
+ [args[0],
49
+ args[1].delete('-'),
50
+ args[2].delete(':'),
51
+ args[3],
52
+ args[4],
53
+ args[5],
54
+ args[7],
55
+ secret].sum
56
+ end
57
+
58
+ CuentaDigital::Payment.new(params)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -4,6 +4,7 @@ require 'cuenta_digital/version'
4
4
  require 'cuenta_digital/coupon'
5
5
  require 'cuenta_digital/response'
6
6
  require 'cuenta_digital/payment'
7
+ require 'cuenta_digital/webhook'
7
8
  require 'cuenta_digital/exception'
8
9
 
9
10
  require 'digest'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cuenta_digital
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ga6ix
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-07-15 00:00:00.000000000 Z
11
+ date: 2019-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -103,6 +103,7 @@ files:
103
103
  - lib/cuenta_digital/payment.rb
104
104
  - lib/cuenta_digital/response.rb
105
105
  - lib/cuenta_digital/version.rb
106
+ - lib/cuenta_digital/webhook.rb
106
107
  homepage: https://github.com/gedera/cuenta_digital
107
108
  licenses:
108
109
  - MIT