Class: Payment

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/payment.rb

Overview

initialize Payment class

Direct Known Subclasses

Pay::BuyGoods, Pay::PayBill

Constant Summary collapse

C2B =
%w[CustomerPayBillOnline CustomerBuyGoodsOnline].freeze
B2C =
%w[BusinessPayment SalaryPayment PromotionPayment].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: AppConfig.new, pass_key: nil, push_type: 0) ⇒ Payment

Returns a new instance of Payment.



10
11
12
13
14
15
# File 'lib/ruby/payment.rb', line 10

def initialize(config: AppConfig.new, pass_key: nil, push_type: 0)
  @config = config
  @pass_key = pass_key
  @push_type = push_type
  @payment_credentials = encode_password
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/ruby/payment.rb', line 5

def config
  @config
end

#pass_keyObject

Returns the value of attribute pass_key.



6
7
8
# File 'lib/ruby/payment.rb', line 6

def pass_key
  @pass_key
end

#payment_credentialsObject (readonly)

Returns the value of attribute payment_credentials.



5
6
7
# File 'lib/ruby/payment.rb', line 5

def payment_credentials
  @payment_credentials
end

#push_typeObject (readonly)

Returns the value of attribute push_type.



5
6
7
# File 'lib/ruby/payment.rb', line 5

def push_type
  @push_type
end

Instance Method Details

#initiate_b2c(phone_number:, amount:, remarks:, type: 0, occasion: nil) ⇒ Hash<String, String|nil>

send B2C request

Parameters:

  • phone_number (String)
  • amount (String)
  • remarks (String)
  • type (int) (defaults to: 0)
  • occasion (String|nil) (defaults to: nil)

Returns:

  • (Hash<String, String|nil>)

    response



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby/payment.rb', line 50

def initiate_b2c(phone_number:, amount:, remarks:, type: 0, occasion: nil)
  response = @connection.post('/mpesa/b2c/v1/paymentrequest') do |req|
    req.headers['Authorization'] = "Basic #{@config.provider.token}"
    req.body = {
      InitiatorName: @config.initiator_name,
      SecurityCredential: security_credential,
      CommandID: B2C[type],
      Amount: amount,
      PartyA: @config.short_code,
      PartyB: phone_number,
      Remarks: remarks,
      QueueTimeOutURL: @config.b2c_result_url,
      ResultURL: @config.b2c_result_url,
      Occasion: occasion
    }.to_json
  end
  JSON.parse(response)
end

#initiate_balance_request(remarks: nil) ⇒ Hash<String, String|nil>

initiate balance request

Parameters:

  • remarks (String?) (defaults to: nil)

Returns:

  • (Hash<String, String|nil>)

    response



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ruby/payment.rb', line 72

def initiate_balance_request(remarks: nil)
  response = @connection.post('/mpesa/accountbalance/v1/query') do |req|
    req.headers['Authorization'] = "Basic #{@config.provider.token}"
    req.body = {
      Initiator: @config.initiator_name,
      SecurityCredential: security_credential,
      CommandID: 'AccountBalance',
      PartyA: @config.short_code,
      IdentifierType: 4,
      Remarks: remarks,
      QueueTimeOutURL: @config.balance_result_url,
      ResultURL: @config.balance_result_url
    }.to_json
  end
  JSON.parse(response)
end

#initiate_stk_push(phone_number:, amount:, reference:, description:) ⇒ Hash

send STK Push request

Parameters:

  • phone_number (String)
  • amount (String)
  • reference (String)
  • description (String)

Returns:

  • (Hash)

    response



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ruby/payment.rb', line 23

def initiate_stk_push(phone_number:, amount:, reference:, description:)
  response = @connection.post('/mpesa/stkpush/v1/processrequest') do |req|
    req.headers['Authorization'] = "Basic #{@config.provider.token}"
    req.body = {
      BusinessShortCode: @config.short_code,
      Password: @payment_credentials.password,
      Timestamp: @payment_credentials.timestamp,
      TransactionType: C2B[@push_type],
      Amount: amount,
      PartyA: phone_number,
      PartyB: @config.short_code,
      PhoneNumber: phone_number,
      CallBackURL: @config.confirmation_url,
      AccountReference: reference,
      TransactionDesc: description
    }.to_json
  end
  JSON.parse(response.body)
end