Class: Vpago::PaywayV2::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/vpago/payway_v2/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(payment, options = {}) ⇒ Base

Returns a new instance of Base.



4
5
6
7
8
# File 'lib/vpago/payway_v2/base.rb', line 4

def initialize(payment, options = {})
  @options = options
  @payment = payment
  @disable_return_deeplink = options[:disable_return_deeplink] || false
end

Instance Method Details

#amountObject



26
27
28
# File 'lib/vpago/payway_v2/base.rb', line 26

def amount
  format('%.2f', @payment.amount + transaction_fee)
end

#api_keyObject



88
89
90
# File 'lib/vpago/payway_v2/base.rb', line 88

def api_key
  @payment.payment_method.preferences[:api_key]
end

#continue_success_urlObject



70
71
72
# File 'lib/vpago/payway_v2/base.rb', line 70

def continue_success_url
  @payment.processing_url
end

#emailObject

optional



51
52
53
# File 'lib/vpago/payway_v2/base.rb', line 51

def email
  @payment.order.email.presence || ENV.fetch('DEFAULT_EMAIL_FOR_PAYMENT', nil)
end

#first_nameObject



55
56
57
# File 'lib/vpago/payway_v2/base.rb', line 55

def first_name
  @payment.order.billing_address&.first_name&.strip
end

#hash_dataObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/vpago/payway_v2/base.rb', line 143

def hash_data
  result = "#{req_time}#{merchant_id}#{transaction_id}#{amount}#{first_name}#{last_name}"

  result += email if email.present?
  result += phone if phone.present?
  result += type if type.present?
  result += payment_option if payment_option.present?
  result += return_url if return_url.present?
  result += continue_success_url if continue_success_url.present?
  result += return_deeplink if return_deeplink.present?
  result += return_params if return_params.present?
  result += payout if payout.present?
  result += skip_success_page.to_s if skip_success_page.present?

  log_hash_data = "Hash data: #{result}"
  Rails.logger.info(log_hash_data)

  result
end

#hash_hmacObject



134
135
136
137
138
139
140
141
# File 'lib/vpago/payway_v2/base.rb', line 134

def hash_hmac
  hash = Base64.strict_encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha512'), api_key, hash_data))

  log_hash_data = "Hash hmac: #{hash}"
  Rails.logger.info(log_hash_data)

  hash
end

#hostObject



18
19
20
# File 'lib/vpago/payway_v2/base.rb', line 18

def host
  @payment.payment_method.preferences[:host]
end

#last_nameObject



59
60
61
# File 'lib/vpago/payway_v2/base.rb', line 59

def last_name
  @payment.order.billing_address&.last_name&.strip
end

#merchant_idObject



42
43
44
# File 'lib/vpago/payway_v2/base.rb', line 42

def merchant_id
  @payment.payment_method.preferences[:merchant_id]
end

#order_jwt_tokenObject



163
164
165
166
# File 'lib/vpago/payway_v2/base.rb', line 163

def order_jwt_token
  payload = { order_number: @payment.order.number, order_id: @payment.order.id }
  JWT.encode(payload, @payment.order.token, 'HS256')
end

#payment_optionObject



79
80
81
# File 'lib/vpago/payway_v2/base.rb', line 79

def payment_option
  @payment.payment_method.preferences[:payment_option]
end

#payoutObject



98
99
100
101
102
103
# File 'lib/vpago/payway_v2/base.rb', line 98

def payout
  @payout ||= begin
    payouts = Vpago::PaywayV2::PayoutsParamsConstructor.new(@payment).call
    payouts.empty? ? nil : Base64.strict_encode64(payouts.to_json)
  end
end

#phoneObject

optional



84
85
86
# File 'lib/vpago/payway_v2/base.rb', line 84

def phone
  @payment.order.billing_address&.phone
end

#public_keyObject



22
23
24
# File 'lib/vpago/payway_v2/base.rb', line 22

def public_key
  @payment.payment_method.preferences[:public_key]
end

#req_timeObject



10
11
12
# File 'lib/vpago/payway_v2/base.rb', line 10

def req_time
  @payment.created_at.strftime('%Y%m%d%H%M%S')
end


128
129
130
131
132
# File 'lib/vpago/payway_v2/base.rb', line 128

def return_deeplink
  return nil unless return_deeplink_url.present?

  Base64.strict_encode64({ android_scheme: return_deeplink_url, ios_scheme: return_deeplink_url }.to_json)
end


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/vpago/payway_v2/base.rb', line 105

def return_deeplink_url
  # In Telegram, Facebook, and Messenger webviews, there is no reliable way for
  # ABA to detect the originating app. Using `continue_success_url` may open the
  # default browser instead of the intended app (e.g., Telegram or Facebook),
  # resulting in a poor user experience.
  #
  # To prevent ABA from returning to the wrong app, we allow returning nil by
  # setting `disable_return_deeplink: true`:
  # - Android: ABA app automatically returns to the previous app (expected behavior)
  # - iOS: no automatic redirect occurs, but users can tap "Back to <App>" at the top of ABA
  return nil if @disable_return_deeplink

  app_scheme = @payment.payment_method.preferences[:app_scheme]

  if app_scheme.present?
    uri = URI.parse(continue_success_url)
    uri.scheme = app_scheme
    uri.to_s
  else
    continue_success_url
  end
end

#return_paramsObject



92
93
94
95
96
# File 'lib/vpago/payway_v2/base.rb', line 92

def return_params
  uri = URI.parse(@payment.process_payment_url)
  query_params = Rack::Utils.parse_nested_query(uri.query)
  query_params.to_json
end

#return_urlObject



63
64
65
66
67
68
# File 'lib/vpago/payway_v2/base.rb', line 63

def return_url
  uri = URI.parse(@payment.process_payment_url)
  uri.query = nil

  Base64.encode64(uri.to_s).delete("\n")
end

#skip_success_pageObject



168
169
170
# File 'lib/vpago/payway_v2/base.rb', line 168

def skip_success_page
  1
end

#transaction_feeObject



38
39
40
# File 'lib/vpago/payway_v2/base.rb', line 38

def transaction_fee
  transaction_fee_fix + ((@payment.amount * transaction_fee_percentage) / 100)
end

#transaction_fee_fixObject



30
31
32
# File 'lib/vpago/payway_v2/base.rb', line 30

def transaction_fee_fix
  @payment.payment_method.preferences[:transaction_fee_fix].to_f
end

#transaction_fee_percentageObject



34
35
36
# File 'lib/vpago/payway_v2/base.rb', line 34

def transaction_fee_percentage
  @payment.payment_method.preferences[:transaction_fee_percentage].to_f
end

#transaction_idObject



46
47
48
# File 'lib/vpago/payway_v2/base.rb', line 46

def transaction_id
  @payment.number
end

#typeObject



14
15
16
# File 'lib/vpago/payway_v2/base.rb', line 14

def type
  @payment.payment_method.enable_pre_auth ? 'pre-auth' : 'purchase'
end

#view_typeObject

null, hosted_view, checkout, qr



75
76
77
# File 'lib/vpago/payway_v2/base.rb', line 75

def view_type
  'hosted_view'
end