lib/paynow_sdk.rb in paynow_sdk-0.1.9 vs lib/paynow_sdk.rb in paynow_sdk-0.2.0

- old
+ new

@@ -2,10 +2,11 @@ require "cgi" require "digest" require "httparty" require "uri" require "rest-client" +require "net/http" #throws error when hash from Paynow does not match locally generated hash class HashMismatchException < Exception def initialize(message) @@ -44,45 +45,45 @@ @hash = data["hash"] end end end - def self.paid; @@paid; end - def self.paid=(val); @@paid = val; end + # def self.paid; @@paid; end + # def self.paid=(val); @@paid = val; end - def paid; @paid = @@paid if @paid.nil?; @paid; end - def paid=(val); @paid = val; end + # def paid; @paid = @@paid if @paid.nil?; @paid; end + # def paid=(val); @paid = val; end - def self.status; @@status; end - def self.status=(val); @@status = val; end + # def self.status; @@status; end + # def self.status=(val); @@status = val; end - def status; @status = @@status if @status.nil?; @status; end - def status=(val); @status = val; end + # def status; @status = @@status if @status.nil?; @status; end + # def status=(val); @status = val; end - def self.amount; @@amount; end - def self.amount=(val); @@amount = val; end + # def self.amount; @@amount; end + # def self.amount=(val); @@amount = val; end - def amount; @amount = @@amount if @amount.nil?; @amount; end - def amount=(val); @amount = val; end + # def amount; @amount = @@amount if @amount.nil?; @amount; end + # def amount=(val); @amount = val; end - def self.reference; @@reference; end - def self.reference=(val); @@reference = val; end + # def self.reference; @@reference; end + # def self.reference=(val); @@reference = val; end - def reference; @reference = @@reference if @reference.nil?; @reference; end - def reference=(val); @reference = val; end + # def reference; @reference = @@reference if @reference.nil?; @reference; end + # def reference=(val); @reference = val; end - def self.paynow_reference; @@paynow_reference; end - def self.paynow_reference=(val); @@paynow_reference = val; end + # def self.paynow_reference; @@paynow_reference; end + # def self.paynow_reference=(val); @@paynow_reference = val; end - def paynow_reference; @paynow_reference = @@paynow_reference if @paynow_reference.nil?; @paynow_reference; end - def paynow_reference=(val); @paynow_reference = val; end + # def paynow_reference; @paynow_reference = @@paynow_reference if @paynow_reference.nil?; @paynow_reference; end + # def paynow_reference=(val); @paynow_reference = val; end - def self.hash; @@hash; end - def self.hash=(val); @@hash = val; end + # def self.hash; @@hash; end + # def self.hash=(val); @@hash = val; end - def hash; @hash = @@hash if @hash.nil?; @hash; end - def hash=(val); @hash = val; end + # def hash; @hash = @@hash if @hash.nil?; @hash; end + # def hash=(val); @hash = val; end end class InitResponse @@success = true @@instructions = "" @@ -95,11 +96,11 @@ def initialize(data) @status = data["status"] @success = data["status"].downcase != "error" @has_redirect = data.include?("browserurl") @hash = data.include?("hash") - if !@success + if @success @poll_url = data["pollurl"] end if !@success @error = data["error"] end @@ -169,15 +170,15 @@ @items.push([title, amount]) self end def total - total = 0.0 + total = 0 for item in @items - total += item[1].to_f + total += item[1] end - total + total.round(2) end def info out = "" for item in @items @@ -242,40 +243,27 @@ def process_status_update(data) StatusResponse.new(data, true) end - # def qs_to_hash(querystring) - # keyvals = querystring.split("&").inject({}) do |result, q| - # k, v = q.split("=") - # if !v.nil? - # result.merge({ k => v }) - # elsif !result.key?(k) - # result.merge({ k => true }) - # else - # result - # end - # end - # keyvals - # end - def init(payment) if payment.total <= 0 raise TypeError, "Transaction total cannot be less than 1" end - data = build(payment) + puts data = build(payment) - response = RestClient.post("https://www.paynow.co.zw/interface/initiatetransaction/", data) - response_object = rebuild_response(response) + p response = HTTParty.post("https://www.paynow.co.zw/interface/initiatetransaction?#{data.slice(1, -1)}") - if response_object["status"].to_s.downcase == "error" - InitResponse.new(response_object) - end - # if !verify_hash(response_object, @integration_key) + # response_object = rebuild_response(response) + + # 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) + # InitResponse.new(response_object) end # def init_mobile(payment, phone, method) # if payment.total <= 0 # raise TypeError, "Transaction total cannot be less than 1" @@ -300,39 +288,61 @@ response_object = rebuild_response(response) StatusResponse.new(response_object, false) end def build(payment) - body = { "resulturl": @result_url, "returnurl": @return_url, "reference": payment.reference, "amount": payment.total, "id": @integration_id, "additionalinfo": payment.info, "authemail": payment.auth_email || "", "status": "Message" } + body = { + "id": @integration_id, + "reference": payment.reference, + "amount": payment.total, + "additionalinfo": payment.info, + "returnurl": @return_url, + "resulturl": @result_url, + "authemail": payment.auth_email, + "status": "Message", + } + joined = body.values.join.to_s add_key = joined += @integration_key - body["hash"] = createdhash(add_key) + body["hash"] = createdhash(joined) body = URI.encode_www_form(body) - body + CGI.escape(body) end 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.to_s add_key = joined += @integration_key body["hash"] = createdhash(add_key) - body = URI.encode_www_form(body) + # body = URI.encode_www_form(body) body end def createdhash(out) Digest::SHA2.new(512).hexdigest(out).upcase end #verify the hash send to paynow is equal to the hash from paynow - # 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 = createdhash(response) - # old_hash == new_hash - # end + 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 + + 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 # rebuild a response from paynow into hash like the we send def rebuild_response(response) URI.decode_www_form(response).to_h