k2-connect-ruby 2.0.0 → 3.0.0
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/.ruby-version +1 -0
- data/Gemfile.lock +78 -64
- data/README.md +70 -49
- data/k2-connect-ruby.gemspec +12 -11
- data/lib/k2-connect-ruby/{k2_entity/entity.rb → entity.rb} +5 -0
- data/lib/k2-connect-ruby/k2_entity/k2_entity.rb +24 -26
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/k2_pay.rb +92 -98
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/k2_settlement.rb +43 -44
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/k2_stk.rb +43 -44
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/k2_transfer.rb +35 -36
- data/lib/k2-connect-ruby/k2_entity/k2_notification.rb +34 -35
- data/lib/k2-connect-ruby/k2_entity/k2_polling.rb +32 -34
- data/lib/k2-connect-ruby/k2_entity/k2_subscribe.rb +34 -36
- data/lib/k2-connect-ruby/k2_entity/k2_token.rb +58 -54
- data/lib/k2-connect-ruby/k2_errors.rb +64 -62
- data/lib/k2-connect-ruby/k2_services/k2_client.rb +27 -0
- data/lib/k2-connect-ruby/k2_services/payload_process.rb +10 -5
- data/lib/k2-connect-ruby/k2_services/payloads/k2_transaction.rb +52 -46
- data/lib/k2-connect-ruby/k2_services/payloads/k2_webhooks.rb +57 -51
- data/lib/k2-connect-ruby/k2_services/payloads/transactions/incoming_payment.rb +50 -42
- data/lib/k2-connect-ruby/k2_services/payloads/transactions/outgoing_payment.rb +19 -11
- data/lib/k2-connect-ruby/k2_services/payloads/transactions/transfer.rb +16 -8
- data/lib/k2-connect-ruby/k2_services/payloads/webhooks/b2b_transaction_received.rb +18 -0
- data/lib/k2-connect-ruby/k2_services/payloads/webhooks/buygoods_transaction_received.rb +13 -0
- data/lib/k2-connect-ruby/k2_services/payloads/webhooks/buygoods_transaction_reversed.rb +13 -0
- data/lib/k2-connect-ruby/k2_services/payloads/webhooks/customer_created.rb +20 -12
- data/lib/k2-connect-ruby/k2_services/payloads/webhooks/settlement_webhook.rb +40 -32
- data/lib/k2-connect-ruby/k2_utilities/config/k2_config.rb +49 -46
- data/lib/k2-connect-ruby/k2_utilities/k2_authenticator.rb +19 -0
- data/lib/k2-connect-ruby/k2_utilities/k2_connection.rb +39 -32
- data/lib/k2-connect-ruby/k2_utilities/k2_process_result.rb +42 -37
- data/lib/k2-connect-ruby/k2_utilities/k2_process_webhook.rb +51 -50
- data/lib/k2-connect-ruby/k2_utilities/k2_url_parse.rb +11 -6
- data/lib/k2-connect-ruby/k2_utilities/k2_validation.rb +104 -102
- data/lib/k2-connect-ruby/{utilities.rb → k2_utilities.rb} +8 -8
- data/lib/k2-connect-ruby/version.rb +1 -1
- data/lib/k2-connect-ruby.rb +7 -25
- metadata +56 -43
- data/lib/k2-connect-ruby/k2_services/client/k2_client.rb +0 -24
- data/lib/k2-connect-ruby/k2_services/payloads/webhooks/b2b_received.rb +0 -10
- data/lib/k2-connect-ruby/k2_services/payloads/webhooks/buygoods_received.rb +0 -5
- data/lib/k2-connect-ruby/k2_services/payloads/webhooks/buygoods_reversal.rb +0 -5
- data/lib/k2-connect-ruby/k2_services/payloads/webhooks/m2m_transaction.rb +0 -8
- data/lib/k2-connect-ruby/k2_utilities/k2_authorize.rb +0 -14
- data/lib/k2-connect-ruby/k2_utilities/spec_modules/spec_config.rb +0 -41
@@ -1,55 +1,56 @@
|
|
1
|
-
|
2
|
-
module
|
3
|
-
|
4
|
-
|
5
|
-
self.check_topic(payload) if K2Authenticator.authenticate(payload, secret_key, signature)
|
6
|
-
end
|
1
|
+
module K2ConnectRuby
|
2
|
+
module K2Utilities
|
3
|
+
module K2ProcessWebhook
|
4
|
+
extend self
|
7
5
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
when 'buygoods_transaction_received'
|
13
|
-
buygoods_received = BuygoodsTransactionReceived.new(payload)
|
14
|
-
return buygoods_received
|
15
|
-
# Buygoods Transaction Reversed
|
16
|
-
when 'buygoods_transaction_reversed'
|
17
|
-
buygoods_reversed = BuygoodsTransactionReversed.new(payload)
|
18
|
-
return buygoods_reversed
|
19
|
-
# B2b Transaction
|
20
|
-
when 'b2b_transaction_received'
|
21
|
-
b2b_transaction = B2b.new(payload)
|
22
|
-
return b2b_transaction
|
23
|
-
# Merchant to Merchant
|
24
|
-
when 'm2m_transaction_received'
|
25
|
-
m2m_transaction_received = MerchantToMerchant.new(payload)
|
26
|
-
return m2m_transaction_received
|
27
|
-
# Settlement Transfer
|
28
|
-
when 'settlement_transfer_completed'
|
29
|
-
settlement_transfer = SettlementWebhook.new(payload)
|
30
|
-
return settlement_transfer
|
31
|
-
# Customer Created
|
32
|
-
when 'customer_created'
|
33
|
-
customer_created = CustomerCreated.new(payload)
|
34
|
-
return customer_created
|
35
|
-
else
|
36
|
-
raise ArgumentError, 'No Other Specified Event!'
|
37
|
-
end
|
38
|
-
end
|
6
|
+
def process(payload, secret_key, signature)
|
7
|
+
raise ArgumentError, 'Empty/Nil Request Body Argument!' if payload.blank?
|
8
|
+
check_topic(payload) if K2ConnectRuby::K2Utilities::K2Authenticator.authenticate(payload, secret_key, signature)
|
9
|
+
end
|
39
10
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
11
|
+
def check_topic(payload)
|
12
|
+
result_topic = payload.dig('topic')
|
13
|
+
case result_topic
|
14
|
+
# Buygoods Transaction Received
|
15
|
+
when 'buygoods_transaction_received'
|
16
|
+
buygoods_received = K2ConnectRuby::K2Services::Payloads::Webhooks::BuygoodsTransactionReceived.new(payload)
|
17
|
+
return buygoods_received
|
18
|
+
# Buygoods Transaction Reversed
|
19
|
+
when 'buygoods_transaction_reversed'
|
20
|
+
buygoods_reversed = K2ConnectRuby::K2Services::Payloads::Webhooks::BuygoodsTransactionReversed.new(payload)
|
21
|
+
return buygoods_reversed
|
22
|
+
# B2b Transaction
|
23
|
+
when 'b2b_transaction_received'
|
24
|
+
b2b_transaction = K2ConnectRuby::K2Services::Payloads::Webhooks::B2bTransactionReceived.new(payload)
|
25
|
+
return b2b_transaction
|
26
|
+
# Settlement Transfer
|
27
|
+
when 'settlement_transfer_completed'
|
28
|
+
settlement_transfer = K2ConnectRuby::K2Services::Payloads::Webhooks::SettlementWebhook.new(payload)
|
29
|
+
return settlement_transfer
|
30
|
+
# Customer Created
|
31
|
+
when 'customer_created'
|
32
|
+
customer_created = K2ConnectRuby::K2Services::Payloads::Webhooks::CustomerCreated.new(payload)
|
33
|
+
return customer_created
|
34
|
+
else
|
35
|
+
raise ArgumentError, 'No Other Specified Event!'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Returns a Hash Object
|
40
|
+
def return_obj_hash(instance_hash = HashWithIndifferentAccess.new, obj)
|
41
|
+
obj.instance_variables.each do |value|
|
42
|
+
instance_hash[:"#{value.to_s.tr('@', '')}"] = obj.instance_variable_get(value)
|
43
|
+
end
|
44
|
+
instance_hash.each(&:freeze).freeze
|
45
|
+
end
|
47
46
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
47
|
+
# Returns an Array Object
|
48
|
+
def return_obj_array(instance_array = [], obj)
|
49
|
+
obj.instance_variables.each do |value|
|
50
|
+
instance_array << obj.instance_variable_get(value)
|
51
|
+
end
|
52
|
+
instance_array.each(&:freeze).freeze
|
53
|
+
end
|
52
54
|
end
|
53
|
-
instance_array.each(&:freeze).freeze
|
54
55
|
end
|
55
|
-
end
|
56
|
+
end
|
@@ -1,7 +1,12 @@
|
|
1
|
-
|
2
|
-
module
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
module K2ConnectRuby
|
2
|
+
module K2Utilities
|
3
|
+
module K2UrlParse
|
4
|
+
extend self
|
5
|
+
|
6
|
+
def remove_localhost(url)
|
7
|
+
url.host = '127.0.0.1' if url.host.eql?('localhost')
|
8
|
+
url
|
9
|
+
end
|
10
|
+
end
|
6
11
|
end
|
7
|
-
end
|
12
|
+
end
|
@@ -1,126 +1,128 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
module K2Validation
|
4
|
-
|
5
|
-
|
1
|
+
module K2ConnectRuby
|
2
|
+
module K2Utilities
|
3
|
+
module K2Validation
|
4
|
+
ALL_EVENT_TYPES = %[buygoods_transaction_received b2b_transaction_received buygoods_transaction_reversed customer_created settlement_transfer_completed]
|
5
|
+
TILL_SCOPE_EVENT_TYPES = %[buygoods_transaction_received b2b_transaction_received buygoods_transaction_reversed]
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
else
|
12
|
-
unless !!the_input == the_input
|
13
|
-
if the_input.is_a?(Hash)
|
14
|
-
validate_hash(the_input.with_indifferent_access, the_array)
|
7
|
+
# Validating Method
|
8
|
+
def validate_input(the_input, the_array)
|
9
|
+
if the_input.blank?
|
10
|
+
raise ArgumentError, "Empty or Nil Input!\n No Input Content has been given."
|
15
11
|
else
|
16
|
-
|
17
|
-
if the_input.is_a?(
|
18
|
-
|
19
|
-
elsif the_input.is_a?(Integer)
|
20
|
-
raise ArgumentError, "Wrong Input Format.\n The Input is an Integer."
|
21
|
-
elsif the_input.key?(:authenticity_token)
|
22
|
-
nil_values(the_input.permit(the_array).to_hash.with_indifferent_access)
|
12
|
+
unless !!the_input == the_input
|
13
|
+
if the_input.is_a?(Hash)
|
14
|
+
validate_hash(the_input.with_indifferent_access, the_array)
|
23
15
|
else
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
16
|
+
begin
|
17
|
+
if the_input.is_a?(String)
|
18
|
+
raise ArgumentError, "Wrong Input Format.\n The Input is a String."
|
19
|
+
elsif the_input.is_a?(Integer)
|
20
|
+
raise ArgumentError, "Wrong Input Format.\n The Input is an Integer."
|
21
|
+
elsif the_input.key?(:authenticity_token)
|
22
|
+
nil_values(the_input.permit(the_array).to_hash.with_indifferent_access)
|
23
|
+
else
|
24
|
+
raise ArgumentError, "Undefined Input Format.\n The Input is Neither a Hash nor an ActionController::Parameter Object."
|
25
|
+
end
|
26
|
+
rescue NoMethodError => nme
|
27
|
+
if nme.message.include?('has_key?')
|
28
|
+
raise ArgumentError, "Undefined Input Format.\n The Input is Neither a Hash nor an ActionController::Parameter Object."
|
29
|
+
end
|
30
|
+
end
|
29
31
|
end
|
30
32
|
end
|
31
33
|
end
|
34
|
+
to_indifferent_access(the_input)
|
32
35
|
end
|
33
|
-
end
|
34
|
-
to_indifferent_access(the_input)
|
35
|
-
end
|
36
36
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
37
|
+
# Validate the Hash Input Parameters
|
38
|
+
def validate_hash(the_input, the_array)
|
39
|
+
nil_values(the_input)
|
40
|
+
incorrect_keys(the_input, the_array)
|
41
|
+
end
|
42
42
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
43
|
+
# Return Incorrect Key Symbols for Hashes
|
44
|
+
def incorrect_keys(the_input, invalid_hash = [], the_array)
|
45
|
+
the_input.each_key do |key|
|
46
|
+
validate_network(the_input[:network]) if key.eql?("network")
|
47
|
+
validate_settlement_method(the_input[:settlement_method]) if key.eql?("settlement_method")
|
48
|
+
invalid_hash << key unless the_array.include?(key.to_s)
|
49
|
+
end
|
50
|
+
raise K2IncorrectParams, invalid_hash if invalid_hash.present?
|
51
|
+
end
|
52
52
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
53
|
+
# Return Key Symbols with Blank Values
|
54
|
+
def nil_values(the_input, nil_keys_array = [])
|
55
|
+
the_input.select { |_, v| v.blank? }.each_key do |key|
|
56
|
+
nil_keys_array << key.to_s
|
57
|
+
end
|
58
|
+
raise K2EmptyParams, nil_keys_array unless nil_keys_array.blank?
|
59
|
+
end
|
60
60
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
61
|
+
# Validate Phone Number
|
62
|
+
def validate_phone(phone)
|
63
|
+
raise ArgumentError, "No Phone Number given." if phone.blank?
|
64
|
+
# Kenyan Phone Numbers
|
65
|
+
unless phone.blank?
|
66
|
+
if phone[-(number = phone.to_i.to_s.size).to_i, 3].eql?(254.to_s)
|
67
|
+
raise ArgumentError, 'Invalid Kenyan Phone Number.' unless phone[-9, 9][0].eql?(7.to_s)
|
68
|
+
else
|
69
|
+
raise ArgumentError, 'Invalid Phone Number.' unless number.eql?(9)
|
70
|
+
end
|
71
|
+
phone.tr('+', '')
|
72
|
+
end
|
70
73
|
end
|
71
|
-
phone.tr('+', '')
|
72
|
-
end
|
73
|
-
end
|
74
74
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
75
|
+
# Validate Email Format
|
76
|
+
def validate_email(email)
|
77
|
+
unless email.blank?
|
78
|
+
raise ArgumentError, 'Invalid Email Address.' unless email.match(URI::MailTo::EMAIL_REGEXP).present?
|
79
|
+
end
|
80
|
+
email
|
81
|
+
end
|
82
82
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
83
|
+
# Validate the Network Operator
|
84
|
+
def validate_network(network)
|
85
|
+
raise ArgumentError, "Invalid Network Operator." unless K2ConnectRuby::K2Utilities::Config::K2Config.network_operators.include?(network.to_s.upcase)
|
86
|
+
end
|
87
87
|
|
88
|
-
|
89
|
-
|
90
|
-
|
88
|
+
def validate_settlement_method(settlement_method)
|
89
|
+
raise ArgumentError, "Wrong Settlement Method" unless settlement_method.eql?("EFT") || settlement_method.eql?("RTS")
|
90
|
+
end
|
91
91
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
92
|
+
# Converts Hash Objects to HashWithIndifferentAccess Objects
|
93
|
+
def to_indifferent_access(params)
|
94
|
+
params.with_indifferent_access
|
95
|
+
end
|
96
96
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
97
|
+
def validate_url(url)
|
98
|
+
raise ArgumentError, 'Invalid URL Format.' unless url =~ /\A#{URI.regexp(%w[https http])}\z/
|
99
|
+
url
|
100
|
+
end
|
101
101
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
102
|
+
def validate_till_number_prefix(till_number)
|
103
|
+
raise ArgumentError, "No Till Number given." if till_number.blank?
|
104
|
+
raise ArgumentError, "Invalid Till Number format." unless till_number[0, 1].eql?('K')
|
105
|
+
till_number
|
106
|
+
end
|
107
107
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
108
|
+
def validate_webhook(params)
|
109
|
+
raise ArgumentError, 'Subscription Service does not Exist!' unless params[:event_type].in?(ALL_EVENT_TYPES)
|
110
|
+
determine_scope_details(params)
|
111
|
+
end
|
112
112
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
113
|
+
def validate_webhook_input(params)
|
114
|
+
if params[:event_type].in?(TILL_SCOPE_EVENT_TYPES)
|
115
|
+
validate_input(params, %w[event_type scope scope_reference url])
|
116
|
+
else
|
117
|
+
validate_input(params.except(:scope_reference), %w[event_type scope url])
|
118
|
+
end
|
119
|
+
end
|
120
120
|
|
121
|
-
|
122
|
-
|
123
|
-
|
121
|
+
def determine_scope_details(params)
|
122
|
+
if params[:scope].eql?('till')
|
123
|
+
raise ArgumentError, "Invalid scope till for the event type" unless params[:event_type].in?(TILL_SCOPE_EVENT_TYPES)
|
124
|
+
end
|
125
|
+
end
|
124
126
|
end
|
125
127
|
end
|
126
128
|
end
|
@@ -1,22 +1,22 @@
|
|
1
1
|
require 'k2-connect-ruby/k2_utilities/k2_url_parse'
|
2
|
-
require 'k2-connect-ruby/k2_utilities/
|
2
|
+
require 'k2-connect-ruby/k2_utilities/k2_authenticator'
|
3
3
|
require 'k2-connect-ruby/k2_utilities/k2_connection'
|
4
4
|
require 'k2-connect-ruby/k2_utilities/k2_validation'
|
5
5
|
require 'k2-connect-ruby/k2_utilities/config/k2_config'
|
6
6
|
require 'k2-connect-ruby/k2_utilities/k2_process_result'
|
7
7
|
require 'k2-connect-ruby/k2_utilities/k2_process_webhook'
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
def make_hash(path_url, request, access_token, class_type, body)
|
14
|
-
{
|
9
|
+
module K2ConnectRuby
|
10
|
+
module K2Utilities
|
11
|
+
def make_hash(path_url, request, access_token, class_type, body, requires_token = true)
|
12
|
+
{
|
15
13
|
path_url: path_url,
|
16
14
|
access_token: access_token,
|
17
15
|
request_type: request,
|
16
|
+
requires_token: requires_token,
|
18
17
|
class_type: class_type,
|
19
18
|
params: body
|
20
|
-
|
19
|
+
}.with_indifferent_access
|
20
|
+
end
|
21
21
|
end
|
22
22
|
end
|
data/lib/k2-connect-ruby.rb
CHANGED
@@ -1,34 +1,16 @@
|
|
1
|
-
# Base
|
2
|
-
require 'k2-connect-ruby/k2_errors'
|
3
|
-
require 'k2-connect-ruby/version'
|
4
1
|
require 'active_model'
|
5
2
|
require 'active_support/json'
|
6
|
-
# For the blank? check
|
7
3
|
require 'active_support/dependencies/autoload'
|
8
4
|
require 'active_support/core_ext'
|
9
|
-
|
10
|
-
# Utilities
|
11
|
-
require 'k2-connect-ruby/utilities'
|
12
|
-
|
13
|
-
# Services
|
14
|
-
require 'k2-connect-ruby/k2_services/payload_process'
|
15
|
-
|
16
|
-
# Entity
|
17
|
-
require 'k2-connect-ruby/k2_entity/k2_entity'
|
18
|
-
require 'k2-connect-ruby/k2_entity/entity'
|
19
|
-
|
20
|
-
# ActiveSupport
|
21
5
|
require 'active_support/core_ext/hash/indifferent_access'
|
22
|
-
|
23
|
-
# YAJL
|
24
|
-
require 'yajl'
|
25
|
-
|
26
|
-
# YAML
|
27
6
|
require 'yaml'
|
28
|
-
|
29
|
-
# Rest Client
|
30
7
|
require 'rest-client'
|
31
|
-
|
32
|
-
# JSON
|
33
8
|
require 'json'
|
34
9
|
|
10
|
+
require 'k2-connect-ruby/k2_utilities'
|
11
|
+
require 'k2-connect-ruby/k2_services/payload_process'
|
12
|
+
require 'k2-connect-ruby/entity'
|
13
|
+
require 'k2-connect-ruby/k2_errors'
|
14
|
+
require 'k2-connect-ruby/version'
|
15
|
+
|
16
|
+
module K2ConnectRuby; end
|