Integrating Payfort Payment with Ruby On Rails App

2019-09-14 01:21发布

问题:

I am trying to implement the Payfort payment gateway with rails app. But i am getting following response message:

"response_message":"Signature mismatch"

Following is my try:

params = {command: "AUTHORIZATION",
            currency: "USD",
            access_code: "z7TfXF2xxxxxxxxxxxx",
            merchant_identifier: "xoNbjDoq",
            merchant_reference: "405",
            language: "en",
            amount: 250,
            token_name: "token_is_here",
            expiry_date: "07/2023",
            card_number: "5200421234563432",
            card_security_code: "417",
            card_holder_name: "Abc Xyz",
            remember_me: "YES",
            return_url: "http://lvh.me:3000/payments/test"}
params = params.except(:card_security_code, :card_number, :expiry_date, :card_holder_name, :remember_me)

    params = params.sort.to_h
    string = params.to_query(nil)
    string = string.gsub! '&', ''
    string = @@sha_request + string + @@sha_request
    string = Digest::SHA256.hexdigest string
uri = URI.parse("https://sbpaymentservices.payfort.com/FortAPI/paymentApi")

    header = {'Content-Type': 'application/json'}

    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    request = Net::HTTP::Post.new(uri.request_uri, header)
    request.body = params.to_json

    response = http.request(request)

回答1:

Check sequence of parameters while generating signature. and check for algorithm which u have setup in account and use same algorithm while generating signature

Or else try using their gem

https://github.com/payfort/start-ruby



回答2:

there may be many reasons for such issue one of them is rails form params and also hashing algorithm, here's my implementation for it

def sign_with_key(params, key)
    string_to_digest = params.sort { |a, b| a[0].upcase <=> b[0].upcase }.map { |k, v| "#{k}=#{v}" }.join()
    string_to_digest.prepend(key)
    string_to_digest << key
    "Digest::#{@options[:sha].upcase}".constantize.hexdigest(string_to_digest)
  end