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.
- checksums.yaml +4 -4
- data/lib/culqi/card.rb +16 -16
- data/lib/culqi/charge.rb +20 -20
- data/lib/culqi/customer.rb +17 -17
- data/lib/culqi/event.rb +14 -14
- data/lib/culqi/iins.rb +14 -14
- data/lib/culqi/order.rb +18 -18
- data/lib/culqi/plan.rb +16 -16
- data/lib/culqi/refund.rb +15 -15
- data/lib/culqi/subscription.rb +16 -16
- data/lib/culqi/token.rb +37 -37
- data/lib/culqi/transfer.rb +14 -14
- data/lib/culqi/version.rb +3 -3
- data/lib/culqi/yape.rb +13 -13
- data/lib/culqi-ruby.rb +38 -38
- data/lib/operation/confirm_type.rb +24 -24
- data/lib/operation/delete.rb +19 -19
- data/lib/operation/get.rb +57 -57
- data/lib/operation/list.rb +64 -64
- data/lib/operation/post.rb +85 -79
- data/lib/operation/update.rb +22 -22
- data/lib/util/connect.rb +54 -54
- data/lib/util/country-codes.rb +21 -21
- data/lib/util/encrypt-data.rb +49 -49
- data/lib/util/validation/card.rb +38 -36
- data/lib/util/validation/charge.rb +131 -129
- data/lib/util/validation/customer.rb +41 -39
- data/lib/util/validation/error.rb +17 -17
- data/lib/util/validation/helper.rb +40 -40
- data/lib/util/validation/order.rb +78 -76
- data/lib/util/validation/plan.rb +71 -69
- data/lib/util/validation/refund.rb +47 -45
- data/lib/util/validation/subscription.rb +26 -24
- data/lib/util/validation/token.rb +70 -65
- metadata +1 -3
- data/lib/CulqiCRUD.rb +0 -344
- data/lib/test_list.rb +0 -37
data/lib/util/encrypt-data.rb
CHANGED
@@ -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
|
data/lib/util/validation/card.rb
CHANGED
@@ -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
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
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
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
HelperValidation.validate_string_start(source_id, "
|
35
|
-
elsif source_id.start_with?("
|
36
|
-
HelperValidation.validate_string_start(source_id, "
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
-
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
|
-
|
11
|
-
|
12
|
-
|
13
|
-
raise CustomException.new('
|
14
|
-
raise CustomException.new('
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|