kount_complete 2.0.2 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,90 +1,90 @@
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.hash_payment_token(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.hash_gift_card(ptok, ksalt, merchant_id)
48
- params.merge!(PTOK: ptok, PENC: 'KHASH')
49
- when 'CHEK', 'OTHER'
50
- ptok = Kount::Khash.hash_check_payment(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
+ 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.hash_payment_token(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.hash_gift_card(ptok, ksalt, merchant_id)
48
+ params.merge!(PTOK: ptok, PENC: 'KHASH')
49
+ when 'CHEK', 'OTHER'
50
+ ptok = Kount::Khash.hash_check_payment(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,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
-
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,56 +1,56 @@
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.hash_payment_token(plain_text, ksalt)
31
- return plain_text if khashed?(plain_text)
32
- first_six = plain_text[0..5]
33
- mashed = getkhash(plain_text, 14, ksalt)
34
- "#{first_six}#{mashed}"
35
- end
36
-
37
- def self.hash_check_payment(plain_text, ksalt)
38
- return plain_text if khashed?(plain_text)
39
- first_six = plain_text[0..5]
40
- mashed = getkhash(plain_text, 14, ksalt)
41
- "#{first_six}#{mashed}"
42
- end
43
-
44
- def self.hash_gift_card(plain_text, ksalt, merchant_id)
45
- mashed = getkhash(plain_text, 14, ksalt)
46
- "#{merchant_id}#{mashed}"
47
- end
48
-
49
- # @param val [String] Token that may or may not be khashed
50
- # @return [Boolean] True if token is already khashed
51
- def self.khashed?(val)
52
- true if val =~ /(\d{6}[A-Z0-9]{14})/
53
- end
54
-
55
- end
56
- 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
+ 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.hash_payment_token(plain_text, ksalt)
31
+ return plain_text if khashed?(plain_text)
32
+ first_six = plain_text[0..5]
33
+ mashed = getkhash(plain_text, 14, ksalt)
34
+ "#{first_six}#{mashed}"
35
+ end
36
+
37
+ def self.hash_check_payment(plain_text, ksalt)
38
+ return plain_text if khashed?(plain_text)
39
+ first_six = plain_text[0..5]
40
+ mashed = getkhash(plain_text, 14, ksalt)
41
+ "#{first_six}#{mashed}"
42
+ end
43
+
44
+ def self.hash_gift_card(plain_text, ksalt, merchant_id)
45
+ mashed = getkhash(plain_text, 14, ksalt)
46
+ "#{merchant_id}#{mashed}"
47
+ end
48
+
49
+ # @param val [String] Token that may or may not be khashed
50
+ # @return [Boolean] True if token is already khashed
51
+ def self.khashed?(val)
52
+ true if val =~ /(\d{6}[A-Z0-9]{14})/
53
+ end
54
+
55
+ end
56
+ end