k2-connect-ruby 3.0.0 → 4.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/.github/pull_request_template.md +31 -0
- data/.gitignore +2 -0
- data/Gemfile.lock +38 -5
- data/README.md +499 -198
- data/k2-connect-ruby.gemspec +39 -35
- data/lib/k2-connect-ruby/entity.rb +39 -9
- data/lib/k2-connect-ruby/http_client.rb +43 -0
- data/lib/k2-connect-ruby/k2_entity/k2_entity.rb +12 -11
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/destination/destination_request.rb +27 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/destination/external_bank_account.rb +32 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/destination/external_mpesa_wallet.rb +38 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/destination/external_paybill.rb +31 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/destination/external_till.rb +30 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/destination/merchant_bank_account.rb +27 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/destination/merchant_mpesa_wallet.rb +27 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/external_recipient/bank_account.rb +17 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/external_recipient/mobile_wallet.rb +24 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/external_recipient/paybill.rb +17 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/external_recipient/recipient_request.rb +32 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/external_recipient/till.rb +17 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/external_recipient.rb +54 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/k2_stk.rb +14 -31
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/payment_link.rb +53 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/payment_links/payment_link_request.rb +43 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/reversal.rb +39 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/reversals/reversal_request.rb +39 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/send_money/send_money_request.rb +53 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/send_money.rb +73 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/stk_push/stk_push_request.rb +51 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/transfer_account/merchant_bank_account.rb +35 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/transfer_account/merchant_mpesa_wallet.rb +38 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/transfer_account/transfer_account_request.rb +25 -0
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/transfer_account.rb +49 -0
- data/lib/k2-connect-ruby/k2_entity/k2_polling.rb +16 -13
- data/lib/k2-connect-ruby/k2_entity/k2_subscribe.rb +16 -14
- data/lib/k2-connect-ruby/k2_entity/k2_token.rb +14 -39
- data/lib/k2-connect-ruby/k2_entity/polling/polling_request.rb +48 -0
- data/lib/k2-connect-ruby/k2_entity/webhook/webhook_subscription_request.rb +66 -0
- data/lib/k2-connect-ruby/k2_errors.rb +15 -71
- data/lib/k2-connect-ruby/k2_services/base_service.rb +25 -0
- data/lib/k2-connect-ruby/k2_services/k2_client.rb +7 -6
- data/lib/k2-connect-ruby/k2_services/payloads/daraja_webhooks.rb +43 -0
- data/lib/k2-connect-ruby/k2_services/payloads/k2_transaction.rb +20 -18
- data/lib/k2-connect-ruby/k2_services/payloads/k2_webhooks.rb +36 -32
- data/lib/k2-connect-ruby/k2_services/payloads/transactions/incoming_payment.rb +31 -29
- data/lib/k2-connect-ruby/k2_services/payloads/transactions/outgoing_payment.rb +3 -1
- data/lib/k2-connect-ruby/k2_services/payloads/transactions/payment_link.rb +38 -0
- data/lib/k2-connect-ruby/k2_services/payloads/transactions/reversal.rb +35 -0
- data/lib/k2-connect-ruby/k2_services/payloads/transactions/send_money_payment.rb +25 -0
- data/lib/k2-connect-ruby/k2_services/payloads/transactions/transfer.rb +2 -0
- data/lib/k2-connect-ruby/k2_services/payloads/webhooks/b2b_transaction_received.rb +9 -3
- data/lib/k2-connect-ruby/k2_services/payloads/webhooks/b2b_transaction_reversed.rb +22 -0
- data/lib/k2-connect-ruby/k2_services/payloads/webhooks/buygoods_transaction_received.rb +6 -0
- data/lib/k2-connect-ruby/k2_services/payloads/webhooks/buygoods_transaction_reversed.rb +4 -0
- data/lib/k2-connect-ruby/k2_services/payloads/webhooks/card_transaction_received.rb +18 -0
- data/lib/k2-connect-ruby/k2_services/payloads/webhooks/card_transaction_reversed.rb +17 -0
- data/lib/k2-connect-ruby/k2_services/payloads/webhooks/card_transaction_voided.rb +17 -0
- data/lib/k2-connect-ruby/k2_services/payloads/webhooks/customer_created.rb +11 -7
- data/lib/k2-connect-ruby/k2_services/payloads/webhooks/transfer_webhook.rb +50 -0
- data/lib/k2-connect-ruby/k2_services/send_introspect_token_request_service.rb +56 -0
- data/lib/k2-connect-ruby/k2_services/send_k2_connect_get_request_service.rb +42 -0
- data/lib/k2-connect-ruby/k2_services/send_k2_connect_post_request_service.rb +44 -0
- data/lib/k2-connect-ruby/k2_services/send_request_token_request_service.rb +55 -0
- data/lib/k2-connect-ruby/k2_services/send_revoke_token_request_service.rb +56 -0
- data/lib/k2-connect-ruby/k2_services/send_token_info_request_service.rb +44 -0
- data/lib/k2-connect-ruby/k2_services.rb +29 -0
- data/lib/k2-connect-ruby/k2_utilities/config/k2_config.rb +18 -26
- data/lib/k2-connect-ruby/k2_utilities/config/k2_config.yml +9 -10
- data/lib/k2-connect-ruby/k2_utilities/k2_authenticator.rb +12 -5
- data/lib/k2-connect-ruby/k2_utilities/k2_connection.rb +0 -48
- data/lib/k2-connect-ruby/k2_utilities/k2_connection_helper.rb +21 -0
- data/lib/k2-connect-ruby/k2_utilities/k2_process_result.rb +16 -15
- data/lib/k2-connect-ruby/k2_utilities/k2_process_webhook.rb +34 -21
- data/lib/k2-connect-ruby/k2_utilities/k2_url_parse.rb +3 -1
- data/lib/k2-connect-ruby/k2_utilities/phone_number_validator.rb +15 -0
- data/lib/k2-connect-ruby/k2_utilities.rb +9 -20
- data/lib/k2-connect-ruby/version.rb +1 -1
- data/lib/k2-connect-ruby.rb +14 -13
- metadata +119 -36
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/k2_pay.rb +0 -98
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/k2_settlement.rb +0 -48
- data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/k2_transfer.rb +0 -44
- data/lib/k2-connect-ruby/k2_entity/k2_notification.rb +0 -40
- data/lib/k2-connect-ruby/k2_services/payload_process.rb +0 -19
- data/lib/k2-connect-ruby/k2_services/payloads/webhooks/settlement_webhook.rb +0 -44
- data/lib/k2-connect-ruby/k2_utilities/k2_validation.rb +0 -128
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module K2ConnectRuby
|
|
2
4
|
module K2Entity
|
|
3
5
|
class K2Token
|
|
@@ -10,57 +12,30 @@ module K2ConnectRuby
|
|
|
10
12
|
end
|
|
11
13
|
|
|
12
14
|
def request_token
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
grant_type: 'client_credentials'
|
|
17
|
-
}
|
|
18
|
-
token_hash = K2ConnectRuby::K2Entity::K2Token.make_hash(K2ConnectRuby::K2Utilities::Config::K2Config.path_url('oauth_token'), 'post', 'Access Token', token_params)
|
|
19
|
-
@access_token = K2ConnectRuby::K2Utilities::K2Connection.make_request(token_hash)
|
|
15
|
+
result = K2ConnectRuby::K2Services::SendRequestTokenRequestService.call(@client_id, @client_secret)
|
|
16
|
+
|
|
17
|
+
@access_token = result.data[:response_body][:access_token]
|
|
20
18
|
end
|
|
21
19
|
|
|
22
20
|
def revoke_token(access_token)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
token: access_token
|
|
27
|
-
}
|
|
28
|
-
token_hash = K2ConnectRuby::K2Entity::K2Token.make_hash(K2ConnectRuby::K2Utilities::Config::K2Config.path_url('revoke_token'), 'post', 'Access Token', token_params)
|
|
29
|
-
@token_response = K2ConnectRuby::K2Utilities::K2Connection.make_request(token_hash)
|
|
21
|
+
result = K2ConnectRuby::K2Services::SendRevokeTokenRequestService.call(@client_id, @client_secret, access_token)
|
|
22
|
+
|
|
23
|
+
true
|
|
30
24
|
end
|
|
31
25
|
|
|
32
26
|
def introspect_token(access_token)
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
token: access_token
|
|
37
|
-
}
|
|
38
|
-
token_hash = K2ConnectRuby::K2Entity::K2Token.make_hash(K2ConnectRuby::K2Utilities::Config::K2Config.path_url('introspect_token'), 'post', 'Access Token', token_params)
|
|
39
|
-
@token_response = K2ConnectRuby::K2Utilities::K2Connection.make_request(token_hash)
|
|
27
|
+
result = K2ConnectRuby::K2Services::SendIntrospectTokenRequestService.call(@client_id, @client_secret, access_token)
|
|
28
|
+
|
|
29
|
+
result.data[:response_body]
|
|
40
30
|
end
|
|
41
31
|
|
|
42
32
|
def token_info(access_token)
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
token_hash[:access_token] = access_token
|
|
46
|
-
@token_response = K2ConnectRuby::K2Utilities::K2Connection.make_request(token_hash)
|
|
33
|
+
result = K2ConnectRuby::K2Services::SendTokenInfoRequestService.call(access_token)
|
|
34
|
+
result.data[:response_body]
|
|
47
35
|
end
|
|
48
36
|
|
|
49
37
|
def validate_client_credentials(client_id, client_secret)
|
|
50
|
-
raise ArgumentError,
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
class << K2ConnectRuby::K2Entity::K2Token
|
|
54
|
-
# Create a Hash containing important details accessible for K2Connect
|
|
55
|
-
def make_hash(path_url, request, class_type, body, requires_token = true)
|
|
56
|
-
{
|
|
57
|
-
path_url: path_url,
|
|
58
|
-
request_type: request,
|
|
59
|
-
class_type: class_type,
|
|
60
|
-
requires_token: requires_token,
|
|
61
|
-
params: body
|
|
62
|
-
}.with_indifferent_access
|
|
63
|
-
end
|
|
38
|
+
raise ArgumentError, "Nil or Empty Client Id or Secret!" if client_id.blank? || client_secret.blank?
|
|
64
39
|
end
|
|
65
40
|
end
|
|
66
41
|
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module K2ConnectRuby
|
|
4
|
+
module K2Entity
|
|
5
|
+
module K2FinancialEntities
|
|
6
|
+
module Polling
|
|
7
|
+
class PollingRequest
|
|
8
|
+
include ActiveModel::Validations
|
|
9
|
+
|
|
10
|
+
attr_accessor :from_time, :to_time, :callback_url, :scope, :scope_reference
|
|
11
|
+
|
|
12
|
+
validates :from_time, :to_time, :callback_url, :scope, presence: true
|
|
13
|
+
validates :scope_reference, presence: true, if: :till_scope?
|
|
14
|
+
validates :scope, inclusion: {
|
|
15
|
+
in: ["till", "company"],
|
|
16
|
+
message: "must be one of 'till' or 'company'.",
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
def initialize(kwargs)
|
|
20
|
+
kwargs.each do |key, value|
|
|
21
|
+
instance_variable_set("@#{key}", value)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def endpoint
|
|
26
|
+
K2ConnectRuby::K2Utilities::Config::K2Config.endpoint("poll")
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def request_body
|
|
30
|
+
{
|
|
31
|
+
scope: scope,
|
|
32
|
+
scope_reference: scope_reference,
|
|
33
|
+
from_time: from_time,
|
|
34
|
+
to_time: to_time,
|
|
35
|
+
_links: {
|
|
36
|
+
callback_url: callback_url,
|
|
37
|
+
},
|
|
38
|
+
}
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def till_scope?
|
|
42
|
+
scope == "till"
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module K2ConnectRuby
|
|
4
|
+
module K2Entity
|
|
5
|
+
module K2FinancialEntities
|
|
6
|
+
module Webhook
|
|
7
|
+
class WebhookSubscriptionRequest
|
|
8
|
+
include ActiveModel::Validations
|
|
9
|
+
|
|
10
|
+
attr_accessor :event_type, :url, :scope, :scope_reference, :enable_daraja_payload
|
|
11
|
+
|
|
12
|
+
validates :event_type, :url, :scope, presence: true
|
|
13
|
+
validates :scope_reference, presence: true, if: :till_scope?
|
|
14
|
+
validates :event_type, inclusion: {
|
|
15
|
+
in: [
|
|
16
|
+
"buygoods_transaction_received",
|
|
17
|
+
"b2b_transaction_received",
|
|
18
|
+
"settlement_transfer_completed",
|
|
19
|
+
"customer_created",
|
|
20
|
+
"buygoods_transaction_reversed",
|
|
21
|
+
"card_transaction_received",
|
|
22
|
+
"card_transaction_voided",
|
|
23
|
+
"card_transaction_reversed"
|
|
24
|
+
],
|
|
25
|
+
message: "is invalid",
|
|
26
|
+
}
|
|
27
|
+
validates :scope, inclusion: {
|
|
28
|
+
in: ["till", "company"],
|
|
29
|
+
message: "must be one of 'till' or 'company'.",
|
|
30
|
+
}
|
|
31
|
+
validate :validate_daraja_payload_flag, if: :enable_daraja_payload
|
|
32
|
+
|
|
33
|
+
def initialize(kwargs)
|
|
34
|
+
kwargs.each do |key, value|
|
|
35
|
+
instance_variable_set("@#{key}", value)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def endpoint
|
|
40
|
+
K2ConnectRuby::K2Utilities::Config::K2Config.endpoint("webhook_subscriptions")
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def request_body
|
|
44
|
+
{
|
|
45
|
+
event_type: event_type,
|
|
46
|
+
url: url,
|
|
47
|
+
scope: scope,
|
|
48
|
+
scope_reference: scope_reference,
|
|
49
|
+
enable_daraja_payload: enable_daraja_payload || false,
|
|
50
|
+
}
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def till_scope?
|
|
54
|
+
scope == "till"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def validate_daraja_payload_flag
|
|
58
|
+
unless event_type.in?(["buygoods_transaction_received", "b2b_transaction_received"])
|
|
59
|
+
errors.add(:base, "Can only enable daraja_payloads for 'buygoods_transaction_received' and 'b2b_transaction_received' webhooks.")
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -1,79 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
# Standard K2Error
|
|
3
|
-
class K2Errors < StandardError
|
|
4
|
-
attr_reader :status, :message
|
|
5
|
-
def initialize(msg = @message)
|
|
6
|
-
super(msg)
|
|
7
|
-
end
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
# For errors concerning the Status code returned from Kopo Kopo
|
|
11
|
-
class K2ConnectionError < K2Errors
|
|
12
|
-
def initialize(error)
|
|
13
|
-
@error = error
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def message
|
|
17
|
-
case @error
|
|
18
|
-
when 400.to_s
|
|
19
|
-
STDERR.puts("Bad Request.\n\tYour request is Invalid")
|
|
20
|
-
when 401.to_s
|
|
21
|
-
STDERR.puts("Unauthorized.\n Your API key is wrong")
|
|
22
|
-
when 403.to_s
|
|
23
|
-
STDERR.puts("Forbidden.\n The resource requested cannot be accessed")
|
|
24
|
-
when 404.to_s
|
|
25
|
-
STDERR.puts("Not Found.\n\tThe specified resource could not be found")
|
|
26
|
-
when 405.to_s
|
|
27
|
-
STDERR.puts("Method Not Allowed.\n You tried to access a resource with an invalid method")
|
|
28
|
-
when 406.to_s
|
|
29
|
-
STDERR.puts("Not Acceptable.\n You requested a format that isn't valid json")
|
|
30
|
-
when 410.to_s
|
|
31
|
-
STDERR.puts("Gone.\n The resource requested has been moved")
|
|
32
|
-
when 429.to_s
|
|
33
|
-
STDERR.puts("Too Many Requests.\n Request threshold has been exceeded")
|
|
34
|
-
when 500.to_s
|
|
35
|
-
STDERR.puts("Internal Server Error.\n We had a problem with our server. Try again later")
|
|
36
|
-
when 503.to_s
|
|
37
|
-
STDERR.puts("Service Unavailable.\n We're temporarily offline for maintenance. Please try again later")
|
|
38
|
-
else
|
|
39
|
-
STDERR.puts("Undefined Kopo Kopo Server Response.")
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
end
|
|
1
|
+
# frozen_string_literal: true
|
|
43
2
|
|
|
44
|
-
|
|
45
|
-
class K2ValidateErrors < K2Errors
|
|
46
|
-
attr_reader :the_keys
|
|
3
|
+
module K2ConnectRuby
|
|
47
4
|
|
|
48
|
-
|
|
49
|
-
super
|
|
50
|
-
@the_keys = the_keys
|
|
51
|
-
@status = :bad_request
|
|
52
|
-
end
|
|
5
|
+
module K2Errors
|
|
53
6
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
end
|
|
7
|
+
class Error < StandardError; end
|
|
8
|
+
class TimeoutError < Error; end
|
|
9
|
+
class NotFoundError < Error; end
|
|
10
|
+
class UnauthorizedError < Error; end
|
|
11
|
+
class ConnectionError < Error; end
|
|
58
12
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
end
|
|
62
|
-
end
|
|
13
|
+
class ApiError < Error
|
|
14
|
+
attr_reader :code, :details
|
|
63
15
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
end
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
# Error for Incorrect Hash Key Symbols (K2Entity validate => input)
|
|
73
|
-
class K2IncorrectParams < K2ValidateErrors
|
|
74
|
-
def initialize(the_keys)
|
|
75
|
-
@message = "Incorrect Hash/Parameters Object!\n The Following Parameter(s) are Incorrect: "
|
|
76
|
-
super
|
|
16
|
+
def initialize(message:, code: nil, details: nil)
|
|
17
|
+
@code = code
|
|
18
|
+
@details = details
|
|
19
|
+
super(message)
|
|
20
|
+
end
|
|
77
21
|
end
|
|
78
22
|
end
|
|
79
23
|
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module K2ConnectRuby
|
|
4
|
+
module K2Services
|
|
5
|
+
class BaseService
|
|
6
|
+
CallResult = Struct.new(:success?, :data, :errors) do
|
|
7
|
+
class << self
|
|
8
|
+
def success(data = nil)
|
|
9
|
+
new(true, data, [])
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def errors(messages)
|
|
13
|
+
new(false, nil, messages)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
class << self
|
|
19
|
+
def call(*args, &block)
|
|
20
|
+
new(*args, &block).execute
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -1,26 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module K2ConnectRuby
|
|
2
4
|
module K2Services
|
|
3
5
|
class K2Client
|
|
4
|
-
attr_accessor :api_secret_key,
|
|
5
|
-
:hash_body,
|
|
6
|
-
:k2_signature
|
|
6
|
+
attr_accessor :api_secret_key, :hash_body, :k2_signature
|
|
7
7
|
|
|
8
8
|
# Initialize method
|
|
9
9
|
def initialize(api_secret_key)
|
|
10
|
-
raise ArgumentError,
|
|
10
|
+
raise ArgumentError, "No Secret Key Given!" if api_secret_key.blank?
|
|
11
|
+
|
|
11
12
|
@api_secret_key = api_secret_key
|
|
12
13
|
end
|
|
13
14
|
|
|
14
15
|
# Method for parsing the Entire Request. Come back to it later to trim. L8r call it set_client_variables
|
|
15
16
|
def parse_request(the_request)
|
|
16
|
-
raise ArgumentError,
|
|
17
|
+
raise ArgumentError, "Nil Request Parameter Input!" if the_request.blank?
|
|
17
18
|
|
|
18
19
|
# The Response Body.
|
|
19
20
|
@hash_body = JSON.parse(the_request.body.read)
|
|
20
21
|
# The Response Header
|
|
21
22
|
hash_header = JSON.parse(the_request.env.select { |k, _| k =~ /^HTTP_/ }.to_json)
|
|
22
23
|
# The K2 Signature
|
|
23
|
-
@k2_signature = hash_header[
|
|
24
|
+
@k2_signature = hash_header["HTTP_X_KOPOKOPO_SIGNATURE"]
|
|
24
25
|
end
|
|
25
26
|
end
|
|
26
27
|
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module K2ConnectRuby
|
|
4
|
+
module K2Services
|
|
5
|
+
module Payloads
|
|
6
|
+
class DarajaWebhooks
|
|
7
|
+
include ActiveModel::Validations
|
|
8
|
+
|
|
9
|
+
attr_reader :transaction_type,
|
|
10
|
+
:transaction_id,
|
|
11
|
+
:transaction_time,
|
|
12
|
+
:transaction_amount,
|
|
13
|
+
:business_short_code,
|
|
14
|
+
:bill_ref_number,
|
|
15
|
+
:invoice_number,
|
|
16
|
+
:org_account_balance,
|
|
17
|
+
:third_party_transaction_id,
|
|
18
|
+
:msisdn,
|
|
19
|
+
:first_name,
|
|
20
|
+
:middle_name,
|
|
21
|
+
:last_name
|
|
22
|
+
|
|
23
|
+
validates :transaction_type, :transaction_id, :transaction_time, :transaction_amount, presence: true
|
|
24
|
+
|
|
25
|
+
def initialize(payload)
|
|
26
|
+
@transaction_type = payload.dig("TransactionType")
|
|
27
|
+
@transaction_id = payload.dig("TransID")
|
|
28
|
+
@transaction_time = payload.dig("TransTime")
|
|
29
|
+
@transaction_amount = payload.dig("TransAmount")
|
|
30
|
+
@business_short_code = payload.dig("BusinessShortCode")
|
|
31
|
+
@bill_ref_number = payload.dig("BillRefNumber")
|
|
32
|
+
@invoice_number = payload.dig("InvoiceNumber")
|
|
33
|
+
@org_account_balance = payload.dig("OrgAccountBalance")
|
|
34
|
+
@third_party_transaction_id = payload.dig("ThirdPartyTransId")
|
|
35
|
+
@msisdn = payload.dig("MSISDN")
|
|
36
|
+
@first_name = payload.dig("FirstName")
|
|
37
|
+
@middle_name = payload.dig("MiddleName")
|
|
38
|
+
@last_name = payload.dig("LastName")
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -1,19 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module K2ConnectRuby
|
|
2
4
|
module K2Services
|
|
3
5
|
module Payloads
|
|
4
6
|
class K2Transaction
|
|
5
7
|
attr_reader :id,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
:type,
|
|
9
|
+
:metadata,
|
|
10
|
+
:links_self,
|
|
11
|
+
:callback_url
|
|
10
12
|
|
|
11
13
|
def initialize(payload)
|
|
12
|
-
@id = payload.dig(
|
|
13
|
-
@type = payload.dig(
|
|
14
|
-
@metadata = payload.dig(
|
|
15
|
-
@links_self = payload.dig(
|
|
16
|
-
@callback_url = payload.dig(
|
|
14
|
+
@id = payload.dig("data", "id")
|
|
15
|
+
@type = payload.dig("data", "type")
|
|
16
|
+
@metadata = payload.dig("data", "attributes", "metadata")
|
|
17
|
+
@links_self = payload.dig("data", "attributes", "_links", "self")
|
|
18
|
+
@callback_url = payload.dig("data", "attributes", "_links", "callback_url")
|
|
17
19
|
end
|
|
18
20
|
end
|
|
19
21
|
|
|
@@ -21,14 +23,14 @@ module K2ConnectRuby
|
|
|
21
23
|
include ActiveModel::Validations
|
|
22
24
|
|
|
23
25
|
attr_reader :status,
|
|
24
|
-
|
|
26
|
+
:initiation_time
|
|
25
27
|
|
|
26
28
|
validate :valid_payment_type
|
|
27
29
|
|
|
28
30
|
def initialize(payload)
|
|
29
31
|
super
|
|
30
|
-
@status = payload.dig(
|
|
31
|
-
@initiation_time = payload.dig(
|
|
32
|
+
@status = payload.dig("data", "attributes", "status")
|
|
33
|
+
@initiation_time = payload.dig("data", "attributes", "initiation_time") if @type.eql?("incoming_payment")
|
|
32
34
|
end
|
|
33
35
|
|
|
34
36
|
private
|
|
@@ -38,15 +40,15 @@ module K2ConnectRuby
|
|
|
38
40
|
|
|
39
41
|
class OutgoingTransaction < CommonPayment
|
|
40
42
|
attr_reader :created_at,
|
|
41
|
-
|
|
42
|
-
|
|
43
|
+
:transfer_batches,
|
|
44
|
+
:total_value
|
|
43
45
|
|
|
44
46
|
def initialize(payload)
|
|
45
47
|
super
|
|
46
|
-
@created_at = payload.dig(
|
|
47
|
-
@currency = payload.dig(
|
|
48
|
-
@total_value = payload.dig(
|
|
49
|
-
@transfer_batches = payload.dig(
|
|
48
|
+
@created_at = payload.dig("data", "attributes", "created_at")
|
|
49
|
+
@currency = payload.dig("data", "attributes", "amount", "currency")
|
|
50
|
+
@total_value = payload.dig("data", "attributes", "amount", "value")
|
|
51
|
+
@transfer_batches = payload.dig("data", "attributes", "transfer_batches")
|
|
50
52
|
end
|
|
51
53
|
end
|
|
52
54
|
end
|
|
@@ -1,26 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module K2ConnectRuby
|
|
2
4
|
module K2Services
|
|
3
5
|
module Payloads
|
|
4
6
|
class K2Webhooks
|
|
5
7
|
attr_reader :id,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
:topic,
|
|
9
|
+
:created_at,
|
|
10
|
+
:links_self,
|
|
11
|
+
:event_type,
|
|
12
|
+
:links_resource,
|
|
13
|
+
:event_resource,
|
|
14
|
+
:resource_id
|
|
13
15
|
|
|
14
16
|
def initialize(payload)
|
|
15
|
-
@id = payload.dig(
|
|
16
|
-
@topic = payload.dig(
|
|
17
|
-
@created_at = payload.dig(
|
|
17
|
+
@id = payload.dig("id")
|
|
18
|
+
@topic = payload.dig("topic")
|
|
19
|
+
@created_at = payload.dig("created_at")
|
|
18
20
|
# Event
|
|
19
|
-
@event_type = payload.dig(
|
|
20
|
-
@resource_id = payload.dig(
|
|
21
|
+
@event_type = payload.dig("event", "type")
|
|
22
|
+
@resource_id = payload.dig("event", "resource", "id") unless @event_type.eql?("Customer Created")
|
|
21
23
|
# Links
|
|
22
|
-
@links_self = payload.dig(
|
|
23
|
-
@links_resource = payload.dig(
|
|
24
|
+
@links_self = payload.dig("_links", "self")
|
|
25
|
+
@links_resource = payload.dig("_links", "resource")
|
|
24
26
|
end
|
|
25
27
|
end
|
|
26
28
|
|
|
@@ -28,36 +30,38 @@ module K2ConnectRuby
|
|
|
28
30
|
REFERENCE_EXCEPTIONS = ["Merchant to Merchant Transaction", "Settlement Transfer"]
|
|
29
31
|
|
|
30
32
|
attr_reader :reference,
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
:origination_time,
|
|
34
|
+
:amount,
|
|
35
|
+
:currency,
|
|
36
|
+
:status
|
|
35
37
|
|
|
36
38
|
def initialize(payload)
|
|
37
39
|
super
|
|
38
|
-
@reference = payload.dig(
|
|
39
|
-
@origination_time = payload.dig(
|
|
40
|
-
@amount = payload.dig(
|
|
41
|
-
@currency = payload.dig(
|
|
42
|
-
@status = payload.dig(
|
|
40
|
+
@reference = payload.dig("event", "resource", "reference") unless REFERENCE_EXCEPTIONS.include?(@event_type)
|
|
41
|
+
@origination_time = payload.dig("event", "resource", "origination_time")
|
|
42
|
+
@amount = payload.dig("event", "resource", "amount")
|
|
43
|
+
@currency = payload.dig("event", "resource", "currency")
|
|
44
|
+
@status = payload.dig("event", "resource", "status")
|
|
43
45
|
end
|
|
44
46
|
|
|
45
47
|
end
|
|
46
48
|
|
|
47
49
|
class Buygoods < K2CommonEvents
|
|
48
50
|
attr_reader :system,
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
:till_number,
|
|
52
|
+
:sender_phone_number,
|
|
53
|
+
:hashed_sender_phone,
|
|
54
|
+
:sender_first_name,
|
|
55
|
+
:sender_last_name
|
|
53
56
|
|
|
54
57
|
def initialize(payload)
|
|
55
58
|
super
|
|
56
|
-
@system = payload.dig(
|
|
57
|
-
@till_number = payload.dig(
|
|
58
|
-
@sender_phone_number = payload.dig(
|
|
59
|
-
@
|
|
60
|
-
@
|
|
59
|
+
@system = payload.dig("event", "resource", "system")
|
|
60
|
+
@till_number = payload.dig("event", "resource", "till_number")
|
|
61
|
+
@sender_phone_number = payload.dig("event", "resource", "sender_phone_number")
|
|
62
|
+
@hashed_sender_phone = payload.dig("event", "resource", "hashed_sender_phone")
|
|
63
|
+
@sender_first_name = payload.dig("event", "resource", "sender_first_name")
|
|
64
|
+
@sender_last_name = payload.dig("event", "resource", "sender_last_name")
|
|
61
65
|
end
|
|
62
66
|
end
|
|
63
67
|
end
|
|
@@ -1,44 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module K2ConnectRuby
|
|
2
4
|
module K2Services
|
|
3
5
|
module Payloads
|
|
4
6
|
module Transactions
|
|
5
7
|
class IncomingPayment < CommonPayment
|
|
6
8
|
attr_reader :event,
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
9
|
+
:event_type,
|
|
10
|
+
:transaction_reference,
|
|
11
|
+
:origination_time,
|
|
12
|
+
:sender_phone_number,
|
|
13
|
+
:amount,
|
|
14
|
+
:currency,
|
|
15
|
+
:till_number,
|
|
16
|
+
:system,
|
|
17
|
+
:resource_id,
|
|
18
|
+
:resource_status,
|
|
19
|
+
:sender_first_name,
|
|
20
|
+
:sender_middle_name,
|
|
21
|
+
:sender_last_name,
|
|
22
|
+
:k2_errors
|
|
21
23
|
|
|
22
24
|
def initialize(payload)
|
|
23
25
|
super
|
|
24
26
|
# Event details
|
|
25
|
-
@event = payload.dig(
|
|
26
|
-
@event_type = payload.dig(
|
|
27
|
+
@event = payload.dig("data", "attributes", "event")
|
|
28
|
+
@event_type = payload.dig("data", "attributes", "event", "type")
|
|
27
29
|
# Resource details
|
|
28
|
-
@resource_id = payload.dig(
|
|
29
|
-
@transaction_reference = payload.dig(
|
|
30
|
-
@origination_time = payload.dig(
|
|
31
|
-
@sender_phone_number = payload.dig(
|
|
32
|
-
@amount = payload.dig(
|
|
33
|
-
@currency = payload.dig(
|
|
34
|
-
@till_number = payload.dig(
|
|
35
|
-
@system = payload.dig(
|
|
36
|
-
@resource_status = payload.dig(
|
|
37
|
-
@sender_first_name = payload.dig(
|
|
38
|
-
@sender_middle_name = payload.dig(
|
|
39
|
-
@sender_last_name = payload.dig(
|
|
30
|
+
@resource_id = payload.dig("data", "attributes", "event", "resource", "id")
|
|
31
|
+
@transaction_reference = payload.dig("data", "attributes", "event", "resource", "reference")
|
|
32
|
+
@origination_time = payload.dig("data", "attributes", "event", "resource", "origination_time")
|
|
33
|
+
@sender_phone_number = payload.dig("data", "attributes", "event", "resource", "sender_phone_number")
|
|
34
|
+
@amount = payload.dig("data", "attributes", "event", "resource", "amount")
|
|
35
|
+
@currency = payload.dig("data", "attributes", "event", "resource", "currency")
|
|
36
|
+
@till_number = payload.dig("data", "attributes", "event", "resource", "till_number")
|
|
37
|
+
@system = payload.dig("data", "attributes", "event", "resource", "system")
|
|
38
|
+
@resource_status = payload.dig("data", "attributes", "event", "resource", "status")
|
|
39
|
+
@sender_first_name = payload.dig("data", "attributes", "event", "resource", "sender_first_name")
|
|
40
|
+
@sender_middle_name = payload.dig("data", "attributes", "event", "resource", "sender_middle_name")
|
|
41
|
+
@sender_last_name = payload.dig("data", "attributes", "event", "resource", "sender_last_name")
|
|
40
42
|
# Errors
|
|
41
|
-
@
|
|
43
|
+
@k2_errors = payload.dig("data", "attributes", "event", "errors")
|
|
42
44
|
end
|
|
43
45
|
|
|
44
46
|
private
|