class Mpesa::Client

Attributes

adapter[R]
env[R]
key[R]
pass_key[R]
secret[R]
shortcode[R]

Public Class Methods

new(key:, secret:, shortcode: nil, pass_key: nil, env: 'production', adapter: Faraday.default_adapter) click to toggle source
# File lib/mpesa/client.rb, line 8
def initialize(key:, secret:, shortcode: nil, pass_key: nil, env: 'production', adapter: Faraday.default_adapter)
  @key = key
  @secret = secret
  @env = env
  @adapter = adapter
  @pass_key = pass_key
  @shortcode = shortcode
end

Public Instance Methods

auth() click to toggle source
# File lib/mpesa/client.rb, line 17
def auth
  Token.new(self).token
end
connection(basic_auth: false) click to toggle source
# File lib/mpesa/client.rb, line 33
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 :basic_auth, key, secret if basic_auth
    conn.request :authorization, :Bearer, auth.access_token unless basic_auth
  end
end
payout(**args) click to toggle source
# File lib/mpesa/client.rb, line 29
def payout(**args)
  Payout.new(self, args).call
end
register(**args) click to toggle source
# File lib/mpesa/client.rb, line 21
def register(**args)
  Register.new(self, args).call
end
stk(**args) click to toggle source
# File lib/mpesa/client.rb, line 25
def stk(**args)
  Stk.new(self, args).call
end
subdomain() click to toggle source
# File lib/mpesa/client.rb, line 44
def subdomain
  env == 'production' ? 'api' : 'sandbox'
end