Class: Payfast::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payment_params) ⇒ Client

Returns a new instance of Client.



12
13
14
# File 'lib/payfast/client.rb', line 12

def initialize(payment_params)
  @payment_params = payment_params
end

Instance Attribute Details

#payment_paramsObject (readonly)

Returns the value of attribute payment_params.



6
7
8
# File 'lib/payfast/client.rb', line 6

def payment_params
  @payment_params
end

Class Method Details

.create_payment(payment_params) ⇒ Object



8
9
10
# File 'lib/payfast/client.rb', line 8

def self.create_payment(payment_params)
  new(payment_params).create_payment
end

Instance Method Details

#create_paymentObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/payfast/client.rb', line 16

def create_payment
  logger.info("[PAYFAST] STARTED POST to #{uri}")

  response = Net::HTTP.post(uri, payment_data, headers)

  if response.code =~ /^[4-5]/
    raise Payfast::PaymentError, "[PAYFAST] Error: Failed POST to #{uri}. HTTP #{response.code} - #{response.message}"
  else
    logger.info("[PAYFAST] COMPLETED POST to #{uri} - status: #{response.code}, message: #{response.message}")
  end

  endpoint_response = JSON.parse(response.body).transform_keys(&:to_sym)

  response_hash = { uuid: endpoint_response[:uuid], status: response.code, message: response.message }

  payment.new(payload.merge(response_hash))
end