k2-connect-ruby 3.1.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.
Files changed (87) hide show
  1. checksums.yaml +4 -4
  2. data/.github/pull_request_template.md +31 -0
  3. data/.gitignore +2 -0
  4. data/Gemfile.lock +38 -5
  5. data/README.md +499 -198
  6. data/k2-connect-ruby.gemspec +39 -35
  7. data/lib/k2-connect-ruby/entity.rb +39 -9
  8. data/lib/k2-connect-ruby/http_client.rb +43 -0
  9. data/lib/k2-connect-ruby/k2_entity/k2_entity.rb +12 -11
  10. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/destination/destination_request.rb +27 -0
  11. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/destination/external_bank_account.rb +32 -0
  12. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/destination/external_mpesa_wallet.rb +38 -0
  13. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/destination/external_paybill.rb +31 -0
  14. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/destination/external_till.rb +30 -0
  15. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/destination/merchant_bank_account.rb +27 -0
  16. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/destination/merchant_mpesa_wallet.rb +27 -0
  17. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/external_recipient/bank_account.rb +17 -0
  18. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/external_recipient/mobile_wallet.rb +24 -0
  19. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/external_recipient/paybill.rb +17 -0
  20. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/external_recipient/recipient_request.rb +32 -0
  21. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/external_recipient/till.rb +17 -0
  22. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/external_recipient.rb +54 -0
  23. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/k2_stk.rb +14 -31
  24. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/payment_link.rb +53 -0
  25. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/payment_links/payment_link_request.rb +43 -0
  26. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/reversal.rb +39 -0
  27. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/reversals/reversal_request.rb +39 -0
  28. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/send_money/send_money_request.rb +53 -0
  29. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/send_money.rb +73 -0
  30. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/stk_push/stk_push_request.rb +51 -0
  31. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/transfer_account/merchant_bank_account.rb +35 -0
  32. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/transfer_account/merchant_mpesa_wallet.rb +38 -0
  33. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/transfer_account/transfer_account_request.rb +25 -0
  34. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/transfer_account.rb +49 -0
  35. data/lib/k2-connect-ruby/k2_entity/k2_polling.rb +16 -13
  36. data/lib/k2-connect-ruby/k2_entity/k2_subscribe.rb +16 -14
  37. data/lib/k2-connect-ruby/k2_entity/k2_token.rb +14 -39
  38. data/lib/k2-connect-ruby/k2_entity/polling/polling_request.rb +48 -0
  39. data/lib/k2-connect-ruby/k2_entity/webhook/webhook_subscription_request.rb +66 -0
  40. data/lib/k2-connect-ruby/k2_errors.rb +15 -71
  41. data/lib/k2-connect-ruby/k2_services/base_service.rb +25 -0
  42. data/lib/k2-connect-ruby/k2_services/k2_client.rb +7 -6
  43. data/lib/k2-connect-ruby/k2_services/payloads/daraja_webhooks.rb +43 -0
  44. data/lib/k2-connect-ruby/k2_services/payloads/k2_transaction.rb +20 -18
  45. data/lib/k2-connect-ruby/k2_services/payloads/k2_webhooks.rb +36 -32
  46. data/lib/k2-connect-ruby/k2_services/payloads/transactions/incoming_payment.rb +31 -29
  47. data/lib/k2-connect-ruby/k2_services/payloads/transactions/outgoing_payment.rb +3 -1
  48. data/lib/k2-connect-ruby/k2_services/payloads/transactions/payment_link.rb +38 -0
  49. data/lib/k2-connect-ruby/k2_services/payloads/transactions/reversal.rb +35 -0
  50. data/lib/k2-connect-ruby/k2_services/payloads/transactions/send_money_payment.rb +25 -0
  51. data/lib/k2-connect-ruby/k2_services/payloads/transactions/transfer.rb +2 -0
  52. data/lib/k2-connect-ruby/k2_services/payloads/webhooks/b2b_transaction_received.rb +9 -3
  53. data/lib/k2-connect-ruby/k2_services/payloads/webhooks/b2b_transaction_reversed.rb +22 -0
  54. data/lib/k2-connect-ruby/k2_services/payloads/webhooks/buygoods_transaction_received.rb +6 -0
  55. data/lib/k2-connect-ruby/k2_services/payloads/webhooks/buygoods_transaction_reversed.rb +4 -0
  56. data/lib/k2-connect-ruby/k2_services/payloads/webhooks/card_transaction_received.rb +18 -0
  57. data/lib/k2-connect-ruby/k2_services/payloads/webhooks/card_transaction_reversed.rb +17 -0
  58. data/lib/k2-connect-ruby/k2_services/payloads/webhooks/card_transaction_voided.rb +17 -0
  59. data/lib/k2-connect-ruby/k2_services/payloads/webhooks/customer_created.rb +11 -7
  60. data/lib/k2-connect-ruby/k2_services/payloads/webhooks/transfer_webhook.rb +50 -0
  61. data/lib/k2-connect-ruby/k2_services/send_introspect_token_request_service.rb +56 -0
  62. data/lib/k2-connect-ruby/k2_services/send_k2_connect_get_request_service.rb +42 -0
  63. data/lib/k2-connect-ruby/k2_services/send_k2_connect_post_request_service.rb +44 -0
  64. data/lib/k2-connect-ruby/k2_services/send_request_token_request_service.rb +55 -0
  65. data/lib/k2-connect-ruby/k2_services/send_revoke_token_request_service.rb +56 -0
  66. data/lib/k2-connect-ruby/k2_services/send_token_info_request_service.rb +44 -0
  67. data/lib/k2-connect-ruby/k2_services.rb +29 -0
  68. data/lib/k2-connect-ruby/k2_utilities/config/k2_config.rb +18 -26
  69. data/lib/k2-connect-ruby/k2_utilities/config/k2_config.yml +9 -10
  70. data/lib/k2-connect-ruby/k2_utilities/k2_authenticator.rb +12 -5
  71. data/lib/k2-connect-ruby/k2_utilities/k2_connection.rb +0 -49
  72. data/lib/k2-connect-ruby/k2_utilities/k2_connection_helper.rb +21 -0
  73. data/lib/k2-connect-ruby/k2_utilities/k2_process_result.rb +16 -15
  74. data/lib/k2-connect-ruby/k2_utilities/k2_process_webhook.rb +34 -21
  75. data/lib/k2-connect-ruby/k2_utilities/k2_url_parse.rb +3 -1
  76. data/lib/k2-connect-ruby/k2_utilities/phone_number_validator.rb +15 -0
  77. data/lib/k2-connect-ruby/k2_utilities.rb +9 -20
  78. data/lib/k2-connect-ruby/version.rb +1 -1
  79. data/lib/k2-connect-ruby.rb +14 -13
  80. metadata +101 -33
  81. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/k2_pay.rb +0 -98
  82. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/k2_settlement.rb +0 -48
  83. data/lib/k2-connect-ruby/k2_entity/k2_financial_entities/k2_transfer.rb +0 -44
  84. data/lib/k2-connect-ruby/k2_entity/k2_notification.rb +0 -40
  85. data/lib/k2-connect-ruby/k2_services/payload_process.rb +0 -19
  86. data/lib/k2-connect-ruby/k2_services/payloads/webhooks/settlement_webhook.rb +0 -44
  87. data/lib/k2-connect-ruby/k2_utilities/k2_validation.rb +0 -128
@@ -1,46 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module K2ConnectRuby
2
4
  module K2Entity
3
5
  class K2Stk < K2ConnectRuby::K2Entity::K2Entity
4
6
  # Receive payments from M-PESA users.
5
- def receive_mpesa_payments(params)
6
- # Validation
7
- params = validate_input(params, @exception_array += %w[payment_channel till_number first_name last_name phone_number email currency value metadata callback_url])
8
- # The Request Body Parameters
9
- k2_request_subscriber = {
10
- first_name: params[:first_name],
11
- middle_name: params[:middle_name],
12
- last_name: params[:last_name],
13
- phone_number: validate_phone(params[:phone_number]),
14
- email: validate_email(params[:email])
15
- }
16
- k2_request_amount = {
17
- currency: 'KES',
18
- value: params[:value]
19
- }
20
- k2_request_metadata = params[:metadata]
21
- k2_request_links = {
22
- callback_url: params[:callback_url]
23
- }
24
- receive_body = {
25
- payment_channel: params[:payment_channel],
26
- till_number: validate_till_number_prefix(params[:till_number]),
27
- subscriber: k2_request_subscriber,
28
- amount: k2_request_amount,
29
- meta_data: k2_request_metadata,
30
- _links: k2_request_links
31
- }
32
- receive_hash = make_hash(K2ConnectRuby::K2Utilities::Config::K2Config.path_url('incoming_payments'), 'post', @access_token, 'STK', receive_body)
33
- @location_url = K2ConnectRuby::K2Utilities::K2Connection.make_request(receive_hash)
7
+ def send_stk_request(params)
8
+ stk_push_request = K2ConnectRuby::K2Entity::K2FinancialEntities::StkPush::StkPushRequest.new(params)
9
+ raise(ArgumentError, stk_push_request.errors.full_messages.first) unless stk_push_request.valid?
10
+
11
+ result = K2ConnectRuby::K2Services::SendK2ConnectPostRequestService.call(
12
+ access_token,
13
+ stk_push_request.endpoint,
14
+ stk_push_request.request_body,
15
+ )
16
+ @location_url = result.data[:response_headers][:location]
34
17
  end
35
18
 
36
19
  # Query/Check STK Payment Request Status
37
20
  def query_status
38
- super('STK', path_url=@location_url)
21
+ super(location_url)
39
22
  end
40
23
 
41
24
  # Query Location URL
42
25
  def query_resource(url)
43
- super('STK', url)
26
+ super(url)
44
27
  end
45
28
  end
46
29
  end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module K2ConnectRuby
4
+ module K2Entity
5
+ # Service class to initiate or cancel a payment link
6
+ class PaymentLink < K2ConnectRuby::K2Entity::K2Entity
7
+ attr_accessor :payment_link_location_url
8
+
9
+ def create_payment_link(params)
10
+ payment_link_request = build_payment_link_request(params)
11
+ raise(ArgumentError, payment_link_request.errors.full_messages.first) unless payment_link_request.valid?
12
+
13
+ result = K2ConnectRuby::K2Services::SendK2ConnectPostRequestService.call(
14
+ access_token,
15
+ payment_link_request.endpoint,
16
+ payment_link_request.request_body,
17
+ )
18
+ raise(result.errors.first) unless result.success?
19
+
20
+ @payment_link_location_url = result.data[:response_headers][:location]
21
+ end
22
+
23
+ def cancel_payment_link(resource_url)
24
+ result = K2ConnectRuby::K2Services::SendK2ConnectPostRequestService.call(
25
+ access_token,
26
+ "#{resource_url}/cancel",
27
+ nil,
28
+ )
29
+ raise(result.errors.first) unless result.success?
30
+
31
+ result.data[:response_body][:message]
32
+ end
33
+
34
+ def query_status
35
+ super(payment_link_location_url)
36
+ end
37
+
38
+ private
39
+
40
+ def build_payment_link_request(params)
41
+ K2ConnectRuby::K2Entity::K2FinancialEntities::PaymentLinks::PaymentLinkRequest.new(
42
+ till_number: params[:till_number],
43
+ currency: params[:currency],
44
+ amount: params[:amount],
45
+ payment_reference: params[:payment_reference],
46
+ note: params[:note],
47
+ metadata: params[:metadata],
48
+ callback_url: params[:callback_url],
49
+ )
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module K2ConnectRuby
4
+ module K2Entity
5
+ module K2FinancialEntities
6
+ module PaymentLinks
7
+ # Request to create a payment link
8
+ class PaymentLinkRequest
9
+ include ActiveModel::Validations
10
+
11
+ attr_accessor :till_number, :currency, :amount, :payment_reference, :note, :metadata, :callback_url
12
+
13
+ validates :till_number, :currency, :amount, :callback_url, presence: true
14
+ validates :currency, inclusion: { in: ["KES"], message: "must be 'KES'" }
15
+
16
+ def initialize(kwargs)
17
+ kwargs.each do |key, value|
18
+ instance_variable_set("@#{key}", value)
19
+ end
20
+ end
21
+
22
+ def request_body
23
+ {
24
+ till_number: till_number,
25
+ currency: currency,
26
+ amount: amount,
27
+ payment_reference: payment_reference,
28
+ note: note,
29
+ metadata: metadata,
30
+ _links: {
31
+ callback_url: callback_url,
32
+ },
33
+ }
34
+ end
35
+
36
+ def endpoint
37
+ K2ConnectRuby::K2Utilities::Config::K2Config.endpoint("payment_links")
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module K2ConnectRuby
4
+ module K2Entity
5
+ # Initiate or get status of a reversal request
6
+ class Reversal < K2ConnectRuby::K2Entity::K2Entity
7
+ attr_accessor :reversal_location_url
8
+
9
+ def initiate_reversal(params)
10
+ reversal_request = build_reversal_request(params)
11
+ raise ArgumentError, reversal_request.errors.full_messages.first unless reversal_request.valid?
12
+
13
+ result = K2ConnectRuby::K2Services::SendK2ConnectPostRequestService.call(
14
+ access_token,
15
+ reversal_request.endpoint,
16
+ reversal_request.request_body,
17
+ )
18
+ raise(result.errors.first) unless result.success?
19
+
20
+ @reversal_location_url = result.data[:response_headers][:location]
21
+ end
22
+
23
+ def query_status
24
+ super(reversal_location_url)
25
+ end
26
+
27
+ private
28
+
29
+ def build_reversal_request(params)
30
+ K2ConnectRuby::K2Entity::K2FinancialEntities::Reversals::ReversalRequest.new(
31
+ transaction_reference: params[:transaction_reference],
32
+ reason: params[:reason],
33
+ metadata: params[:metadata],
34
+ callback_url: params[:callback_url],
35
+ )
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module K2ConnectRuby
4
+ module K2Entity
5
+ module K2FinancialEntities
6
+ module Reversals
7
+ # Request to initiate a reversal
8
+ class ReversalRequest
9
+ include ActiveModel::Validations
10
+
11
+ attr_accessor :transaction_reference, :reason, :metadata, :callback_url
12
+
13
+ validates :transaction_reference, :reason, :callback_url, presence: true
14
+
15
+ def initialize(kwargs)
16
+ kwargs.each do |key, value|
17
+ instance_variable_set("@#{key}", value)
18
+ end
19
+ end
20
+
21
+ def request_body
22
+ {
23
+ transaction_reference: transaction_reference,
24
+ reason: reason,
25
+ metadata: metadata,
26
+ _links: {
27
+ callback_url: callback_url
28
+ }
29
+ }
30
+ end
31
+
32
+ def endpoint
33
+ K2ConnectRuby::K2Utilities::Config::K2Config.endpoint("reversals")
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module K2ConnectRuby
4
+ module K2Entity
5
+ module K2FinancialEntities
6
+ module SendMoney
7
+ class SendMoneyRequest
8
+ include ActiveModel::Validations
9
+
10
+ attr_accessor :source_identifier, :destinations, :destination_requests, :currency, :callback_url, :metadata
11
+
12
+ validates :currency, :callback_url, presence: true
13
+ validates :currency, inclusion: { in: ["KES"], message: "must be 'KES'." }
14
+ validate :validate_destination_details
15
+
16
+ def initialize(kwargs)
17
+ kwargs.each do |key, value|
18
+ instance_variable_set("@#{key}", value)
19
+ end
20
+ end
21
+
22
+ def endpoint
23
+ K2ConnectRuby::K2Utilities::Config::K2Config.endpoint("send_money")
24
+ end
25
+
26
+ def request_body
27
+ {
28
+ source_identifier: source_identifier,
29
+ destinations: destination_requests&.map(&:destination_payload),
30
+ currency: currency,
31
+ metadata: metadata,
32
+ _links: {
33
+ callback_url: callback_url,
34
+ },
35
+ }
36
+ end
37
+
38
+ private
39
+
40
+ def validate_destination_details
41
+ return if destination_requests.blank?
42
+
43
+ invalid_destination_request = destination_requests.find { |request| !request.valid? }
44
+
45
+ if invalid_destination_request.present?
46
+ invalid_destination_request.errors.full_messages.each { |message| self.errors.add(:base, message) }
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Create a send money request (Either a payment to an external recipient/third party, or transferring funds to ones own accounts).
4
+ module K2ConnectRuby
5
+ module K2Entity
6
+ class SendMoney < K2ConnectRuby::K2Entity::K2Entity
7
+ attr_accessor :payments_location_url
8
+
9
+ def create_payment(params)
10
+ send_money_request = build_send_money_request(params)
11
+ raise(ArgumentError, send_money_request.errors.full_messages.first) unless send_money_request.valid?
12
+
13
+ result = K2ConnectRuby::K2Services::SendK2ConnectPostRequestService.call(
14
+ access_token,
15
+ send_money_request.endpoint,
16
+ send_money_request.request_body,
17
+ )
18
+ if result.success?
19
+ @payments_location_url = result.data[:response_headers][:location]
20
+ else
21
+ raise(result.errors.first)
22
+ end
23
+ end
24
+
25
+ # Query/Check the status of a recently initiated Send money request
26
+ def query_status
27
+ super(payments_location_url)
28
+ end
29
+
30
+ # Query specific URL
31
+ def query_resource(url)
32
+ super(url)
33
+ end
34
+
35
+ private
36
+
37
+ def build_send_money_request(params)
38
+ K2ConnectRuby::K2Entity::K2FinancialEntities::SendMoney::SendMoneyRequest.new(
39
+ source_identifier: params[:source_identifier],
40
+ destination_requests: build_destination_requests(params[:destinations]),
41
+ metadata: params[:metadata],
42
+ currency: params[:currency],
43
+ callback_url: params[:callback_url],
44
+ )
45
+ end
46
+
47
+ def build_destination_requests(destination_params)
48
+ destination_params&.map do |params|
49
+ build_destination_request(params)
50
+ end
51
+ end
52
+
53
+ def build_destination_request(destination_request)
54
+ case destination_request[:type]
55
+ when "mobile_wallet"
56
+ K2ConnectRuby::K2Entity::K2FinancialEntities::Destination::ExternalMpesaWallet.new(destination_request)
57
+ when "bank_account"
58
+ K2ConnectRuby::K2Entity::K2FinancialEntities::Destination::ExternalBankAccount.new(destination_request)
59
+ when "paybill"
60
+ K2ConnectRuby::K2Entity::K2FinancialEntities::Destination::ExternalPaybill.new(destination_request)
61
+ when "till"
62
+ K2ConnectRuby::K2Entity::K2FinancialEntities::Destination::ExternalTill.new(destination_request)
63
+ when "merchant_bank_account"
64
+ K2ConnectRuby::K2Entity::K2FinancialEntities::Destination::MerchantBankAccount.new(destination_request)
65
+ when "merchant_wallet"
66
+ K2ConnectRuby::K2Entity::K2FinancialEntities::Destination::MerchantMpesaWallet.new(destination_request)
67
+ else
68
+ raise(ArgumentError, "Undefined destination type.")
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module K2ConnectRuby
4
+ module K2Entity
5
+ module K2FinancialEntities
6
+ module StkPush
7
+ class StkPushRequest
8
+ include ActiveModel::Validations
9
+
10
+ attr_accessor :till_number, :payment_channel, :first_name, :middle_name, :last_name, :phone_number, :email, :amount, :callback_url, :metadata
11
+
12
+ validates :till_number, :phone_number, :amount, :callback_url, presence: true
13
+
14
+ validates_with K2ConnectRuby::K2Utilities::PhoneNumberValidator
15
+
16
+ def initialize(kwargs)
17
+ kwargs.each do |key, value|
18
+ instance_variable_set("@#{key}", value)
19
+ end
20
+ end
21
+
22
+ def endpoint
23
+ K2ConnectRuby::K2Utilities::Config::K2Config.endpoint("incoming_payments")
24
+ end
25
+
26
+ def request_body
27
+ {
28
+ payment_channel: payment_channel,
29
+ till_number: till_number,
30
+ subscriber: {
31
+ first_name: first_name,
32
+ middle_name: middle_name,
33
+ last_name: last_name,
34
+ phone_number: phone_number,
35
+ email: email,
36
+ },
37
+ amount: {
38
+ currency: "KES",
39
+ value: amount,
40
+ },
41
+ metadata: metadata,
42
+ _links: {
43
+ callback_url: callback_url,
44
+ },
45
+ }
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module K2ConnectRuby
4
+ module K2Entity
5
+ module K2FinancialEntities
6
+ module TransferAccount
7
+ class MerchantBankAccount < TransferAccountRequest
8
+ include ActiveModel::Validations
9
+
10
+ attr_accessor :account_name, :account_number, :bank_branch_ref, :settlement_method
11
+
12
+ validates :account_name, :account_number, :bank_branch_ref, :settlement_method, presence: true
13
+ validates :settlement_method, inclusion: {
14
+ in: ["EFT", "RTS"],
15
+ message: "is invalid.",
16
+ }
17
+
18
+ def endpoint
19
+ K2ConnectRuby::K2Utilities::Config::K2Config.endpoint("settlement_bank_account")
20
+ end
21
+
22
+ def request_body
23
+ {
24
+ nickname: nickname,
25
+ account_name: account_name,
26
+ account_number: account_number,
27
+ bank_branch_ref: bank_branch_ref,
28
+ settlement_method: settlement_method,
29
+ }
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module K2ConnectRuby
4
+ module K2Entity
5
+ module K2FinancialEntities
6
+ module TransferAccount
7
+ class MerchantMpesaWallet < TransferAccountRequest
8
+ include ActiveModel::Validations
9
+
10
+ attr_accessor :first_name, :last_name, :phone_number, :email, :network
11
+
12
+ validates :first_name, :last_name, :email, presence: true
13
+ validates :phone_number, presence: true, length: { maximum: 13 }
14
+ validates :email, format: {
15
+ with: URI::MailTo::EMAIL_REGEXP,
16
+ message: "is invalid.",
17
+ }
18
+
19
+ validates_with K2ConnectRuby::K2Utilities::PhoneNumberValidator
20
+
21
+ def endpoint
22
+ K2ConnectRuby::K2Utilities::Config::K2Config.endpoint("settlement_mobile_wallet")
23
+ end
24
+
25
+ def request_body
26
+ {
27
+ nickname: nickname,
28
+ first_name: first_name,
29
+ last_name: last_name,
30
+ phone_number: phone_number,
31
+ network: network,
32
+ }
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module K2ConnectRuby
4
+ module K2Entity
5
+ module K2FinancialEntities
6
+ module TransferAccount
7
+ class TransferAccountRequest
8
+ include ActiveModel::Validations
9
+
10
+ attr_accessor :type, :nickname
11
+
12
+ def initialize(kwargs)
13
+ kwargs.each do |key, value|
14
+ instance_variable_set("@#{key}", value)
15
+ end
16
+ end
17
+
18
+ def endpoint; end
19
+
20
+ def request_body; end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Adding either one's own bank account or M-PESA phone
4
+ module K2ConnectRuby
5
+ module K2Entity
6
+ class TransferAccount < K2ConnectRuby::K2Entity::K2Entity
7
+ attr_accessor :transfer_account_location_url
8
+
9
+ def add_transfer_account(params)
10
+ params = params.with_indifferent_access
11
+ transfer_account = build_transfer_account(params)
12
+ raise(ArgumentError, transfer_account.errors.full_messages.first) unless transfer_account.valid?
13
+
14
+ result = K2ConnectRuby::K2Services::SendK2ConnectPostRequestService.call(
15
+ access_token,
16
+ transfer_account.endpoint,
17
+ transfer_account.request_body,
18
+ )
19
+ if result.success?
20
+ @transfer_account_location_url = result.data[:response_headers][:location]
21
+ else
22
+ raise(result.errors.first)
23
+ end
24
+ end
25
+
26
+ # Query/Check the status of a recently initiated Send money request
27
+ def query_status
28
+ super(transfer_account_location_url)
29
+ end
30
+
31
+ # Query specific URL
32
+ def query_resource(url)
33
+ super(url)
34
+ end
35
+
36
+ private
37
+
38
+ def build_transfer_account(params)
39
+ if params[:type].eql?("merchant_wallet")
40
+ K2ConnectRuby::K2Entity::K2FinancialEntities::TransferAccount::MerchantMpesaWallet.new(params)
41
+ elsif params[:type].eql?("merchant_bank_account")
42
+ K2ConnectRuby::K2Entity::K2FinancialEntities::TransferAccount::MerchantBankAccount.new(params)
43
+ else
44
+ raise(ArgumentError, "Unknown transfer account type.")
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,32 +1,35 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module K2ConnectRuby
2
4
  module K2Entity
3
5
  class K2Polling
4
- include K2ConnectRuby::K2Utilities::K2Validation, K2ConnectRuby::K2Utilities
6
+ include K2ConnectRuby::K2Utilities
5
7
  attr_reader :location_url, :k2_response_body
6
8
  attr_accessor :access_token
7
9
 
8
10
  # Initialize with access token
9
11
  def initialize(access_token)
10
- raise ArgumentError, 'Nil or Empty Access Token Given!' if access_token.blank?
12
+ raise ArgumentError, "Nil or Empty Access Token Given!" if access_token.blank?
13
+
11
14
  @access_token = access_token
12
15
  end
13
16
 
14
17
  def poll(params)
15
- k2_request_body = {
16
- scope: params[:scope],
17
- scope_reference: params[:scope_reference],
18
- from_time: params[:from_time],
19
- to_time: params[:to_time],
20
- _links: { callback_url: params[:callback_url] }
21
- }
22
- poll_hash = make_hash(K2ConnectRuby::K2Utilities::Config::K2Config.path_url('poll'), 'post', @access_token,'Polling', k2_request_body)
23
- @location_url = K2ConnectRuby::K2Utilities::K2Connection.make_request(poll_hash)
18
+ polling_request = K2ConnectRuby::K2Entity::K2FinancialEntities::Polling::PollingRequest.new(params)
19
+ raise(ArgumentError, polling_request.errors.full_messages.first) unless polling_request.valid?
20
+
21
+ result = K2ConnectRuby::K2Services::SendK2ConnectPostRequestService.call(
22
+ access_token,
23
+ polling_request.endpoint,
24
+ polling_request.request_body,
25
+ )
26
+ @location_url = result.data[:response_headers][:location]
24
27
  end
25
28
 
26
29
  # Retrieve your newly created polling request by its resource location
27
30
  def query_resource(location_url = @location_url)
28
- query_hash = make_hash(location_url, 'get', @access_token, 'Polling', nil)
29
- @k2_response_body = K2ConnectRuby::K2Utilities::K2Connection.make_request(query_hash)
31
+ result = K2ConnectRuby::K2Services::SendK2ConnectGetRequestService.call(access_token, location_url)
32
+ @k2_response_body = result.data[:response_body]
30
33
  end
31
34
 
32
35
  # Retrieve your newly created polling request by specific resource location
@@ -1,34 +1,36 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module K2ConnectRuby
2
4
  module K2Entity
3
5
  class K2Subscribe
4
- include K2ConnectRuby::K2Utilities::K2Validation, K2ConnectRuby::K2Utilities
6
+ include K2ConnectRuby::K2Utilities
5
7
  attr_reader :location_url, :k2_response_body
6
8
  attr_accessor :access_token, :webhook_secret
7
9
 
8
10
  # Initialize with access token
9
11
  def initialize(access_token)
10
- raise ArgumentError, 'Nil or Empty Access Token Given!' if access_token.blank?
12
+ raise ArgumentError, "Nil or Empty Access Token Given!" if access_token.blank?
13
+
11
14
  @access_token = access_token
12
15
  end
13
16
 
14
17
  # Implemented a Case condition that minimises repetition
15
18
  def webhook_subscribe(params)
16
- params = validate_webhook_input(params)
17
- validate_webhook(params)
18
- k2_request_body = {
19
- event_type: params[:event_type],
20
- url: params[:url],
21
- scope: params[:scope],
22
- scope_reference: params[:scope_reference]
23
- }
24
- subscribe_hash = make_hash(K2ConnectRuby::K2Utilities::Config::K2Config.path_url('webhook_subscriptions'), 'post', @access_token,'Subscription', k2_request_body)
25
- @location_url = K2ConnectRuby::K2Utilities::K2Connection.make_request(subscribe_hash)
19
+ webhook_subscription_request = K2ConnectRuby::K2Entity::K2FinancialEntities::Webhook::WebhookSubscriptionRequest.new(params)
20
+ raise(ArgumentError, webhook_subscription_request.errors.full_messages.first) unless webhook_subscription_request.valid?
21
+
22
+ result = K2ConnectRuby::K2Services::SendK2ConnectPostRequestService.call(
23
+ access_token,
24
+ webhook_subscription_request.endpoint,
25
+ webhook_subscription_request.request_body,
26
+ )
27
+ @location_url = result.data[:response_headers][:location]
26
28
  end
27
29
 
28
30
  # Query Recent Webhook
29
31
  def query_webhook(location_url = @location_url)
30
- query_hash = make_hash(location_url, 'get', @access_token, 'Subscription', nil)
31
- @k2_response_body = K2ConnectRuby::K2Utilities::K2Connection.make_request(query_hash)
32
+ result = K2ConnectRuby::K2Services::SendK2ConnectGetRequestService.call(access_token, location_url)
33
+ @k2_response_body = result.data[:response_body]
32
34
  end
33
35
 
34
36
  # Query Specific Webhook URL