Class: Vpago::PaywayV2::Base

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

Direct Known Subclasses

Checkout, PreAuthCompleter, TransactionStatus

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Base.



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

def initialize(payment, options = {})
  @options = options
  @payment = payment
end

Instance Method Details

#amountObject



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

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



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

def continue_success_url
  @payment.processing_url
end

#emailObject

optional



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

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

#first_nameObject



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

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

#hash_dataObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/vpago/payway_v2/base.rb', line 134

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?

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

  result
end

#hash_hmacObject



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

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



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

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

#last_nameObject



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

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

#merchant_idObject



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

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

#order_jwt_tokenObject



153
154
155
156
# File 'lib/vpago/payway_v2/base.rb', line 153

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



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

def payment_option
  card_option = @payment.payment_method.preferences[:payment_option]
  Vpago::Payway::CARD_TYPES.index(card_option).nil? ? Vpago::Payway::CARD_TYPE_ABAPAY : card_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



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

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

#req_timeObject



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

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


119
120
121
122
123
# File 'lib/vpago/payway_v2/base.rb', line 119

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

redirect to continue URL, let it handle redirecting to app. allowed override to specific app eg. from tg://t.me



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/vpago/payway_v2/base.rb', line 107

def return_deeplink_url
  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



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

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

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

#transaction_feeObject



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

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

#transaction_fee_fixObject



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

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

#transaction_fee_percentageObject



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

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

#transaction_idObject



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

def transaction_id
  @payment.number
end

#typeObject



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

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

#view_typeObject

null, hosted_view, checkout, qr



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

def view_type
  'hosted_view'
end