Class: Mpesa::Stk

Inherits:
Resource show all
Defined in:
lib/mpesa/resources/stk.rb

Constant Summary collapse

PATH =
'mpesa/stkpush/v1/processrequest'

Instance Attribute Summary

Attributes inherited from Resource

#args, #client

Instance Method Summary collapse

Methods inherited from Resource

#format_phone, #get_request, #handle_response, #initialize, #post_request

Constructor Details

This class inherits a constructor from Mpesa::Resource

Instance Method Details

#bodyObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mpesa/resources/stk.rb', line 13

def body
  {
    'BusinessShortCode': shortcode,
    'Password': password,
    'Timestamp': timestamp,
    'Amount': args[:amount],
    'PartyA': format_phone(args[:phone]),
    'PhoneNumber': format_phone(args[:phone]),
    'CallBackURL': args[:callback_url],
    'AccountReference': args[:reference],
    'TransactionDesc': args[:trans_desc]
  }.merge(transaction_details)
end

#callObject



9
10
11
# File 'lib/mpesa/resources/stk.rb', line 9

def call
  Object.new post_request(url: PATH, body: body).body
end

#passwordObject



35
36
37
# File 'lib/mpesa/resources/stk.rb', line 35

def password
  Base64.strict_encode64("#{shortcode}#{args[:pass_key] || client.pass_key}#{timestamp}")
end

#shortcodeObject



43
44
45
# File 'lib/mpesa/resources/stk.rb', line 43

def shortcode
  args[:shortcode] || client.shortcode
end

#timestampObject



39
40
41
# File 'lib/mpesa/resources/stk.rb', line 39

def timestamp
  Time.now.strftime('%Y%m%d%H%M%S')
end

#transaction_detailsObject



27
28
29
30
31
32
33
# File 'lib/mpesa/resources/stk.rb', line 27

def transaction_details
  till_no = args[:till_no] unless args[:till_no].nil? || args[:till_no].strip.empty?
  {
    'TransactionType': till_no.nil? ? 'CustomerPayBillOnline' : 'CustomerBuyGoodsOnline',
    'PartyB': till_no || shortcode
  }
end