Class: Mobile::Pesa::B2cPayment
- Inherits:
-
Object
- Object
- Mobile::Pesa::B2cPayment
- Defined in:
- lib/mobile/pesa/b2c_payment.rb
Instance Attribute Summary collapse
-
#amount ⇒ Object
readonly
Returns the value of attribute amount.
-
#command_id ⇒ Object
readonly
Returns the value of attribute command_id.
-
#occasion ⇒ Object
readonly
Returns the value of attribute occasion.
-
#phone_number ⇒ Object
readonly
Returns the value of attribute phone_number.
-
#remarks ⇒ Object
readonly
Returns the value of attribute remarks.
-
#short_code ⇒ Object
readonly
Returns the value of attribute short_code.
Class Method Summary collapse
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(amount, phone_number, short_code, command_id, remarks, occasion) ⇒ B2cPayment
constructor
A new instance of B2cPayment.
Constructor Details
#initialize(amount, phone_number, short_code, command_id, remarks, occasion) ⇒ B2cPayment
Returns a new instance of B2cPayment.
19 20 21 22 23 24 25 26 |
# File 'lib/mobile/pesa/b2c_payment.rb', line 19 def initialize(amount, phone_number, short_code, command_id, remarks, occasion) @amount = amount @phone_number = phone_number @short_code = short_code @command_id = command_id @remarks = remarks @occasion = occasion end |
Instance Attribute Details
#amount ⇒ Object (readonly)
Returns the value of attribute amount.
13 14 15 |
# File 'lib/mobile/pesa/b2c_payment.rb', line 13 def amount @amount end |
#command_id ⇒ Object (readonly)
Returns the value of attribute command_id.
13 14 15 |
# File 'lib/mobile/pesa/b2c_payment.rb', line 13 def command_id @command_id end |
#occasion ⇒ Object (readonly)
Returns the value of attribute occasion.
13 14 15 |
# File 'lib/mobile/pesa/b2c_payment.rb', line 13 def occasion @occasion end |
#phone_number ⇒ Object (readonly)
Returns the value of attribute phone_number.
13 14 15 |
# File 'lib/mobile/pesa/b2c_payment.rb', line 13 def phone_number @phone_number end |
#remarks ⇒ Object (readonly)
Returns the value of attribute remarks.
13 14 15 |
# File 'lib/mobile/pesa/b2c_payment.rb', line 13 def remarks @remarks end |
#short_code ⇒ Object (readonly)
Returns the value of attribute short_code.
13 14 15 |
# File 'lib/mobile/pesa/b2c_payment.rb', line 13 def short_code @short_code end |
Class Method Details
.call(amount:, phone_number:, short_code:, command_id:, remarks:, occasion:) ⇒ Object
15 16 17 |
# File 'lib/mobile/pesa/b2c_payment.rb', line 15 def self.call(amount:, phone_number:, short_code:, command_id:, remarks:, occasion:) new(amount, phone_number, short_code, command_id, remarks, occasion).call end |
Instance Method Details
#call ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mobile/pesa/b2c_payment.rb', line 28 def call url = URI("https://sandbox.safaricom.co.ke/mpesa/b2c/v1/paymentrequest") 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.body = JSON.dump(body) response = http.request(request) parsed_body = JSON.parse(response.read_body) if parsed_body.key?("errorCode") error = OpenStruct.new( error_code: parsed_body["errorCode"], error_message: parsed_body["errorMessage"], request_id: parsed_body["requestId"] ) OpenStruct.new(result: nil, error: error) else result = OpenStruct.new( conversation_id: parsed_body["ConversationID"], originator_conversation_id: parsed_body["OriginatorConversationID"], response_code: parsed_body["ResponseCode"], response_description: parsed_body["ResponseDescription"] ) OpenStruct.new(result: result, error: nil) end rescue JSON::ParserError => error OpenStruct.new(result: nil, error: error) end |