Class: Prodamus::LinkConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/services/link_config.rb

Overview

Configure request wich be sent for get purchase link

Constant Summary collapse

DEFAULT_PAYMENTS_LIMIT =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ LinkConfig

Returns a new instance of LinkConfig.

Yields:

  • (_self)

Yield Parameters:



23
24
25
26
# File 'lib/services/link_config.rb', line 23

def initialize
  @products = []
  yield self if block_given?
end

Instance Attribute Details

#available_payment_methodsObject (readonly)

Returns the value of attribute available_payment_methods.



21
22
23
# File 'lib/services/link_config.rb', line 21

def available_payment_methods
  @available_payment_methods
end

#currencyObject

rubocop:disable Naming/MethodName



14
15
16
# File 'lib/services/link_config.rb', line 14

def currency
  @currency
end

#customer_emailObject

rubocop:disable Naming/MethodName



14
15
16
# File 'lib/services/link_config.rb', line 14

def customer_email
  @customer_email
end

#customer_extraObject

rubocop:disable Naming/MethodName



14
15
16
# File 'lib/services/link_config.rb', line 14

def customer_extra
  @customer_extra
end

#customer_phoneObject

rubocop:disable Naming/MethodName



14
15
16
# File 'lib/services/link_config.rb', line 14

def customer_phone
  @customer_phone
end

#form_access_durationObject



97
98
99
# File 'lib/services/link_config.rb', line 97

def form_access_duration
  @form_access_duration || 1.day
end

#order_idObject

rubocop:disable Naming/MethodName



14
15
16
# File 'lib/services/link_config.rb', line 14

def order_id
  @order_id
end

#payment_methodObject

rubocop:disable Naming/MethodName



14
15
16
# File 'lib/services/link_config.rb', line 14

def payment_method
  @payment_method
end

#payments_limitObject

rubocop:disable Naming/MethodName



14
15
16
# File 'lib/services/link_config.rb', line 14

def payments_limit
  @payments_limit
end

#productsObject (readonly)

Returns the value of attribute products.



21
22
23
# File 'lib/services/link_config.rb', line 21

def products
  @products
end

#sysObject

rubocop:disable Naming/MethodName



14
15
16
# File 'lib/services/link_config.rb', line 14

def sys
  @sys
end

#timezoneObject



101
102
103
# File 'lib/services/link_config.rb', line 101

def timezone
  @timezone || 'Moscow'
end

#urlNotificationObject

rubocop:disable Naming/MethodName



14
15
16
# File 'lib/services/link_config.rb', line 14

def urlNotification
  @urlNotification
end

#urlReturnObject

rubocop:disable Naming/MethodName



14
15
16
# File 'lib/services/link_config.rb', line 14

def urlReturn
  @urlReturn
end

#urlSuccessObject

rubocop:disable Naming/MethodName



14
15
16
# File 'lib/services/link_config.rb', line 14

def urlSuccess
  @urlSuccess
end

Instance Method Details

#add_available_payment_methods(*payment_methods) ⇒ Object

Raises:

  • (ArgumentError)


69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/services/link_config.rb', line 69

def add_available_payment_methods(*payment_methods)
  payment_methods.flatten!

  raise 'payment_method is already defined' if @payment
  raise ArgumentError, 'Not avaible payment methods.' unless (payment_methods - ALL_AVAIBLE_PAYMENT_METHODS).empty?

  if @installments_disabled&.positive? && !(payment_methods - NON_INSTALLMENTS_PAYMENT_METHODS).empty?
    raise ArgumentError, 'Installments payment methods is not avaible when installments disabled.'
  end

  @available_payment_methods = [] if @available_payment_methods.nil?
  @available_payment_methods += payment_methods
end

#add_product(name:, price:, quantity: 1, sku: nil) ⇒ Object

rubocop:enable Metrics/MethodLength



65
66
67
# File 'lib/services/link_config.rb', line 65

def add_product(name:, price:, quantity: 1, sku: nil)
  @products << Product.new(name: name, price: price, quantity: quantity, sku: sku)
end

#format_resultObject

Transform payload to be sent rubocop:disable Metrics/MethodLength



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/services/link_config.rb', line 30

def format_result
  raise 'Product must be exists' if @products.empty?

  result = {
    'do' => 'link',
    'callbackType' => 'json',
    'sys' => @sys,
    'order_id' => @order_id,
    'customer_phone' => @customer_phone,
    'customer_email' => @customer_email,
    'customer_extra' => @customer_extra,
    'payment_method' => @payment_method,
    'available_payment_methods' => (@available_payment_methods || NON_INSTALLMENTS_PAYMENT_METHODS).join('|'),
    'urlReturn' => @urlReturn,
    'urlSuccess' => @urlSuccess,
    'urlNotification' => @urlNotification,
    'installments_disabled' => (@installments_disabled || 1).to_s,
    'currency' => @currency,
    'payments_limit' => (@payments_limit || DEFAULT_PAYMENTS_LIMIT).to_s,
    'link_expired' => link_expired
  }.compact!

  @products.each_with_index do |product, index|
    result.merge!({
                    "products[#{index}][name]" => product.name,
                    "products[#{index}][sku]" => product.sku,
                    "products[#{index}][price]" => product.price,
                    "products[#{index}][quantity]" => product.quantity
                  })
  end

  result
end

#installments_disabledObject



87
88
89
# File 'lib/services/link_config.rb', line 87

def installments_disabled
  !@installments_disabled.zero?
end

#installments_disabled=(disabled) ⇒ Object



83
84
85
# File 'lib/services/link_config.rb', line 83

def installments_disabled=(disabled)
  @installments_disabled = disabled ? 1 : 0
end


91
92
93
94
95
# File 'lib/services/link_config.rb', line 91

def link_expired
  tz = ActiveSupport::TimeZone.new(timezone)
  date = tz.now + form_access_duration.to_i.seconds
  date.strftime('%Y-%m-%d %H:%M')
end