Class: Mpesa::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/mpesa/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, secret:, shortcode: nil, pass_key: nil, env: 'production', adapter: Faraday.default_adapter, raise_errors: true) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
16
17
# File 'lib/mpesa/client.rb', line 9

def initialize(key:, secret:, shortcode: nil, pass_key: nil, env: 'production', adapter: Faraday.default_adapter, raise_errors: true)
  @key = key
  @secret = secret
  @env = env
  @adapter = adapter
  @pass_key = pass_key
  @shortcode = shortcode
  @raise_errors = raise_errors
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



7
8
9
# File 'lib/mpesa/client.rb', line 7

def adapter
  @adapter
end

#envObject (readonly)

Returns the value of attribute env.



7
8
9
# File 'lib/mpesa/client.rb', line 7

def env
  @env
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/mpesa/client.rb', line 7

def key
  @key
end

#pass_keyObject (readonly)

Returns the value of attribute pass_key.



7
8
9
# File 'lib/mpesa/client.rb', line 7

def pass_key
  @pass_key
end

#raise_errorsObject (readonly)

Returns the value of attribute raise_errors.



7
8
9
# File 'lib/mpesa/client.rb', line 7

def raise_errors
  @raise_errors
end

#secretObject (readonly)

Returns the value of attribute secret.



7
8
9
# File 'lib/mpesa/client.rb', line 7

def secret
  @secret
end

#shortcodeObject (readonly)

Returns the value of attribute shortcode.



7
8
9
# File 'lib/mpesa/client.rb', line 7

def shortcode
  @shortcode
end

Instance Method Details

#authObject



19
20
21
# File 'lib/mpesa/client.rb', line 19

def auth
  Token.new(self).token
end

#balance(**args) ⇒ Object



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

def balance(**args)
  Balance.new(self, args).call
end

#connection(basic_auth: false) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/mpesa/client.rb', line 47

def connection(basic_auth: false)
  @connection ||= Faraday.new do |conn|
    conn.url_prefix = "https://#{subdomain}.safaricom.co.ke"
    conn.request :json
    conn.response :json, content_type: 'application/json'
    conn.adapter adapter
    conn.request :authorization, :basic, key, secret if basic_auth
    conn.request :authorization, 'Bearer', auth.access_token unless basic_auth
  end
end

#payout(**args) ⇒ Object



31
32
33
# File 'lib/mpesa/client.rb', line 31

def payout(**args)
  Payout.new(self, args).call
end

#register(**args) ⇒ Object



23
24
25
# File 'lib/mpesa/client.rb', line 23

def register(**args)
  Register.new(self, args).call
end

#reversal(**args) ⇒ Object



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

def reversal(**args)
  Reversal.new(self, args).call
end

#status(**args) ⇒ Object



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

def status(**args)
  Status.new(self, args).call
end

#stk(**args) ⇒ Object



27
28
29
# File 'lib/mpesa/client.rb', line 27

def stk(**args)
  Stk.new(self, args).call
end

#subdomainObject



58
59
60
# File 'lib/mpesa/client.rb', line 58

def subdomain
  env == 'production' ? 'api' : 'sandbox'
end