Class: MpesaStk::B2C

Inherits:
Client show all
Defined in:
lib/mpesa_stk/b2c.rb

Overview

Business-to-customer payments (salary, promotions, etc.).

Constant Summary

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, **options) ⇒ B2C

Returns a new instance of B2C.



16
17
18
19
20
# File 'lib/mpesa_stk/b2c.rb', line 16

def initialize(amount, phone_number, **options)
  super(**options)
  @amount = amount
  @phone_number = phone_number
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



14
15
16
# File 'lib/mpesa_stk/b2c.rb', line 14

def amount
  @amount
end

#phone_numberObject (readonly)

Returns the value of attribute phone_number.



14
15
16
# File 'lib/mpesa_stk/b2c.rb', line 14

def phone_number
  @phone_number
end

Class Method Details

.call(amount, phone_number, **options) ⇒ Object



9
10
11
# File 'lib/mpesa_stk/b2c.rb', line 9

def call(amount, phone_number, **options)
  new(amount, phone_number, **options).send_payment
end

Instance Method Details

#send_paymentObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mpesa_stk/b2c.rb', line 22

def send_payment
  body = {
    InitiatorName: option('initiator_name'),
    SecurityCredential: option('security_credential'),
    CommandID: @options.fetch(:command_id, 'BusinessPayment'),
    Amount: amount.to_s,
    PartyA: option('business_short_code', :party_a),
    PartyB: phone_number,
    Remarks: @options.fetch(:remarks, 'Payment'),
    QueueTimeOutURL: option('queue_timeout_url'),
    ResultURL: option('result_url')
  }
  body[:Occasion] = @options[:occasion] if @options[:occasion]

  post('b2c_url', body, error_message: 'Failed to send B2C payment')
end