Class: MpesaStk::C2B

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

Overview

Register C2B URLs and simulate customer-to-business payments.

Constant Summary

Constants inherited from Client

MpesaStk::Client::AUTH_KEYS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Client

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

Constructor Details

This class inherits a constructor from MpesaStk::Client

Class Method Details

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



13
14
15
# File 'lib/mpesa_stk/c2b.rb', line 13

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

.register(**options) ⇒ Object



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

def register(**options)
  new(**options).register
end

Instance Method Details

#registerObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mpesa_stk/c2b.rb', line 18

def register
  body = {
    ShortCode: option('business_short_code', :short_code),
    ResponseType: @options.fetch(:response_type, 'Completed'),
    ConfirmationURL: option('confirmation_url')
  }
  validation_url = optional_option('validation_url')
  body[:ValidationURL] = validation_url if validation_url

  post('c2b_register_url', body, error_message: 'Failed to register C2B URL')
end

#simulate_payment(amount, phone_number) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mpesa_stk/c2b.rb', line 30

def simulate_payment(amount, phone_number)
  post(
    'c2b_simulate_url',
    {
      ShortCode: option('business_short_code', :short_code),
      CommandID: @options.fetch(:command_id, 'CustomerPayBillOnline'),
      Amount: amount.to_s,
      Msisdn: phone_number,
      BillRefNumber: @options.fetch(:bill_ref_number, '')
    },
    error_message: 'Failed to simulate C2B payment'
  )
end