smartpay 0.2.17 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +13 -13
- data/Gemfile +2 -0
- data/lib/generators/smartpay/templates/controller.rb +2 -2
- data/lib/smartpay/api.rb +42 -13
- data/lib/smartpay/client.rb +53 -45
- data/lib/smartpay/configuration.rb +48 -48
- data/lib/smartpay/requests/checkout_session.rb +140 -209
- data/lib/smartpay/requests/payment.rb +39 -0
- data/lib/smartpay/requests/refund.rb +40 -0
- data/lib/smartpay/responses/base.rb +21 -0
- data/lib/smartpay/responses/checkout_session.rb +30 -40
- data/lib/smartpay/version.rb +1 -1
- data/lib/smartpay.rb +31 -25
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 794e3de526f66e64a6c05638932f2c3c8a3cb7bcea3708f8df266ad9f02e7d4a
|
4
|
+
data.tar.gz: f04b6f575b3be6e2320336a343fa9db842560654c5c7076d6b7bf07b2fcdab70
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54c1bd5ae7e587b67409e4eb9eed4c50d36690f09958e9003f3b9e9aadf927c429a41dc8c7b9fb18581f5594653f15fa0b3271896486f1276b07c1dd022f8910
|
7
|
+
data.tar.gz: 7cefc78c6463a95317d3f87abfc3e9ccc83cf5aeac7f2efed30194fba42faa2069e8e35ce695720ed9a83cdb523cec27a494b0611d51cee90d92bd62ecefd0a9
|
data/.rubocop.yml
CHANGED
@@ -1,13 +1,13 @@
|
|
1
|
-
AllCops:
|
2
|
-
TargetRubyVersion: 2.5
|
3
|
-
|
4
|
-
Style/StringLiterals:
|
5
|
-
Enabled: true
|
6
|
-
EnforcedStyle: double_quotes
|
7
|
-
|
8
|
-
Style/StringLiteralsInInterpolation:
|
9
|
-
Enabled: true
|
10
|
-
EnforcedStyle: double_quotes
|
11
|
-
|
12
|
-
Layout/LineLength:
|
13
|
-
Max: 120
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.5
|
3
|
+
|
4
|
+
Style/StringLiterals:
|
5
|
+
Enabled: true
|
6
|
+
EnforcedStyle: double_quotes
|
7
|
+
|
8
|
+
Style/StringLiteralsInInterpolation:
|
9
|
+
Enabled: true
|
10
|
+
EnforcedStyle: double_quotes
|
11
|
+
|
12
|
+
Layout/LineLength:
|
13
|
+
Max: 120
|
data/Gemfile
CHANGED
@@ -36,8 +36,8 @@ class SmartpaysController < ApplicationController
|
|
36
36
|
country: "JP"
|
37
37
|
},
|
38
38
|
reference: "order_ref_1234567",
|
39
|
-
|
40
|
-
|
39
|
+
successUrl: "https://docs.smartpay.co/example-pages/checkout-successful",
|
40
|
+
cancelUrl: "https://docs.smartpay.co/example-pages/checkout-canceled",
|
41
41
|
test: true
|
42
42
|
})
|
43
43
|
redirect_to session.redirect_url
|
data/lib/smartpay/api.rb
CHANGED
@@ -1,13 +1,42 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Smartpay
|
4
|
-
class Api
|
5
|
-
class << self
|
6
|
-
def create_checkout_session(payload)
|
7
|
-
Responses::CheckoutSession.new(
|
8
|
-
Client.post("/checkout-sessions", Requests::CheckoutSession.new(payload).as_hash)
|
9
|
-
)
|
10
|
-
end
|
11
|
-
|
12
|
-
|
13
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Smartpay
|
4
|
+
class Api
|
5
|
+
class << self
|
6
|
+
def create_checkout_session(payload)
|
7
|
+
Responses::CheckoutSession.new(
|
8
|
+
Client.post("/checkout-sessions", Requests::CheckoutSession.new(payload).as_hash)
|
9
|
+
)
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_orders(page_token: nil, max_results: nil, expand: '' )
|
13
|
+
Responses::Base.new(Client.get("/orders", { pageToken: page_token, maxResults: max_results, expand: expand }))
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_order(id, expand: '' )
|
17
|
+
Responses::Base.new(Client.get("/orders/%s" % id, { expand: expand }))
|
18
|
+
end
|
19
|
+
|
20
|
+
def create_payment(payload)
|
21
|
+
Responses::Base.new(
|
22
|
+
Client.post("/payments", Requests::Payment.new(payload).as_hash)
|
23
|
+
)
|
24
|
+
end
|
25
|
+
|
26
|
+
def capture(payload)
|
27
|
+
create_payment(payload)
|
28
|
+
end
|
29
|
+
|
30
|
+
def create_refund(payload)
|
31
|
+
Responses::Base.new(
|
32
|
+
Client.post("/refunds", Requests::Refund.new(payload).as_hash)
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
def refund(payload)
|
37
|
+
create_refund(payload)
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/smartpay/client.rb
CHANGED
@@ -1,45 +1,53 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "rest-client"
|
4
|
-
|
5
|
-
module Smartpay
|
6
|
-
class Client
|
7
|
-
class << self
|
8
|
-
def
|
9
|
-
request_payload = default_payload.merge(payload)
|
10
|
-
response = RestClient::Request.execute(method: :
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
def
|
26
|
-
{
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
def
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rest-client"
|
4
|
+
|
5
|
+
module Smartpay
|
6
|
+
class Client
|
7
|
+
class << self
|
8
|
+
def get(path, payload = {})
|
9
|
+
request_payload = default_payload.merge(payload)
|
10
|
+
response = RestClient::Request.execute(method: :get, url: api_url(path),
|
11
|
+
headers: headers.merge(params: request_payload),
|
12
|
+
timeout: timeout)
|
13
|
+
JSON.parse(response.body, symbolize_names: true)
|
14
|
+
end
|
15
|
+
|
16
|
+
def post(path, payload = {})
|
17
|
+
request_payload = default_payload.merge(payload)
|
18
|
+
response = RestClient::Request.execute(method: :post, url: api_url(path), headers: headers, timeout: timeout,
|
19
|
+
payload: request_payload.to_json)
|
20
|
+
JSON.parse(response.body, symbolize_names: true)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def api_url(path)
|
26
|
+
"#{Smartpay.configuration.api_url}#{path}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def timeout
|
30
|
+
Smartpay.configuration.post_timeout
|
31
|
+
end
|
32
|
+
|
33
|
+
def headers
|
34
|
+
{
|
35
|
+
accept: :json,
|
36
|
+
content_type: :json,
|
37
|
+
Authorization: "Basic #{secret_key}"
|
38
|
+
}
|
39
|
+
end
|
40
|
+
|
41
|
+
def secret_key
|
42
|
+
Smartpay.configuration.secret_key
|
43
|
+
end
|
44
|
+
|
45
|
+
def default_payload
|
46
|
+
{
|
47
|
+
'dev-lang': :ruby,
|
48
|
+
'sdk-version': Smartpay::VERSION
|
49
|
+
}.freeze
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -1,48 +1,48 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Smartpay
|
4
|
-
class Configuration
|
5
|
-
attr_accessor :public_key, :secret_key
|
6
|
-
attr_writer :post_timeout, :api_url, :checkout_url
|
7
|
-
|
8
|
-
DEFAULT_TIMEOUT_SETTING = 30
|
9
|
-
DEFAULT_API_URL = "https://api.smartpay.co/v1"
|
10
|
-
DEFAULT_CHECKOUT_URL = "https://checkout.smartpay.co"
|
11
|
-
|
12
|
-
def initialize
|
13
|
-
@post_timeout = DEFAULT_TIMEOUT_SETTING
|
14
|
-
@api_url = if in_development_mode?
|
15
|
-
ENV["SMARTPAY_API_PREFIX"].downcase || DEFAULT_API_URL
|
16
|
-
else
|
17
|
-
DEFAULT_API_URL
|
18
|
-
end
|
19
|
-
@checkout_url = if in_development_mode? && ENV["SMARTPAY_CHECKOUT_URL"].is_a?(String)
|
20
|
-
ENV["SMARTPAY_CHECKOUT_URL"].downcase || DEFAULT_CHECKOUT_URL
|
21
|
-
else
|
22
|
-
DEFAULT_CHECKOUT_URL
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def post_timeout
|
27
|
-
@post_timeout || DEFAULT_TIMEOUT_SETTING
|
28
|
-
end
|
29
|
-
|
30
|
-
def api_url
|
31
|
-
if in_development_mode?
|
32
|
-
@api_url || ENV["SMARTPAY_API_PREFIX"].downcase || DEFAULT_API_URL
|
33
|
-
else
|
34
|
-
@api_url || DEFAULT_API_URL
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def checkout_url
|
39
|
-
@checkout_url || DEFAULT_CHECKOUT_URL
|
40
|
-
end
|
41
|
-
|
42
|
-
private
|
43
|
-
|
44
|
-
def in_development_mode?
|
45
|
-
ENV["SMARTPAY_API_PREFIX"].downcase.include?("api.smartpay") if ENV["SMARTPAY_API_PREFIX"].is_a?(String)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Smartpay
|
4
|
+
class Configuration
|
5
|
+
attr_accessor :public_key, :secret_key
|
6
|
+
attr_writer :post_timeout, :api_url, :checkout_url
|
7
|
+
|
8
|
+
DEFAULT_TIMEOUT_SETTING = 30
|
9
|
+
DEFAULT_API_URL = "https://api.smartpay.co/v1"
|
10
|
+
DEFAULT_CHECKOUT_URL = "https://checkout.smartpay.co"
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@post_timeout = DEFAULT_TIMEOUT_SETTING
|
14
|
+
@api_url = if in_development_mode?
|
15
|
+
ENV["SMARTPAY_API_PREFIX"].downcase || DEFAULT_API_URL
|
16
|
+
else
|
17
|
+
DEFAULT_API_URL
|
18
|
+
end
|
19
|
+
@checkout_url = if in_development_mode? && ENV["SMARTPAY_CHECKOUT_URL"].is_a?(String)
|
20
|
+
ENV["SMARTPAY_CHECKOUT_URL"].downcase || DEFAULT_CHECKOUT_URL
|
21
|
+
else
|
22
|
+
DEFAULT_CHECKOUT_URL
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def post_timeout
|
27
|
+
@post_timeout || DEFAULT_TIMEOUT_SETTING
|
28
|
+
end
|
29
|
+
|
30
|
+
def api_url
|
31
|
+
if in_development_mode?
|
32
|
+
@api_url || ENV["SMARTPAY_API_PREFIX"].downcase || DEFAULT_API_URL
|
33
|
+
else
|
34
|
+
@api_url || DEFAULT_API_URL
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def checkout_url
|
39
|
+
@checkout_url || DEFAULT_CHECKOUT_URL
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def in_development_mode?
|
45
|
+
ENV["SMARTPAY_API_PREFIX"].downcase.include?("api.smartpay") if ENV["SMARTPAY_API_PREFIX"].is_a?(String)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -1,209 +1,140 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Smartpay
|
4
|
-
module Requests
|
5
|
-
class CheckoutSession
|
6
|
-
attr_accessor :payload
|
7
|
-
|
8
|
-
REQUIREMENT_KEY_NAME = [:
|
9
|
-
CAN_FALLBACK_KEYS = [:
|
10
|
-
|
11
|
-
def initialize(raw_payload)
|
12
|
-
@payload = raw_payload.transform_keys(&:to_sym)
|
13
|
-
end
|
14
|
-
|
15
|
-
def as_hash
|
16
|
-
check_requirement!
|
17
|
-
normalize_payload
|
18
|
-
end
|
19
|
-
|
20
|
-
private
|
21
|
-
|
22
|
-
def check_requirement!
|
23
|
-
REQUIREMENT_KEY_NAME.each do |key_name|
|
24
|
-
next if CAN_FALLBACK_KEYS.include?(key_name)
|
25
|
-
raise Errors::InvalidRequestPayloadError, key_name unless payload.include?(key_name)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def normalize_payload
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
end
|
142
|
-
|
143
|
-
def normalize_price_data(data)
|
144
|
-
return if data.nil?
|
145
|
-
data = data.transform_keys(&:to_sym)
|
146
|
-
{
|
147
|
-
productData: normalize_product_data(data.dig(:productData) || {}),
|
148
|
-
amount: data.dig(:amount),
|
149
|
-
currency: data.dig(:currency),
|
150
|
-
metadata: data.dig(:metadata)
|
151
|
-
}
|
152
|
-
end
|
153
|
-
|
154
|
-
def normalize_product_data(product)
|
155
|
-
return if product.nil?
|
156
|
-
product = product.transform_keys(&:to_sym)
|
157
|
-
{
|
158
|
-
name: product.dig(:name),
|
159
|
-
brand: product.dig(:brand),
|
160
|
-
categories: product.dig(:categories),
|
161
|
-
description: product.dig(:description),
|
162
|
-
gtin: product.dig(:gtin),
|
163
|
-
images: product.dig(:images),
|
164
|
-
reference: product.dig(:reference),
|
165
|
-
url: product.dig(:url),
|
166
|
-
metadata: product.dig(:metadata)
|
167
|
-
}
|
168
|
-
end
|
169
|
-
|
170
|
-
def get_currency
|
171
|
-
currency = payload.dig(:orderData, :currency) || payload.dig(:orderData, 'currency')
|
172
|
-
if currency.nil?
|
173
|
-
items = get_items
|
174
|
-
if !items.nil? && items.count > 0
|
175
|
-
currency = items.first.dig(:currency) || items.first.dig('currency')
|
176
|
-
end
|
177
|
-
end
|
178
|
-
currency
|
179
|
-
end
|
180
|
-
|
181
|
-
def get_total_amount
|
182
|
-
total_amount = payload.dig(:orderData, :amount) || payload.dig(:orderData, 'amount')
|
183
|
-
|
184
|
-
if total_amount.nil?
|
185
|
-
items = get_items
|
186
|
-
|
187
|
-
if !items.nil? && items.count > 0
|
188
|
-
total_amount = items.inject(0) { |sum, item| sum + (item[:amount] || item['amount'] || 0) }
|
189
|
-
end
|
190
|
-
|
191
|
-
shipping_fee = payload.dig(:orderData, :shippingInfo, :feeAmount) ||
|
192
|
-
payload.dig(:orderData, :shippingInfo, 'feeAmount') ||
|
193
|
-
payload.dig(:shipping, :feeAmount) ||
|
194
|
-
payload.dig(:shipping, 'feeAmount') ||
|
195
|
-
0
|
196
|
-
total_amount = shipping_fee + (total_amount || 0)
|
197
|
-
end
|
198
|
-
|
199
|
-
total_amount
|
200
|
-
end
|
201
|
-
|
202
|
-
def get_items
|
203
|
-
@items ||= payload.dig(:orderData, :lineItemData, :priceData) ||
|
204
|
-
payload.dig(:orderData, 'lineItemData', 'priceData') ||
|
205
|
-
payload.dig(:items)
|
206
|
-
end
|
207
|
-
end
|
208
|
-
end
|
209
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Smartpay
|
4
|
+
module Requests
|
5
|
+
class CheckoutSession
|
6
|
+
attr_accessor :payload
|
7
|
+
|
8
|
+
REQUIREMENT_KEY_NAME = [:successUrl, :cancelUrl, :customer, :currency, :items].freeze
|
9
|
+
CAN_FALLBACK_KEYS = [:customer].freeze
|
10
|
+
|
11
|
+
def initialize(raw_payload)
|
12
|
+
@payload = raw_payload.transform_keys(&:to_sym)
|
13
|
+
end
|
14
|
+
|
15
|
+
def as_hash
|
16
|
+
check_requirement!
|
17
|
+
normalize_payload
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def check_requirement!
|
23
|
+
REQUIREMENT_KEY_NAME.each do |key_name|
|
24
|
+
next if CAN_FALLBACK_KEYS.include?(key_name)
|
25
|
+
raise Errors::InvalidRequestPayloadError, key_name unless payload.include?(key_name)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def normalize_payload
|
30
|
+
shipping_info = payload.dig(:shippingInfo) || normalize_shipping(payload.dig(:shipping))
|
31
|
+
if shipping_info && shipping_info[:feeCurrency].nil? && !(shipping_info[:feeAmount].nil?)
|
32
|
+
shipping_info[:feeCurrency] = payload.dig(:currency)
|
33
|
+
end
|
34
|
+
|
35
|
+
total_amount = get_total_amount
|
36
|
+
|
37
|
+
{
|
38
|
+
customerInfo: normalize_customer_info(payload.dig(:customerInfo) || payload.dig(:customer) || {}),
|
39
|
+
amount: total_amount,
|
40
|
+
captureMethod: payload.dig(:captureMethod),
|
41
|
+
currency: payload.dig(:currency),
|
42
|
+
description: payload.dig(:description),
|
43
|
+
shippingInfo: shipping_info,
|
44
|
+
items: normalize_items(payload.dig(:items)),
|
45
|
+
metadata: payload.dig(:metadata) || {},
|
46
|
+
reference: payload.dig(:reference),
|
47
|
+
successUrl: payload.dig(:successUrl),
|
48
|
+
cancelUrl: payload.dig(:cancelUrl),
|
49
|
+
}
|
50
|
+
end
|
51
|
+
|
52
|
+
def normalize_customer_info(info)
|
53
|
+
return if info.nil?
|
54
|
+
customer = info.transform_keys(&:to_sym)
|
55
|
+
{
|
56
|
+
accountAge: customer.dig(:accountAge),
|
57
|
+
emailAddress: customer.dig(:emailAddress) || customer.dig(:email),
|
58
|
+
firstName: customer.dig(:firstName),
|
59
|
+
lastName: customer.dig(:lastName),
|
60
|
+
firstNameKana: customer.dig(:firstNameKana),
|
61
|
+
lastNameKana: customer.dig(:lastNameKana),
|
62
|
+
address: customer.dig(:address),
|
63
|
+
phoneNumber: customer.dig(:phoneNumber) || customer.dig(:phone),
|
64
|
+
dateOfBirth: customer.dig(:dateOfBirth),
|
65
|
+
legalGender: customer.dig(:legalGender) || customer.dig(:gender),
|
66
|
+
reference: customer.dig(:reference)
|
67
|
+
}
|
68
|
+
end
|
69
|
+
|
70
|
+
def normalize_shipping(shipping)
|
71
|
+
return if shipping.nil?
|
72
|
+
shipping= shipping.transform_keys(&:to_sym)
|
73
|
+
{
|
74
|
+
address: shipping.dig(:address) || {
|
75
|
+
line1: shipping.dig(:line1),
|
76
|
+
line2: shipping.dig(:line2),
|
77
|
+
line3: shipping.dig(:line3),
|
78
|
+
line4: shipping.dig(:line4),
|
79
|
+
line5: shipping.dig(:line5),
|
80
|
+
subLocality: shipping.dig(:subLocality),
|
81
|
+
locality: shipping.dig(:locality),
|
82
|
+
administrativeArea: shipping.dig(:administrativeArea),
|
83
|
+
postalCode: shipping.dig(:postalCode),
|
84
|
+
country: shipping.dig(:country),
|
85
|
+
},
|
86
|
+
addressType: shipping.dig(:addressType),
|
87
|
+
feeAmount: shipping.dig(:feeAmount),
|
88
|
+
feeCurrency: shipping.dig(:feeCurrency),
|
89
|
+
}
|
90
|
+
end
|
91
|
+
|
92
|
+
def normalize_items(data)
|
93
|
+
return [] if data.nil?
|
94
|
+
|
95
|
+
data.map do |item|
|
96
|
+
line_item = item.transform_keys(&:to_sym)
|
97
|
+
{
|
98
|
+
quantity: line_item.dig(:quantity),
|
99
|
+
label: line_item.dig(:label),
|
100
|
+
name: line_item.dig(:name),
|
101
|
+
amount: line_item.dig(:amount),
|
102
|
+
currency: line_item.dig(:currency),
|
103
|
+
brand: line_item.dig(:brand),
|
104
|
+
categories: line_item.dig(:categories),
|
105
|
+
gtin: line_item.dig(:gtin),
|
106
|
+
images: line_item.dig(:images),
|
107
|
+
reference: line_item.dig(:reference),
|
108
|
+
url: line_item.dig(:url),
|
109
|
+
description: line_item.dig(:description),
|
110
|
+
priceDescription: line_item.dig(:priceDescription),
|
111
|
+
productDescription: line_item.dig(:productDescription),
|
112
|
+
metadata: line_item.dig(:metadata),
|
113
|
+
productMetadata: line_item.dig(:productMetadata),
|
114
|
+
priceMetadata: line_item.dig(:priceMetadata)
|
115
|
+
}
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
|
120
|
+
def get_total_amount
|
121
|
+
total_amount = payload.dig(:amount) || payload.dig('amount')
|
122
|
+
|
123
|
+
if total_amount.nil?
|
124
|
+
items = payload.dig(:items)
|
125
|
+
|
126
|
+
if !items.nil? && items.count > 0
|
127
|
+
total_amount = items.inject(0) { |sum, item| sum + (item[:amount] || item['amount'] || 0) }
|
128
|
+
end
|
129
|
+
|
130
|
+
shipping_fee = payload.dig(:shippingInfo, :feeAmount) ||
|
131
|
+
payload.dig(:shippingInfo, 'feeAmount') ||
|
132
|
+
0
|
133
|
+
total_amount = shipping_fee + (total_amount || 0)
|
134
|
+
end
|
135
|
+
|
136
|
+
total_amount
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Smartpay
|
4
|
+
module Requests
|
5
|
+
class Payment
|
6
|
+
attr_accessor :payload
|
7
|
+
|
8
|
+
REQUIREMENT_KEY_NAME = [:order, :amount, :currency].freeze
|
9
|
+
|
10
|
+
def initialize(raw_payload)
|
11
|
+
@payload = raw_payload.transform_keys(&:to_sym)
|
12
|
+
end
|
13
|
+
|
14
|
+
def as_hash
|
15
|
+
check_requirement!
|
16
|
+
normalize_payload
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def check_requirement!
|
22
|
+
REQUIREMENT_KEY_NAME.each do |key_name|
|
23
|
+
raise Errors::InvalidRequestPayloadError, key_name unless payload.include?(key_name)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def normalize_payload
|
28
|
+
{
|
29
|
+
order: payload.dig(:order),
|
30
|
+
amount: payload.dig(:amount),
|
31
|
+
currency: payload.dig(:currency),
|
32
|
+
reference: payload.dig(:reference),
|
33
|
+
description: payload.dig(:description),
|
34
|
+
metadata: payload.dig(:metadata) || {},
|
35
|
+
}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Smartpay
|
4
|
+
module Requests
|
5
|
+
class Refund
|
6
|
+
attr_accessor :payload
|
7
|
+
|
8
|
+
REQUIREMENT_KEY_NAME = [:payment, :amount, :currency].freeze
|
9
|
+
|
10
|
+
def initialize(raw_payload)
|
11
|
+
@payload = raw_payload.transform_keys(&:to_sym)
|
12
|
+
end
|
13
|
+
|
14
|
+
def as_hash
|
15
|
+
check_requirement!
|
16
|
+
normalize_payload
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def check_requirement!
|
22
|
+
REQUIREMENT_KEY_NAME.each do |key_name|
|
23
|
+
raise Errors::InvalidRequestPayloadError, key_name unless payload.include?(key_name)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def normalize_payload
|
28
|
+
{
|
29
|
+
payment: payload.dig(:payment),
|
30
|
+
amount: payload.dig(:amount),
|
31
|
+
currency: payload.dig(:currency),
|
32
|
+
reason: payload.dig(:reason),
|
33
|
+
reference: payload.dig(:reference),
|
34
|
+
description: payload.dig(:description),
|
35
|
+
metadata: payload.dig(:metadata) || {},
|
36
|
+
}
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Smartpay
|
4
|
+
module Responses
|
5
|
+
class Base
|
6
|
+
attr_reader :response
|
7
|
+
|
8
|
+
def initialize(response)
|
9
|
+
@response = response
|
10
|
+
end
|
11
|
+
|
12
|
+
def as_hash
|
13
|
+
@response
|
14
|
+
end
|
15
|
+
|
16
|
+
def as_json
|
17
|
+
@response.to_json
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -1,40 +1,30 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Smartpay
|
4
|
-
module Responses
|
5
|
-
class CheckoutSession
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
def
|
22
|
-
|
23
|
-
end
|
24
|
-
|
25
|
-
def
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
def checkout_url
|
32
|
-
Smartpay.configuration.checkout_url
|
33
|
-
end
|
34
|
-
|
35
|
-
def public_key
|
36
|
-
Smartpay.configuration.public_key
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Smartpay
|
4
|
+
module Responses
|
5
|
+
class CheckoutSession < Base
|
6
|
+
|
7
|
+
def redirect_url(options = {})
|
8
|
+
url = response[:url]
|
9
|
+
|
10
|
+
if options && options[:promotionCode]
|
11
|
+
qs = "?promotion-code=#{options[:promotionCode]}"
|
12
|
+
|
13
|
+
return "#{url}#{qs}"
|
14
|
+
end
|
15
|
+
|
16
|
+
url
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def checkout_url
|
22
|
+
Smartpay.configuration.checkout_url
|
23
|
+
end
|
24
|
+
|
25
|
+
def public_key
|
26
|
+
Smartpay.configuration.public_key
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/smartpay/version.rb
CHANGED
data/lib/smartpay.rb
CHANGED
@@ -1,25 +1,31 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "json"
|
4
|
-
|
5
|
-
require_relative "smartpay/version"
|
6
|
-
require_relative "smartpay/configuration"
|
7
|
-
require_relative "smartpay/client"
|
8
|
-
require_relative "smartpay/api"
|
9
|
-
require_relative "smartpay/errors/invalid_request_payload_error"
|
10
|
-
require_relative "smartpay/requests/checkout_session"
|
11
|
-
require_relative "smartpay/
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
require_relative "smartpay/version"
|
6
|
+
require_relative "smartpay/configuration"
|
7
|
+
require_relative "smartpay/client"
|
8
|
+
require_relative "smartpay/api"
|
9
|
+
require_relative "smartpay/errors/invalid_request_payload_error"
|
10
|
+
require_relative "smartpay/requests/checkout_session"
|
11
|
+
require_relative "smartpay/requests/payment"
|
12
|
+
require_relative "smartpay/requests/refund"
|
13
|
+
require_relative "smartpay/responses/base"
|
14
|
+
require_relative "smartpay/responses/checkout_session"
|
15
|
+
|
16
|
+
module Smartpay
|
17
|
+
REJECT_REQUEST_BY_CUSTOMER = 'requested_by_customer'
|
18
|
+
REJECT_FRAUDULENT = 'fraudulent'
|
19
|
+
|
20
|
+
class << self
|
21
|
+
attr_accessor :configuration
|
22
|
+
|
23
|
+
def configuration
|
24
|
+
@configuration ||= Smartpay::Configuration.new
|
25
|
+
end
|
26
|
+
|
27
|
+
def configure
|
28
|
+
yield(configuration)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smartpay
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Smartpay
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-03-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
@@ -66,6 +66,9 @@ files:
|
|
66
66
|
- lib/smartpay/configuration.rb
|
67
67
|
- lib/smartpay/errors/invalid_request_payload_error.rb
|
68
68
|
- lib/smartpay/requests/checkout_session.rb
|
69
|
+
- lib/smartpay/requests/payment.rb
|
70
|
+
- lib/smartpay/requests/refund.rb
|
71
|
+
- lib/smartpay/responses/base.rb
|
69
72
|
- lib/smartpay/responses/checkout_session.rb
|
70
73
|
- lib/smartpay/version.rb
|
71
74
|
homepage: https://smartpay.co
|
@@ -90,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
93
|
- !ruby/object:Gem::Version
|
91
94
|
version: '0'
|
92
95
|
requirements: []
|
93
|
-
rubygems_version: 3.
|
96
|
+
rubygems_version: 3.2.32
|
94
97
|
signing_key:
|
95
98
|
specification_version: 4
|
96
99
|
summary: The Smartpay Ruby SDK offers easy access to Smartpay API from applications
|