clt-sdk 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/CHANGELOG.md +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +128 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/clt-sdk.gemspec +29 -0
- data/lib/clt-sdk.rb +23 -0
- data/lib/clt/credit_card.rb +139 -0
- data/lib/clt/cvs.rb +149 -0
- data/lib/clt/error_message.rb +24 -0
- data/lib/clt/version.rb +3 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dac5fc58c37112a3a2f409cec2d60c2c9088cc02
|
4
|
+
data.tar.gz: 1b4cca4b527ddef1472a4b0cbff7a976e4f07fc7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 79de1ae9768d688d853cb6cc1104c85b6812f98605d74947c8d38e8a38208826485f4232bf9da6ac1a3dd6ad71d7b003fc0b5bbecca1018a8d96dd75e8367d55
|
7
|
+
data.tar.gz: 68d4534447392ea3f0a195f6ca460b4eda57a3d0de7b413a02d869d935064c7460d28e1c7e8f4c514dbcba482f1bca24d6f0c9d1bbc11242c498ccc4e9eff4d7
|
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Calvert
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
[](http://badge.fury.io/rb/clt-sdk)
|
2
|
+

|
3
|
+
|
4
|
+
# Clt SDK
|
5
|
+
|
6
|
+
Basic API client for President Collect Service.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem "clt-sdk"
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
```bash
|
19
|
+
$ bundle
|
20
|
+
```
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
```bash
|
25
|
+
$ gem install clt-sdk
|
26
|
+
```
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
#### Initialize
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
require "clt-sdk"
|
34
|
+
|
35
|
+
Clt.setup do |config|
|
36
|
+
config.cvs_cust_id = "YOUR_CVS_CUST_ID"
|
37
|
+
config.cvs_cust_password = "YOUR_CVS_CUST_PASSWORD"
|
38
|
+
config.cvs_expire_after = 1
|
39
|
+
config.cocs_link_id = "YOUR_COCS_LINK_ID"
|
40
|
+
config.cocs_hash_base = "YOUR_COCS_HASH_BASE"
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
`cvs_expire_after`: [**Optional**] The ibon payment code will expired after this setting day (Default: 1)
|
45
|
+
|
46
|
+
## Example
|
47
|
+
|
48
|
+
* ibon
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
cvs = Clt::Cvs.new
|
52
|
+
```
|
53
|
+
|
54
|
+
#### Order Create
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
result = cvs.order_create(
|
58
|
+
service_url: "SERVICE_URL",
|
59
|
+
order_number: "TESTORDER000001",
|
60
|
+
amount: 100,
|
61
|
+
mobile: "0987654321",
|
62
|
+
email: "test@localhost.com"
|
63
|
+
)
|
64
|
+
```
|
65
|
+
|
66
|
+
Optional parameters: `name`, `postcode`, `address`
|
67
|
+
|
68
|
+
#### Order Query
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
result = cvs.order_query(
|
72
|
+
service_url: "SERVICE_URL",
|
73
|
+
update_time_begin: "2016-05-27T00:00:00+08:00"
|
74
|
+
)
|
75
|
+
```
|
76
|
+
|
77
|
+
Optional parameters: `update_time_end`
|
78
|
+
|
79
|
+
* Credit Card
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
credit_card = Clt::CreditCard.new
|
83
|
+
```
|
84
|
+
|
85
|
+
#### Order Create
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
result = credit_card.order_create(
|
89
|
+
service_url: "SERVICE_URL",
|
90
|
+
cust_order_no: "TESTORDER000001",
|
91
|
+
order_amount: 100,
|
92
|
+
order_detail: "Order Test"
|
93
|
+
)
|
94
|
+
```
|
95
|
+
|
96
|
+
Optional parameters: `cust_order_no`, `limit_product_id`
|
97
|
+
|
98
|
+
> System will automatic generate `cust_order_no` if empty
|
99
|
+
|
100
|
+
#### Order Cancel
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
result = credit_card.order_cancel(
|
104
|
+
service_url: "SERVICE_URL",
|
105
|
+
cust_order_no: "TESTORDER000001",
|
106
|
+
order_amount: 100
|
107
|
+
)
|
108
|
+
```
|
109
|
+
|
110
|
+
#### Order Refund
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
result = credit_card.order_refund(
|
114
|
+
service_url: "SERVICE_URL",
|
115
|
+
cust_order_no: "TESTORDER000001",
|
116
|
+
order_amount: 100,
|
117
|
+
refund_amount: 100
|
118
|
+
)
|
119
|
+
```
|
120
|
+
|
121
|
+
## Contributing
|
122
|
+
|
123
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/CalvertYang/clt-sdk.
|
124
|
+
|
125
|
+
|
126
|
+
## License
|
127
|
+
|
128
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "clt-sdk"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/clt-sdk.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'clt/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "clt-sdk"
|
8
|
+
spec.version = Clt::VERSION
|
9
|
+
spec.authors = ["Calvert"]
|
10
|
+
spec.email = [""]
|
11
|
+
|
12
|
+
spec.summary = "Basic API client for President Collect Payment Service."
|
13
|
+
spec.description = ""
|
14
|
+
spec.homepage = "https://github.com/CalvertYang/clt-sdk"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "bin"
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "nokogiri", "~> 1.6"
|
23
|
+
spec.add_dependency "nori", "~> 2.6"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
26
|
+
spec.add_development_dependency "rake", "~> 11.1"
|
27
|
+
|
28
|
+
spec.required_ruby_version = ">= 2.1.5"
|
29
|
+
end
|
data/lib/clt-sdk.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require "clt/version"
|
2
|
+
require "clt/error_message"
|
3
|
+
require "clt/cvs"
|
4
|
+
require "clt/credit_card"
|
5
|
+
|
6
|
+
require "nokogiri"
|
7
|
+
require "nori"
|
8
|
+
require "json"
|
9
|
+
|
10
|
+
module Clt
|
11
|
+
class << self
|
12
|
+
attr_accessor :cvs_cust_id, :cvs_cust_password, :cvs_expire_after
|
13
|
+
attr_accessor :cocs_link_id, :cocs_hash_base
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.cvs_expire_after
|
17
|
+
@cvs_expire_after || 1
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.setup
|
21
|
+
yield(self)
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
module Clt
|
2
|
+
class CreditCard
|
3
|
+
def initialize
|
4
|
+
raise ArgumentError, "Please setup cocs_link_id first" if Clt.cocs_link_id.nil?
|
5
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :cocs_link_id, data: "String") unless Clt.cocs_link_id.is_a? String
|
6
|
+
ErrorMessage.raise_argument_error(msg: :cannot_be_empty, field: :cocs_link_id) if Clt.cocs_link_id.empty?
|
7
|
+
|
8
|
+
raise ArgumentError, "Please setup cocs_hash_base first" if Clt.cocs_hash_base.nil?
|
9
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :cocs_hash_base, data: "String") unless Clt.cocs_hash_base.is_a? String
|
10
|
+
ErrorMessage.raise_argument_error(msg: :cannot_be_empty, field: :cocs_hash_base) if Clt.cocs_hash_base.empty?
|
11
|
+
end
|
12
|
+
|
13
|
+
# Order Create
|
14
|
+
def order_create params = {}
|
15
|
+
ErrorMessage.raise_argument_error(msg: :wrong_parameter_type, type: "Hash") unless params.is_a? Hash
|
16
|
+
|
17
|
+
ErrorMessage.raise_argument_error(msg: :missing_parameter, field: :service_url) if params[:service_url].nil?
|
18
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :service_url, data: "String") unless params[:service_url].is_a? String
|
19
|
+
ErrorMessage.raise_argument_error(msg: :cannot_be_empty, field: :service_url) if params[:service_url].empty?
|
20
|
+
|
21
|
+
if params.has_key? :cust_order_no
|
22
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :cust_order_no, data: "String") unless params[:cust_order_no].is_a? String
|
23
|
+
ErrorMessage.raise_argument_error(msg: :cannot_be_empty, field: :cust_order_no) if params[:cust_order_no].empty?
|
24
|
+
ErrorMessage.raise_argument_error(msg: :data_length_too_short, field: :cust_order_no) if params[:cust_order_no].size < 3
|
25
|
+
end
|
26
|
+
|
27
|
+
ErrorMessage.raise_argument_error(msg: :missing_parameter, field: :order_amount) if params[:order_amount].nil?
|
28
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :order_amount, data: "Integer") unless params[:order_amount].is_a? Fixnum
|
29
|
+
|
30
|
+
ErrorMessage.raise_argument_error(msg: :missing_parameter, field: :order_detail) if params[:order_detail].nil?
|
31
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :order_detail, data: "String") unless params[:order_detail].is_a? String
|
32
|
+
ErrorMessage.raise_argument_error(msg: :cannot_be_empty, field: :order_detail) if params[:order_detail].empty?
|
33
|
+
|
34
|
+
if params.has_key? :limit_product_id
|
35
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :limit_product_id, data: "String") unless params[:limit_product_id].is_a? String
|
36
|
+
ErrorMessage.raise_argument_error(msg: :cannot_be_empty, field: :limit_product_id) if params[:limit_product_id].empty?
|
37
|
+
end
|
38
|
+
|
39
|
+
service_url = params[:service_url]
|
40
|
+
params.delete :service_url
|
41
|
+
|
42
|
+
data = {
|
43
|
+
link_id: Clt.cocs_link_id,
|
44
|
+
cust_order_no: "",
|
45
|
+
limit_product_id: "esun.normal",
|
46
|
+
send_time: Time.now.strftime("%F %T"),
|
47
|
+
return_type: "json"
|
48
|
+
}.merge(params)
|
49
|
+
data[:chk] = create_chk "#{Clt.cocs_hash_base}$#{params[:order_amount]}$#{data[:send_time]}"
|
50
|
+
|
51
|
+
request(method: "POST", service_url: service_url, data: data)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Order Cancel
|
55
|
+
def order_cancel params = {}
|
56
|
+
ErrorMessage.raise_argument_error(msg: :wrong_parameter_type, type: "Hash") unless params.is_a? Hash
|
57
|
+
|
58
|
+
ErrorMessage.raise_argument_error(msg: :missing_parameter, field: :service_url) if params[:service_url].nil?
|
59
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :service_url, data: "String") unless params[:service_url].is_a? String
|
60
|
+
ErrorMessage.raise_argument_error(msg: :cannot_be_empty, field: :service_url) if params[:service_url].empty?
|
61
|
+
|
62
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :cust_order_no, data: "String") unless params[:cust_order_no].is_a? String
|
63
|
+
ErrorMessage.raise_argument_error(msg: :cannot_be_empty, field: :cust_order_no) if params[:cust_order_no].empty?
|
64
|
+
|
65
|
+
ErrorMessage.raise_argument_error(msg: :missing_parameter, field: :order_amount) if params[:order_amount].nil?
|
66
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :order_amount, data: "Integer") unless params[:order_amount].is_a? Fixnum
|
67
|
+
|
68
|
+
data = {
|
69
|
+
link_id: Clt.cocs_link_id,
|
70
|
+
send_time: Time.now.strftime("%F %T"),
|
71
|
+
return_type: "json"
|
72
|
+
}.merge(params)
|
73
|
+
data[:chk] = create_chk "#{Clt.cocs_hash_base}$#{params[:cust_order_no]}$#{params[:order_amount]}$#{data[:send_time]}"
|
74
|
+
|
75
|
+
request(method: "POST", service_url: params[:service_url], data: data)
|
76
|
+
end
|
77
|
+
|
78
|
+
# Order Refund
|
79
|
+
def order_refund params = {}
|
80
|
+
ErrorMessage.raise_argument_error(msg: :wrong_parameter_type, type: "Hash") unless params.is_a? Hash
|
81
|
+
|
82
|
+
ErrorMessage.raise_argument_error(msg: :missing_parameter, field: :service_url) if params[:service_url].nil?
|
83
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :service_url, data: "String") unless params[:service_url].is_a? String
|
84
|
+
ErrorMessage.raise_argument_error(msg: :cannot_be_empty, field: :service_url) if params[:service_url].empty?
|
85
|
+
|
86
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :cust_order_no, data: "String") unless params[:cust_order_no].is_a? String
|
87
|
+
ErrorMessage.raise_argument_error(msg: :cannot_be_empty, field: :cust_order_no) if params[:cust_order_no].empty?
|
88
|
+
|
89
|
+
ErrorMessage.raise_argument_error(msg: :missing_parameter, field: :order_amount) if params[:order_amount].nil?
|
90
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :order_amount, data: "Integer") unless params[:order_amount].is_a? Fixnum
|
91
|
+
|
92
|
+
ErrorMessage.raise_argument_error(msg: :missing_parameter, field: :refund_amount) if params[:refund_amount].nil?
|
93
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :refund_amount, data: "Integer") unless params[:refund_amount].is_a? Fixnum
|
94
|
+
|
95
|
+
data = {
|
96
|
+
link_id: Clt.cocs_link_id,
|
97
|
+
send_time: Time.now.strftime("%F %T"),
|
98
|
+
return_type: "json"
|
99
|
+
}.merge(params)
|
100
|
+
data[:chk] = create_chk "#{Clt.cocs_hash_base}$#{params[:cust_order_no]}$#{params[:order_amount]}$#{params[:refund_amount]}$#{data[:send_time]}"
|
101
|
+
|
102
|
+
request(method: "POST", service_url: params[:service_url], data: data)
|
103
|
+
end
|
104
|
+
|
105
|
+
# Response data validation
|
106
|
+
def data_valid? params
|
107
|
+
chk = params["chk"]
|
108
|
+
generated_chk = create_chk "#{Clt.cocs_hash_base}$#{params['order_amount']}$#{params['send_time']}$#{params['ret']}$#{params['acquire_time']}$#{params['auth_code']}$#{params['card_no']}$#{params['notify_time']}$#{params['cust_order_no']}"
|
109
|
+
|
110
|
+
chk == generated_chk
|
111
|
+
end
|
112
|
+
|
113
|
+
private
|
114
|
+
|
115
|
+
def create_chk data
|
116
|
+
Digest::MD5.hexdigest data
|
117
|
+
end
|
118
|
+
|
119
|
+
def request method:, service_url:, data:
|
120
|
+
api_url = URI.parse(service_url)
|
121
|
+
response = Net::HTTP.post_form api_url, data
|
122
|
+
|
123
|
+
case response
|
124
|
+
when Net::HTTPOK
|
125
|
+
begin
|
126
|
+
result = JSON.parse(response.body)
|
127
|
+
rescue
|
128
|
+
result = { "status" => "ERROR", "msg" => "Response parsing failed", "response" => response.body }
|
129
|
+
end
|
130
|
+
|
131
|
+
result
|
132
|
+
when Net::HTTPClientError, Net::HTTPInternalServerError
|
133
|
+
raise Net::HTTPError, response.message
|
134
|
+
else
|
135
|
+
raise Net::HTTPError, "Unexpected HTTP response"
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
data/lib/clt/cvs.rb
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
module Clt
|
2
|
+
class Cvs
|
3
|
+
def initialize
|
4
|
+
raise ArgumentError, "Please setup cvs_cust_id first" if Clt.cvs_cust_id.nil?
|
5
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :cvs_cust_id, data: "String") unless Clt.cvs_cust_id.is_a? String
|
6
|
+
ErrorMessage.raise_argument_error(msg: :cannot_be_empty, field: :cvs_cust_id) if Clt.cvs_cust_id.empty?
|
7
|
+
|
8
|
+
raise ArgumentError, "Please setup cvs_cust_password first" if Clt.cvs_cust_password.nil?
|
9
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :cvs_cust_password, data: "String") unless Clt.cvs_cust_password.is_a? String
|
10
|
+
ErrorMessage.raise_argument_error(msg: :cannot_be_empty, field: :cvs_cust_password) if Clt.cvs_cust_password.empty?
|
11
|
+
|
12
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :cvs_cust_password, data: "Integer") unless Clt.cvs_expire_after.is_a? Fixnum
|
13
|
+
end
|
14
|
+
|
15
|
+
def order_create params = {}
|
16
|
+
ErrorMessage.raise_argument_error(msg: :wrong_parameter_type, type: "Hash") unless params.is_a? Hash
|
17
|
+
|
18
|
+
ErrorMessage.raise_argument_error(msg: :missing_parameter, field: :service_url) if params[:service_url].nil?
|
19
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :service_url, data: "String") unless params[:service_url].is_a? String
|
20
|
+
ErrorMessage.raise_argument_error(msg: :cannot_be_empty, field: :service_url) if params[:service_url].empty?
|
21
|
+
|
22
|
+
ErrorMessage.raise_argument_error(msg: :missing_parameter, field: :order_number) if params[:order_number].nil?
|
23
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :order_number, data: "String") unless params[:order_number].is_a? String
|
24
|
+
ErrorMessage.raise_argument_error(msg: :cannot_be_empty, field: :order_number) if params[:order_number].empty?
|
25
|
+
|
26
|
+
ErrorMessage.raise_argument_error(msg: :missing_parameter, field: :amount) if params[:amount].nil?
|
27
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :amount, data: "Integer") unless params[:amount].is_a? Fixnum
|
28
|
+
|
29
|
+
if params.has_key? :name
|
30
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :name, data: "String") unless params[:name].is_a? String
|
31
|
+
ErrorMessage.raise_argument_error(msg: :cannot_be_empty, field: :name) if params[:name].empty?
|
32
|
+
end
|
33
|
+
|
34
|
+
if params.has_key? :postcode
|
35
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :postcode, data: "String") unless params[:postcode].is_a? String
|
36
|
+
ErrorMessage.raise_argument_error(msg: :cannot_be_empty, field: :postcode) if params[:postcode].empty?
|
37
|
+
end
|
38
|
+
|
39
|
+
if params.has_key? :address
|
40
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :address, data: "String") unless params[:address].is_a? String
|
41
|
+
ErrorMessage.raise_argument_error(msg: :cannot_be_empty, field: :address) if params[:address].empty?
|
42
|
+
end
|
43
|
+
|
44
|
+
ErrorMessage.raise_argument_error(msg: :missing_parameter, field: :mobile) if params[:mobile].nil?
|
45
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :mobile, data: "String") unless params[:mobile].is_a? String
|
46
|
+
ErrorMessage.raise_argument_error(msg: :cannot_be_empty, field: :mobile) if params[:mobile].empty?
|
47
|
+
|
48
|
+
ErrorMessage.raise_argument_error(msg: :missing_parameter, field: :email) if params[:email].nil?
|
49
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :email, data: "String") unless params[:email].is_a? String
|
50
|
+
ErrorMessage.raise_argument_error(msg: :cannot_be_empty, field: :email) if params[:email].empty?
|
51
|
+
|
52
|
+
expire_date = get_cvs_expire_date
|
53
|
+
|
54
|
+
# build xml data
|
55
|
+
builder = Nokogiri::XML::Builder.new(:encoding => "UTF-8") do
|
56
|
+
request {
|
57
|
+
header {
|
58
|
+
cmd "cvs_order_regiater"
|
59
|
+
cust_id Clt.cvs_cust_id
|
60
|
+
cust_password Clt.cvs_cust_password
|
61
|
+
}
|
62
|
+
order {
|
63
|
+
cust_order_number params[:order_number]
|
64
|
+
order_amount params[:amount]
|
65
|
+
expire_date expire_date
|
66
|
+
payer_name params[:name] if params.has_key? :name
|
67
|
+
payer_postcode params[:postcode] if params.has_key? :postcode
|
68
|
+
payer_address params[:address] if params.has_key? :address
|
69
|
+
payer_mobile params[:mobile]
|
70
|
+
payer_email params[:email]
|
71
|
+
}
|
72
|
+
}
|
73
|
+
end
|
74
|
+
|
75
|
+
request(method: "POST", service_url: params[:service_url], data: builder.to_xml)
|
76
|
+
end
|
77
|
+
|
78
|
+
def order_query params = {}
|
79
|
+
ErrorMessage.raise_argument_error(msg: :wrong_parameter_type, type: "Hash") unless params.is_a? Hash
|
80
|
+
|
81
|
+
ErrorMessage.raise_argument_error(msg: :missing_parameter, field: :service_url) if params[:service_url].nil?
|
82
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :service_url, data: "String") unless params[:service_url].is_a? String
|
83
|
+
ErrorMessage.raise_argument_error(msg: :cannot_be_empty, field: :service_url) if params[:service_url].empty?
|
84
|
+
|
85
|
+
ErrorMessage.raise_argument_error(msg: :missing_parameter, field: :update_time_begin) if params[:update_time_begin].nil?
|
86
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :update_time_begin, data: "String") unless params[:update_time_begin].is_a? String
|
87
|
+
ErrorMessage.raise_argument_error(msg: :cannot_be_empty, field: :update_time_begin) if params[:update_time_begin].empty?
|
88
|
+
|
89
|
+
if params.has_key? :update_time_end
|
90
|
+
ErrorMessage.raise_argument_error(msg: :wrong_data, field: :update_time_end, data: "String") unless params[:update_time_end].is_a? String
|
91
|
+
ErrorMessage.raise_argument_error(msg: :cannot_be_empty, field: :update_time_end) if params[:update_time_end].empty?
|
92
|
+
ErrorMessage.raise_argument_error(msg: :wrong_format, field: :update_time_end) unless /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+\d{2}:\d{2}/.match(params[:update_time_end])
|
93
|
+
end
|
94
|
+
|
95
|
+
builder = Nokogiri::XML::Builder.new(:encoding => "UTF-8") do
|
96
|
+
request {
|
97
|
+
header {
|
98
|
+
cmd "cvs_order_query"
|
99
|
+
cust_id Clt.cvs_cust_id
|
100
|
+
cust_password Clt.cvs_cust_password
|
101
|
+
}
|
102
|
+
query {
|
103
|
+
process_code_update_time_begin params[:update_time_begin]
|
104
|
+
process_code_update_time_end params[:update_time_end] if params.has_key? :update_time_end
|
105
|
+
}
|
106
|
+
}
|
107
|
+
end
|
108
|
+
|
109
|
+
request(method: "POST", service_url: params[:service_url], data: builder.to_xml)
|
110
|
+
end
|
111
|
+
|
112
|
+
private
|
113
|
+
|
114
|
+
def get_cvs_expire_date
|
115
|
+
offset = 60 * 60 * 24 * Clt.cvs_expire_after
|
116
|
+
|
117
|
+
(Time.now + offset).iso8601
|
118
|
+
end
|
119
|
+
|
120
|
+
def request method: "POST", service_url:, data:
|
121
|
+
api_url = URI.parse(service_url)
|
122
|
+
|
123
|
+
http = Net::HTTP.new(api_url.host, api_url.port)
|
124
|
+
http.use_ssl = true if api_url.scheme == "https"
|
125
|
+
|
126
|
+
req = Net::HTTP::Post.new(api_url.request_uri, initheader = { "Content-Type" => "text/xml" })
|
127
|
+
req.body = data
|
128
|
+
|
129
|
+
response = http.request(req)
|
130
|
+
|
131
|
+
case response
|
132
|
+
when Net::HTTPOK
|
133
|
+
parser = Nori.new(:advanced_typecasting => false)
|
134
|
+
|
135
|
+
begin
|
136
|
+
response_hash = parser.parse(response.body)["response"]
|
137
|
+
rescue
|
138
|
+
response_hash = { "status" => "ERROR", "msg" => "Response parsing failed", "response" => response.body }
|
139
|
+
end
|
140
|
+
|
141
|
+
response_hash
|
142
|
+
when Net::HTTPClientError, Net::HTTPInternalServerError
|
143
|
+
raise Net::HTTPError, response.message
|
144
|
+
else
|
145
|
+
raise Net::HTTPError, "Unexpected HTTP response"
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Clt
|
2
|
+
class ErrorMessage
|
3
|
+
def self.raise_argument_error params
|
4
|
+
message = nil
|
5
|
+
|
6
|
+
case params[:msg]
|
7
|
+
when :missing_parameter
|
8
|
+
message = "Missing required parameter: #{params[:field]}"
|
9
|
+
when :wrong_parameter_type
|
10
|
+
message = "Parameter should be #{params[:type]}"
|
11
|
+
when :wrong_data
|
12
|
+
message = "#{params[:field]} should be #{params[:data]}"
|
13
|
+
when :data_length_too_short
|
14
|
+
message = "The length of #{params[:field]} is too short"
|
15
|
+
when :wrong_format
|
16
|
+
message = "The format for #{params[:field]} is wrong"
|
17
|
+
when :cannot_be_empty
|
18
|
+
message = "#{params[:field]} cannot be empty"
|
19
|
+
end
|
20
|
+
|
21
|
+
raise ArgumentError, message
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/clt/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: clt-sdk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Calvert
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: nokogiri
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: nori
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.6'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.12'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.12'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '11.1'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '11.1'
|
69
|
+
description: ''
|
70
|
+
email:
|
71
|
+
- ''
|
72
|
+
executables:
|
73
|
+
- console
|
74
|
+
- setup
|
75
|
+
extensions: []
|
76
|
+
extra_rdoc_files: []
|
77
|
+
files:
|
78
|
+
- ".gitignore"
|
79
|
+
- CHANGELOG.md
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- clt-sdk.gemspec
|
87
|
+
- lib/clt-sdk.rb
|
88
|
+
- lib/clt/credit_card.rb
|
89
|
+
- lib/clt/cvs.rb
|
90
|
+
- lib/clt/error_message.rb
|
91
|
+
- lib/clt/version.rb
|
92
|
+
homepage: https://github.com/CalvertYang/clt-sdk
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: 2.1.5
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.5.1
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Basic API client for President Collect Payment Service.
|
116
|
+
test_files: []
|