Class: Airtel::Pesa::UssdPushPayment

Inherits:
Object
  • Object
show all
Defined in:
lib/airtel/pesa/ussd_push_payment.rb,
lib/airtel/pesa/disbursement_payment.rb

Constant Summary collapse

STAGING_URL =
"https://openapiuat.airtel.africa".freeze
PRODUCTION_URL =
"https://openapi.airtel.africa".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount, phone_number, reference, pin, transaction_country_code, transaction_currency_code, unique_random_id) ⇒ UssdPushPayment

Returns a new instance of UssdPushPayment.



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

def initialize(
  amount, phone_number, country_code, currency_code,
  transaction_country_code, transaction_currency_code,
  unique_random_id
)
  @amount = amount
  @phone_number = phone_number
  @country_code = country_code
  @currency_code = currency_code
  @transaction_country_code = transaction_country_code
  @transaction_currency_code = transaction_currency_code
  @unique_random_id = unique_random_id
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



15
16
17
# File 'lib/airtel/pesa/ussd_push_payment.rb', line 15

def amount
  @amount
end

#country_codeObject (readonly)

Returns the value of attribute country_code.



15
16
17
# File 'lib/airtel/pesa/ussd_push_payment.rb', line 15

def country_code
  @country_code
end

#currency_codeObject (readonly)

Returns the value of attribute currency_code.



15
16
17
# File 'lib/airtel/pesa/ussd_push_payment.rb', line 15

def currency_code
  @currency_code
end

#phone_numberObject (readonly)

Returns the value of attribute phone_number.



15
16
17
# File 'lib/airtel/pesa/ussd_push_payment.rb', line 15

def phone_number
  @phone_number
end

#pinObject (readonly)

Returns the value of attribute pin.



15
16
17
# File 'lib/airtel/pesa/disbursement_payment.rb', line 15

def pin
  @pin
end

#referenceObject (readonly)

Returns the value of attribute reference.



15
16
17
# File 'lib/airtel/pesa/disbursement_payment.rb', line 15

def reference
  @reference
end

#transaction_country_codeObject (readonly)

Returns the value of attribute transaction_country_code.



15
16
17
# File 'lib/airtel/pesa/ussd_push_payment.rb', line 15

def transaction_country_code
  @transaction_country_code
end

#transaction_currency_codeObject (readonly)

Returns the value of attribute transaction_currency_code.



15
16
17
# File 'lib/airtel/pesa/ussd_push_payment.rb', line 15

def transaction_currency_code
  @transaction_currency_code
end

#unique_random_idObject (readonly)

Returns the value of attribute unique_random_id.



15
16
17
# File 'lib/airtel/pesa/ussd_push_payment.rb', line 15

def unique_random_id
  @unique_random_id
end

Class Method Details

.call(amount:, phone_number:, reference:, pin:, transaction_country_code:, transaction_currency_code:, unique_random_id:) ⇒ Object



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

def self.call(
  amount:, phone_number:, country_code:, currency_code:,
  transaction_country_code:, transaction_currency_code:,
  unique_random_id:
)
  new(
    amount, phone_number, country_code, currency_code,
    transaction_country_code, transaction_currency_code,
    unique_random_id
  ).call
end

Instance Method Details

#callObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/airtel/pesa/ussd_push_payment.rb', line 44

def call
  url = URI("#{env_url}/merchant/v1/payments")

  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