Class: Vpago::Payway::Base

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

Direct Known Subclasses

Checkout, 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/base.rb', line 4

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

Instance Method Details

#action_urlObject



107
108
109
# File 'lib/vpago/payway/base.rb', line 107

def action_url
  "#{host}#{endpoint}"
end

#amountObject



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

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

#api_keyObject



87
88
89
# File 'lib/vpago/payway/base.rb', line 87

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

#app_checkoutObject



56
57
58
# File 'lib/vpago/payway/base.rb', line 56

def app_checkout
  app_checkout? ? 'yes' : 'no'
end

#app_checkout?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
# File 'lib/vpago/payway/base.rb', line 60

def app_checkout?
  return false if @options[:app_checkout].blank?

  @options[:app_checkout]
end

#continue_success_urlObject



66
67
68
69
70
71
72
# File 'lib/vpago/payway/base.rb', line 66

def continue_success_url
  preferred_continue_url = @payment.payment_method.preferences[:continue_success_url]
  return nil if preferred_continue_url.blank?

  query_string = "tran_id=#{transaction_id}&app_checkout=#{app_checkout}"
  preferred_continue_url.index('?').nil? ? "#{preferred_continue_url}?#{query_string}" : "#{preferred_continue_url}&#{query_string}"
end

#emailObject



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

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

#endpointObject



103
104
105
# File 'lib/vpago/payway/base.rb', line 103

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

#first_nameObject



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

def first_name
  @payment.order.billing_address.first_name
end

#hash_hmacObject



95
96
97
98
99
100
101
# File 'lib/vpago/payway/base.rb', line 95

def hash_hmac
  data = "#{merchant_id}#{transaction_id}#{amount}"
  hash = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha512'), api_key, data))

  # somehow php counter part are not able to decode if the \n present.
  hash.delete("\n")
end

#hostObject



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

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

#last_nameObject



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

def last_name
  @payment.order.billing_address.last_name
end

#merchant_idObject



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

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

#payment_optionObject



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

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

#phoneObject



83
84
85
# File 'lib/vpago/payway/base.rb', line 83

def phone
  @payment.order.billing_address.phone
end

#phone_country_codeObject



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

def phone_country_code
  '+855'
end

#return_paramsObject



91
92
93
# File 'lib/vpago/payway/base.rb', line 91

def return_params
  { tran_id: transaction_id }.to_json
end

#return_urlObject



49
50
51
52
53
54
# File 'lib/vpago/payway/base.rb', line 49

def return_url
  preferred_return_url = @payment.payment_method.preferences[:return_url]
  return nil if preferred_return_url.blank?

  Base64.encode64(preferred_return_url)
end

#transaction_feeObject



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

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

#transaction_fee_fixObject



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

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

#transaction_fee_percentageObject



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

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

#transaction_idObject



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

def transaction_id
  @payment.number
end