culqi-ruby-oficial 1.0.4 → 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.
@@ -1,49 +1,49 @@
1
- require 'openssl'
2
- require 'base64'
3
- require 'json'
4
- require 'openssl/oaep'
5
-
6
- module Encrypt
7
- def self.generate_random_bytes(length)
8
- OpenSSL::Random.random_bytes(length)
9
- end
10
-
11
- def self.encrypt_with_aes_rsa(data, public_key, is_json)
12
- key = generate_random_bytes(32) # Generate a 256-bit random key for AES encryption
13
- iv = generate_random_bytes(12) # GCM mode requires a 96-bit (12 bytes) random initialization vector
14
- auth_data = generate_random_bytes(16)
15
- cipher = OpenSSL::Cipher.new('AES-256-GCM')
16
- cipher.encrypt
17
- cipher.key = key
18
- cipher.iv = iv
19
- cipher.auth_data = auth_data
20
-
21
- # Since GCM does not require padding, we can directly pass the data to be encrypted
22
- cipher_text = cipher.update(data.to_json) + cipher.final
23
-
24
- # Get the auth tag
25
- auth_tag = cipher.auth_tag
26
-
27
- # Combine cipher text and auth tag
28
- encrypted = cipher_text #+ auth_tag
29
- #encrypted = encrypted.slice(0...-16)
30
- encrypted_data = Base64.strict_encode64(encrypted)
31
-
32
- ####
33
- rsa_public_key = OpenSSL::PKey::RSA.new(public_key)
34
- encrypted_key = rsa_encrypt(key, rsa_public_key)
35
- encrypted_iv = rsa_encrypt(iv, rsa_public_key)
36
-
37
- { encrypted_data: encrypted_data, encrypted_key: encrypted_key, encrypted_iv: encrypted_iv }
38
- end
39
-
40
- def self.rsa_encrypt(data, public_key)
41
- # Define the encryption parameters
42
- label = ''
43
-
44
- md = OpenSSL::Digest::SHA256
45
- cipher_text = public_key.public_encrypt_oaep(data, label, md, md)
46
-
47
- Base64.strict_encode64(cipher_text)
48
- end
49
- end
1
+ require 'openssl'
2
+ require 'base64'
3
+ require 'json'
4
+ require 'openssl/oaep'
5
+
6
+ module Encrypt
7
+ def self.generate_random_bytes(length)
8
+ OpenSSL::Random.random_bytes(length)
9
+ end
10
+
11
+ def self.encrypt_with_aes_rsa(data, public_key, is_json)
12
+ key = generate_random_bytes(32) # Generate a 256-bit random key for AES encryption
13
+ iv = generate_random_bytes(12) # GCM mode requires a 96-bit (12 bytes) random initialization vector
14
+ auth_data = generate_random_bytes(16)
15
+ cipher = OpenSSL::Cipher.new('AES-256-GCM')
16
+ cipher.encrypt
17
+ cipher.key = key
18
+ cipher.iv = iv
19
+ cipher.auth_data = auth_data
20
+
21
+ # Since GCM does not require padding, we can directly pass the data to be encrypted
22
+ cipher_text = cipher.update(data.to_json) + cipher.final
23
+
24
+ # Get the auth tag
25
+ auth_tag = cipher.auth_tag
26
+
27
+ # Combine cipher text and auth tag
28
+ encrypted = cipher_text #+ auth_tag
29
+ #encrypted = encrypted.slice(0...-16)
30
+ encrypted_data = Base64.strict_encode64(encrypted)
31
+
32
+ ####
33
+ rsa_public_key = OpenSSL::PKey::RSA.new(public_key)
34
+ encrypted_key = rsa_encrypt(key, rsa_public_key)
35
+ encrypted_iv = rsa_encrypt(iv, rsa_public_key)
36
+
37
+ { encrypted_data: encrypted_data, encrypted_key: encrypted_key, encrypted_iv: encrypted_iv }
38
+ end
39
+
40
+ def self.rsa_encrypt(data, public_key)
41
+ # Define the encryption parameters
42
+ label = ''
43
+
44
+ md = OpenSSL::Digest::SHA256
45
+ cipher_text = public_key.public_encrypt_oaep(data, label, md, md)
46
+
47
+ Base64.strict_encode64(cipher_text)
48
+ end
49
+ end
@@ -1,36 +1,38 @@
1
- require 'date'
2
- require 'uri'
3
- require 'json'
4
- require 'util/validation/helper'
5
- require 'util/validation/error'
6
-
7
- class CardValidation
8
- def self.create(data)
9
- HelperValidation.validate_string_start(data[:customer_id], "cus")
10
- HelperValidation.validate_string_start(data[:token_id], "tkn")
11
- end
12
-
13
- def self.list(data)
14
- # Validate card_brand
15
- if data.key?('card_brand')
16
- allowed_brand_values = ['Visa', 'Mastercard', 'Amex', 'Diners']
17
- Helpers.validate_value(data[:card_brand], allowed_brand_values)
18
- end
19
-
20
- # Validate card_type
21
- if data.key?('card_type')
22
- allowed_card_type_values = ['credito', 'debito', 'internacional']
23
- Helpers.validate_value(data[:card_type], allowed_card_type_values)
24
- end
25
-
26
- # Validate date filter
27
- if data.key?('creation_date_from') && data.key?('creation_date_to')
28
- Helpers.validate_date_filter(data[:creation_date_from], data[:creation_date_to])
29
- end
30
-
31
- # Validate country_code
32
- if data.key?('country_code')
33
- Helpers.validate_value(data[:country_code], get_country_codes)
34
- end
35
- end
36
- end
1
+ require 'date'
2
+ require 'uri'
3
+ require 'json'
4
+ require 'util/validation/helper'
5
+ require 'util/validation/error'
6
+
7
+ class CardValidation
8
+ def self.create(data)
9
+ data = data.to_json
10
+ data = JSON.parse(data)
11
+ HelperValidation.validate_string_start(data['customer_id'], "cus")
12
+ HelperValidation.validate_string_start(data['token_id'], "tkn")
13
+ end
14
+
15
+ def self.list(data)
16
+ # Validate card_brand
17
+ if data.key?('card_brand')
18
+ allowed_brand_values = ['Visa', 'Mastercard', 'Amex', 'Diners']
19
+ Helpers.validate_value(data[:card_brand], allowed_brand_values)
20
+ end
21
+
22
+ # Validate card_type
23
+ if data.key?('card_type')
24
+ allowed_card_type_values = ['credito', 'debito', 'internacional']
25
+ Helpers.validate_value(data[:card_type], allowed_card_type_values)
26
+ end
27
+
28
+ # Validate date filter
29
+ if data.key?('creation_date_from') && data.key?('creation_date_to')
30
+ Helpers.validate_date_filter(data[:creation_date_from], data[:creation_date_to])
31
+ end
32
+
33
+ # Validate country_code
34
+ if data.key?('country_code')
35
+ Helpers.validate_value(data[:country_code], get_country_codes)
36
+ end
37
+ end
38
+ end
@@ -1,129 +1,131 @@
1
- require 'date'
2
- require 'uri'
3
- require 'json'
4
- require 'util/validation/helper'
5
- require 'util/validation/error'
6
-
7
- class ChargeValidation
8
- def self.create(data)
9
- # Validate email
10
- raise 'Invalid email.' unless HelperValidation.is_valid_email(data[:email])
11
-
12
- # Validate amount
13
- amount = data[:amount]
14
-
15
- if amount.is_a?(String)
16
- begin
17
- amount = Integer(amount)
18
- print amount
19
- rescue ArgumentError
20
- raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
21
- end
22
- end
23
-
24
- unless amount.is_a?(Integer)
25
- raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
26
- end
27
-
28
- HelperValidation.validate_currency_code(data[:currency_code])
29
- source_id = data[:source_id]
30
-
31
- if source_id.start_with?("tkn")
32
- HelperValidation.validate_string_start(source_id, "tkn")
33
- elsif source_id.start_with?("ype")
34
- HelperValidation.validate_string_start(source_id, "ype")
35
- elsif source_id.start_with?("crd")
36
- HelperValidation.validate_string_start(source_id, "crd")
37
- else
38
- raise CustomException.new("Incorrect format. The format must start with tkn, ype, or crd")
39
- end
40
- end
41
-
42
- def self.list(data)
43
- # Validate email
44
- if data.key?('email')
45
- unless Helpers.is_valid_email(data[:email])
46
- raise CustomException.new('Invalid email.')
47
- end
48
- end
49
-
50
- # Validate amount
51
- if data.key?('amount')
52
- amount = data[:amount]
53
-
54
- if amount.is_a?(String)
55
- begin
56
- amount = Integer(amount)
57
- rescue ArgumentError
58
- raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
59
- end
60
- end
61
-
62
- unless amount.is_a?(Integer)
63
- raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
64
- end
65
- end
66
-
67
- # Validate min_amount
68
- if data.key?('min_amount')
69
- unless data[:min_amount].is_a?(Integer)
70
- raise CustomException.new('Invalid min amount.')
71
- end
72
- end
73
-
74
- # Validate max_amount
75
- if data.key?('max_amount')
76
- unless data[:max_amount].is_a?(Integer)
77
- raise CustomException.new('Invalid max amount.')
78
- end
79
- end
80
-
81
- # Validate installments
82
- if data.key?('installments')
83
- unless data[:installments].is_a?(Integer)
84
- raise CustomException.new('Invalid installments.')
85
- end
86
- end
87
-
88
- # Validate min_installments
89
- if data.key?('min_installments')
90
- unless data[:min_installments].is_a?(Integer)
91
- raise CustomException.new('Invalid min installments.')
92
- end
93
- end
94
-
95
- # Validate max_installments
96
- if data.key?('max_installments')
97
- unless data[:max_installments].is_a?(Integer)
98
- raise CustomException.new('Invalid max installments.')
99
- end
100
- end
101
-
102
- # Validate currency_code
103
- if data.key?('currency_code')
104
- Helpers.validate_currency_code(data[:currency_code])
105
- end
106
-
107
- # Validate card_brand
108
- if data.key?('card_brand')
109
- allowed_brand_values = ['Visa', 'Mastercard', 'Amex', 'Diners']
110
- Helpers.validate_value(data[:card_brand], allowed_brand_values)
111
- end
112
-
113
- # Validate card_type
114
- if data.key?('card_type')
115
- allowed_card_type_values = ['credito', 'debito', 'internacional']
116
- Helpers.validate_value(data[:card_type], allowed_card_type_values)
117
- end
118
-
119
- # Validate date filter
120
- if data.key?('creation_date_from') && data.key?('creation_date_to')
121
- Helpers.validate_date_filter(data[:creation_date_from], data[:creation_date_to])
122
- end
123
-
124
- # Validate country_code
125
- if data.key?('country_code')
126
- Helpers.validate_value(data[:country_code], get_country_codes)
127
- end
128
- end
129
- end
1
+ require 'date'
2
+ require 'uri'
3
+ require 'json'
4
+ require 'util/validation/helper'
5
+ require 'util/validation/error'
6
+
7
+ class ChargeValidation
8
+ def self.create(data)
9
+ data = data.to_json
10
+ data = JSON.parse(data)
11
+ # Validate email
12
+ raise 'Invalid email.' unless HelperValidation.is_valid_email(data['email'])
13
+
14
+ # Validate amount
15
+ amount = data['amount']
16
+
17
+ if amount.is_a?(String)
18
+ begin
19
+ amount = Integer(amount)
20
+ print amount
21
+ rescue ArgumentError
22
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
23
+ end
24
+ end
25
+
26
+ unless amount.is_a?(Integer)
27
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
28
+ end
29
+
30
+ HelperValidation.validate_currency_code(data['currency_code'])
31
+ source_id = data['source_id']
32
+
33
+ if source_id.start_with?("tkn")
34
+ HelperValidation.validate_string_start(source_id, "tkn")
35
+ elsif source_id.start_with?("ype")
36
+ HelperValidation.validate_string_start(source_id, "ype")
37
+ elsif source_id.start_with?("crd")
38
+ HelperValidation.validate_string_start(source_id, "crd")
39
+ else
40
+ raise CustomException.new("Incorrect format. The format must start with tkn, ype, or crd")
41
+ end
42
+ end
43
+
44
+ def self.list(data)
45
+ # Validate email
46
+ if data.key?('email')
47
+ unless Helpers.is_valid_email(data[:email])
48
+ raise CustomException.new('Invalid email.')
49
+ end
50
+ end
51
+
52
+ # Validate amount
53
+ if data.key?('amount')
54
+ amount = data[:amount]
55
+
56
+ if amount.is_a?(String)
57
+ begin
58
+ amount = Integer(amount)
59
+ rescue ArgumentError
60
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
61
+ end
62
+ end
63
+
64
+ unless amount.is_a?(Integer)
65
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
66
+ end
67
+ end
68
+
69
+ # Validate min_amount
70
+ if data.key?('min_amount')
71
+ unless data[:min_amount].is_a?(Integer)
72
+ raise CustomException.new('Invalid min amount.')
73
+ end
74
+ end
75
+
76
+ # Validate max_amount
77
+ if data.key?('max_amount')
78
+ unless data[:max_amount].is_a?(Integer)
79
+ raise CustomException.new('Invalid max amount.')
80
+ end
81
+ end
82
+
83
+ # Validate installments
84
+ if data.key?('installments')
85
+ unless data[:installments].is_a?(Integer)
86
+ raise CustomException.new('Invalid installments.')
87
+ end
88
+ end
89
+
90
+ # Validate min_installments
91
+ if data.key?('min_installments')
92
+ unless data[:min_installments].is_a?(Integer)
93
+ raise CustomException.new('Invalid min installments.')
94
+ end
95
+ end
96
+
97
+ # Validate max_installments
98
+ if data.key?('max_installments')
99
+ unless data[:max_installments].is_a?(Integer)
100
+ raise CustomException.new('Invalid max installments.')
101
+ end
102
+ end
103
+
104
+ # Validate currency_code
105
+ if data.key?('currency_code')
106
+ Helpers.validate_currency_code(data[:currency_code])
107
+ end
108
+
109
+ # Validate card_brand
110
+ if data.key?('card_brand')
111
+ allowed_brand_values = ['Visa', 'Mastercard', 'Amex', 'Diners']
112
+ Helpers.validate_value(data[:card_brand], allowed_brand_values)
113
+ end
114
+
115
+ # Validate card_type
116
+ if data.key?('card_type')
117
+ allowed_card_type_values = ['credito', 'debito', 'internacional']
118
+ Helpers.validate_value(data[:card_type], allowed_card_type_values)
119
+ end
120
+
121
+ # Validate date filter
122
+ if data.key?('creation_date_from') && data.key?('creation_date_to')
123
+ Helpers.validate_date_filter(data[:creation_date_from], data[:creation_date_to])
124
+ end
125
+
126
+ # Validate country_code
127
+ if data.key?('country_code')
128
+ Helpers.validate_value(data[:country_code], get_country_codes)
129
+ end
130
+ end
131
+ end
@@ -1,39 +1,41 @@
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 CustomerValidation
9
- def self.create(data)
10
- # Validate address, firstname, and lastname
11
- raise CustomException.new('first name is empty.') if data[:first_name].nil? || data[:first_name].empty?
12
- raise CustomException.new('last name is empty.') if data[:last_name].nil? || data[:last_name].empty?
13
- raise CustomException.new('address is empty.') if data[:address].nil? || data[:address].empty?
14
- raise CustomException.new('address_city is empty.') if data[:address_city].nil? || data[:address_city].empty?
15
-
16
- unless data[:phone_number].is_a?(String)
17
- raise CustomException.new("Invalid 'phone_number'. It should be a string.")
18
- end
19
-
20
- # Validate country code
21
- HelperValidation.validate_value(data[:country_code], CountryCodes.get_country_codes)
22
-
23
- # Validate email
24
- raise 'Invalid email.' unless HelperValidation.is_valid_email(data[:email])
25
- end
26
-
27
- def self.list(data)
28
- # Validate email
29
- if data.key?('email')
30
- unless Helpers.is_valid_email(data[:email])
31
- raise CustomException.new('Invalid email.')
32
- end
33
- end
34
- # Validate country_code
35
- if data.key?('country_code')
36
- Helpers.validate_value(data[:country_code], get_country_codes)
37
- end
38
- end
39
- end
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 CustomerValidation
9
+ def self.create(data)
10
+ data = data.to_json
11
+ data = JSON.parse(data)
12
+ # Validate address, firstname, and lastname
13
+ raise CustomException.new('first name is empty.') if data['first_name'].nil? || data['first_name'].empty?
14
+ raise CustomException.new('last name is empty.') if data['last_name'].nil? || data['last_name'].empty?
15
+ raise CustomException.new('address is empty.') if data['address'].nil? || data['address'].empty?
16
+ raise CustomException.new('address_city is empty.') if data['address_city'].nil? || data['address_city'].empty?
17
+
18
+ unless data['phone_number'].is_a?(String)
19
+ raise CustomException.new("Invalid 'phone_number'. It should be a string.")
20
+ end
21
+
22
+ # Validate country code
23
+ HelperValidation.validate_value(data['country_code'], CountryCodes.get_country_codes)
24
+
25
+ # Validate email
26
+ raise 'Invalid email.' unless HelperValidation.is_valid_email(data['email'])
27
+ end
28
+
29
+ def self.list(data)
30
+ # Validate email
31
+ if data.key?('email')
32
+ unless Helpers.is_valid_email(data[:email])
33
+ raise CustomException.new('Invalid email.')
34
+ end
35
+ end
36
+ # Validate country_code
37
+ if data.key?('country_code')
38
+ Helpers.validate_value(data[:country_code], get_country_codes)
39
+ end
40
+ end
41
+ end
@@ -1,17 +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
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
@@ -1,40 +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
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