Class: Liqpay::Request

Inherits:
SignedPayload show all
Defined in:
lib/liqpay/request.rb

Overview

Represends a request to the LiqPay API

Constant Summary collapse

REQUIRED_FIELDS =
%i[public_key amount currency description order_id action].freeze
OPTIONAL_FIELDS =
%i[result_url server_url].freeze
REQUEST_FIELDS =
(REQUIRED_FIELDS + OPTIONAL_FIELDS + %i[version]).freeze

Instance Attribute Summary collapse

Attributes inherited from SignedPayload

#private_key, #public_key

Instance Method Summary collapse

Methods inherited from SignedPayload

#signature

Constructor Details

#initialize(options = {}) ⇒ Request

Returns a new instance of Request.



28
29
30
31
32
33
34
35
# File 'lib/liqpay/request.rb', line 28

def initialize(options = {})
  super(options)

  (REQUIRED_FIELDS + OPTIONAL_FIELDS).each { |field| send("#{field}=", options[field]) }
  @action ||= Liqpay::ACTION_PAY

  @kamikaze = options[:kamikaze]
end

Instance Attribute Details

#actionObject

REQUIRED = either Liqpay::ACTION_PAY or Liqpay::ACTION_DONATE



22
23
24
# File 'lib/liqpay/request.rb', line 22

def action
  @action
end

#amountObject

REQUIRED Amount of payment (Float), in :currency



14
15
16
# File 'lib/liqpay/request.rb', line 14

def amount
  @amount
end

#currencyObject

REQUIRED Currency of payment - one of ‘Liqpay::SUPPORTED_CURRENCIES`



16
17
18
# File 'lib/liqpay/request.rb', line 16

def currency
  @currency
end

#descriptionObject

REQUIRED Description to be displayed to the user



18
19
20
# File 'lib/liqpay/request.rb', line 18

def description
  @description
end

#order_idObject

REQUIRED Arbitrary but unique ID



20
21
22
# File 'lib/liqpay/request.rb', line 20

def order_id
  @order_id
end

#result_urlObject

RECOMMENDED URL that the user will be redirected to after payment



24
25
26
# File 'lib/liqpay/request.rb', line 24

def result_url
  @result_url
end

#server_urlObject

RECOMMENDED URL that’ll receive the order details in the background.



26
27
28
# File 'lib/liqpay/request.rb', line 26

def server_url
  @server_url
end

Instance Method Details

#dataObject



41
42
43
44
45
46
47
48
49
# File 'lib/liqpay/request.rb', line 41

def data
  json_data = REQUEST_FIELDS
              .map { |field| [field, send(field)] }
              .to_h
              .reject { |_k, v| v.nil? }
              .transform_values(&:to_s)
  puts JSON.dump(json_data)
  @data ||= Base64.strict_encode64(JSON.dump(json_data)).strip
end

#form_fieldsObject



51
52
53
54
# File 'lib/liqpay/request.rb', line 51

def form_fields
  validate! unless @kamikaze
  { data: data, signature: signature }
end

#versionObject



37
38
39
# File 'lib/liqpay/request.rb', line 37

def version
  Liqpay::LIQPAY_API_VERSION
end