Class: Vpago::TrueMoney::Base
- Inherits:
-
Object
- Object
- Vpago::TrueMoney::Base
show all
- Defined in:
- lib/vpago/true_money/base.rb
Constant Summary
collapse
{ '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_token ⇒ Object
46
47
48
|
# File 'lib/vpago/true_money/base.rb', line 46
def access_token
@access_token ||= fetch_access_token
end
|
#access_token_url ⇒ Object
88
89
90
|
# File 'lib/vpago/true_money/base.rb', line 88
def access_token_url
payment_method.preferred_access_token_url
end
|
#algorithm ⇒ Object
150
151
152
|
# File 'lib/vpago/true_money/base.rb', line 150
def algorithm
DEFAULT_ALGORITHM
end
|
#amount ⇒ Object
38
39
40
|
# File 'lib/vpago/true_money/base.rb', line 38
def amount
@payment.amount
end
|
#android_package_name ⇒ Object
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_url ⇒ Object
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_id ⇒ Object
14
15
16
|
# File 'lib/vpago/true_money/base.rb', line 14
def client_id
payment_method.preferred_client_id
end
|
#client_secret ⇒ Object
18
19
20
|
# File 'lib/vpago/true_money/base.rb', line 18
def client_secret
payment_method.preferred_client_secret
end
|
#currency ⇒ Object
35
|
# File 'lib/vpago/true_money/base.rb', line 35
def currency = 'USD'
|
66
67
68
69
70
71
72
73
74
|
# File 'lib/vpago/true_money/base.rb', line 66
def
{
'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_id ⇒ Object
30
31
32
|
# File 'lib/vpago/true_money/base.rb', line 30
def external_ref_id
@payment.number
end
|
#fetch_access_token ⇒ Object
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_url ⇒ Object
84
85
86
|
# File 'lib/vpago/true_money/base.rb', line 84
def generate_payment_url
payment_method.preferred_generate_payment_url
end
|
#jwt_payload ⇒ Object
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_version ⇒ Object
154
155
156
|
# File 'lib/vpago/true_money/base.rb', line 154
def key_version
DEFAULT_KEY_VERSION
end
|
#order ⇒ Object
138
139
140
|
# File 'lib/vpago/true_money/base.rb', line 138
def order
@payment.order
end
|
#order_jwt_token ⇒ Object
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
|
#payload ⇒ Object
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_method ⇒ Object
146
147
148
|
# File 'lib/vpago/true_money/base.rb', line 146
def payment_method
@payment.payment_method
end
|
#private_key ⇒ Object
22
23
24
|
# File 'lib/vpago/true_money/base.rb', line 22
def private_key
payment_method.preferred_private_key
end
|
#query_string ⇒ Object
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_type ⇒ Object
26
27
28
|
# File 'lib/vpago/true_money/base.rb', line 26
def redirect_type
payment_method.preferred_redirect_type
end
|
#refund_url ⇒ Object
80
81
82
|
# File 'lib/vpago/true_money/base.rb', line 80
def refund_url
payment_method.preferred_refund_url
end
|
#return_deeplink ⇒ Object
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_scheme ⇒ Object
118
119
120
|
# File 'lib/vpago/true_money/base.rb', line 118
def return_url_scheme
payment_method.preferred_return_url_scheme
end
|
#service_type ⇒ Object
34
|
# File 'lib/vpago/true_money/base.rb', line 34
def service_type = '01'
|
#signature ⇒ Object
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
|
158
159
160
|
# File 'lib/vpago/true_money/base.rb', line 158
def signature_input
"#{timestamp}#{payload.to_json}"
end
|
#timestamp ⇒ Object
42
43
44
|
# File 'lib/vpago/true_money/base.rb', line 42
def timestamp
@timestamp ||= Time.now.to_i
end
|
#user_type ⇒ Object
36
|
# File 'lib/vpago/true_money/base.rb', line 36
def user_type = 'CUSTOMER'
|