Class: MpesaStk::Push

Inherits:
Client
  • Object
show all
Defined in:
lib/mpesa_stk/push.rb

Overview

STK Push (Lipa na M-Pesa): PayBill and Buy Goods.

MpesaStk::Push.call(amount, phone)                    # PayBill, ENV only
MpesaStk::Push.call(amount, phone, type: :buy_goods)  # Buy Goods
MpesaStk::Push.call(amount, phone, key: '...', ...)   # per-request overrides

Constant Summary collapse

TRANSACTION_TYPES =
{
  pay_bill: 'CustomerPayBillOnline',
  buy_goods: 'CustomerBuyGoodsOnline'
}.freeze

Constants inherited from Client

Client::AUTH_KEYS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Client

extract_auth_options, #json_headers, #option, #optional_option, #post, #random_reference, #stk_password, #stk_timestamp, #token

Constructor Details

#initialize(amount, phone_number, type: :pay_bill, **options) ⇒ Push

Returns a new instance of Push.



25
26
27
28
29
30
31
32
33
# File 'lib/mpesa_stk/push.rb', line 25

def initialize(amount, phone_number, type: :pay_bill, **options)
  @transaction_type = TRANSACTION_TYPES.fetch(type) do
    raise ArgumentError, "Unknown STK type: #{type}. Use :pay_bill or :buy_goods"
  end

  super(**options)
  @amount = amount
  @phone_number = phone_number
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



23
24
25
# File 'lib/mpesa_stk/push.rb', line 23

def amount
  @amount
end

#phone_numberObject (readonly)

Returns the value of attribute phone_number.



23
24
25
# File 'lib/mpesa_stk/push.rb', line 23

def phone_number
  @phone_number
end

#transaction_typeObject (readonly)

Returns the value of attribute transaction_type.



23
24
25
# File 'lib/mpesa_stk/push.rb', line 23

def transaction_type
  @transaction_type
end

Class Method Details

.call(amount, phone_number, type: :pay_bill, **options) ⇒ Object



18
19
20
# File 'lib/mpesa_stk/push.rb', line 18

def call(amount, phone_number, type: :pay_bill, **options)
  new(amount, phone_number, type: type, **options).push_payment
end

Instance Method Details

#push_paymentObject



35
36
37
# File 'lib/mpesa_stk/push.rb', line 35

def push_payment
  post('process_request_url', stk_push_payload, error_message: 'Failed to push payment')
end