class Airtel::Pesa::UssdPushPayment

Constants

PRODUCTION_URL
STAGING_URL

Attributes

amount[R]
country_code[R]
currency_code[R]
phone_number[R]
pin[R]
reference[R]
transaction_country_code[R]
transaction_currency_code[R]
unique_random_id[R]

Public Class Methods

call( amount:, phone_number:, reference:, pin:, transaction_country_code:, transaction_currency_code:, unique_random_id: ) click to toggle source
# File lib/airtel/pesa/disbursement_payment.rb, line 18
def self.call(
  amount:, phone_number:, reference:, pin:,
  transaction_country_code:, transaction_currency_code:,
  unique_random_id:
)
  new(
    amount, phone_number, reference, pin,
    transaction_country_code, transaction_currency_code,
    unique_random_id
  ).call
end
new( amount, phone_number, reference, pin, transaction_country_code, transaction_currency_code, unique_random_id ) click to toggle source
# File lib/airtel/pesa/disbursement_payment.rb, line 30
def initialize(
  amount, phone_number, reference, pin,
  transaction_country_code, transaction_currency_code,
  unique_random_id
)
  @amount = amount
  @phone_number = phone_number
  @reference = reference
  @pin = pin
  @transaction_country_code = transaction_country_code
  @transaction_currency_code = transaction_currency_code
  @unique_random_id = unique_random_id
end

Public Instance Methods

call() click to toggle source
# File lib/airtel/pesa/disbursement_payment.rb, line 44
def call
  url = URI("#{env_url}/standard/v1/disbursements")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Post.new(url)
  request["Content-Type"] = 'application/json'
  request["Authorization"] = "Bearer #{token}"
  request["X-Country"] = transaction_country_code
  request["X-Currency"] = transaction_currency_code
  request.body = JSON.dump(body)

  response = http.request(request)
  parsed_response = JSON.parse(response.read_body)
  result = Airtel::Pesa.to_recursive_ostruct(parsed_response)
  OpenStruct.new(result: result, error: nil)
rescue JSON::ParserError => error
  OpenStruct.new(result: nil, error: error)
end

Private Instance Methods

body() click to toggle source
# File lib/airtel/pesa/disbursement_payment.rb, line 77
def body
  {
    "payee": {
      "msisdn": phone_number
    },
    "reference": reference,
    "pin": pin || Airtel::Pesa::Encryption.call.result,
    "transaction": {
      "amount": amount,
      "id": unique_random_id
    }
  }
end
env_url() click to toggle source
# File lib/airtel/pesa/disbursement_payment.rb, line 68
def env_url
  return STAGING_URL Airtel::Pesa.configuration.env == 'staging'
  return PRODUCTION_URL Airtel::Pesa.configuration.env == 'production'
end
token() click to toggle source
# File lib/airtel/pesa/disbursement_payment.rb, line 73
def token
  Airtel::Pesa::Authorization.call.result.access_token
end