Class: Vpago::TrueMoney::Base

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

Direct Known Subclasses

Checkout, RefundIssuer, TransactionStatus

Constant Summary collapse

TOKEN_HEADERS =
{ 'Content-Type' => 'application/x-www-form-urlencoded' }.freeze
CONTENT_TYPE_JSON =
'application/json'.freeze
DEFAULT_ALGORITHM =
'rsa-sha256'.freeze
DEFAULT_KEY_VERSION =
1

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Base.



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

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

Instance Method Details

#access_tokenObject



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

def access_token
  @access_token ||= fetch_access_token
end

#access_token_urlObject



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

def access_token_url
  payment_method.preferred_access_token_url
end

#algorithmObject



150
151
152
# File 'lib/vpago/true_money/base.rb', line 150

def algorithm
  DEFAULT_ALGORITHM
end

#amountObject



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

def amount
  @payment.amount
end

#android_package_nameObject



92
93
94
# File 'lib/vpago/true_money/base.rb', line 92

def android_package_name
  payment_method.preferred_android_package_name
end

#check_transaction_urlObject



76
77
78
# File 'lib/vpago/true_money/base.rb', line 76

def check_transaction_url
  "#{payment_method.preferred_check_transaction_url}/#{external_ref_id}"
end

#client_idObject



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

def client_id
  payment_method.preferred_client_id
end

#client_secretObject



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

def client_secret
  payment_method.preferred_client_secret
end

#currencyObject



35
# File 'lib/vpago/true_money/base.rb', line 35

def currency     = 'USD'

#default_headersObject



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

def default_headers
  {
    'Client-Id' => client_id,
    'Authorization' => "Bearer #{access_token}",
    'Signature' => "algorithm=#{algorithm};keyVersion=#{key_version};signature=#{signature}",
    'Timestamp' => timestamp.to_s,
    'Content-Type' => CONTENT_TYPE_JSON
  }
end

#external_ref_idObject



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

def external_ref_id
  @payment.number
end

#fetch_access_tokenObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/vpago/true_money/base.rb', line 102

def fetch_access_token
  response = Faraday.post(
    access_token_url,
    URI.encode_www_form(
      grant_type: 'client_credentials',
      client_id: client_id,
      client_secret: client_secret
    ),
    TOKEN_HEADERS
  )

  raise "Access Token Error: #{response.status} - #{response.body}" unless response.success?

  JSON.parse(response.body).fetch('access_token')
end

#generate_payment_urlObject



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

def generate_payment_url
  payment_method.preferred_generate_payment_url
end

#jwt_payloadObject



142
143
144
# File 'lib/vpago/true_money/base.rb', line 142

def jwt_payload
  { order_number: order.number, order_id: order.id }
end

#key_versionObject



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

def key_version
  DEFAULT_KEY_VERSION
end

#orderObject



138
139
140
# File 'lib/vpago/true_money/base.rb', line 138

def order
  @payment.order
end

#order_jwt_tokenObject



134
135
136
# File 'lib/vpago/true_money/base.rb', line 134

def order_jwt_token
  @order_jwt_token ||= JWT.encode(jwt_payload, order.token, 'HS256')
end

#parse_json(body) ⇒ Object



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

def parse_json(body)
  JSON.parse(body)
rescue JSON::ParserError
  {}
end

#payloadObject



50
51
52
53
54
55
56
57
58
# File 'lib/vpago/true_money/base.rb', line 50

def payload
  {
    service_type: service_type,
    external_ref_id: external_ref_id,
    amount: amount,
    currency: currency,
    user_type: user_type
  }
end

#payment_methodObject



146
147
148
# File 'lib/vpago/true_money/base.rb', line 146

def payment_method
  @payment.payment_method
end

#private_keyObject



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

def private_key
  payment_method.preferred_private_key
end

#query_stringObject



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

def query_string
  {
    payment_number: external_ref_id,
    order_number: order.number,
    order_jwt_token: order_jwt_token
  }.to_param
end

#redirect_typeObject



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

def redirect_type
  payment_method.preferred_redirect_type
end

#refund_urlObject



80
81
82
# File 'lib/vpago/true_money/base.rb', line 80

def refund_url
  payment_method.preferred_refund_url
end


122
123
124
# File 'lib/vpago/true_money/base.rb', line 122

def return_deeplink
  "#{return_url_scheme}vpago_payments/processing?#{query_string}"
end

#return_url_schemeObject



118
119
120
# File 'lib/vpago/true_money/base.rb', line 118

def return_url_scheme
  payment_method.preferred_return_url_scheme
end

#service_typeObject



34
# File 'lib/vpago/true_money/base.rb', line 34

def service_type = '01'

#signatureObject



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

def signature
  Vpago::RsaHandler
    .new(private_key: private_key)
    .generate_signature(signature_input)
end

#signature_inputObject



158
159
160
# File 'lib/vpago/true_money/base.rb', line 158

def signature_input
  "#{timestamp}#{payload.to_json}"
end

#timestampObject



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

def timestamp
  @timestamp ||= Time.now.to_i
end

#user_typeObject



36
# File 'lib/vpago/true_money/base.rb', line 36

def user_type    = 'CUSTOMER'