kount_complete 1.0.9 → 2.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,90 +1,88 @@
1
- module Kount
2
- ##
3
- # This class extends the Request class.
4
- class Inquiry < Request
5
- attr_accessor :cart
6
-
7
- # Initialize an Inquiry object
8
- #
9
- # Example usage
10
- # {:MACK => "Y", :AUTH => "A"}
11
- #
12
- # @param initial_params [Hash] Initial params for request
13
- def initialize(initial_params = {})
14
- super(initial_params)
15
- @cart = Cart.new
16
- # We want Request to default to MODE Q unless a different mode has
17
- # been passed.
18
- add_params(MODE: 'Q') unless initial_params.key?(:MODE)
19
- end
20
-
21
- # @param version [String] RIS version
22
- # @param merchant_id [String] Merchant ID
23
- # @param response_format [String] Response format (JSON)
24
- # @param ksalt [String] Kount supplied secret salt for KHASH
25
- def prepare_params(version, merchant_id, response_format, ksalt)
26
- super(version, merchant_id, response_format, ksalt)
27
- begin
28
- params.merge! collect_cart_items
29
- # The Kount::Request has no knowledge of the KSALT or merchant_id, both
30
- # of which are needed for KHASH. Request form params have everything we
31
- # need at this point to do the KHASH if needed.
32
- fixup_payment_params(ksalt, merchant_id)
33
- end
34
- params
35
- end
36
-
37
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
38
- def fixup_payment_params(ksalt, merchant_id)
39
- ptok = params[:PTOK]
40
- case params[:PTYP]
41
- when 'CARD', 'TOKEN'
42
- #ptok = Kount::SecurityMash.hash_credit_card(ptok, ksalt)
43
- ptok = Kount::Khash.HashPaymentToken(ptok, ksalt)
44
- params.merge!(PTOK: ptok, PENC: 'KHASH')
45
- when 'GIFT', 'OTHER'
46
- #ptok = Kount::SecurityMash.hash_gift_card(ptok, ksalt, merchant_id)
47
- ptok = Kount::Khash.HashGiftCard(ptok, ksalt, merchant_id)
48
- params.merge!(PTOK: ptok, PENC: 'KHASH')
49
- when 'CHEK', 'OTHER'
50
- ptok = Kount::Khash.HashCheckPayment(ptok, ksalt)
51
- params.merge!(PTOK: ptok, PENC: 'KHASH')
52
- when 'NONE'
53
- params.merge!(PTOK: nil, PENC: nil)
54
- else
55
- params[:PENC] ||= 'NONE'
56
- end
57
- end
58
-
59
- # Pulls the cart data into the request params hash
60
- def collect_cart_items
61
- {
62
- PROD_TYPE: cart.get_item(:TYPE),
63
- PROD_DESC: cart.get_item(:DESC),
64
- PROD_ITEM: cart.get_item(:ITEM),
65
- PROD_PRICE: cart.get_item(:PRICE),
66
- PROD_QUANT: cart.get_item(:QUANT)
67
- }
68
- end
69
-
70
- # Puts the cart object into the request for processing
71
- # @param cart [Kount::Cart] Cart object
72
- def add_cart(cart)
73
- @cart = cart
74
- end
75
-
76
- # Add UDF to request
77
- # @param name [String] UDF label name
78
- # @param value [String] UDF value
79
- def add_udf(name, value)
80
- @params.merge!("UDF[#{name}]" => value)
81
- end
82
-
83
- # Convenience method to create the payment params
84
- # @param type [String] Payment type
85
- # @param token [String] Payment token
86
- def add_payment(type, token = '')
87
- add_params(PTYP: type, PTOK: token)
88
- end
89
- end
90
- end
1
+ module Kount
2
+ ##
3
+ # This class extends the Request class.
4
+ class Inquiry < Request
5
+ attr_accessor :cart
6
+
7
+ # Initialize an Inquiry object
8
+ #
9
+ # Example usage
10
+ # {:MACK => "Y", :AUTH => "A"}
11
+ #
12
+ # @param initial_params [Hash] Initial params for request
13
+ def initialize(initial_params = {})
14
+ super(initial_params)
15
+ @cart = Cart.new
16
+ # We want Request to default to MODE Q unless a different mode has
17
+ # been passed.
18
+ add_params(MODE: 'Q') unless initial_params.key?(:MODE)
19
+ end
20
+
21
+ # @param version [String] RIS version
22
+ # @param merchant_id [String] Merchant ID
23
+ # @param response_format [String] Response format (JSON)
24
+ # @param ksalt [String] Kount supplied secret salt for KHASH
25
+ def prepare_params(version, merchant_id, response_format, ksalt)
26
+ super(version, merchant_id, response_format, ksalt)
27
+ params.merge! collect_cart_items
28
+ # The Kount::Request has no knowledge of the KSALT or merchant_id, both
29
+ # of which are needed for KHASH. Request form params have everything we
30
+ # need at this point to do the KHASH if needed.
31
+ fixup_payment_params(ksalt, merchant_id)
32
+ params
33
+ end
34
+
35
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
36
+ def fixup_payment_params(ksalt, merchant_id)
37
+ ptok = params[:PTOK]
38
+ case params[:PTYP]
39
+ when 'CARD', 'TOKEN'
40
+ #ptok = Kount::SecurityMash.hash_credit_card(ptok, ksalt)
41
+ ptok = Kount::Khash.hash_payment_token(ptok, ksalt)
42
+ params.merge!(PTOK: ptok, PENC: 'KHASH')
43
+ when 'GIFT', 'OTHER'
44
+ #ptok = Kount::SecurityMash.hash_gift_card(ptok, ksalt, merchant_id)
45
+ ptok = Kount::Khash.hash_gift_card(ptok, ksalt, merchant_id)
46
+ params.merge!(PTOK: ptok, PENC: 'KHASH')
47
+ when 'CHEK', 'OTHER'
48
+ ptok = Kount::Khash.hash_check_payment(ptok, ksalt)
49
+ params.merge!(PTOK: ptok, PENC: 'KHASH')
50
+ when 'NONE'
51
+ params.merge!(PTOK: nil, PENC: nil)
52
+ else
53
+ params[:PENC] ||= 'NONE'
54
+ end
55
+ end
56
+
57
+ # Pulls the cart data into the request params hash
58
+ def collect_cart_items
59
+ {
60
+ PROD_TYPE: cart.get_item(:TYPE),
61
+ PROD_DESC: cart.get_item(:DESC),
62
+ PROD_ITEM: cart.get_item(:ITEM),
63
+ PROD_PRICE: cart.get_item(:PRICE),
64
+ PROD_QUANT: cart.get_item(:QUANT)
65
+ }
66
+ end
67
+
68
+ # Puts the cart object into the request for processing
69
+ # @param cart [Kount::Cart] Cart object
70
+ def add_cart(cart)
71
+ @cart = cart
72
+ end
73
+
74
+ # Add UDF to request
75
+ # @param name [String] UDF label name
76
+ # @param value [String] UDF value
77
+ def add_udf(name, value)
78
+ @params.merge!("UDF[#{name}]" => value)
79
+ end
80
+
81
+ # Convenience method to create the payment params
82
+ # @param type [String] Payment type
83
+ # @param token [String] Payment token
84
+ def add_payment(type, token = '')
85
+ add_params(PTYP: type, PTOK: token)
86
+ end
87
+ end
88
+ end
@@ -1,28 +1,28 @@
1
- module Kount
2
- ##
3
- # This class extends the Request class and is used in the
4
- # process of sending updates to an existing transaction
5
- # generated by a previous Inquiry request.
6
- class Update < Request
7
- # Initialize an Update object
8
- #
9
- # Example usage
10
- # {:MACK => "Y", :AUTH => "A"}
11
- #
12
- # @param initial_params [Hash] Initial params for request
13
- def initialize(initial_params = {})
14
- super(initial_params)
15
- # Default to mode U unless mode X is explicitly set
16
- add_params(MODE: 'U') unless initial_params[:MODE] == 'X'
17
- end
18
-
19
- # @param version [String] RIS version
20
- # @param merchant_id [String] Merchant ID
21
- # @param response_format [String] Response format (JSON)
22
- # @param ksalt [String] Kount supplied secret salt for KHASH
23
- def prepare_params(version, merchant_id, response_format, ksalt)
24
- super(version, merchant_id, response_format, ksalt)
25
- params
26
- end
27
- end
28
- end
1
+ module Kount
2
+ ##
3
+ # This class extends the Request class and is used in the
4
+ # process of sending updates to an existing transaction
5
+ # generated by a previous Inquiry request.
6
+ class Update < Request
7
+ # Initialize an Update object
8
+ #
9
+ # Example usage
10
+ # {:MACK => "Y", :AUTH => "A"}
11
+ #
12
+ # @param initial_params [Hash] Initial params for request
13
+ def initialize(initial_params = {})
14
+ super(initial_params)
15
+ # Default to mode U unless mode X is explicitly set
16
+ add_params(MODE: 'U') unless initial_params[:MODE] == 'X'
17
+ end
18
+
19
+ # @param version [String] RIS version
20
+ # @param merchant_id [String] Merchant ID
21
+ # @param response_format [String] Response format (JSON)
22
+ # @param ksalt [String] Kount supplied secret salt for KHASH
23
+ def prepare_params(version, merchant_id, response_format, ksalt)
24
+ super(version, merchant_id, response_format, ksalt)
25
+ params
26
+ end
27
+ end
28
+ end
@@ -1,82 +1,82 @@
1
- require 'digest/sha1'
2
- module Kount
3
- ##
4
- # This class implements the Kount KHASH for cards and gift cards.
5
- # rubocop:disable Style/Documentation
6
- class SecurityMash
7
- # @param plain_text [String] String to be hashed
8
- # @param ptyp [String] Payment type code: CARD, GIFT, or OTHER
9
- # @return [String] KHASH version of string
10
- def self.hash_token(plain_text, ptyp, ksalt, merchant_id = '')
11
- if ptyp == 'CARD'
12
- hash_credit_card(plain_text, ksalt)
13
- else
14
- hash_gift_card(plain_text, ksalt, merchant_id)
15
- end
16
- end
17
-
18
- # Hash a credit card number.
19
- # Preserves first six characters of the input so that hashed cards can be
20
- # categorized by Bank Identification Number (BIN).
21
- #
22
- # Example usage:
23
- # hashed = Kount::SecurityMash.hash_credit_card("4111111111111111")
24
- # Expect: 411111WMS5YA6FUZA1KC
25
- # hashed = Kount::SecurityMash.hash_credit_card("5199185454061655")
26
- # Expect: 5199182NOQRXNKTTFL11
27
- # hashed = Kount::SecurityMash.hash_credit_card("4259344583883")
28
- # Expect: 425934FEXQI1QS6TH2O5
29
- #
30
- # @param plain_text [String] String to be hashed
31
- # @return [String] KHASH version of string
32
- def self.hash_credit_card(plain_text, ksalt)
33
- return plain_text if khashed?(plain_text)
34
- first_six = plain_text[0..5]
35
- mashed = mash(plain_text, 14, ksalt)
36
- "#{first_six}#{mashed}"
37
- end
38
-
39
- # Hash a gift card number.
40
- # Use the six characters of the merchant id so that hashed cards can be
41
- # unique across the entire domain.
42
- #
43
- # Example usage:
44
- # hashed = Kount::SecurityMash.hash_gift_card("123456", "3245876")
45
- # Expect: 1234569HXH32Y5NNJCGB
46
-
47
- # @param plain_text [String] String to be hashed
48
- # @return [String] KHASH version of string
49
- def self.hash_gift_card(plain_text, ksalt, merchant_id)
50
- mashed = mash(plain_text, 14, ksalt)
51
- "#{merchant_id}#{mashed}"
52
- end
53
-
54
- # Compute a base64 hash of the provided data.
55
- #
56
- # @param data [String] Data to hash
57
- # @param len [int] Length of hash to retain
58
- # @return [String] Hashed data
59
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
60
- def self.mash(data, len, m)
61
- a = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
62
- r = Digest::SHA1.hexdigest("#{data}.#{m}")
63
- c = ''
64
- len = 17 if len > 17
65
- limit = 2 * len
66
- i = 0
67
- while i < limit
68
- c << a[r[i..i + 6].to_i(16) % 36]
69
- i += 2
70
- end
71
- c
72
- end
73
-
74
- # end mash
75
-
76
- # @param val [String] Token that may or may not be khashed
77
- # @return [Boolean] True if token is already khashed
78
- def self.khashed?(val)
79
- true if val =~ /(\d{6}[A-Z0-9]{14})/
80
- end
81
- end # end KountSecurityMash
82
- end
1
+ require 'digest/sha1'
2
+ module Kount
3
+ ##
4
+ # This class implements the Kount KHASH for cards and gift cards.
5
+
6
+ class SecurityMash
7
+ # @param plain_text [String] String to be hashed
8
+ # @param ptyp [String] Payment type code: CARD, GIFT, or OTHER
9
+ # @return [String] KHASH version of string
10
+ def self.hash_token(plain_text, ptyp, ksalt, merchant_id = '')
11
+ if ptyp == 'CARD'
12
+ hash_credit_card(plain_text, ksalt)
13
+ else
14
+ hash_gift_card(plain_text, ksalt, merchant_id)
15
+ end
16
+ end
17
+
18
+ # Hash a credit card number.
19
+ # Preserves first six characters of the input so that hashed cards can be
20
+ # categorized by Bank Identification Number (BIN).
21
+ #
22
+ # Example usage:
23
+ # hashed = Kount::SecurityMash.hash_credit_card("4111111111111111")
24
+ # Expect: 411111WMS5YA6FUZA1KC
25
+ # hashed = Kount::SecurityMash.hash_credit_card("5199185454061655")
26
+ # Expect: 5199182NOQRXNKTTFL11
27
+ # hashed = Kount::SecurityMash.hash_credit_card("4259344583883")
28
+ # Expect: 425934FEXQI1QS6TH2O5
29
+ #
30
+ # @param plain_text [String] String to be hashed
31
+ # @return [String] KHASH version of string
32
+ def self.hash_credit_card(plain_text, ksalt)
33
+ return plain_text if khashed?(plain_text)
34
+ first_six = plain_text[0..5]
35
+ mashed = mash(plain_text, 14, ksalt)
36
+ "#{first_six}#{mashed}"
37
+ end
38
+
39
+ # Hash a gift card number.
40
+ # Use the six characters of the merchant id so that hashed cards can be
41
+ # unique across the entire domain.
42
+ #
43
+ # Example usage:
44
+ # hashed = Kount::SecurityMash.hash_gift_card("123456", "3245876")
45
+ # Expect: 1234569HXH32Y5NNJCGB
46
+
47
+ # @param plain_text [String] String to be hashed
48
+ # @return [String] KHASH version of string
49
+ def self.hash_gift_card(plain_text, ksalt, merchant_id)
50
+ mashed = mash(plain_text, 14, ksalt)
51
+ "#{merchant_id}#{mashed}"
52
+ end
53
+
54
+ # Compute a base64 hash of the provided data.
55
+ #
56
+ # @param data [String] Data to hash
57
+ # @param len [int] Length of hash to retain
58
+ # @return [String] Hashed data
59
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
60
+ def self.mash(data, len, m)
61
+ a = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
62
+ r = Digest::SHA1.hexdigest("#{data}.#{m}")
63
+ c = ''
64
+ len = 17 if len > 17
65
+ limit = 2 * len
66
+ i = 0
67
+ while i < limit
68
+ c << a[r[i..i + 6].to_i(16) % 36]
69
+ i += 2
70
+ end
71
+ c
72
+ end
73
+
74
+ # end mash
75
+
76
+ # @param val [String] Token that may or may not be khashed
77
+ # @return [Boolean] True if token is already khashed
78
+ def self.khashed?(val)
79
+ true if val =~ /(\d{6}[A-Z0-9]{14})/
80
+ end
81
+ end # end KountSecurityMash
82
+ end
@@ -1,47 +1,59 @@
1
- module Kount
2
- class Khash
3
- # @param plain_text [String] String to be hashed
4
- # @param ptyp [String] Payment type code: CARD, GIFT, or OTHER
5
- # @return [String] KHASH version of string
6
- def self.hash_token(plain_text, ptyp, ksalt, merchant_id = '')
7
- if ptyp == 'CARD'
8
- HashPaymentToken(plain_text, ksalt)
9
- elsif ptyp == 'CHEK'
10
- HashCheckPayment(plain_text, ksalt)
11
- else
12
- HashGiftCard(plain_text, ksalt, merchant_id)
13
- end
14
- end
15
-
16
- def self.getKhash(data, len, m)
17
- a = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
18
- r = Digest::SHA1.hexdigest("#{data}.#{m}")
19
- c = ''
20
- len = 17 if len > 17
21
- limit = 2 * len
22
- i = 0
23
- while i < limit
24
- c << a[r[i..i + 6].to_i(16) % 36]
25
- i += 2
26
- end
27
- c
28
- end
29
-
30
- def self.HashPaymentToken(plain_text, ksalt)
31
- first_six = plain_text[0..5]
32
- mashed = getKhash(plain_text, 14, ksalt)
33
- "#{first_six}#{mashed}"
34
- end
35
-
36
- def self.HashCheckPayment(plain_text, ksalt)
37
- first_six = plain_text[0..5]
38
- mashed = getKhash(plain_text, 14, ksalt)
39
- "#{first_six}#{mashed}"
40
- end
41
-
42
- def self.HashGiftCard(plain_text, ksalt, merchant_id)
43
- mashed = getKhash(plain_text, 14, ksalt)
44
- "#{merchant_id}#{mashed}"
45
- end
46
- end
47
- end
1
+ module Kount
2
+ class Khash
3
+ # @param plain_text [String] String to be hashed
4
+ # @param ptyp [String] Payment type code: CARD, GIFT, or OTHER
5
+ # @return [String] KHASH version of string
6
+ def self.hash_token(plain_text, ptyp, ksalt, merchant_id = '')
7
+ # changed if-else to case-when to follow ruby coding standard
8
+
9
+ case ptyp
10
+ when 'CARD'
11
+ HashPaymentToken(plain_text, ksalt)
12
+ when 'CHEK'
13
+ HashCheckPayment(plain_text, ksalt)
14
+ else
15
+ HashGiftCard(plain_text, ksalt, merchant_id)
16
+ end
17
+ end
18
+
19
+ def self.getkhash(data, len, m)
20
+ a = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
21
+ r = Digest::SHA1.hexdigest("#{data}.#{m}")
22
+ c = ''
23
+ len = 17 if len > 17
24
+ limit = 2 * len
25
+ i = 0
26
+ while i < limit
27
+ c << a[r[i..i + 6].to_i(16) % 36]
28
+ i += 2
29
+ end
30
+ c
31
+ end
32
+
33
+ def self.hash_payment_token(plain_text, ksalt)
34
+ return plain_text if khashed?(plain_text)
35
+ first_six = plain_text[0..5]
36
+ mashed = getkhash(plain_text, 14, ksalt)
37
+ "#{first_six}#{mashed}"
38
+ end
39
+
40
+ def self.hash_check_payment(plain_text, ksalt)
41
+ return plain_text if khashed?(plain_text)
42
+ first_six = plain_text[0..5]
43
+ mashed = getkhash(plain_text, 14, ksalt)
44
+ "#{first_six}#{mashed}"
45
+ end
46
+
47
+ def self.hash_gift_card(plain_text, ksalt, merchant_id)
48
+ mashed = getkhash(plain_text, 14, ksalt)
49
+ "#{merchant_id}#{mashed}"
50
+ end
51
+
52
+ # @param val [String] Token that may or may not be khashed
53
+ # @return [Boolean] True if token is already khashed
54
+ def self.khashed?(val)
55
+ true if val =~ /(\d{6}[A-Z0-9]{14})/
56
+ end
57
+
58
+ end
59
+ end