class Paynow

Public Class Methods

integration_id() click to toggle source
# File lib/paynow_sdk.rb, line 410
def self.integration_id; @@integration_id; end
integration_id=(val) click to toggle source
# File lib/paynow_sdk.rb, line 411
def self.integration_id=(val); @@integration_id = val; end
integration_key() click to toggle source
# File lib/paynow_sdk.rb, line 416
def self.integration_key; @@integration_key; end
integration_key=(val) click to toggle source
# File lib/paynow_sdk.rb, line 417
def self.integration_key=(val); @@integration_key = val; end
new(integration_id, integration_key, return_url, result_url) click to toggle source
# File lib/paynow_sdk.rb, line 226
def initialize(integration_id, integration_key, return_url, result_url)
  @integration_id = integration_id
  @integration_key = integration_key
  @return_url = return_url
  @result_url = result_url
end
result_url() click to toggle source
# File lib/paynow_sdk.rb, line 428
def self.result_url; @@result_url; end
result_url=(val) click to toggle source
# File lib/paynow_sdk.rb, line 429
def self.result_url=(val); @@result_url = val; end
return_url() click to toggle source
# File lib/paynow_sdk.rb, line 422
def self.return_url; @@return_url; end
return_url=(val) click to toggle source
# File lib/paynow_sdk.rb, line 423
def self.return_url=(val); @@return_url = val; end
url_initiate_mobile_transaction() click to toggle source
# File lib/paynow_sdk.rb, line 404
def self.url_initiate_mobile_transaction; @@url_initiate_mobile_transaction; end
url_initiate_mobile_transaction=(val) click to toggle source
# File lib/paynow_sdk.rb, line 405
def self.url_initiate_mobile_transaction=(val); @@url_initiate_mobile_transaction = val; end
url_initiate_transaction() click to toggle source
# File lib/paynow_sdk.rb, line 398
def self.url_initiate_transaction; @@url_initiate_transaction; end
url_initiate_transaction=(val) click to toggle source
# File lib/paynow_sdk.rb, line 399
def self.url_initiate_transaction=(val); @@url_initiate_transaction = val; end

Public Instance Methods

build(payment) click to toggle source

web payments

# File lib/paynow_sdk.rb, line 324
def build(payment)
  body = {
    "id": @integration_id,
    "reference": payment.reference,
    "amount": payment.total,
    "additionalinfo": payment.info,
    "returnurl": @return_url,
    "resulturl": @result_url,
    "authemail": payment.auth_email,
    "status": "Payment for goods",
  }

  joined = body.values.join
  add_key = joined += @integration_key
  body["hash"] = createdhash(add_key)
  body = URI.encode_www_form(body)
  body
end
build_mobile(payment, phone, method) click to toggle source

mobile payments

# File lib/paynow_sdk.rb, line 345
def build_mobile(payment, phone, method)
  body = {
    "resulturl": @result_url,
    "returnurl": @return_url,
    "reference": payment.reference,
    "amount": payment.total,
    "id": @integration_id,
    "additionalinfo": payment.info,
    "authemail": payment.auth_email,
    "phone": phone,
    "method": method,
    "status": "Message",
  }

  joined = body.values.join
  add_key = joined += @integration_key
  body["hash"] = createdhash(add_key)
  body = URI.encode_www_form(body)
  body
end
create_payment(reference, auth_email) click to toggle source
# File lib/paynow_sdk.rb, line 241
def create_payment(reference, auth_email)
  Payment.new(reference, auth_email)
end
createdhash(out) click to toggle source
# File lib/paynow_sdk.rb, line 366
def createdhash(out)
  Digest::SHA2.new(512).hexdigest(out).upcase
end
init(payment) click to toggle source
# File lib/paynow_sdk.rb, line 257
def init(payment)
  if payment.total <= 0
    raise TypeError, "Transaction total cannot be less than 1"
  end

  data = build(payment)

  url = URI("https://www.paynow.co.zw/interface/initiatetransaction/")

  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/x-www-form-urlencoded"
  request.body = data

  response = http.request(request)
  response.read_body

  response_object = rebuild_response(response.read_body)

  if response_object["status"].to_s.downcase == "error"
    InitResponse.new(response_object)
  end
  if !verify_hash(response_object)
    raise HashMismatchException, "Hashes do not match"
  end
  InitResponse.new(response_object)
end
init_mobile(payment, phone, method) click to toggle source
# File lib/paynow_sdk.rb, line 288
def init_mobile(payment, phone, method)
  if payment.total <= 0
    raise TypeError, "Transaction total cannot be less than 1"
  end
  if !payment.auth_email || payment.auth_email.size <= 0
    raise TypeError, "Auth email is required for mobile transactions. You can pass the auth email as the second parameter in the create_payment method call"
  end

  data = build_mobile(payment, phone, method)

  url = URI("https://www.paynow.co.zw/interface/remotetransaction")

  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/x-www-form-urlencoded"
  request.body = data

  response = http.request(request)
  response.read_body

  response_object = rebuild_response(response.read_body)

  if response_object["status"].to_s.downcase == "error"
    InitResponse.new(response_object)
  end
  if !verify_hash(response_object)
    raise HashMismatchException, "Hashes do not match"
  end
  InitResponse.new(response_object)
end
integration_id() click to toggle source
# File lib/paynow_sdk.rb, line 413
def integration_id; @integration_id = @@integration_id if @integration_id.nil?; @integration_id; end
integration_id=(val) click to toggle source
# File lib/paynow_sdk.rb, line 414
def integration_id=(val); @integration_id = val; end
integration_key() click to toggle source
# File lib/paynow_sdk.rb, line 419
def integration_key; @integration_key = @@integration_key if @integration_key.nil?; @integration_key; end
integration_key=(val) click to toggle source
# File lib/paynow_sdk.rb, line 420
def integration_key=(val); @integration_key = val; end
process_status_update(data) click to toggle source
# File lib/paynow_sdk.rb, line 253
def process_status_update(data)
  StatusResponse.new(data, true)
end
rebuild_response(response) click to toggle source

rebuild a response from paynow into hash like the we send

# File lib/paynow_sdk.rb, line 394
def rebuild_response(response)
  URI.decode_www_form(response).to_h
end
result_url() click to toggle source
# File lib/paynow_sdk.rb, line 431
def result_url; @result_url = @@result_url if @result_url.nil?; @result_url; end
result_url=(val) click to toggle source
# File lib/paynow_sdk.rb, line 432
def result_url=(val); @result_url = val; end
return_url() click to toggle source
# File lib/paynow_sdk.rb, line 425
def return_url; @return_url = @@return_url if @return_url.nil?; @return_url; end
return_url=(val) click to toggle source
# File lib/paynow_sdk.rb, line 426
def return_url=(val); @return_url = val; end
send(payment) click to toggle source
# File lib/paynow_sdk.rb, line 245
def send(payment)
  init(payment)
end
send_mobile(payment, phone, method) click to toggle source
# File lib/paynow_sdk.rb, line 249
def send_mobile(payment, phone, method)
  init_mobile(payment, phone, method)
end
set_result_url(url) click to toggle source
# File lib/paynow_sdk.rb, line 233
def set_result_url(url)
  @result_url = url
end
set_return_url(url) click to toggle source
# File lib/paynow_sdk.rb, line 237
def set_return_url(url)
  @return_url = url
end
url_initiate_mobile_transaction() click to toggle source
# File lib/paynow_sdk.rb, line 407
def url_initiate_mobile_transaction; @url_initiate_mobile_transaction = @@url_initiate_mobile_transaction if @url_initiate_mobile_transaction.nil?; @url_initiate_mobile_transaction; end
url_initiate_mobile_transaction=(val) click to toggle source
# File lib/paynow_sdk.rb, line 408
def url_initiate_mobile_transaction=(val); @url_initiate_mobile_transaction = val; end
url_initiate_transaction() click to toggle source
# File lib/paynow_sdk.rb, line 401
def url_initiate_transaction; @url_initiate_transaction = @@url_initiate_transaction if @url_initiate_transaction.nil?; @url_initiate_transaction; end
url_initiate_transaction=(val) click to toggle source
# File lib/paynow_sdk.rb, line 402
def url_initiate_transaction=(val); @url_initiate_transaction = val; end
verify(item) click to toggle source
# File lib/paynow_sdk.rb, line 380
def verify(item)
  out = ""
  for key, value in item
    if key.to_s == "hash"
      next
    end
    out += value.to_s
  end
  out += @integration_key.downcase
  Digest::SHA2.new(512).hexdigest(out).upcase
end
verify_hash(response) click to toggle source

verify the hash send to paynow is equal to the hash from paynow

# File lib/paynow_sdk.rb, line 371
def verify_hash(response)
  if !response.include?("hash")
    raise TypeError, "Response from Paynow does not contain a hash"
  end
  old_hash = response["hash"]
  new_hash = verify(response)
  old_hash == new_hash
end