Class: Payfast::OnsitePayments

Inherits:
Object
  • Object
show all
Defined in:
lib/payfast/onsite_payments.rb

Overview

Encaspulates the utils for makeing payfast payments using their onsite feature

Class Method Summary collapse

Class Method Details

.create_hash(data, algorithm) ⇒ Object



55
56
57
58
59
# File 'lib/payfast/onsite_payments.rb', line 55

def self.create_hash(data, algorithm)
  digest = Digest.const_get(algorithm.upcase).new
  digest.update(data)
  digest.hexdigest
end

.data_to_string(payload) ⇒ Object



51
52
53
# File 'lib/payfast/onsite_payments.rb', line 51

def self.data_to_string(payload)
  URI.encode_www_form(payload)
end

.generate_signature(payload) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/payfast/onsite_payments.rb', line 7

def self.generate_signature(payload)
  passphrase = Rails.application.config_for(:payfast).passphrase

  payload[:passphrase] = passphrase if passphrase

  url_encoded = data_to_string(payload)
  create_hash(url_encoded, 'md5')
end

.requestPayment(payload) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/payfast/onsite_payments.rb', line 16

def self.requestPayment(payload)
  payload_with_config = {
    merchant_id: Rails.application.config_for(:payfast).merchant_id,
    merchant_key: Rails.application.config_for(:payfast).merchant_key,
    return_url: Rails.application.config_for(:payfast).return_url,
    cancel_url: Rails.application.config_for(:payfast).cancel_url,
    notify_url: Rails.application.config_for(:payfast).notify_url
  }.merge(payload)

  puts payload_with_config

  signature = generate_signature(payload_with_config)
  payload_with_config[:signature] = signature

  pf_param_string = data_to_string(payload_with_config)
  base_url = Rails.application.config_for(:payfast).base_url

  puts pf_param_string

  uri = URI.parse(base_url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == 'https'

  request = Net::HTTP::Post.new(uri.path)
  request.body = pf_param_string

  begin
    response = http.request(request)
    JSON.parse(response.body)
  rescue StandardError => e
    puts "Error: #{e.message}"
    false
  end
end