culqi-ruby-oficial 1.0.0 → 1.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ecb2fa769b2c48bff2617ebdbf33ca50d5c27621215d9e6d769e1e2236b60616
4
- data.tar.gz: ada3857f363a49eb69eba554eec7c3929d34c111cf73aa802ff85629a16156b3
3
+ metadata.gz: 0a19bc65fc13ea192da12a845fe32e5be4f88b93f4c7802b095a84aa7110651c
4
+ data.tar.gz: bb473a88137d68eb356ebe1c80a4138e9649cb68b11277567ab432b9d7c99af0
5
5
  SHA512:
6
- metadata.gz: 3a658abafef77c3a968e20199f346763215566238d7f77b783710efab78d29b60a196cdfd711ed43df4f301de10d73e628fe8711ecadb8f9d6770aafc0f61e7c
7
- data.tar.gz: c4abdc144f4c182cfd44af1fb410094c57982088539f76acfe84b060fdc1e26e968f5d0d4b7e56caf52f18e9871a747fc606f83305f358698de343d84cc9602c
6
+ metadata.gz: 3b5a5fd53ede88c2b21d7c75062a53b803b539f80b1b1f01ad3230b773b79666c0439e7979ea76ebb6e22afb43fdfdf30c7de0a4400a3c19b8a8b6aee4e77dbf
7
+ data.tar.gz: 1b1c086f79684d8b7150a8b38c3193e57501137805ed8e6ee50f627148a18f18ea943624ae268a59fc518b8f9444768b30fa1661265c8a29cd1539aa078b03e2
data/lib/culqi/charge.rb CHANGED
@@ -12,7 +12,7 @@ module Culqi
12
12
 
13
13
  def self.capture(id)
14
14
  response, statuscode = Culqi.connect("#{@url}#{id}/capture/", Culqi.secret_key, nil, "post", Culqi::READ_TIMEOUT)
15
- return response.read_body, statuscode
15
+ return response, statuscode
16
16
  end
17
17
 
18
18
  end
data/lib/culqi/order.rb CHANGED
@@ -5,6 +5,7 @@ module Culqi
5
5
  extend List
6
6
  extend Post
7
7
  extend Get
8
+ extend Delete
8
9
  extend ConfirmType
9
10
  extend Update
10
11
 
data/lib/culqi/token.rb CHANGED
@@ -1,3 +1,7 @@
1
+ require 'util/validation/token'
2
+ require 'util/validation/error'
3
+
4
+
1
5
  module Culqi
2
6
 
3
7
  class Token
@@ -10,6 +14,24 @@ module Culqi
10
14
 
11
15
  @url = URL
12
16
 
17
+ def self.createYape(params={}, rsa_key='', rsa_id='')
18
+ begin
19
+ key = ''
20
+ puts params
21
+ if @url.include? 'token'
22
+ TokenValidation.create_token_yape_validation(params)
23
+ if(rsa_key != '')
24
+ params = Encrypt.encrypt_with_aes_rsa(params, rsa_key, true)
25
+ end
26
+ key = Culqi.public_key
27
+ response, statuscode = Culqi.connect(@url, key, params, 'post', Culqi::READ_TIMEOUT, true, rsa_id)
28
+ return response, statuscode
29
+ end
30
+ rescue CustomException => e
31
+ return e.message
32
+ end
33
+ end
34
+
13
35
  end
14
36
 
15
37
  end
data/lib/culqi/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Culqi
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.4"
3
3
  end
data/lib/culqi-ruby.rb CHANGED
@@ -24,6 +24,12 @@ module Culqi
24
24
  API_BASE_SECURE = 'https://secure.culqi.com/v2'
25
25
  READ_TIMEOUT = 120
26
26
  LIST_TIMEOUT = 360
27
+ X_CULQI_ENV_TEST = "test"
28
+ X_CULQI_ENV_LIVE = "live"
29
+ X_API_VERSION = "2"
30
+ X_CULQI_CLIENT = "culqi-ruby"
31
+ X_CULQI_CLIENT_VERSION = "0.1.0"
32
+
27
33
 
28
34
  class << self
29
35
  attr_accessor :public_key, :secret_key, :rsa_id, :rsa_key
@@ -1,4 +1,5 @@
1
1
  require 'util/connect'
2
+ require 'util/validation/helper'
2
3
 
3
4
  module Culqi::ConfirmType
4
5
 
@@ -7,6 +8,10 @@ module Culqi::ConfirmType
7
8
  end
8
9
 
9
10
  def confirm(params={}, rsa_key='', rsa_id='')
11
+ error = HelperValidation.validate_string_start(params[:order_id], "ord")
12
+ if error
13
+ return error
14
+ end
10
15
  key = ''
11
16
  if(rsa_key != '')
12
17
  params = Encrypt.encrypt_with_aes_rsa(params, rsa_key, true)
@@ -1,4 +1,5 @@
1
1
  require 'util/connect'
2
+ require 'util/validation/helper'
2
3
 
3
4
  module Culqi::Delete
4
5
 
@@ -7,8 +8,12 @@ module Culqi::Delete
7
8
  end
8
9
 
9
10
  def delete(id)
11
+ error = verifyClassValidationGet(@url, id)
12
+ if error
13
+ return error
14
+ end
15
+
10
16
  response = Culqi.connect("#{@url}#{id}/", Culqi.secret_key, nil, 'delete', Culqi::READ_TIMEOUT)
11
- return response.read_body
17
+ return response
12
18
  end
13
-
14
19
  end
data/lib/operation/get.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'util/connect'
2
+ require 'util/validation/helper'
2
3
 
3
4
  module Culqi::Get
4
5
 
@@ -7,8 +8,50 @@ module Culqi::Get
7
8
  end
8
9
 
9
10
  def get(id)
11
+ error = verifyClassValidationGet(@url, id)
12
+ if error
13
+ return error
14
+ end
10
15
  response = Culqi.connect("#{@url}#{id}/", Culqi.secret_key, nil, 'get', Culqi::READ_TIMEOUT)
11
16
  return response
12
17
  end
13
18
 
19
+ def verifyClassValidationGet(url='', id)
20
+ begin
21
+ if @url.include? 'token'
22
+ HelperValidation.validate_string_start(id, "tkn")
23
+ end
24
+
25
+ if @url.include? 'charge'
26
+ HelperValidation.validate_string_start(id, "chr")
27
+ end
28
+
29
+ if @url.include? 'card'
30
+ HelperValidation.validate_string_start(id, "crd")
31
+ end
32
+
33
+ if @url.include? 'customer'
34
+ HelperValidation.validate_string_start(id, "cus")
35
+ end
36
+
37
+ if @url.include? 'refund'
38
+ HelperValidation.validate_string_start(id, "ref")
39
+ end
40
+
41
+ if @url.include? 'plan'
42
+ HelperValidation.validate_string_start(id, "pln")
43
+ end
44
+
45
+ if @url.include? 'subscription'
46
+ HelperValidation.validate_string_start(id, "sxn")
47
+ end
48
+
49
+ if @url.include? 'order'
50
+ HelperValidation.validate_string_start(id, "ord")
51
+ end
52
+ rescue CustomException => e
53
+ return e.message
54
+ end
55
+ end
56
+
14
57
  end
@@ -1,4 +1,12 @@
1
1
  require 'util/connect'
2
+ require 'util/validation/token'
3
+ require 'util/validation/charge'
4
+ require 'util/validation/customer'
5
+ require 'util/validation/refund'
6
+ require 'util/validation/plan'
7
+ require 'util/validation/card'
8
+ require 'util/validation/subscription'
9
+ require 'util/validation/order'
2
10
 
3
11
  module Culqi::List
4
12
 
@@ -7,8 +15,50 @@ module Culqi::List
7
15
  end
8
16
 
9
17
  def list(params={})
18
+ error = verifyClassValidationList(@url, params)
19
+ if error
20
+ return error
21
+ end
10
22
  response = Culqi.connect(@url, Culqi.secret_key, params, 'get', Culqi::LIST_TIMEOUT)
11
- return response.read_body
23
+ return response
24
+ end
25
+
26
+ def verifyClassValidationList(url='', params)
27
+ begin
28
+ if @url.include? 'token'
29
+ TokenValidation.list(params)
30
+ end
31
+
32
+ if @url.include? 'charge'
33
+ ChargeValidation.list(params)
34
+ end
35
+
36
+ if @url.include? 'card'
37
+ CardValidation.list(params)
38
+ end
39
+
40
+ if @url.include? 'customer'
41
+ CustomerValidation.list(params)
42
+ end
43
+
44
+ if @url.include? 'refund'
45
+ RefundValidation.list(params)
46
+ end
47
+
48
+ if @url.include? 'plan'
49
+ PlanValidation.list(params)
50
+ end
51
+
52
+ if @url.include? 'subscription'
53
+ SubscriptionValidation.list(params)
54
+ end
55
+
56
+ if @url.include? 'order'
57
+ OrderValidation.list(params)
58
+ end
59
+ rescue CustomException => e
60
+ return e.message
61
+ end
12
62
  end
13
63
 
14
64
  end
@@ -1,5 +1,14 @@
1
1
  require 'util/connect'
2
2
  require 'util/encrypt-data'
3
+ require 'util/validation/token'
4
+ require 'util/validation/charge'
5
+ require 'util/validation/customer'
6
+ require 'util/validation/refund'
7
+ require 'util/validation/plan'
8
+ require 'util/validation/card'
9
+ require 'util/validation/subscription'
10
+ require 'util/validation/order'
11
+
3
12
  module Culqi::Post
4
13
 
5
14
  def initialize
@@ -9,6 +18,11 @@ module Culqi::Post
9
18
  def create(params={}, rsa_key='', rsa_id='')
10
19
  key = ''
11
20
  puts params
21
+ error = verifyClassValidation(@url, params)
22
+ if error
23
+ return error
24
+ end
25
+
12
26
  if @url.include? 'token'
13
27
  if(rsa_key != '')
14
28
  params = Encrypt.encrypt_with_aes_rsa(params, rsa_key, true)
@@ -24,4 +38,42 @@ module Culqi::Post
24
38
 
25
39
  end
26
40
 
41
+ def verifyClassValidation(url='', params)
42
+ begin
43
+ if @url.include? 'token'
44
+ TokenValidation.create(params)
45
+ end
46
+
47
+ if @url.include? 'charge'
48
+ ChargeValidation.create(params)
49
+ end
50
+
51
+ if @url.include? 'card'
52
+ CardValidation.create(params)
53
+ end
54
+
55
+ if @url.include? 'customer'
56
+ CustomerValidation.create(params)
57
+ end
58
+
59
+ if @url.include? 'refund'
60
+ RefundValidation.create(params)
61
+ end
62
+
63
+ if @url.include? 'plan'
64
+ PlanValidation.create(params)
65
+ end
66
+
67
+ if @url.include? 'subscription'
68
+ SubscriptionValidation.create(params)
69
+ end
70
+
71
+ if @url.include? 'order'
72
+ OrderValidation.create(params)
73
+ end
74
+ rescue CustomException => e
75
+ return e.message
76
+ end
77
+ end
78
+
27
79
  end
@@ -1,4 +1,5 @@
1
1
  require 'util/connect'
2
+ require 'util/validation/helper'
2
3
 
3
4
  module Culqi::Update
4
5
 
@@ -7,6 +8,10 @@ module Culqi::Update
7
8
  end
8
9
 
9
10
  def update(id, params={}, rsa_key='', rsa_id='')
11
+ error = verifyClassValidationGet(@url, id)
12
+ if error
13
+ return error
14
+ end
10
15
  if(rsa_key != '')
11
16
  params = Encrypt.encrypt_with_aes_rsa(params, rsa_key, true)
12
17
  end
data/lib/util/connect.rb CHANGED
@@ -7,9 +7,21 @@ module Culqi
7
7
  base_url = secure_url ? Culqi::API_BASE_SECURE : Culqi::API_BASE
8
8
  full_url = "#{base_url}#{url}"
9
9
 
10
+ print full_url
11
+
12
+ if(api_key.include? 'test')
13
+ env = Culqi::X_CULQI_ENV_TEST
14
+ else
15
+ env = Culqi::X_CULQI_ENV_LIVE
16
+ end
17
+
10
18
  headers = {
11
19
  "Authorization" => "Bearer #{api_key}",
12
20
  "Content-Type" => "application/json",
21
+ "x-culqi-env" => env,
22
+ "x-api-version" => Culqi::X_API_VERSION,
23
+ "x-culqi-client" => Culqi::X_CULQI_CLIENT,
24
+ "x-culqi-client-version" => Culqi::X_CULQI_CLIENT,
13
25
  "x-culqi-rsa-id" => rsa_id
14
26
  }
15
27
 
@@ -0,0 +1,22 @@
1
+ # country_codes.rb
2
+ module CountryCodes
3
+ def self.get_country_codes
4
+ ['AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AO', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AW', 'AX', 'AZ',
5
+ 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BL', 'BM', 'BN', 'BO', 'BQ', 'BR', 'BS',
6
+ 'BT', 'BV', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN',
7
+ 'CO', 'CR', 'CU', 'CV', 'CW', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE',
8
+ 'EG', 'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GB', 'GD', 'GE', 'GF',
9
+ 'GG', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GS', 'GT', 'GU', 'GW', 'GY', 'HK', 'HM',
10
+ 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IM', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JE', 'JM',
11
+ 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC',
12
+ 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'ME', 'MF', 'MG', 'MH', 'MK',
13
+ 'ML', 'MM', 'MN', 'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA',
14
+ 'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG',
15
+ 'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PW', 'PY', 'QA', 'RE', 'RO', 'RS', 'RU', 'RW',
16
+ 'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'SS',
17
+ 'ST', 'SV', 'SX', 'SY', 'SZ', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO',
18
+ 'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'UM', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI',
19
+ 'VN', 'VU', 'WF', 'WS', 'YE', 'YT', 'ZA', 'ZM', 'ZW']
20
+ end
21
+ end
22
+
@@ -0,0 +1,36 @@
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
@@ -0,0 +1,129 @@
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
@@ -0,0 +1,39 @@
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
@@ -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,76 @@
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
+ amount = data[:amount]
11
+
12
+ if amount.is_a?(String)
13
+ begin
14
+ amount = Integer(amount)
15
+ rescue ArgumentError
16
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
17
+ end
18
+ end
19
+
20
+ unless amount.is_a?(Integer)
21
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
22
+ end
23
+
24
+ # Validate currency
25
+ HelperValidation.validate_currency_code(data[:currency_code])
26
+
27
+ # Validate firstname, lastname, and phone
28
+ client_details = data[:client_details] || {}
29
+ raise CustomException.new('first name is empty.') if client_details[:first_name].nil? || client_details[:first_name].empty?
30
+ raise CustomException.new('last name is empty.') if client_details[:last_name].nil? || client_details[:last_name].empty?
31
+ raise CustomException.new('phone_number is empty.') if client_details[:phone_number].nil? || client_details[:phone_number].empty?
32
+
33
+ # Validate email
34
+ raise CustomException.new('Invalid email.') unless HelperValidation.is_valid_email(client_details[:email])
35
+
36
+ # Validate expiration date
37
+ raise CustomException.new('expiration_date must be a future date.') unless HelperValidation.is_future_date(data[:expiration_date])
38
+ end
39
+
40
+ def self.list(data)
41
+ # Validate amount
42
+ if data.key?('amount')
43
+ amount = data[:amount]
44
+
45
+ if amount.is_a?(String)
46
+ begin
47
+ amount = Integer(amount)
48
+ rescue ArgumentError
49
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
50
+ end
51
+ end
52
+
53
+ unless amount.is_a?(Integer)
54
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
55
+ end
56
+ end
57
+
58
+ # Validate min_amount
59
+ if data.key?('min_amount')
60
+ unless data[:min_amount].is_a?(Integer)
61
+ raise CustomException.new('Invalid min amount.')
62
+ end
63
+ end
64
+
65
+ # Validate max_amount
66
+ if data.key?('max_amount')
67
+ unless data[:max_amount].is_a?(Integer)
68
+ raise CustomException.new('Invalid max amount.')
69
+ end
70
+ end
71
+
72
+ if data.key?('creation_date_from') && data.key?('creation_date_to')
73
+ HelperValidation.validate_date_filter(data[:creation_date_from], data[:creation_date_to])
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,69 @@
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
+ # Validate amount
10
+ amount = data[:amount]
11
+
12
+ if amount.is_a?(String)
13
+ begin
14
+ amount = Integer(amount)
15
+ print amount
16
+ rescue ArgumentError
17
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
18
+ end
19
+ end
20
+
21
+ unless amount.is_a?(Integer)
22
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
23
+ end
24
+
25
+ # Validate interval
26
+ allowed_values = ['dias', 'semanas', 'meses', 'años']
27
+ HelperValidation.validate_value(data[:interval], allowed_values)
28
+
29
+ # Validate currency
30
+ HelperValidation.validate_currency_code(data[:currency_code])
31
+ end
32
+
33
+ def self.list(data)
34
+ # Validate amount
35
+ if data.key?('amount')
36
+ amount = data[:amount]
37
+
38
+ if amount.is_a?(String)
39
+ begin
40
+ amount = Integer(amount)
41
+ rescue ArgumentError
42
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
43
+ end
44
+ end
45
+
46
+ unless amount.is_a?(Integer)
47
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
48
+ end
49
+ end
50
+
51
+ # Validate min_amount
52
+ if data.key?('min_amount')
53
+ unless data[:min_amount].is_a?(Integer)
54
+ raise CustomException.new('Invalid min amount.')
55
+ end
56
+ end
57
+
58
+ # Validate max_amount
59
+ if data.key?('max_amount')
60
+ unless data[:max_amount].is_a?(Integer)
61
+ raise CustomException.new('Invalid max amount.')
62
+ end
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
+ end
@@ -0,0 +1,45 @@
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
+ # Validate charge format
10
+ HelperValidation.validate_string_start(data[:charge_id], "chr")
11
+
12
+ # Validate reason
13
+ allowed_values = ['duplicado', 'fraudulento', 'solicitud_comprador']
14
+ HelperValidation.validate_value(data[:reason], allowed_values)
15
+
16
+ # Validate amount
17
+ amount = data[:amount]
18
+
19
+ if amount.is_a?(String)
20
+ begin
21
+ amount = Integer(amount)
22
+ print amount
23
+ rescue ArgumentError
24
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
25
+ end
26
+ end
27
+
28
+ unless amount.is_a?(Integer)
29
+ raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
30
+ end
31
+ end
32
+
33
+ def self.list(data)
34
+ # Validate card_brand
35
+ if data.key?('reason')
36
+ allowed_brand_values = ['duplicado', 'fraudulento', 'solicitud_comprador']
37
+ Helpers.validate_value(data[:reason], allowed_brand_values)
38
+ end
39
+
40
+ # Validate date filter
41
+ if data.key?('creation_date_from') && data.key?('creation_date_to')
42
+ Helpers.validate_date_filter(data[:creation_date_from], data[:creation_date_to])
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,24 @@
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
+ HelperValidation.validate_string_start(data[:card_id], "crd")
10
+ HelperValidation.validate_string_start(data[:plan_id], "pln")
11
+ end
12
+
13
+ def self.list(data)
14
+ # Validate card_brand
15
+ if data.key?('plan_id')
16
+ HelperValidation.validate_string_start(data[:plan_id], "pln")
17
+ end
18
+
19
+ # Validate date filter
20
+ if data.key?('creation_date_from') && data.key?('creation_date_to')
21
+ Helpers.validate_date_filter(data[:creation_date_from], data[:creation_date_to])
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,65 @@
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
+ # Validate card number
11
+ raise CustomException.new('Invalid card number.') unless HelperValidation.is_valid_card_number(data[:card_number])
12
+
13
+ # Validate CVV
14
+ raise CustomException.new('Invalid CVV.') unless data[:cvv]&.match?(/^\d{3,4}$/)
15
+
16
+ # Validate email
17
+ raise CustomException.new('Invalid email.') unless HelperValidation.is_valid_email(data[:email])
18
+
19
+ # Validate expiration month
20
+ raise 'Invalid expiration month.' unless data[:expiration_month].to_s.match?(/^(0?[1-9]|1[012])$/)
21
+
22
+ # Validate expiration year
23
+ current_year = Date.today.year
24
+ if !data[:expiration_year].to_s.match?(/^\d{4}$/) || data[:expiration_year].to_s.to_i < current_year
25
+ raise 'Invalid expiration year.'
26
+ end
27
+
28
+ # Check if the card is expired
29
+ exp_date = Date.strptime("#{data[:expiration_year]}-#{data[:expiration_month]}", '%Y-%m')
30
+ raise 'Card has expired.' if exp_date < Date.today
31
+ end
32
+
33
+ def self.create_token_yape_validation(data)
34
+ # Validate amount
35
+ unless data[:amount].is_a?(Numeric) && data[:amount].to_i == data[:amount]
36
+ raise CustomException.new('Invalid amount.')
37
+ end
38
+ end
39
+
40
+ def self.list(data)
41
+ if data.key?('device_type')
42
+ allowed_device_values = ['desktop', 'mobile', 'tablet']
43
+ HelperValidation.validate_value(data[:device_type], allowed_device_values)
44
+ end
45
+
46
+ if data.key?('card_brand')
47
+ allowed_brand_values = ['Visa', 'Mastercard', 'Amex', 'Diners']
48
+ HelperValidation.validate_value(data[:card_brand], allowed_brand_values)
49
+ end
50
+
51
+ if data.key?('card_type')
52
+ allowed_card_type_values = ['credito', 'debito', 'internacional']
53
+ HelperValidation.validate_value(data[:card_type], allowed_card_type_values)
54
+ end
55
+
56
+ if data.key?('country_code')
57
+ HelperValidation.validate_value(data[:country_code], get_country_codes)
58
+ end
59
+
60
+ if data.key?('creation_date_from') && data.key?('creation_date_to')
61
+ HelperValidation.validate_date_filter(data[:creation_date_from], data[:creation_date_to])
62
+ end
63
+ end
64
+
65
+ 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.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Culqi Team
@@ -42,7 +42,18 @@ files:
42
42
  - lib/operation/update.rb
43
43
  - lib/test_list.rb
44
44
  - lib/util/connect.rb
45
+ - lib/util/country-codes.rb
45
46
  - lib/util/encrypt-data.rb
47
+ - lib/util/validation/card.rb
48
+ - lib/util/validation/charge.rb
49
+ - lib/util/validation/customer.rb
50
+ - lib/util/validation/error.rb
51
+ - lib/util/validation/helper.rb
52
+ - lib/util/validation/order.rb
53
+ - lib/util/validation/plan.rb
54
+ - lib/util/validation/refund.rb
55
+ - lib/util/validation/subscription.rb
56
+ - lib/util/validation/token.rb
46
57
  homepage: http://rubygems.org/gems/culqi-ruby-oficial
47
58
  licenses:
48
59
  - MIT