finnet_api 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Evan Kristian
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,35 @@
1
+ = FinnetApi
2
+
3
+ Connect to Finnet API to get payment status
4
+
5
+ == Installation
6
+
7
+ === Gem
8
+
9
+ sudo gem install finnet_api
10
+
11
+
12
+ == Finnet Generator
13
+
14
+ This Generator will generate controller,model,migration and add line to route. In order to use generator, you should add parameter, in this case model name that will related to finnet transaction table.
15
+
16
+ script/generate finnet_api model_name
17
+
18
+ == Setting Finnet Configuration
19
+
20
+ After Generate Finnet, add your configuration in config/finnet.yml
21
+
22
+ example
23
+
24
+ development:
25
+ username: 'username'
26
+ password: 'password'
27
+ return_url: 'http://evandavid.alwaysdata.net/telkom195/195_return_url_secure.php'
28
+ bill_host: 'https://demos.finnet-indonesia.com/195/'
29
+ request_url: 'https://demos.finnet-indonesia.com/195/response-insert.php'
30
+ check_status_url: 'https://demos.finnet-indonesia.com/195/check-status.php'
31
+ timeout: 60
32
+
33
+ == Copyright
34
+
35
+ Copyright (c) 2012 Walden Global Services. See LICENSE for details.
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "finnet_api"
8
+ gem.summary = "finnet"
9
+ gem.description = "finnet_api"
10
+ gem.email = "ev.kristian@gmail.com"
11
+ gem.homepage = "http://github.com/evandavid/finnet_api"
12
+ gem.authors = ["Evan Kristian"]
13
+ gem.files.include 'generators/**/*'
14
+ gem.files.include 'lib/finnet_api.rb'
15
+ gem.files.include 'test/*'
16
+ end
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/*_test.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/*_test.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ if File.exist?('VERSION')
48
+ version = File.read('VERSION')
49
+ else
50
+ version = ""
51
+ end
52
+
53
+ rdoc.rdoc_dir = 'rdoc'
54
+ rdoc.title = "finnet_api #{version}"
55
+ rdoc.rdoc_files.include('README*')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.4
Binary file
@@ -0,0 +1,54 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "finnet_api"
8
+ s.version = "0.0.4"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Evan Kristian"]
12
+ s.date = "2012-02-22"
13
+ s.description = "finnet_api"
14
+ s.email = "ev.kristian@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ "LICENSE",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "finnet_api-0.0.3.gem",
25
+ "finnet_api.gemspec",
26
+ "generators/finnet_api/USAGE",
27
+ "generators/finnet_api/finnet_api_generator.rb",
28
+ "generators/finnet_api/templates/finnet.yml",
29
+ "generators/finnet_api/templates/finnet_transaction.rb",
30
+ "generators/finnet_api/templates/finnet_transactions.yml",
31
+ "generators/finnet_api/templates/finnet_transactions_controller.rb",
32
+ "generators/finnet_api/templates/finnet_transactions_controller_test.rb",
33
+ "generators/finnet_api/templates/migration.rb",
34
+ "init.rb",
35
+ "lib/finnet_api.rb",
36
+ "test/finnet_api_test.rb",
37
+ "test/finnet_transactions_controller_test.rb",
38
+ "test/test_helper.rb"
39
+ ]
40
+ s.homepage = "http://github.com/evandavid/finnet_api"
41
+ s.require_paths = ["lib"]
42
+ s.rubygems_version = "1.8.15"
43
+ s.summary = "finnet"
44
+
45
+ if s.respond_to? :specification_version then
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ else
50
+ end
51
+ else
52
+ end
53
+ end
54
+
@@ -0,0 +1 @@
1
+ #on construction
@@ -0,0 +1,87 @@
1
+ class FinnetApiGenerator < Rails::Generator::NamedBase
2
+
3
+ def manifest
4
+ record do |m|
5
+ m.migration_template("migration.rb", "db/migrate", :migration_file_name => "create_finnet_transactions")
6
+ m.sleep(1)
7
+ m.migration_template 'migration:migration.rb', "db/migrate", {:assigns => add_finnet_api_foreign_key,
8
+ :migration_file_name => "add_#{file_name}_id_to_finnet_transactions"
9
+ }
10
+ m.route :name => 'transaction_response',
11
+ :controller => 'finnet_transactions',
12
+ :action => 'response_finnet'
13
+ m.file 'finnet_transaction.rb', 'app/models/finnet_transaction.rb'
14
+ m.file 'finnet_transactions_controller.rb', 'app/controllers/finnet_transactions_controller.rb'
15
+ m.file 'finnet.yml', 'config/finnet.yml'
16
+ m.file 'finnet_transactions_controller_test.rb', 'test/functional/finnet_transactions_controller_test.rb'
17
+ m.file 'finnet_transactions.yml', 'test/fixtures/finnet_transactions.yml'
18
+ m.sleep(1)
19
+ m.finnet_model
20
+ m.model
21
+ end
22
+ end
23
+
24
+ def gsub_file(relative_destination, regexp, *args, &block)
25
+ path = destination_path(relative_destination)
26
+ content = File.read(path).gsub(regexp, *args, &block)
27
+ File.open(path, 'wb') { |file| file.write(content) }
28
+ end
29
+
30
+
31
+ def route(route_options)
32
+ sentinel = 'ActionController::Routing::Routes.draw do |map|'
33
+ logger.route route_code(route_options)
34
+ gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |m|
35
+ "#{m}\n #{route_code(route_options)}\n"
36
+ end
37
+ end
38
+
39
+ def finnet_model
40
+ sentinel = 'class FinnetTransaction < ActiveRecord::Base'
41
+ logger.model "app/models/finnet_transaction.rb >> belongs_to :#{file_name}"
42
+ gsub_file 'app/models/finnet_transaction.rb', /(#{Regexp.escape(sentinel)})/mi do |m|
43
+ "#{m}\n belongs_to :#{file_name}\n"
44
+ end
45
+ end
46
+
47
+ def model
48
+ sentinel = "class #{file_name.camelize} < ActiveRecord::Base"
49
+ logger.model "app/models/#{file_name}.rb >> has_one :finnet_transaction"
50
+ gsub_file "app/models/#{file_name.underscore}.rb", /(#{Regexp.escape(sentinel)})/mi do |m|
51
+ "#{m}\n has_one :finnet_transaction \n"
52
+ end
53
+ end
54
+
55
+ def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
56
+ if first_letter_in_uppercase
57
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
58
+ else
59
+ lower_case_and_underscored_word.to_s[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1]
60
+ end
61
+ end
62
+
63
+ def underscore(camel_cased_word)
64
+ word = camel_cased_word.to_s.dup
65
+ word.gsub!(/::/, '/')
66
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
67
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
68
+ word.tr!("-", "_")
69
+ word.downcase!
70
+ word
71
+ end
72
+
73
+ def route_code(route_options)
74
+ "map.#{route_options[:name]} '/#{route_options[:name]}', :controller => '#{route_options[:controller]}', :action => '#{route_options[:action]}'"
75
+ end
76
+
77
+ private
78
+ def add_finnet_api_foreign_key
79
+ returning(assigns = {}) do
80
+ assigns[:migration_action] = "add"
81
+ assigns[:class_name] = "add_#{file_name}_id_to_finnet_transactions"
82
+ assigns[:table_name] = "finnet_transactions"
83
+ assigns[:attributes] = [Rails::Generator::GeneratedAttribute.new("#{file_name}_id", "integer")]
84
+ end
85
+ end
86
+
87
+ end
@@ -0,0 +1,28 @@
1
+ development:
2
+ username: 'your-username'
3
+ password: 'your-password'
4
+ return_url: 'http://your-domain.com/transaction_response'
5
+ bill_host: 'http://bill_host'
6
+ request_url: 'http://bill_host/request-url'
7
+ check_status_url: 'http://bill_host/check_status_url'
8
+ timeout: 60
9
+
10
+ test:
11
+ username:
12
+ password:
13
+ return_url:
14
+ bill_host:
15
+ request_url:
16
+ check_status_url:
17
+ timeout:
18
+
19
+ production:
20
+ username:
21
+ password:
22
+ return_url:
23
+ bill_host:
24
+ request_url:
25
+ check_status_url:
26
+ timeout:
27
+
28
+
@@ -0,0 +1,3 @@
1
+ class FinnetTransaction < ActiveRecord::Base
2
+ validates_presence_of :invoice,:amount,:return_url
3
+ end
@@ -0,0 +1,22 @@
1
+ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2
+
3
+ # one:
4
+ # column: value
5
+ #
6
+ # two:
7
+ # column: value
8
+
9
+ one:
10
+ trax_type : "195Code"
11
+ mer_signature : "321B773E4219AAB60305335BFE5C9403A72CE926A8500243F5ECC47CC31AE353"
12
+ merchant_id : "TLKM034"
13
+ invoice : "IMB-2011121910284340"
14
+ amount : 6500000.0
15
+ add_info1 : "Tigor bukan bang toyib"
16
+ add_info2 : "Pembayaran Izin Mendirikan Bangunan Bengkel/Pabrik"
17
+ timeout : 60
18
+ return_url : "http://evandavid.alwaysdata.net/transaction_response"
19
+ result_code : ""
20
+ result_desc : ""
21
+ log_no : ""
22
+ payment_source : ""
@@ -0,0 +1,61 @@
1
+ class FinnetTransactionsController < ApplicationController
2
+
3
+ def response_finnet
4
+ request = Array.new
5
+ convertArray=create_array(params)
6
+ if params["trax_type"] == "195Code"
7
+ request["trax_type"] = params[:trax_type]
8
+ request["payment_code"] = params[:payment_code]
9
+ if update_valid(convertArray,"invoice",params[:invoice],request,params[:mer_signature])
10
+ #pesan update berhasil
11
+ end
12
+ elsif params["trax_type"] == "Payment" && (params["result_code"] == "00" || params["result_code"] == "05")
13
+ request["trax_type"] = params[:trax_type]
14
+ request["result_code"] = params[:result_code]
15
+ request["result_desc"] = params[:result_desc]
16
+ request["log_no"] = params[:log_no]
17
+ request["payment_source"] = params[:payment_source]
18
+ if update_valid?(params,"payment_code",params[:payment_code],request)
19
+ #pesan update berhasil
20
+ end
21
+ end
22
+ end
23
+
24
+ private
25
+ def update_valid(data,conditions,value,request,update,signature)
26
+ api = FinnetApi::API.new
27
+ if api.mer_signature_valid?(signature, data)
28
+ trx = FinnetTransaction.find(:first,:conditions => ["#{conditions} = ?",value])
29
+ hashData=convert_to_hash(request)
30
+ if trx.update_attributes(hashData)
31
+ return true
32
+ else
33
+ return false
34
+ end
35
+ end
36
+ end
37
+
38
+ def convert_to_hash(request)
39
+ data=Hash.new
40
+ request.each_with_index do |value,index|
41
+ data["#{index}"] = value
42
+ end
43
+ return data
44
+ end
45
+
46
+ def create_array(request)
47
+ data = Array.new
48
+ data << ["merchant_id","#{request["merchant_id"]}"]
49
+ data << ["invoice","#{request["invoice"]}"]
50
+ data << ["amount",request["amount"]]
51
+ data << ["add_info1","#{request["add_info1"]}"]
52
+ data << ["add_info2","#{request["add_info2"]}"] unless request["add_info2"].nil?
53
+ data << ["add_info3","#{request["add_info3"]}"] unless request["add_info3"].nil?
54
+ data << ["add_info4","#{request["add_info4"]}"] unless request["add_info4"].nil?
55
+ data << ["add_info5","#{request["add_info5"]}"] unless request["add_info5"].nil?
56
+ data << ["timeout",request["timeout"]]
57
+ data << ["return_url",request["return_url"]]
58
+ return data
59
+ end
60
+
61
+ end
@@ -0,0 +1,185 @@
1
+ require 'test_helper'
2
+ require 'httparty'
3
+ require 'webmock'
4
+ include WebMock::API
5
+
6
+ class FinnetTransactionsControllerTest < ActionController::TestCase
7
+ # Replace this with your real tests.
8
+
9
+ def test_response_finnet_with_trax_type_195Code
10
+ params = {"trax_type" => "195Code",
11
+ "mer_signature" => "321B773E4219AAB60305335BFE5C9403A72CE926A8500243F5ECC47CC31AE353",
12
+ "merchant_id" => "TLKM034",
13
+ "invoice" => "IMB-2011121910284340",
14
+ "amount" => 6500000.0,
15
+ "add_info1" => "Tigor bukan bang toyib",
16
+ "add_info2" => "Pembayaran Izin Mendirikan Bangunan Bengkel/Pabrik",
17
+ "timeout" => 60,
18
+ "return_url" => "http://evandavid.alwaysdata.net/transaction_response",
19
+ "result_code" => "",
20
+ "result_desc" => "",
21
+ "log_no" => "" ,
22
+ "payment_source" => ""
23
+ }
24
+ post :response_finnet, params
25
+ assert_not_nil params
26
+ assert_equal(params["trax_type"],"195Code")
27
+ convertToArray=convert_params_to_array(params)
28
+ conditions = "invoice"
29
+ valueConditions = params[:invoice]
30
+ response_finnet_with_trax_type_195Code_valid_update(convertToArray,conditions,valueConditions,params["mer_signature"])
31
+ end
32
+
33
+
34
+ def test_response_finnet_with_trax_type_Payment_result_code_00
35
+ params = {"trax_type" => "Payment",
36
+ "mer_signature" => "321B773E4219AAB60305335BFE5C9403A72CE926A8500243F5ECC47CC31AE353",
37
+ "merchant_id" => "TLKM034",
38
+ "invoice" => "IMB-2011121910284340",
39
+ "amount" => 6500000.0,
40
+ "add_info1" => "Tigor bukan bang toyib",
41
+ "add_info2" => "Pembayaran Izin Mendirikan Bangunan Bengkel/Pabrik",
42
+ "timeout" => 60,
43
+ "return_url" => "http://evandavid.alwaysdata.net/transaction_response",
44
+ "result_code" => "00",
45
+ "result_desc" => "",
46
+ "log_no" => "" ,
47
+ "payment_source" => ""
48
+ }
49
+ post :response_finnet, params
50
+ assert_not_nil params
51
+ assert_equal(params["trax_type"],"Payment")
52
+ assert_equal(params["result_code"],"00")
53
+ convertToArray=convert_params_to_array(params)
54
+ conditions = "payment_code"
55
+ valueConditions = params[:payment_code]
56
+ response_finnet_with_trax_type_195Code_valid_update(convertToArray,conditions,valueConditions,params["mer_signature"])
57
+ end
58
+
59
+ def test_response_finnet_with_trax_type_Payment_result_code_05
60
+ params = {"trax_type" => "Payment",
61
+ "mer_signature" => "321B773E4219AAB60305335BFE5C9403A72CE926A8500243F5ECC47CC31AE353",
62
+ "merchant_id" => "TLKM034",
63
+ "invoice" => "IMB-2011121910284340",
64
+ "amount" => 6500000.0,
65
+ "add_info1" => "Tigor bukan bang toyib",
66
+ "add_info2" => "Pembayaran Izin Mendirikan Bangunan Bengkel/Pabrik",
67
+ "timeout" => 60,
68
+ "return_url" => "http://evandavid.alwaysdata.net/transaction_response",
69
+ "result_code" => "05",
70
+ "result_desc" => "",
71
+ "log_no" => "" ,
72
+ "payment_source" => ""
73
+ }
74
+ post :response_finnet, params
75
+ assert_not_nil params
76
+ assert_equal(params["trax_type"],"Payment")
77
+ assert_equal(params["result_code"],"05")
78
+ convertToArray=convert_params_to_array(params)
79
+ conditions = "payment_code"
80
+ valueConditions = params[:payment_code]
81
+ response_finnet_with_trax_type_195Code_valid_update(convertToArray,conditions,valueConditions,params["mer_signature"])
82
+ end
83
+
84
+ def test_response_finnet_invalid_signature_with_trax_type_195Code
85
+ params = {"trax_type" => "195Code",
86
+ "mer_signature" => "321B773E4219AAB60305335BFE5C9403A72CE926A8500243F5ECC47CC31AE",
87
+ "merchant_id" => "TLKM034",
88
+ "invoice" => "IMB-2011121910284340",
89
+ "amount" => 6500000.0,
90
+ "add_info1" => "Tigor bukan bang toyib",
91
+ "add_info2" => "Pembayaran Izin Mendirikan Bangunan Bengkel/Pabrik",
92
+ "timeout" => 60,
93
+ "return_url" => "http://evandavid.alwaysdata.net/transaction_response",
94
+ "result_code" => "05",
95
+ "result_desc" => "",
96
+ "log_no" => "" ,
97
+ "payment_source" => ""
98
+ }
99
+ post :response_finnet, params
100
+ assert_not_nil params
101
+ assert_equal(params["trax_type"],"195Code")
102
+ convertToArray=convert_params_to_array(params)
103
+ conditions = "invoice"
104
+ valueConditions = params[:invoice]
105
+ response_finnet_with_trax_type_195Code_invalid_update(convertToArray,conditions,valueConditions,params["mer_signature"])
106
+ end
107
+
108
+ def test_response_finnet_invalid_signature_with_trax_type_Payment_result_code_00
109
+ params = {"trax_type" => "Payment",
110
+ "mer_signature" => "321B773E4219AAB60305335BFE5C9403A72CE926A8500243F5ECC47CC31A",
111
+ "merchant_id" => "TLKM034",
112
+ "invoice" => "IMB-2011121910284340",
113
+ "amount" => 6500000.0,
114
+ "add_info1" => "Tigor bukan bang toyib",
115
+ "add_info2" => "Pembayaran Izin Mendirikan Bangunan Bengkel/Pabrik",
116
+ "timeout" => 60,
117
+ "return_url" => "http://evandavid.alwaysdata.net/transaction_response",
118
+ "result_code" => "00",
119
+ "result_desc" => "",
120
+ "log_no" => "" ,
121
+ "payment_source" => ""
122
+ }
123
+ post :response_finnet, params
124
+ assert_not_nil params
125
+ assert_equal(params["trax_type"],"Payment")
126
+ assert_equal(params["result_code"],"00")
127
+ convertToArray=convert_params_to_array(params)
128
+ conditions = "payment_code"
129
+ valueConditions = params[:payment_code]
130
+ response_finnet_with_trax_type_195Code_invalid_update(convertToArray,conditions,valueConditions,params["mer_signature"])
131
+ end
132
+
133
+ def test_response_finnet_invalid_signature_with_trax_type_Payment_result_code_05
134
+ params = {"trax_type" => "Payment",
135
+ "mer_signature" => "321B773E4219AAB60305335BFE5C9403A72CE926A8500243F5ECC47CC31A",
136
+ "merchant_id" => "TLKM034",
137
+ "invoice" => "IMB-2011121910284340",
138
+ "amount" => 6500000.0,
139
+ "add_info1" => "Tigor bukan bang toyib",
140
+ "add_info2" => "Pembayaran Izin Mendirikan Bangunan Bengkel/Pabrik",
141
+ "timeout" => 60,
142
+ "return_url" => "http://evandavid.alwaysdata.net/transaction_response",
143
+ "result_code" => "05",
144
+ "result_desc" => "",
145
+ "log_no" => "" ,
146
+ "payment_source" => ""
147
+ }
148
+ post :response_finnet, params
149
+ assert_not_nil params
150
+ assert_equal(params["trax_type"],"Payment")
151
+ assert_equal(params["result_code"],"05")
152
+ convertToArray=convert_params_to_array(params)
153
+ conditions = "payment_code"
154
+ valueConditions = params[:payment_code]
155
+ response_finnet_with_trax_type_195Code_invalid_update(convertToArray,conditions,valueConditions,params["mer_signature"])
156
+ end
157
+
158
+ def response_finnet_with_trax_type_195Code_valid_update(params,conditions,valueConditions,signature)
159
+ api = FinnetApi::API.new
160
+ data=api.mer_signature_valid(signature,params)
161
+ assert_equal(data,true)
162
+ end
163
+
164
+ def response_finnet_with_trax_type_195Code_invalid_update(params,conditions,valueConditions,signature)
165
+ api = FinnetApi::API.new
166
+ data=api.mer_signature_valid(signature,params)
167
+ assert_equal(data,false)
168
+ end
169
+
170
+ def convert_params_to_array(params)
171
+ data = Array.new
172
+ data << ["merchant_id","#{params["merchant_id"]}"]
173
+ data << ["invoice","#{params["invoice"]}"]
174
+ data << ["amount",params["amount"]]
175
+ data << ["add_info1","#{params["add_info1"]}"]
176
+ data << ["add_info2","#{params["add_info2"]}"] unless params["add_info2"].nil?
177
+ data << ["add_info3","#{params["add_info3"]}"] unless params["add_info3"].nil?
178
+ data << ["add_info4","#{params["add_info4"]}"] unless params["add_info4"].nil?
179
+ data << ["add_info5","#{params["add_info5"]}"] unless params["add_info5"].nil?
180
+ data << ["timeout",params["timeout"]]
181
+ data << ["return_url",params["return_url"]]
182
+ return data
183
+ end
184
+
185
+ end
@@ -0,0 +1,28 @@
1
+ class CreateFinnetTransactions < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :finnet_transactions do |t|
4
+ t.string :invoice
5
+ t.integer :amount
6
+ t.text :add_info1
7
+ t.text :add_info2
8
+ t.text :add_info3
9
+ t.text :add_info4
10
+ t.text :add_info5
11
+ t.integer :timeout
12
+ t.string :return_url
13
+ t.string :trax_type
14
+ t.string :payment_code
15
+ t.string :result_code
16
+ t.string :result_desc
17
+ t.string :log_no
18
+ t.string :payment_source
19
+ t.timestamps
20
+ end
21
+ add_index :finnet_transactions, :invoice
22
+ add_index :finnet_transactions, :payment_code
23
+ end
24
+
25
+ def self.down
26
+ drop_table :finnet_transactions
27
+ end
28
+ end
data/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ require File.join(File.dirname(__FILE__), "lib")
2
+ require 'finnet_api'
3
+
4
+ FinnetApi::Api.new
@@ -0,0 +1,131 @@
1
+ module FinnetApi
2
+ require 'rubygems'
3
+ require 'httparty'
4
+ require 'yaml'
5
+
6
+ class API
7
+ include HTTParty
8
+ config = YAML.load_file("#{RAILS_ROOT}/config/finnet.yml")[RAILS_ENV]
9
+ @@USER_FINNET= config['username']
10
+ @@PASS_FINNET= config['password']
11
+ @@RETURN_URI= config['return_url']
12
+ @@BILLHOST_URL = config['bill_host']
13
+ @@REQUEST_URL_195 = config['request_url']
14
+ @@CHECK_STATUS_URL_195 =config['check_status_url']
15
+ @@TIMEOUT = config['timeout']
16
+
17
+ base_uri @@REQUEST_URL_195
18
+ default_params :output => 'json'
19
+ format :json
20
+
21
+ def send_api(amount,invoice,add_info1,add_info2= nil,add_info3 = nil ,add_info4= nil,add_info5 = nil)
22
+ response = parse_array(amount,invoice,add_info1,add_info2)
23
+ query_string = convert_get(response)
24
+ if insert_trancastion(response)
25
+ api_valid = HTTParty.post(@@REQUEST_URL_195,:body=> "#{query_string}")
26
+ if check_response(api_valid)
27
+ issue="#{api_valid}"
28
+ end
29
+ end
30
+ end
31
+
32
+
33
+ def mer_signature_valid(mer_sign,request)
34
+ comparator = "#{mer_signature(request)}"
35
+ if mer_sign.to_s == hash256(comparator).to_s
36
+ lanjut = true
37
+ else
38
+ lanjut = false
39
+ end
40
+ return lanjut
41
+ end
42
+
43
+ private
44
+ def insert_trancastion(data)
45
+ data_hash = convert_hash(data)
46
+ trx = FinnetTransaction.new(data_hash)
47
+ if trx.save
48
+ return true
49
+ else
50
+ return false
51
+ end
52
+ end
53
+
54
+ def check_response(data)
55
+ if data == 00
56
+ return true
57
+ else
58
+ return false
59
+ end
60
+ end
61
+
62
+
63
+ def create_array(amount,invoice,add_info1,add_info2= nil,add_info3 = nil ,add_info4= nil,add_info5 = nil)
64
+ data = Array.new
65
+ data << ["merchant_id","#{@@USER_FINNET}"]
66
+ data << ["invoice","#{invoice}"]
67
+ data << ["amount",amount]
68
+ data << ["add_info1","#{add_info1}"]
69
+ data << ["add_info2","#{add_info2}"] unless add_info2.nil?
70
+ data << ["add_info3","#{add_info3}"] unless add_info3.nil?
71
+ data << ["add_info4","#{add_info4}"] unless add_info4.nil?
72
+ data << ["add_info5","#{add_info5}"] unless add_info5.nil?
73
+ data << ["timeout",@@TIMEOUT]
74
+ data << ["return_url",@@RETURN_URI]
75
+ return data
76
+ end
77
+
78
+
79
+ def parse_array(amount,invoice,add_info1,add_info2 = nil,add_info3 = nil ,add_info4 = nil,add_info5 = nil)
80
+ data = Array.new
81
+ response = create_array(amount,invoice,add_info1,add_info2,add_info3,add_info4,add_info5)
82
+ signature = mer_signature(response)
83
+ data << ["mer_signature","#{hash256(signature)}"]
84
+ data << ["merchant_id","#{@@USER_FINNET}"]
85
+ data << ["invoice","#{invoice}"]
86
+ data << ["amount",amount]
87
+ data << ["add_info1","#{add_info1}"]
88
+ data << ["add_info2","#{add_info2}"] unless add_info2.nil?
89
+ data << ["add_info3","#{add_info3}"] unless add_info3.nil?
90
+ data << ["add_info4","#{add_info4}"] unless add_info4.nil?
91
+ data << ["add_info5","#{add_info5}"] unless add_info5.nil?
92
+ data << ["timeout",@@TIMEOUT]
93
+ data << ["return_url",@@RETURN_URI]
94
+ return data
95
+ end
96
+
97
+ def convert_get(request)
98
+ data = Array.new
99
+ request.each_with_index do |value,index|
100
+ data << "#{value[0]}=#{value[1]}"
101
+ end
102
+ return data.join("&")
103
+ end
104
+
105
+ def convert_hash(request)
106
+ data = Hash.new
107
+ request.each_with_index do |value,index|
108
+ data["#{value[0]}"] = "#{value[1]}" unless (value[0]=="mer_signature") || (value[0]=="merchant_id") || (value[0]=="paid")
109
+ end
110
+ return data
111
+ end
112
+
113
+ def mer_signature(data)
114
+ data_code = Array.new
115
+ data.each do |val|
116
+ data_code << "#{val[1]}".upcase unless (val[1].to_s.nil?) || (val[0] == "mer_signature" ) || (val[0] == "paid") || (val[0] == "result_code") || (val[0] == "result_desc" || val[0] == "log_no") || (val[0] == "payment_source") || (val[0] == "trax_type")
117
+ end
118
+ data_code << "#{@@PASS_FINNET}"
119
+ data_return = data_code.join("%")
120
+
121
+ return data_return.gsub(/[']/,'')
122
+ end
123
+
124
+ def hash256(code)
125
+ data = Digest::SHA2.new << "#{code}"
126
+ return data.to_s.upcase
127
+ end
128
+
129
+ end
130
+ end
131
+
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+ require 'webmock'
3
+ require 'httparty'
4
+ include WebMock::API
5
+ require 'class_loader'
6
+
7
+ class FinnetApiTest < Test::Unit::TestCase
8
+
9
+ end
@@ -0,0 +1,185 @@
1
+ require 'test_helper'
2
+ require 'httparty'
3
+ require 'webmock'
4
+ include WebMock::API
5
+
6
+ class FinnetTransactionsControllerTest < ActionController::TestCase
7
+ # Replace this with your real tests.
8
+
9
+ def test_response_finnet_with_trax_type_195Code
10
+ params = {"trax_type" => "195Code",
11
+ "mer_signature" => "321B773E4219AAB60305335BFE5C9403A72CE926A8500243F5ECC47CC31AE353",
12
+ "merchant_id" => "TLKM034",
13
+ "invoice" => "IMB-2011121910284340",
14
+ "amount" => 6500000.0,
15
+ "add_info1" => "Tigor bukan bang toyib",
16
+ "add_info2" => "Pembayaran Izin Mendirikan Bangunan Bengkel/Pabrik",
17
+ "timeout" => 60,
18
+ "return_url" => "http://evandavid.alwaysdata.net/transaction_response",
19
+ "result_code" => "",
20
+ "result_desc" => "",
21
+ "log_no" => "" ,
22
+ "payment_source" => ""
23
+ }
24
+ post :response_finnet, params
25
+ assert_not_nil params
26
+ assert_equal(params["trax_type"],"195Code")
27
+ convertToArray=convert_params_to_array(params)
28
+ conditions = "invoice"
29
+ valueConditions = params[:invoice]
30
+ response_finnet_with_trax_type_195Code_valid_update(convertToArray,conditions,valueConditions,params["mer_signature"])
31
+ end
32
+
33
+
34
+ def test_response_finnet_with_trax_type_Payment_result_code_00
35
+ params = {"trax_type" => "Payment",
36
+ "mer_signature" => "321B773E4219AAB60305335BFE5C9403A72CE926A8500243F5ECC47CC31AE353",
37
+ "merchant_id" => "TLKM034",
38
+ "invoice" => "IMB-2011121910284340",
39
+ "amount" => 6500000.0,
40
+ "add_info1" => "Tigor bukan bang toyib",
41
+ "add_info2" => "Pembayaran Izin Mendirikan Bangunan Bengkel/Pabrik",
42
+ "timeout" => 60,
43
+ "return_url" => "http://evandavid.alwaysdata.net/transaction_response",
44
+ "result_code" => "00",
45
+ "result_desc" => "",
46
+ "log_no" => "" ,
47
+ "payment_source" => ""
48
+ }
49
+ post :response_finnet, params
50
+ assert_not_nil params
51
+ assert_equal(params["trax_type"],"Payment")
52
+ assert_equal(params["result_code"],"00")
53
+ convertToArray=convert_params_to_array(params)
54
+ conditions = "payment_code"
55
+ valueConditions = params[:payment_code]
56
+ response_finnet_with_trax_type_195Code_valid_update(convertToArray,conditions,valueConditions,params["mer_signature"])
57
+ end
58
+
59
+ def test_response_finnet_with_trax_type_Payment_result_code_05
60
+ params = {"trax_type" => "Payment",
61
+ "mer_signature" => "321B773E4219AAB60305335BFE5C9403A72CE926A8500243F5ECC47CC31AE353",
62
+ "merchant_id" => "TLKM034",
63
+ "invoice" => "IMB-2011121910284340",
64
+ "amount" => 6500000.0,
65
+ "add_info1" => "Tigor bukan bang toyib",
66
+ "add_info2" => "Pembayaran Izin Mendirikan Bangunan Bengkel/Pabrik",
67
+ "timeout" => 60,
68
+ "return_url" => "http://evandavid.alwaysdata.net/transaction_response",
69
+ "result_code" => "05",
70
+ "result_desc" => "",
71
+ "log_no" => "" ,
72
+ "payment_source" => ""
73
+ }
74
+ post :response_finnet, params
75
+ assert_not_nil params
76
+ assert_equal(params["trax_type"],"Payment")
77
+ assert_equal(params["result_code"],"05")
78
+ convertToArray=convert_params_to_array(params)
79
+ conditions = "payment_code"
80
+ valueConditions = params[:payment_code]
81
+ response_finnet_with_trax_type_195Code_valid_update(convertToArray,conditions,valueConditions,params["mer_signature"])
82
+ end
83
+
84
+ def test_response_finnet_invalid_signature_with_trax_type_195Code
85
+ params = {"trax_type" => "195Code",
86
+ "mer_signature" => "321B773E4219AAB60305335BFE5C9403A72CE926A8500243F5ECC47CC31AE",
87
+ "merchant_id" => "TLKM034",
88
+ "invoice" => "IMB-2011121910284340",
89
+ "amount" => 6500000.0,
90
+ "add_info1" => "Tigor bukan bang toyib",
91
+ "add_info2" => "Pembayaran Izin Mendirikan Bangunan Bengkel/Pabrik",
92
+ "timeout" => 60,
93
+ "return_url" => "http://evandavid.alwaysdata.net/transaction_response",
94
+ "result_code" => "05",
95
+ "result_desc" => "",
96
+ "log_no" => "" ,
97
+ "payment_source" => ""
98
+ }
99
+ post :response_finnet, params
100
+ assert_not_nil params
101
+ assert_equal(params["trax_type"],"195Code")
102
+ convertToArray=convert_params_to_array(params)
103
+ conditions = "invoice"
104
+ valueConditions = params[:invoice]
105
+ response_finnet_with_trax_type_195Code_invalid_update(convertToArray,conditions,valueConditions,params["mer_signature"])
106
+ end
107
+
108
+ def test_response_finnet_invalid_signature_with_trax_type_Payment_result_code_00
109
+ params = {"trax_type" => "Payment",
110
+ "mer_signature" => "321B773E4219AAB60305335BFE5C9403A72CE926A8500243F5ECC47CC31A",
111
+ "merchant_id" => "TLKM034",
112
+ "invoice" => "IMB-2011121910284340",
113
+ "amount" => 6500000.0,
114
+ "add_info1" => "Tigor bukan bang toyib",
115
+ "add_info2" => "Pembayaran Izin Mendirikan Bangunan Bengkel/Pabrik",
116
+ "timeout" => 60,
117
+ "return_url" => "http://evandavid.alwaysdata.net/transaction_response",
118
+ "result_code" => "00",
119
+ "result_desc" => "",
120
+ "log_no" => "" ,
121
+ "payment_source" => ""
122
+ }
123
+ post :response_finnet, params
124
+ assert_not_nil params
125
+ assert_equal(params["trax_type"],"Payment")
126
+ assert_equal(params["result_code"],"00")
127
+ convertToArray=convert_params_to_array(params)
128
+ conditions = "payment_code"
129
+ valueConditions = params[:payment_code]
130
+ response_finnet_with_trax_type_195Code_invalid_update(convertToArray,conditions,valueConditions,params["mer_signature"])
131
+ end
132
+
133
+ def test_response_finnet_invalid_signature_with_trax_type_Payment_result_code_05
134
+ params = {"trax_type" => "Payment",
135
+ "mer_signature" => "321B773E4219AAB60305335BFE5C9403A72CE926A8500243F5ECC47CC31A",
136
+ "merchant_id" => "TLKM034",
137
+ "invoice" => "IMB-2011121910284340",
138
+ "amount" => 6500000.0,
139
+ "add_info1" => "Tigor bukan bang toyib",
140
+ "add_info2" => "Pembayaran Izin Mendirikan Bangunan Bengkel/Pabrik",
141
+ "timeout" => 60,
142
+ "return_url" => "http://evandavid.alwaysdata.net/transaction_response",
143
+ "result_code" => "05",
144
+ "result_desc" => "",
145
+ "log_no" => "" ,
146
+ "payment_source" => ""
147
+ }
148
+ post :response_finnet, params
149
+ assert_not_nil params
150
+ assert_equal(params["trax_type"],"Payment")
151
+ assert_equal(params["result_code"],"05")
152
+ convertToArray=convert_params_to_array(params)
153
+ conditions = "payment_code"
154
+ valueConditions = params[:payment_code]
155
+ response_finnet_with_trax_type_195Code_invalid_update(convertToArray,conditions,valueConditions,params["mer_signature"])
156
+ end
157
+
158
+ def response_finnet_with_trax_type_195Code_valid_update(params,conditions,valueConditions,signature)
159
+ api = FinnetApi::API.new
160
+ data=api.mer_signature_valid(signature,params)
161
+ assert_equal(data,true)
162
+ end
163
+
164
+ def response_finnet_with_trax_type_195Code_invalid_update(params,conditions,valueConditions,signature)
165
+ api = FinnetApi::API.new
166
+ data=api.mer_signature_valid(signature,params)
167
+ assert_equal(data,false)
168
+ end
169
+
170
+ def convert_params_to_array(params)
171
+ data = Array.new
172
+ data << ["merchant_id","#{params["merchant_id"]}"]
173
+ data << ["invoice","#{params["invoice"]}"]
174
+ data << ["amount",params["amount"]]
175
+ data << ["add_info1","#{params["add_info1"]}"]
176
+ data << ["add_info2","#{params["add_info2"]}"] unless params["add_info2"].nil?
177
+ data << ["add_info3","#{params["add_info3"]}"] unless params["add_info3"].nil?
178
+ data << ["add_info4","#{params["add_info4"]}"] unless params["add_info4"].nil?
179
+ data << ["add_info5","#{params["add_info5"]}"] unless params["add_info5"].nil?
180
+ data << ["timeout",params["timeout"]]
181
+ data << ["return_url",params["return_url"]]
182
+ return data
183
+ end
184
+
185
+ end
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'activerecord'
5
+ require 'action_controller'
6
+
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
9
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'generators','finnet_api','templates'))
10
+ require 'finnet_transaction'
11
+ require 'application_controller'
12
+ require 'finnet_transactions_controller'
13
+ require 'finnet_api'
14
+
15
+ class Test::Unit::TestCase
16
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: finnet_api
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 4
10
+ version: 0.0.4
11
+ platform: ruby
12
+ authors:
13
+ - Evan Kristian
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-02-22 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: finnet_api
22
+ email: ev.kristian@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README.rdoc
30
+ files:
31
+ - LICENSE
32
+ - README.rdoc
33
+ - Rakefile
34
+ - VERSION
35
+ - finnet_api-0.0.3.gem
36
+ - finnet_api.gemspec
37
+ - generators/finnet_api/USAGE
38
+ - generators/finnet_api/finnet_api_generator.rb
39
+ - generators/finnet_api/templates/finnet.yml
40
+ - generators/finnet_api/templates/finnet_transaction.rb
41
+ - generators/finnet_api/templates/finnet_transactions.yml
42
+ - generators/finnet_api/templates/finnet_transactions_controller.rb
43
+ - generators/finnet_api/templates/finnet_transactions_controller_test.rb
44
+ - generators/finnet_api/templates/migration.rb
45
+ - init.rb
46
+ - lib/finnet_api.rb
47
+ - test/finnet_api_test.rb
48
+ - test/finnet_transactions_controller_test.rb
49
+ - test/test_helper.rb
50
+ homepage: http://github.com/evandavid/finnet_api
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.8.15
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: finnet
83
+ test_files: []
84
+