class MtnCmMomoSdk::APIService

Your code goes here…

Attributes

body[RW]

Public Class Methods

new(transaction) click to toggle source

@param [Object] transaction

# File lib/mtn_cm_momo_sdk/api.rb, line 10
def initialize(transaction)
  @transaction = transaction.nil? ?
                     {
                         idbouton: 2,
                         typebouton: 'PAIE',
                         _clP: MtnCmMomoSdk.developer_clP,
                         _email: MtnCmMomoSdk.developer_email
                     } : transaction
end

Public Instance Methods

buy_now!(tel, amount) click to toggle source

@param [Object] amount

# File lib/mtn_cm_momo_sdk/api.rb, line 21
def buy_now!(tel, amount)
  da = format_data tel, amount
  call_server '/transactionRequest.xhtml', da
end
checkout!(tel, amount) click to toggle source
# File lib/mtn_cm_momo_sdk/api.rb, line 36
def checkout!(tel, amount)
  da = format_data(tel, amount)
  call_server '/transactionRequest.xhtml', da
end
donate!(tel, amount) click to toggle source
refund!(tel, amount) click to toggle source
# File lib/mtn_cm_momo_sdk/api.rb, line 26
def refund!(tel, amount)
  da = format_data(tel, amount)
  call_server '/transaction.xhtml', da
end

Private Instance Methods

call_server(url, data) click to toggle source
# File lib/mtn_cm_momo_sdk/api.rb, line 68
def call_server(url, data)
  puts MtnCmMomoSdk.check_ssl

  check = MtnCmMomoSdk.check_ssl.to_s.downcase == "true"

  resp = self.class.get(url, query: data,verify: check)
  if resp.parsed_response == "-1"
    {
        :request_status => false,
        :msg => 'the whole transaction failed',
        :server_respond => resp.parsed_response
    }
  else
    resp = JSON.parse resp.parsed_response
    if resp["StatusCode"] == "01"
      {
          :request_status => true,
          :msg => 'Successfully processed transaction',
          :server_respond => resp
      }
    else
      {
          :request_status => false,
          :msg => 'General failure',
          :server_respond => resp
      }
    end
  end

rescue Net::ReadTimeout
  {
      msg: 'User does not authorize the request',
      request_status: false,
      server_respond: nil

  }
rescue Net::OpenTimeout
  {
      msg: 'could not connect to mtn server, please check your internet connection',
      request_status: false,
      server_respond: nil

  }
rescue SocketError
  {
      msg: 'could not connect to mtn server, please check your internet connection',
      request_status: false,
      server_respond: nil

  }
rescue => ex
  {
      msg: ex.message,
      request_status: false,
      server_respond: nil

  }
end
format_data(tel, amount) click to toggle source
# File lib/mtn_cm_momo_sdk/api.rb, line 43
def format_data(tel, amount)
  tel = tel.gsub("+237", "")
  amount = amount.to_i.to_s
  da = body.merge!(_amount: amount)
           .merge!(_tel: tel)
end
headers() click to toggle source
# File lib/mtn_cm_momo_sdk/api.rb, line 64
def headers
  {'Content-Type' => 'application/json'}
end
payload(body) click to toggle source
# File lib/mtn_cm_momo_sdk/api.rb, line 49
def payload(body)
  options = {}
  options.merge!(body: body)
      .merge!(headers: headers)
end