gmo_payment_gem 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +45 -0
- data/Rakefile +8 -0
- data/gmo-payment-gem.gemspec +19 -0
- data/lib/gmo_payment/api_errors.rb +1349 -0
- data/lib/gmo_payment/api_request.rb +72 -0
- data/lib/gmo_payment/api_response.rb +94 -0
- data/lib/gmo_payment/api_urls.rb +39 -0
- data/lib/gmo_payment/configurations.rb +14 -0
- data/lib/gmo_payment/custom_error.rb +1 -0
- data/lib/gmo_payment/logger.rb +10 -0
- data/lib/gmo_payment.rb +98 -0
- data/test/helper.rb +51 -0
- data/test/logs/test.log +0 -0
- data/test/test_api_errors.rb +40 -0
- data/test/test_api_request.rb +43 -0
- data/test/test_api_response.rb +105 -0
- data/test/test_api_urls.rb +21 -0
- data/test/test_configurations.rb +20 -0
- data/test/test_gmo_payment.rb +16 -0
- data/test/test_logger.rb +26 -0
- metadata +91 -0
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'net/http'
|
3
|
+
require 'net/https'
|
4
|
+
|
5
|
+
class GmoPayment::ApiRequest
|
6
|
+
DEFAULT_TIMEOUT = 30 # Seconds
|
7
|
+
ENCODE_PARAMS = %w(CardNo SiteID SitePass ShopID ShopPass).freeze
|
8
|
+
|
9
|
+
def initialize(request_url, request_params, timeout)
|
10
|
+
@request_url = request_url
|
11
|
+
@request_params = request_params
|
12
|
+
@time_out = timeout || DEFAULT_TIMEOUT
|
13
|
+
@requester = nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def make_request(method = 'post')
|
17
|
+
host, path = split_base_url_and_path
|
18
|
+
@requester = init_http_request(host)
|
19
|
+
begin
|
20
|
+
@requester.start
|
21
|
+
response = if method.casecmp('post').zero?
|
22
|
+
@requester.post(path, to_query_params(@request_params))
|
23
|
+
else
|
24
|
+
@requester.get(path, to_query_params(@request_params))
|
25
|
+
end
|
26
|
+
ensure
|
27
|
+
@requester.finish
|
28
|
+
end
|
29
|
+
response
|
30
|
+
end
|
31
|
+
|
32
|
+
def params2log
|
33
|
+
encode_params(to_query_params(@request_params))
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def split_base_url_and_path
|
39
|
+
uri = URI.parse(@request_url)
|
40
|
+
[uri.host, uri.path]
|
41
|
+
end
|
42
|
+
|
43
|
+
def init_http_request(host)
|
44
|
+
http = Net::HTTP.new(host, 443) # SSL port
|
45
|
+
http.open_timeout = @time_out
|
46
|
+
http.read_timeout = @time_out
|
47
|
+
http.use_ssl = true
|
48
|
+
http
|
49
|
+
end
|
50
|
+
|
51
|
+
def to_query_params(options = {})
|
52
|
+
str = ''
|
53
|
+
options.each { |k, v| str << "#{k}=#{v}&" }
|
54
|
+
str
|
55
|
+
end
|
56
|
+
|
57
|
+
# Encode params string to *** format
|
58
|
+
# Eg: CardNo=1234567890123 to CardNo=*********123
|
59
|
+
def encode_params(log_msg)
|
60
|
+
params_patterns = "(#{ENCODE_PARAMS.join('|')})=\\w+"
|
61
|
+
log_msg.gsub(Regexp.new(params_patterns)) { |match| hide_value(match).to_s }
|
62
|
+
end
|
63
|
+
|
64
|
+
# Hide all value if value length <= 10
|
65
|
+
# Otherwise show last 3 characters
|
66
|
+
def hide_value(param_n_value)
|
67
|
+
param, value = param_n_value.split('=')
|
68
|
+
v_hide = '*' * value.length
|
69
|
+
v_hide << value[-3..-1] if value.length > 10
|
70
|
+
"#{param}=#{v_hide}"
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
class GmoPayment::ApiResponse
|
2
|
+
RESPONSE_SUCCESS_CODE = [200, 201].freeze
|
3
|
+
SPLIT_CHARACTER = '|'.freeze
|
4
|
+
|
5
|
+
def initialize(api_url, body, response_http_code)
|
6
|
+
@api_url = api_url
|
7
|
+
@response_body = body
|
8
|
+
@response_http_code = response_http_code
|
9
|
+
@response_body_parsed = parse_response_content
|
10
|
+
end
|
11
|
+
|
12
|
+
def response_code
|
13
|
+
@response_http_code
|
14
|
+
end
|
15
|
+
|
16
|
+
def response_body(raw = false)
|
17
|
+
raw ? @response_body : @response_body_parsed
|
18
|
+
end
|
19
|
+
|
20
|
+
def error_codes
|
21
|
+
r_detail = response_body
|
22
|
+
r_detail.fetch(:ErrCode, '').split(SPLIT_CHARACTER)
|
23
|
+
end
|
24
|
+
|
25
|
+
def error_infos
|
26
|
+
r_detail = response_body
|
27
|
+
r_detail.fetch(:ErrInfo, '').split(SPLIT_CHARACTER)
|
28
|
+
end
|
29
|
+
|
30
|
+
def error_messages
|
31
|
+
error_infos.map do |e_code|
|
32
|
+
GmoPayment::ApiErrors.all.fetch(e_code, "UNKNOWN ERROR with code #{e_code}")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def full_messages
|
37
|
+
error_messages.join(' ')
|
38
|
+
end
|
39
|
+
|
40
|
+
def has_error?(error_info_code)
|
41
|
+
error_infos.include?(error_info_code)
|
42
|
+
end
|
43
|
+
|
44
|
+
def success?
|
45
|
+
if block_given?
|
46
|
+
yield(@api_url, @response_http_code, @response_body, @response_body_parsed)
|
47
|
+
else
|
48
|
+
r_detail = response_body
|
49
|
+
pre_condition = RESPONSE_SUCCESS_CODE.include?(response_code) && !r_detail.key?(:ErrCode)
|
50
|
+
case @api_url
|
51
|
+
when GmoPayment::ApiUrls.all.fetch('MEMBER_REGISTER'),
|
52
|
+
GmoPayment::ApiUrls.all.fetch('MEMBER_DELETE'),
|
53
|
+
GmoPayment::ApiUrls.all.fetch('MEMBER_SEARCH')
|
54
|
+
pre_condition && r_detail.key?(:MemberID)
|
55
|
+
when GmoPayment::ApiUrls.all.fetch('CARD_SAVE_OR_UPDATE')
|
56
|
+
pre_condition && r_detail.key?(:CardSeq) && r_detail.key?(:CardNo)
|
57
|
+
when GmoPayment::ApiUrls.all.fetch('CARD_DELETE')
|
58
|
+
pre_condition && r_detail.key?(:CardSeq)
|
59
|
+
when GmoPayment::ApiUrls.all.fetch('TRANSACTION_REGISTER')
|
60
|
+
pre_condition && r_detail.key?(:AccessID) && r_detail.key?(:AccessPass)
|
61
|
+
when GmoPayment::ApiUrls.all.fetch('TRANSACTION_SUBMIT')
|
62
|
+
pre_condition && r_detail.key?(:TranID) && r_detail.key?(:CheckString)
|
63
|
+
when GmoPayment::ApiUrls.all.fetch('TRANSACTION_UPDATE')
|
64
|
+
pre_condition && r_detail.key?(:AccessID) && r_detail.key?(:AccessPass)
|
65
|
+
else
|
66
|
+
pre_condition
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def response2log(raw = false)
|
72
|
+
response_body(raw)
|
73
|
+
end
|
74
|
+
|
75
|
+
def full_errors2log
|
76
|
+
full_messages
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def parse_response_content
|
82
|
+
result = {}
|
83
|
+
return result unless @response_body && !@response_body.empty?
|
84
|
+
begin
|
85
|
+
@response_body.split('&').each do |key_value|
|
86
|
+
key, value = key_value.split('=')
|
87
|
+
result[key.to_sym] = value
|
88
|
+
end
|
89
|
+
result
|
90
|
+
rescue
|
91
|
+
result
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class GmoPayment::ApiUrls
|
2
|
+
class << self
|
3
|
+
attr_accessor :urls
|
4
|
+
end
|
5
|
+
@urls = {}
|
6
|
+
|
7
|
+
def self.all
|
8
|
+
if frozen?
|
9
|
+
urls
|
10
|
+
else
|
11
|
+
base_payment_url = GmoPayment::Configurations.all.fetch(:base_url)
|
12
|
+
basic_urls = {
|
13
|
+
# Transaction API URL
|
14
|
+
'TRANSACTION_REGISTER' => "#{base_payment_url}/EntryTran.idPass",
|
15
|
+
'TRANSACTION_SUBMIT' => "#{base_payment_url}/ExecTran.idPass",
|
16
|
+
'TRANSACTION_UPDATE' => "#{base_payment_url}/AlterTran.idPass",
|
17
|
+
'TRANSACTION_MONEY_CHANGE' => "#{base_payment_url}/ChangeTran.idPass",
|
18
|
+
'TRANSACTION_SEARCH' => "#{base_payment_url}/SearchTrade.idPass",
|
19
|
+
|
20
|
+
# Member API URL
|
21
|
+
'MEMBER_REGISTER' => "#{base_payment_url}/SaveMember.idPass",
|
22
|
+
'MEMBER_UPDATE' => "#{base_payment_url}/UpdateMember.idPass",
|
23
|
+
'MEMBER_SEARCH' => "#{base_payment_url}/SearchMember.idPass",
|
24
|
+
'MEMBER_DELETE' => "#{base_payment_url}/DeleteMember.idPass",
|
25
|
+
|
26
|
+
# Credit card API URL
|
27
|
+
'CARD_SAVE_AFTER_TRADED' => "#{base_payment_url}/TradedCard.idPass",
|
28
|
+
'CARD_SAVE_OR_UPDATE' => "#{base_payment_url}/SaveCard.idPass",
|
29
|
+
'CARD_SEARCH' => "#{base_payment_url}/SearchCard.idPass",
|
30
|
+
'CARD_DELETE' => "#{base_payment_url}/DeleteCard.idPass"
|
31
|
+
}
|
32
|
+
|
33
|
+
more_urls = GmoPayment::Configurations.all.fetch(:more_urls, {})
|
34
|
+
self.urls = basic_urls.merge(more_urls)
|
35
|
+
freeze
|
36
|
+
urls
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class GmoPayment::Configurations
|
2
|
+
REQUIRED_CONFIG = [:base_url, :log_path, :shop_id, :shop_pass, :site_id, :site_pass].freeze
|
3
|
+
class << self
|
4
|
+
attr_accessor :all
|
5
|
+
end
|
6
|
+
@all = {}
|
7
|
+
|
8
|
+
def self.check_valid!(changeable_on_runtime = false)
|
9
|
+
freeze unless changeable_on_runtime
|
10
|
+
REQUIRED_CONFIG.each do |param|
|
11
|
+
raise GmoPayment::CustomError, "gmo_payment error: #{param} not set" if all.fetch(param, nil).nil?
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
class GmoPayment::CustomError < StandardError; end
|
data/lib/gmo_payment.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
class GmoPayment
|
2
|
+
def initialize(options = {}, type_site = true, logger = nil)
|
3
|
+
@base_params = if type_site
|
4
|
+
{
|
5
|
+
SiteID: GmoPayment::Configurations.all.fetch(:site_id),
|
6
|
+
SitePass: GmoPayment::Configurations.all.fetch(:site_pass)
|
7
|
+
}
|
8
|
+
else
|
9
|
+
{
|
10
|
+
ShopID: GmoPayment::Configurations.all.fetch(:shop_id),
|
11
|
+
ShopPass: GmoPayment::Configurations.all.fetch(:shop_pass)
|
12
|
+
}
|
13
|
+
end
|
14
|
+
@time_out = options.fetch(:time_out, nil)
|
15
|
+
@logger = logger || GmoPayment::Logger
|
16
|
+
end
|
17
|
+
|
18
|
+
def register_member(params = {})
|
19
|
+
api_url = GmoPayment::ApiUrls.all.fetch('MEMBER_REGISTER')
|
20
|
+
call_api(api_url, params)
|
21
|
+
end
|
22
|
+
|
23
|
+
def delete_member(params = {})
|
24
|
+
api_url = GmoPayment::ApiUrls.all.fetch('MEMBER_DELETE')
|
25
|
+
call_api(api_url, params)
|
26
|
+
end
|
27
|
+
|
28
|
+
def search_member(params = {})
|
29
|
+
api_url = GmoPayment::ApiUrls.all.fetch('MEMBER_SEARCH')
|
30
|
+
call_api(api_url, params)
|
31
|
+
end
|
32
|
+
|
33
|
+
def save_card(params = {})
|
34
|
+
api_url = GmoPayment::ApiUrls.all.fetch('CARD_SAVE_OR_UPDATE')
|
35
|
+
call_api(api_url, params)
|
36
|
+
end
|
37
|
+
|
38
|
+
def delete_card(params = {})
|
39
|
+
api_url = GmoPayment::ApiUrls.all.fetch('CARD_DELETE')
|
40
|
+
call_api(api_url, params)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Transaction normal flow
|
44
|
+
# Register > Submit with :JobCd = 'CAPTURE'
|
45
|
+
# Register > Submit > Confirm with :JobCd != 'CAPTURE'
|
46
|
+
def register_transaction(params = {})
|
47
|
+
api_url = GmoPayment::ApiUrls.all.fetch('TRANSACTION_REGISTER')
|
48
|
+
call_api(api_url, params)
|
49
|
+
end
|
50
|
+
|
51
|
+
def submit_transaction(params = {})
|
52
|
+
api_url = GmoPayment::ApiUrls.all.fetch('TRANSACTION_SUBMIT')
|
53
|
+
call_api(api_url, params)
|
54
|
+
end
|
55
|
+
|
56
|
+
def confirm_transaction(params = {})
|
57
|
+
api_url = GmoPayment::ApiUrls.all.fetch('TRANSACTION_UPDATE')
|
58
|
+
call_api(api_url, params)
|
59
|
+
end
|
60
|
+
|
61
|
+
def transaction_status(params = {})
|
62
|
+
api_url = GmoPayment::ApiUrls.all.fetch('TRANSACTION_SEARCH')
|
63
|
+
call_api(api_url, params)
|
64
|
+
end
|
65
|
+
|
66
|
+
protected
|
67
|
+
|
68
|
+
def call_api(api_url, params = {}, method = 'post')
|
69
|
+
params.merge!(@base_params)
|
70
|
+
request = GmoPayment::ApiRequest.new(api_url, params, @time_out)
|
71
|
+
write_log('---------- START REQUEST TO GMO PAYMENT --------')
|
72
|
+
write_log("REQUEST URL: --------- #{api_url}")
|
73
|
+
write_log("REQUEST PARAMS: ------ #{request.params2log}")
|
74
|
+
response_raw = request.make_request(method)
|
75
|
+
response = GmoPayment::ApiResponse.new(api_url, response_raw.body, response_raw.code.to_i)
|
76
|
+
write_log("RESPONSE: ------------ HTTP #{response.response_code}: #{response.response2log}")
|
77
|
+
write_log("WITH ERRORS: --------- #{response.full_errors2log}")
|
78
|
+
write_log(%(----------- END REQUEST TO GMO PAYMENT --------\n))
|
79
|
+
response
|
80
|
+
rescue StandardError => e
|
81
|
+
write_log("gmo_payment INTERNAL ERROR: #{e.message} : #{e.backtrace[0, 5]}")
|
82
|
+
raise GmoPayment::CustomError, "gmo_payment error: #{e.message}"
|
83
|
+
end
|
84
|
+
|
85
|
+
private
|
86
|
+
|
87
|
+
def write_log(log_msg)
|
88
|
+
@logger.write(log_msg)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
require 'gmo_payment/configurations'
|
93
|
+
require 'gmo_payment/api_urls'
|
94
|
+
require 'gmo_payment/api_errors'
|
95
|
+
require 'gmo_payment/logger'
|
96
|
+
require 'gmo_payment/api_request'
|
97
|
+
require 'gmo_payment/api_response'
|
98
|
+
require 'gmo_payment/custom_error'
|
data/test/helper.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'webmock/test_unit'
|
2
|
+
require 'gmo_payment'
|
3
|
+
require 'gmo_payment/custom_error'
|
4
|
+
require 'gmo_payment/configurations'
|
5
|
+
|
6
|
+
def payment_base_url
|
7
|
+
'https://test-example.com/payment'
|
8
|
+
end
|
9
|
+
|
10
|
+
def stub_request_to_payment(res_status = 200, res_body = '')
|
11
|
+
stub_request(:any, /test-example.com\/payment/)
|
12
|
+
.with(headers: {
|
13
|
+
'Accept' => '*/*',
|
14
|
+
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
|
15
|
+
'User-Agent' => 'Ruby'
|
16
|
+
})
|
17
|
+
.to_return(status: res_status, body: res_body, headers: {})
|
18
|
+
end
|
19
|
+
|
20
|
+
def stub_request_to_timeout
|
21
|
+
stub_request(:any, payment_base_url)
|
22
|
+
.with(headers: {
|
23
|
+
'Accept' => '*/*',
|
24
|
+
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
|
25
|
+
'User-Agent' => 'Ruby'
|
26
|
+
})
|
27
|
+
.to_raise(GmoPayment::CustomError)
|
28
|
+
end
|
29
|
+
|
30
|
+
def basic_setting
|
31
|
+
{
|
32
|
+
site_id: 'site_id',
|
33
|
+
site_pass: 'site_id',
|
34
|
+
shop_id: 'site_id',
|
35
|
+
shop_pass: 'site_id',
|
36
|
+
log_path: File.join(File.dirname(__FILE__), '/logs/test.log'),
|
37
|
+
base_url: payment_base_url
|
38
|
+
}
|
39
|
+
end
|
40
|
+
|
41
|
+
def set_basic_config
|
42
|
+
GmoPayment::Configurations.all = basic_setting
|
43
|
+
end
|
44
|
+
|
45
|
+
def clear_config
|
46
|
+
GmoPayment::Configurations.all = {}
|
47
|
+
end
|
48
|
+
|
49
|
+
def set_more_configs(more_configs)
|
50
|
+
GmoPayment::Configurations.all = basic_setting.merge(more_configs)
|
51
|
+
end
|
data/test/logs/test.log
ADDED
File without changes
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'gmo_payment'
|
3
|
+
require 'helper'
|
4
|
+
|
5
|
+
class TestApiErrors < Test::Unit::TestCase
|
6
|
+
def test_no_more_errors
|
7
|
+
set_basic_config
|
8
|
+
obj_dup = GmoPayment::ApiErrors.dup
|
9
|
+
obj_dup.all_errors = obj_dup.errors
|
10
|
+
assert_equal(GmoPayment::ApiErrors.errors.size,
|
11
|
+
obj_dup.all.size)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_has_more_errors
|
15
|
+
more_errors = {
|
16
|
+
more_errors: { 'CE1' => 'CE1_VALUE', # New error
|
17
|
+
'CE2' => 'CE2_VALUE' } # New error
|
18
|
+
}
|
19
|
+
set_more_configs(more_errors)
|
20
|
+
assert_equal(GmoPayment::ApiErrors.errors.size + 2,
|
21
|
+
GmoPayment::ApiErrors.dup.all.size)
|
22
|
+
assert_equal('CE1_VALUE',
|
23
|
+
GmoPayment::ApiErrors.all.fetch('CE1'))
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_overwrite_errors
|
27
|
+
more_errors = {
|
28
|
+
more_errors: {
|
29
|
+
'NC2000001' => 'NC2000001_VALUE', # Existing error
|
30
|
+
'CE2' => 'CE2_VALUE' # New error
|
31
|
+
}
|
32
|
+
}
|
33
|
+
set_more_configs(more_errors)
|
34
|
+
obj_dup = GmoPayment::ApiErrors.dup
|
35
|
+
assert_equal(GmoPayment::ApiErrors.errors.size + 1,
|
36
|
+
obj_dup.all.size)
|
37
|
+
assert_equal('NC2000001_VALUE',
|
38
|
+
obj_dup.all.fetch('NC2000001'))
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'gmo_payment'
|
3
|
+
require 'helper'
|
4
|
+
|
5
|
+
class TestApiRequest < Test::Unit::TestCase
|
6
|
+
def test_make_request_valid
|
7
|
+
url = payment_base_url
|
8
|
+
params = {}
|
9
|
+
timeout = 30 # Seconds
|
10
|
+
request = GmoPayment::ApiRequest.new(url, params, timeout)
|
11
|
+
stub_request_to_payment
|
12
|
+
response = request.make_request
|
13
|
+
assert_equal(true, response.is_a?(Net::HTTPOK))
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_make_request_timeout
|
17
|
+
url = payment_base_url
|
18
|
+
params = {}
|
19
|
+
timeout = 30 # Seconds
|
20
|
+
request = GmoPayment::ApiRequest.new(url, params, timeout)
|
21
|
+
stub_request_to_timeout
|
22
|
+
begin
|
23
|
+
request.make_request
|
24
|
+
rescue => e
|
25
|
+
assert_equal(true, e.is_a?(GmoPayment::CustomError))
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_params_has_been_hide
|
30
|
+
url = payment_base_url
|
31
|
+
params = {
|
32
|
+
ShopID: 'shop_id',
|
33
|
+
ShopPass: 'shop_pass',
|
34
|
+
SiteID: 'site_id',
|
35
|
+
SitePass: 'site_pass',
|
36
|
+
CardNo: 12_345_678_901_234
|
37
|
+
}
|
38
|
+
expect_params = 'ShopID=*******&ShopPass=*********&SiteID=*******&SitePass=*********&CardNo=**************234&'
|
39
|
+
timeout = 30 # Seconds
|
40
|
+
request = GmoPayment::ApiRequest.new(url, params, timeout)
|
41
|
+
assert_equal(expect_params, request.params2log)
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'gmo_payment'
|
3
|
+
require 'helper'
|
4
|
+
|
5
|
+
class TestApiResponse < Test::Unit::TestCase
|
6
|
+
def test_response_code
|
7
|
+
response = GmoPayment::ApiResponse.new(payment_base_url, '', 200)
|
8
|
+
assert_equal(200, response.response_code)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_response_body_raw
|
12
|
+
response_body = 'BODY RAW'
|
13
|
+
response = GmoPayment::ApiResponse.new(payment_base_url, response_body, 200)
|
14
|
+
assert_equal(response_body, response.response_body(true))
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_response_body_parsed
|
18
|
+
response_body = 'something=123&somethingelse=456'
|
19
|
+
expect_parsed_body = {
|
20
|
+
something: '123',
|
21
|
+
somethingelse: '456'
|
22
|
+
}
|
23
|
+
response = GmoPayment::ApiResponse.new(payment_base_url, response_body, 200)
|
24
|
+
assert_equal(expect_parsed_body, response.response_body)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_error_codes_and_infos
|
28
|
+
response_body = 'ErrCode=123|456|789&ErrInfo=ErrInfo1|ErrInfo2'
|
29
|
+
expect_err_codes = %w(123 456 789)
|
30
|
+
expect_err_infos = %w(ErrInfo1 ErrInfo2)
|
31
|
+
response = GmoPayment::ApiResponse.new(payment_base_url, response_body, 200)
|
32
|
+
assert_equal(expect_err_codes, response.error_codes)
|
33
|
+
assert_equal(expect_err_infos, response.error_infos)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_error_in_errors_list
|
37
|
+
response_body = 'ErrCode=123|456|789&ErrInfo=E00000000|E01010001|E01010008'
|
38
|
+
expect_err_messages = ['特になし',
|
39
|
+
'ショップIDが指定されていません。',
|
40
|
+
'ショップIDに半角英数字以外の文字が含まれているか、13文字を超えています。']
|
41
|
+
expect_full_messages = expect_err_messages.join(' ')
|
42
|
+
response = GmoPayment::ApiResponse.new(payment_base_url, response_body, 200)
|
43
|
+
assert_equal(expect_err_messages, response.error_messages)
|
44
|
+
assert_equal(expect_full_messages, response.full_messages)
|
45
|
+
assert_equal(expect_full_messages, response.full_errors2log)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_error_not_in_errors_list
|
49
|
+
response_body = 'ErrCode=123|456|789&ErrInfo=EXXX|EYYY|EZZZ'
|
50
|
+
expect_err_messages = ['UNKNOWN ERROR with code EXXX',
|
51
|
+
'UNKNOWN ERROR with code EYYY',
|
52
|
+
'UNKNOWN ERROR with code EZZZ']
|
53
|
+
expect_full_messages = expect_err_messages.join(' ')
|
54
|
+
response = GmoPayment::ApiResponse.new(payment_base_url, response_body, 200)
|
55
|
+
assert_equal(expect_err_messages, response.error_messages)
|
56
|
+
assert_equal(expect_full_messages, response.full_messages)
|
57
|
+
assert_equal(expect_full_messages, response.full_errors2log)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_no_error
|
61
|
+
response_body = 'somthing=123&somethingelse=456'
|
62
|
+
expect_err_messages = []
|
63
|
+
expect_full_messages = ''
|
64
|
+
response = GmoPayment::ApiResponse.new(payment_base_url, response_body, 200)
|
65
|
+
assert_equal(expect_err_messages, response.error_messages)
|
66
|
+
assert_equal(expect_full_messages, response.full_messages)
|
67
|
+
assert_equal(expect_full_messages, response.full_errors2log)
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_success_without_block
|
71
|
+
set_basic_config
|
72
|
+
api_url_key = 'TRANSACTION_REGISTER'
|
73
|
+
api_url = GmoPayment::ApiUrls.all.fetch(api_url_key)
|
74
|
+
response_body = 'AccessID=123&AccessPass=456'
|
75
|
+
response = GmoPayment::ApiResponse.new(api_url, response_body, 200)
|
76
|
+
assert_equal(true, response.success?)
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_success_with_block
|
80
|
+
set_basic_config
|
81
|
+
api_url_key = 'TRANSACTION_REGISTER'
|
82
|
+
api_url = GmoPayment::ApiUrls.all.fetch(api_url_key)
|
83
|
+
response_body = 'AccessID=123&AccessPass=456'
|
84
|
+
response = GmoPayment::ApiResponse.new(api_url, response_body, 200)
|
85
|
+
assert_equal(true, response.success? { |_, _, _, _| true })
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_success_fail_by_http_code
|
89
|
+
set_basic_config
|
90
|
+
api_url_key = 'TRANSACTION_REGISTER'
|
91
|
+
api_url = GmoPayment::ApiUrls.all.fetch(api_url_key)
|
92
|
+
response_body = 'AccessID=123&AccessPass=456'
|
93
|
+
response = GmoPayment::ApiResponse.new(api_url, response_body, 400)
|
94
|
+
assert_equal(false, response.success?)
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_success_fail_by_has_error
|
98
|
+
set_basic_config
|
99
|
+
api_url_key = 'TRANSACTION_REGISTER'
|
100
|
+
api_url = GmoPayment::ApiUrls.all.fetch(api_url_key)
|
101
|
+
response_body = 'ErrCode=123'
|
102
|
+
response = GmoPayment::ApiResponse.new(api_url, response_body, 400)
|
103
|
+
assert_equal(false, response.success?)
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'gmo_payment'
|
3
|
+
require 'helper'
|
4
|
+
|
5
|
+
class TestApiUrls < Test::Unit::TestCase
|
6
|
+
def test_all_with_no_more_url
|
7
|
+
set_basic_config
|
8
|
+
assert_equal(13, GmoPayment::ApiUrls.all.size)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_all_with_more_urls
|
12
|
+
more_urls = {
|
13
|
+
more_urls: {
|
14
|
+
'MORE_PAYMENT_URL1' => 'MORE_PAYMENT_URL1_VALUE',
|
15
|
+
'MORE_PAYMENT_URL2' => 'MORE_PAYMENT_URL2_VALUE'
|
16
|
+
}
|
17
|
+
}
|
18
|
+
set_more_configs(more_urls)
|
19
|
+
assert_equal(13 + 2, GmoPayment::ApiUrls.dup.all.size)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'gmo_payment'
|
3
|
+
require 'helper'
|
4
|
+
|
5
|
+
class TestConfigurations < Test::Unit::TestCase
|
6
|
+
def test_check_valid_success
|
7
|
+
set_basic_config
|
8
|
+
assert_equal(GmoPayment::Configurations::REQUIRED_CONFIG, GmoPayment::Configurations.check_valid!(true))
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_check_valid_fail
|
12
|
+
clear_config
|
13
|
+
begin
|
14
|
+
GmoPayment::Configurations.check_valid!(true)
|
15
|
+
rescue => e
|
16
|
+
assert_equal(true, e.is_a?(GmoPayment::CustomError))
|
17
|
+
assert_equal('gmo_payment error: base_url not set', e.message)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'gmo_payment'
|
3
|
+
require 'helper'
|
4
|
+
|
5
|
+
class TestGmoPayment < Test::Unit::TestCase
|
6
|
+
def test_public_methods
|
7
|
+
set_basic_config
|
8
|
+
gmo_payment = GmoPayment.new
|
9
|
+
api_methods = gmo_payment.public_methods(false)
|
10
|
+
stub_request_to_payment
|
11
|
+
api_methods.each do |api_call|
|
12
|
+
response = gmo_payment.public_send(api_call)
|
13
|
+
assert_equal(GmoPayment::ApiResponse, response.class)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/test/test_logger.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'gmo_payment'
|
3
|
+
require 'helper'
|
4
|
+
|
5
|
+
class TestLogger < Test::Unit::TestCase
|
6
|
+
def test_write_log_success
|
7
|
+
set_basic_config
|
8
|
+
log_msg = 'this is a log'
|
9
|
+
GmoPayment::Logger.write(log_msg)
|
10
|
+
log_on_file = false
|
11
|
+
File.open(GmoPayment::Configurations.all.fetch(:log_path), 'r') do |f|
|
12
|
+
f.each_line { |line| log_on_file = true if line.index(log_msg).to_i >= 0 }
|
13
|
+
end
|
14
|
+
assert_equal(true, log_on_file)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_log_path_not_found
|
18
|
+
clear_config
|
19
|
+
log_msg = 'this is a log'
|
20
|
+
begin
|
21
|
+
GmoPayment::Logger.write(log_msg)
|
22
|
+
rescue => e
|
23
|
+
assert_equal(true, e.is_a?(KeyError))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|