class Paynow::PaymentBuilder

Constants

PAYMENT_METHOD_TYPES

Public Class Methods

build(params) click to toggle source
# File lib/paynow/payment_builder.rb, line 5
def self.build(params)
  new.build(params)
end

Public Instance Methods

build(attrs) click to toggle source
# File lib/paynow/payment_builder.rb, line 9
def build(attrs)
  payment = { id: id, returnurl: returnurl, resulturl: resulturl, status: "Message"}

  payment.merge!(
    required_info(attrs),
    optional_info(attrs)
  )

  payment.tap do |p|
    p.delete(:method) if p[:method] == pay_with_paynow
    p.merge!(card_info(attrs), optional_card_info(attrs)) if p[:method] == visa_or_mastercard
    p.merge!(mobile_info(attrs)) if p[:method] == mobile_money
  end
end

Private Instance Methods

card_info(attrs) click to toggle source
# File lib/paynow/payment_builder.rb, line 64
def card_info(attrs)
  data = {}

  Paynow::PaymentAttributes::CARD.each do |attr|
    data.merge!({attr => attrs.fetch(attr)})
  end

  data
end
id() click to toggle source
# File lib/paynow/payment_builder.rb, line 28
def id
  Paynow.integration_id
end
merchanttrace() click to toggle source
# File lib/paynow/payment_builder.rb, line 32
def merchanttrace
  SecureRandom.alphanumeric(32)
end
mobile_info(attrs) click to toggle source
# File lib/paynow/payment_builder.rb, line 84
def mobile_info(attrs)
  data = {}

  Paynow::PaymentAttributes::MOBILE.each do |attr|
    data.merge!({attr => attrs.fetch(attr)})
  end

  data
end
mobile_money() click to toggle source
# File lib/paynow/payment_builder.rb, line 104
def mobile_money
  "ecocash" || "onemoney"
end
optional_card_info(attrs) click to toggle source
# File lib/paynow/payment_builder.rb, line 74
def optional_card_info(attrs)
  data = {}

  Paynow::PaymentAttributes::OPTIONAL_CARD.each do |attr|
    data.merge!({attr => attrs.fetch(attr)}) if attrs.has_key?(attr)
  end

  data
end
optional_info(attrs) click to toggle source
# File lib/paynow/payment_builder.rb, line 54
def optional_info(attrs)
  data = {}

  Paynow::PaymentAttributes::OPTIONAL.each do |attr|
    data.merge!({attr => attrs.fetch(attr)}) if attrs.has_key?(attr)
  end

  data
end
pay_with_paynow() click to toggle source
# File lib/paynow/payment_builder.rb, line 112
def pay_with_paynow
  "pay_with_paynow"
end
payment_method(attrs) click to toggle source
# File lib/paynow/payment_builder.rb, line 94
def payment_method(attrs)
  if attrs.has_key?(:method) && !PAYMENT_METHOD_TYPES.include?(attrs[:method])
    raise Paynow::UnknownAttributeError, "Payment method should either be ecocash, onemoney, vmc or other"
  elsif !attrs.has_key?(:method)
    raise Paynow::MissingAttributeError, "Payment method is missing"
  else
    attrs.fetch(:method)
  end
end
required_info(attrs) click to toggle source
# File lib/paynow/payment_builder.rb, line 44
def required_info(attrs)
  data = {}

  Paynow::PaymentAttributes::REQUISITE.each do |attr|
    data.merge!({attr => attrs.fetch(attr)})
  end
  
  data.update(amount: sprintf('%.2f',attrs[:amount]),method: payment_method(attrs))
end
resulturl() click to toggle source
# File lib/paynow/payment_builder.rb, line 36
def resulturl
  Paynow.resulturl
end
returnurl() click to toggle source
# File lib/paynow/payment_builder.rb, line 40
def returnurl
  Paynow.returnurl
end
visa_or_mastercard() click to toggle source
# File lib/paynow/payment_builder.rb, line 108
def visa_or_mastercard
  "vmc"
end