Class: K2Stk

Inherits:
K2Entity show all
Defined in:
lib/k2-connect-ruby/k2_entity/k2_financial_entities/k2_stk.rb

Overview

For STK Push/Receive MPESA Payments from merchant’s customers

Constant Summary

Constants included from K2Validation

K2Validation::ALL_EVENT_TYPES, K2Validation::TILL_SCOPE_EVENT_TYPES

Instance Attribute Summary

Attributes inherited from K2Entity

#access_token, #k2_response_body, #location_url, #query_hash, #the_array

Instance Method Summary collapse

Methods inherited from K2Entity

#initialize, #query

Methods included from K2Validation

#determine_scope_details, #incorrect_keys, #nil_values, #to_indifferent_access, #validate_email, #validate_hash, #validate_input, #validate_network, #validate_phone, #validate_settlement_method, #validate_till_number_prefix, #validate_url, #validate_webhook, #validate_webhook_input

Methods included from K2Utilities

#make_hash

Constructor Details

This class inherits a constructor from K2Entity

Instance Method Details

#query_resource(url) ⇒ Object

Query Location URL



45
46
47
# File 'lib/k2-connect-ruby/k2_entity/k2_financial_entities/k2_stk.rb', line 45

def query_resource(url)
  super('STK', url)
end

#query_statusObject

Query/Check STK Payment Request Status



40
41
42
# File 'lib/k2-connect-ruby/k2_entity/k2_financial_entities/k2_stk.rb', line 40

def query_status
  super('STK', path_url=@location_url)
end

#receive_mpesa_payments(params) ⇒ Object

Receive payments from M-PESA users.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/k2-connect-ruby/k2_entity/k2_financial_entities/k2_stk.rb', line 4

def receive_mpesa_payments(params)
  # Validation
  params = validate_input(params, @exception_array += %w[payment_channel till_number first_name last_name phone_number email currency value metadata callback_url])
  # The Request Body Parameters
  k2_request_subscriber = {
    first_name: params[:first_name],
    middle_name: params[:middle_name],
    last_name: params[:last_name],
    phone_number: validate_phone(params[:phone_number]),
    email: validate_email(params[:email])
  }
  k2_request_amount = {
    currency: 'KES',
    value: params[:value]
  }
   = params[:metadata]
  k2_request_links = {
      callback_url: params[:callback_url]
  }
  receive_body = {
    payment_channel: params[:payment_channel],
    till_number: validate_till_number_prefix(params[:till_number]),
    subscriber: k2_request_subscriber,
    amount: k2_request_amount,
    meta_data: ,
    _links: k2_request_links
  }
  receive_hash = make_hash(K2Config.path_url('incoming_payments'), 'post', @access_token, 'STK', receive_body)
  @threads << Thread.new do
    sleep 0.25
    @location_url = K2Connect.make_request(receive_hash)
  end
  @threads.each(&:join)
end