culqi-ruby-oficial 1.0.0 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ require 'json'
2
+
3
+ class CustomException < StandardError
4
+ def initialize(merchant_message)
5
+ @error_data = {
6
+ "object" => "error",
7
+ "type" => "param_error",
8
+ "merchant_message" => merchant_message,
9
+ "user_message" => merchant_message
10
+ }
11
+ super("CustomException: #{@error_data}")
12
+ end
13
+
14
+ def to_s
15
+ JSON.generate(@error_data)
16
+ end
17
+ end
@@ -0,0 +1,40 @@
1
+ require 'date'
2
+ require 'uri'
3
+ require 'json'
4
+ require 'util/validation/error'
5
+
6
+ class HelperValidation
7
+
8
+ def self.is_valid_card_number(number)
9
+ !number.match(/^\d{13,19}$/).nil?
10
+ end
11
+
12
+ def self.is_valid_email(email)
13
+ !email.match(/^\S+@\S+\.\S+$/).nil?
14
+ end
15
+
16
+ def self.validate_currency_code(currency_code)
17
+ raise CustomException.new('Currency code is empty.') if currency_code.nil? || currency_code.empty?
18
+
19
+ raise CustomException.new('Currency code must be a string.') unless currency_code.is_a?(String)
20
+
21
+ allowed_values = ['PEN', 'USD']
22
+ raise CustomException.new('Currency code must be either "PEN" or "USD".') unless allowed_values.include?(currency_code)
23
+ end
24
+
25
+ def self.validate_string_start(string, start)
26
+ unless string.start_with?("#{start}_test_") || string.start_with?("#{start}_live_")
27
+ raise CustomException.new("Incorrect format. The format must start with #{start}_test_ or #{start}_live_")
28
+ end
29
+ end
30
+
31
+ def self.validate_value(value, allowed_values)
32
+ raise CustomException.new("Invalid value. It must be #{JSON.generate(allowed_values)}.") unless allowed_values.include?(value)
33
+ end
34
+
35
+ def self.is_future_date(expiration_date)
36
+ exp_date = Time.at(expiration_date)
37
+ exp_date > Time.now
38
+ end
39
+
40
+ end
@@ -0,0 +1,78 @@
1
+ require 'date'
2
+ require 'uri'
3
+ require 'json'
4
+ require 'util/validation/helper'
5
+ require 'util/validation/error'
6
+
7
+ class OrderValidation
8
+ def self.create(data)
9
+ # Validate amount
10
+ data = data.to_json
11
+ data = JSON.parse(data)
12
+ amount = data['amount'];
13
+
14
+ if amount.is_a?(String)
15
+ begin
16
+ amount = Integer(amount)
17
+ rescue ArgumentError
18
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
19
+ end
20
+ end
21
+
22
+ unless amount.is_a?(Integer)
23
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
24
+ end
25
+
26
+ # Validate currency
27
+ HelperValidation.validate_currency_code(data['currency_code'])
28
+
29
+ # Validate firstname, lastname, and phone
30
+ client_details = data['client_details'] || {}
31
+ raise CustomException.new('first name is empty.') if client_details['first_name'].nil? || client_details['first_name'].empty?
32
+ raise CustomException.new('last name is empty.') if client_details['last_name'].nil? || client_details['last_name'].empty?
33
+ raise CustomException.new('phone_number is empty.') if client_details['phone_number'].nil? || client_details['phone_number'].empty?
34
+
35
+ # Validate email
36
+ raise CustomException.new('Invalid email.') unless HelperValidation.is_valid_email(client_details['email'])
37
+
38
+ # Validate expiration date
39
+ raise CustomException.new('expiration_date must be a future date.') unless HelperValidation.is_future_date(data['expiration_date'])
40
+ end
41
+
42
+ def self.list(data)
43
+ # Validate amount
44
+ if data.key?('amount')
45
+ amount = data[:amount]
46
+
47
+ if amount.is_a?(String)
48
+ begin
49
+ amount = Integer(amount)
50
+ rescue ArgumentError
51
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
52
+ end
53
+ end
54
+
55
+ unless amount.is_a?(Integer)
56
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
57
+ end
58
+ end
59
+
60
+ # Validate min_amount
61
+ if data.key?('min_amount')
62
+ unless data[:min_amount].is_a?(Integer)
63
+ raise CustomException.new('Invalid min amount.')
64
+ end
65
+ end
66
+
67
+ # Validate max_amount
68
+ if data.key?('max_amount')
69
+ unless data[:max_amount].is_a?(Integer)
70
+ raise CustomException.new('Invalid max amount.')
71
+ end
72
+ end
73
+
74
+ if data.key?('creation_date_from') && data.key?('creation_date_to')
75
+ HelperValidation.validate_date_filter(data[:creation_date_from], data[:creation_date_to])
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,71 @@
1
+ require 'date'
2
+ require 'uri'
3
+ require 'json'
4
+ require 'util/validation/helper'
5
+ require 'util/validation/error'
6
+
7
+ class PlanValidation
8
+ def self.create(data)
9
+ data = data.to_json
10
+ data = JSON.parse(data)
11
+ # Validate amount
12
+ amount = data['amount']
13
+
14
+ if amount.is_a?(String)
15
+ begin
16
+ amount = Integer(amount)
17
+ print amount
18
+ rescue ArgumentError
19
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
20
+ end
21
+ end
22
+
23
+ unless amount.is_a?(Integer)
24
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
25
+ end
26
+
27
+ # Validate interval
28
+ allowed_values = ['dias', 'semanas', 'meses', 'años']
29
+ HelperValidation.validate_value(data['interval'], allowed_values)
30
+
31
+ # Validate currency
32
+ HelperValidation.validate_currency_code(data['currency_code'])
33
+ end
34
+
35
+ def self.list(data)
36
+ # Validate amount
37
+ if data.key?('amount')
38
+ amount = data[:amount]
39
+
40
+ if amount.is_a?(String)
41
+ begin
42
+ amount = Integer(amount)
43
+ rescue ArgumentError
44
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
45
+ end
46
+ end
47
+
48
+ unless amount.is_a?(Integer)
49
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
50
+ end
51
+ end
52
+
53
+ # Validate min_amount
54
+ if data.key?('min_amount')
55
+ unless data[:min_amount].is_a?(Integer)
56
+ raise CustomException.new('Invalid min amount.')
57
+ end
58
+ end
59
+
60
+ # Validate max_amount
61
+ if data.key?('max_amount')
62
+ unless data[:max_amount].is_a?(Integer)
63
+ raise CustomException.new('Invalid max amount.')
64
+ end
65
+ end
66
+
67
+ if data.key?('creation_date_from') && data.key?('creation_date_to')
68
+ HelperValidation.validate_date_filter(data[:creation_date_from], data[:creation_date_to])
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,47 @@
1
+ require 'date'
2
+ require 'uri'
3
+ require 'json'
4
+ require 'util/validation/helper'
5
+ require 'util/validation/error'
6
+
7
+ class RefundValidation
8
+ def self.create(data)
9
+ data = data.to_json
10
+ data = JSON.parse(data)
11
+ # Validate charge format
12
+ HelperValidation.validate_string_start(data['charge_id'], "chr")
13
+
14
+ # Validate reason
15
+ allowed_values = ['duplicado', 'fraudulento', 'solicitud_comprador']
16
+ HelperValidation.validate_value(data['reason'], allowed_values)
17
+
18
+ # Validate amount
19
+ amount = data['amount']
20
+
21
+ if amount.is_a?(String)
22
+ begin
23
+ amount = Integer(amount)
24
+ print amount
25
+ rescue ArgumentError
26
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
27
+ end
28
+ end
29
+
30
+ unless amount.is_a?(Integer)
31
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
32
+ end
33
+ end
34
+
35
+ def self.list(data)
36
+ # Validate card_brand
37
+ if data.key?('reason')
38
+ allowed_brand_values = ['duplicado', 'fraudulento', 'solicitud_comprador']
39
+ Helpers.validate_value(data[:reason], allowed_brand_values)
40
+ end
41
+
42
+ # Validate date filter
43
+ if data.key?('creation_date_from') && data.key?('creation_date_to')
44
+ Helpers.validate_date_filter(data[:creation_date_from], data[:creation_date_to])
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,26 @@
1
+ require 'date'
2
+ require 'uri'
3
+ require 'json'
4
+ require 'util/validation/helper'
5
+ require 'util/validation/error'
6
+
7
+ class SubscriptionValidation
8
+ def self.create(data)
9
+ data = data.to_json
10
+ data = JSON.parse(data)
11
+ HelperValidation.validate_string_start(data['card_id'], "crd")
12
+ HelperValidation.validate_string_start(data['plan_id'], "pln")
13
+ end
14
+
15
+ def self.list(data)
16
+ # Validate card_brand
17
+ if data.key?('plan_id')
18
+ HelperValidation.validate_string_start(data[:plan_id], "pln")
19
+ end
20
+
21
+ # Validate date filter
22
+ if data.key?('creation_date_from') && data.key?('creation_date_to')
23
+ Helpers.validate_date_filter(data[:creation_date_from], data[:creation_date_to])
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,70 @@
1
+ require 'date'
2
+ require 'uri'
3
+ require 'json'
4
+ require 'util/country-codes'
5
+ require 'util/validation/helper'
6
+ require 'util/validation/error'
7
+
8
+ class TokenValidation
9
+ def self.create(data)
10
+ data = data.to_json
11
+ data = JSON.parse(data)
12
+
13
+ # Validate card number
14
+ raise CustomException.new('Invalid card number.') unless HelperValidation.is_valid_card_number(data['card_number'])
15
+
16
+ # Validate CVV
17
+ raise CustomException.new('Invalid CVV.') unless data['cvv']&.match?(/^\d{3,4}$/)
18
+
19
+ # Validate email
20
+ raise CustomException.new('Invalid email.') unless HelperValidation.is_valid_email(data['email'])
21
+
22
+ # Validate expiration month
23
+ raise 'Invalid expiration month.' unless data['expiration_month'].to_s.match?(/^(0?[1-9]|1[012])$/)
24
+
25
+ # Validate expiration year
26
+ current_year = Date.today.year
27
+ if !data['expiration_year'].to_s.match?(/^\d{4}$/) || data['expiration_year'].to_s.to_i < current_year
28
+ raise 'Invalid expiration year.'
29
+ end
30
+
31
+ # Check if the card is expired
32
+ exp_date = Date.strptime("#{data['expiration_year']}-#{data['expiration_month']}", '%Y-%m')
33
+ raise 'Card has expired.' if exp_date < Date.today
34
+ end
35
+
36
+ def self.create_token_yape_validation(data)
37
+ data = data.to_json
38
+ data = JSON.parse(data)
39
+ # Validate amount
40
+ unless data['amount'].is_a?(Numeric) && data['amount'].to_i == data['amount']
41
+ raise CustomException.new('Invalid amount.')
42
+ end
43
+ end
44
+
45
+ def self.list(data)
46
+ if data.key?('device_type')
47
+ allowed_device_values = ['desktop', 'mobile', 'tablet']
48
+ HelperValidation.validate_value(data[:device_type], allowed_device_values)
49
+ end
50
+
51
+ if data.key?('card_brand')
52
+ allowed_brand_values = ['Visa', 'Mastercard', 'Amex', 'Diners']
53
+ HelperValidation.validate_value(data[:card_brand], allowed_brand_values)
54
+ end
55
+
56
+ if data.key?('card_type')
57
+ allowed_card_type_values = ['credito', 'debito', 'internacional']
58
+ HelperValidation.validate_value(data[:card_type], allowed_card_type_values)
59
+ end
60
+
61
+ if data.key?('country_code')
62
+ HelperValidation.validate_value(data[:country_code], get_country_codes)
63
+ end
64
+
65
+ if data.key?('creation_date_from') && data.key?('creation_date_to')
66
+ HelperValidation.validate_date_filter(data[:creation_date_from], data[:creation_date_to])
67
+ end
68
+ end
69
+
70
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: culqi-ruby-oficial
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Culqi Team
@@ -19,7 +19,6 @@ executables: []
19
19
  extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
22
- - lib/CulqiCRUD.rb
23
22
  - lib/culqi-ruby.rb
24
23
  - lib/culqi/card.rb
25
24
  - lib/culqi/charge.rb
@@ -40,9 +39,19 @@ files:
40
39
  - lib/operation/list.rb
41
40
  - lib/operation/post.rb
42
41
  - lib/operation/update.rb
43
- - lib/test_list.rb
44
42
  - lib/util/connect.rb
43
+ - lib/util/country-codes.rb
45
44
  - lib/util/encrypt-data.rb
45
+ - lib/util/validation/card.rb
46
+ - lib/util/validation/charge.rb
47
+ - lib/util/validation/customer.rb
48
+ - lib/util/validation/error.rb
49
+ - lib/util/validation/helper.rb
50
+ - lib/util/validation/order.rb
51
+ - lib/util/validation/plan.rb
52
+ - lib/util/validation/refund.rb
53
+ - lib/util/validation/subscription.rb
54
+ - lib/util/validation/token.rb
46
55
  homepage: http://rubygems.org/gems/culqi-ruby-oficial
47
56
  licenses:
48
57
  - MIT