poli 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in poli.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Andrew Feng
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # Poli
2
+
3
+ API to integrate POLi payment gateway -- http://www.polipayments.com
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'poli'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install poli
18
+
19
+ ## Usage
20
+
21
+ ### Use in irb
22
+ 1. check poli version in terminal:
23
+
24
+ > poli -v
25
+
26
+ 2. generate poli configuration file, and change it to use your merchant code and authentication code:
27
+
28
+ > poli generate
29
+
30
+ 3. go into irb, and type:
31
+
32
+ ```ruby
33
+ > require 'poli'
34
+ > Poli::POLi.load_config # by default will use poli.yml in current folder or ./config folder
35
+ > financialInstitutions = Poli::POLi.get_financialInstitutions
36
+ > puts financialInstitutions
37
+ ```
38
+
39
+ ### Use with rails app
40
+ 1. put configuration file to yourApp/config/ folder, or generate poli one under your app folder:
41
+
42
+ > poli generate
43
+
44
+ 2. put the following code into your initialize file, could be "config/initializers/all_init.rb":
45
+
46
+ > Poli::POLi.load_config
47
+
48
+
49
+ ### Sample of the configuration file
50
+
51
+ defaults: &DEFAULTS
52
+ currency_code: "AUD"
53
+ timeout: 900
54
+
55
+ homepage_url: http://localhost:3000
56
+ notification_url: http://localhost:3000/poli/notification
57
+ checkout_url: http://localhost:3000/profile/poli/checkout
58
+ successful_url: http://localhost:3000/profile/poli/successful
59
+ unsuccessful_url: http://localhost:3000/profile/poli/unsuccessful
60
+
61
+ initiate_transaction: https://merchantapi.apac.paywithpoli.com/MerchantAPIService.svc/Xml/transaction/initiate
62
+ get_transaction: https://merchantapi.apac.paywithpoli.com/MerchantAPIService.svc/Xml/transaction/query
63
+ get_financial_institutions: https://merchantapi.apac.paywithpoli.com/MerchantAPIService.svc/Xml/banks
64
+
65
+ merchant_code: 1111111
66
+ authentication_code: 222222222
67
+
68
+ development:
69
+ <<: *DEFAULTS
70
+ merchant_code: 1111111
71
+ authentication_code: 222222222
72
+
73
+ test:
74
+ <<: *DEFAULTS
75
+ merchant_code: 1111111
76
+ authentication_code: 222222222
77
+
78
+ production:
79
+ <<: *DEFAULTS
80
+ merchant_code: 1111111
81
+ authentication_code: 222222222
82
+
83
+ ## Contributing
84
+
85
+ 1. Fork it
86
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
87
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
88
+ 4. Push to the branch (`git push origin my-new-feature`)
89
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/poli ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- mode: ruby -*-
3
+
4
+ require 'poli/runner'
5
+
6
+ Poli::Runner.start
data/lib/poli.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "poli/version"
2
+ require "poli/poli"
3
+ require "poli/runner"
@@ -0,0 +1,34 @@
1
+ require 'thor/group'
2
+ require 'fileutils'
3
+
4
+ module Poli
5
+ module Generators
6
+ class Config < Thor::Group
7
+ include Thor::Actions
8
+
9
+ argument :path, :type => :string, :optional => true
10
+
11
+ def copy_config
12
+ file_path = path
13
+ unless file_path
14
+ file_path = File.join(Dir.pwd, 'config')
15
+ if File.directory?(file_path)
16
+ say("Seems like there is a /config folder, do you want Poli to generate configuration file to this folder?")
17
+ y = ask("Yes to generate to /config folder, others to generate to current folder. (Y|N): ")
18
+ unless "Y".casecmp(y) == 0
19
+ file_path = Dir.pwd
20
+ end
21
+ else
22
+ file_path = Dir.pwd
23
+ end
24
+ end
25
+
26
+ template "poli.yml", File.join(file_path, 'poli.yml')
27
+ end
28
+
29
+ def self.source_root
30
+ File.dirname(__FILE__)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,31 @@
1
+ defaults: &DEFAULTS
2
+ currency_code: "AUD"
3
+ timeout: 900
4
+
5
+ homepage_url: http://localhost:3000
6
+ notification_url: http://localhost:3000/poli/notification
7
+ checkout_url: http://localhost:3000/profile/poli/checkout
8
+ successful_url: http://localhost:3000/profile/poli/successful
9
+ unsuccessful_url: http://localhost:3000/profile/poli/unsuccessful
10
+
11
+ initiate_transaction: https://merchantapi.apac.paywithpoli.com/MerchantAPIService.svc/Xml/transaction/initiate
12
+ get_transaction: https://merchantapi.apac.paywithpoli.com/MerchantAPIService.svc/Xml/transaction/query
13
+ get_financial_institutions: https://merchantapi.apac.paywithpoli.com/MerchantAPIService.svc/Xml/banks
14
+
15
+ merchant_code: 1111111
16
+ authentication_code: 222222222
17
+
18
+ development:
19
+ <<: *DEFAULTS
20
+ merchant_code: 1111111
21
+ authentication_code: 222222222
22
+
23
+ test:
24
+ <<: *DEFAULTS
25
+ merchant_code: 1111111
26
+ authentication_code: 222222222
27
+
28
+ production:
29
+ <<: *DEFAULTS
30
+ merchant_code: 1111111
31
+ authentication_code: 222222222
data/lib/poli/poli.rb ADDED
@@ -0,0 +1,100 @@
1
+ require "yaml"
2
+ require 'nokogiri'
3
+ require 'httparty'
4
+ require 'date'
5
+
6
+ module Poli
7
+ class POLi
8
+ class << self
9
+
10
+ def load_config(config_file = nil)
11
+ unless config_file
12
+ if defined? Rails
13
+ config_file = "#{Rails.root}/config/poli.yml"
14
+ else
15
+ config_file = File.join(Dir.pwd, 'config/poli.yml')
16
+ unless File.file?(config_file)
17
+ config_file = File.join(Dir.pwd, 'poli.yml')
18
+ end
19
+ end
20
+ raise "poli.yml not found, generate one with 'poli generate [path]'." unless File.file?(config_file)
21
+ end
22
+
23
+ @settings = YAML.load_file(config_file)
24
+ @settings = (defined? Rails) ? @settings[Rails.env] : @settings["defaults"]
25
+ end
26
+
27
+ def settings
28
+ @settings.dup
29
+ end
30
+
31
+ def get_financialInstitutions
32
+ body = build_financialInstitutions_xml
33
+ response = HTTParty.post @settings['get_financial_institutions'], :body => body, :headers => {'Content-type' => 'text/xml'}
34
+ institutions = response.parsed_response["GetFinancialInstitutionsResponse"]
35
+ banks = []
36
+ if institutions and institutions["FinancialInstitutionList"] and institutions["FinancialInstitutionList"]["FinancialInstitution"]
37
+ institutions["FinancialInstitutionList"]["FinancialInstitution"].each do |institute|
38
+ banks << institute["FinancialInstitutionName"]
39
+ end
40
+ end
41
+ banks
42
+ end
43
+
44
+ def initiate_transaction(amount, merchant_ref, ip)
45
+ body = build_initiate_xml(amount, merchant_ref, ip)
46
+ response = HTTParty.post @settings['initiate_transaction'], :body => body, :headers => {'Content-type' => 'text/xml'}
47
+ poli_transfer = response.parsed_response["InitiateTransactionResponse"]
48
+ poli_transfer["Transaction"] if poli_transfer
49
+ end
50
+
51
+ def get_transaction(token)
52
+ body = build_transaction_xml(token)
53
+ response = HTTParty.post @settings['get_transaction'], :body => body, :headers => {'Content-type' => 'text/xml'}
54
+ poli_transaction = response.parsed_response["GetTransactionResponse"]
55
+ return [poli_transaction["Errors"], poli_transaction["TransactionStatusCode"], poli_transaction["Transaction"]] if poli_transaction
56
+ end
57
+
58
+ private
59
+ def build_financialInstitutions_xml
60
+ doc = Nokogiri::XML('<GetFinancialInstitutionsRequest xmlns="http://schemas.datacontract.org/2004/07/Centricom.POLi.Services.MerchantAPI.Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"></GetFinancialInstitutionsRequest>')
61
+ doc.root.add_child("<AuthenticationCode>#{@settings['authentication_code']}</AuthenticationCode>")
62
+ doc.root.add_child("<MerchantCode>#{@settings['merchant_code']}</MerchantCode>")
63
+ doc.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION).strip
64
+ end
65
+
66
+ def build_initiate_xml(amount, merchant_ref, ip)
67
+ doc = Nokogiri::XML('<InitiateTransactionRequest xmlns="http://schemas.datacontract.org/2004/07/Centricom.POLi.Services.MerchantAPI.Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"></InitiateTransactionRequest>')
68
+ doc.root.add_child("<AuthenticationCode>#{@settings['authentication_code']}</AuthenticationCode>")
69
+
70
+ transaction = doc.root.add_child('<Transaction xmlns:dco="http://schemas.datacontract.org/2004/07/Centricom.POLi.Services.MerchantAPI.DCO"></Transaction>')[0]
71
+ transaction.add_child("<dco:CurrencyAmount>#{sprintf("%.2f", amount)}</dco:CurrencyAmount>")
72
+ transaction.add_child("<dco:CurrencyCode>#{@settings['currency_code']}</dco:CurrencyCode>")
73
+ transaction.add_child("<dco:MerchantCheckoutURL>#{@settings['checkout_url']}</dco:MerchantCheckoutURL>")
74
+ transaction.add_child("<dco:MerchantCode>#{@settings['merchant_code']}</dco:MerchantCode>")
75
+
76
+ transaction.add_child("<dco:MerchantData>from-poli</dco:MerchantData>")
77
+ transaction.add_child("<dco:MerchantDateTime>#{DateTime.now.strftime("%Y-%m-%dT%H:%M:%S")}</dco:MerchantDateTime>")
78
+ transaction.add_child("<dco:MerchantHomePageURL>#{@settings['homepage_url']}</dco:MerchantHomePageURL>")
79
+ transaction.add_child("<dco:MerchantRef>#{merchant_ref}</dco:MerchantRef>")
80
+ transaction.add_child("<dco:NotificationURL>#{@settings['notification_url']}</dco:NotificationURL>")
81
+ transaction.add_child("<dco:SuccessfulURL>#{@settings['successful_url']}</dco:SuccessfulURL>")
82
+
83
+ transaction.add_child("<dco:Timeout>#{@settings['timeout']}</dco:Timeout>")
84
+ transaction.add_child("<dco:UnsuccessfulURL>#{@settings['unsuccessful_url']}</dco:UnsuccessfulURL>")
85
+ transaction.add_child("<dco:UserIPAddress>#{ip}</dco:UserIPAddress>")
86
+
87
+ doc.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION).strip
88
+ end
89
+
90
+ def build_transaction_xml(token)
91
+ doc = Nokogiri::XML('<GetTransactionRequest xmlns="http://schemas.datacontract.org/2004/07/Centricom.POLi.Services.MerchantAPI.Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"></GetTransactionRequest>')
92
+ doc.root.add_child("<AuthenticationCode>#{@settings['authentication_code']}</AuthenticationCode>")
93
+ doc.root.add_child("<MerchantCode>#{@settings['merchant_code']}</MerchantCode>")
94
+ doc.root.add_child("<TransactionToken>#{token}</TransactionToken>")
95
+ doc.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION).strip
96
+ end
97
+
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,19 @@
1
+ require 'thor'
2
+ require 'poli/version'
3
+ require 'poli/generators/config.rb'
4
+
5
+ class Poli::Runner < Thor
6
+ map "-v" => :version
7
+
8
+ desc "version", "Show Poli version"
9
+ def version
10
+ say "Poli #{Poli::VERSION}"
11
+ end
12
+
13
+ desc "generate", "Generate configuration file for Poli"
14
+ method_options :path => :string, :optional => true
15
+ def generate
16
+ Poli::Generators::Config.start([options[:path]])
17
+ end
18
+
19
+ end
@@ -0,0 +1,3 @@
1
+ module Poli
2
+ VERSION = "0.0.1"
3
+ end
data/poli.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 'poli/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "poli"
8
+ spec.version = Poli::VERSION
9
+ spec.authors = ["Andrew Feng"]
10
+ spec.email = ["mingliangfeng@gmail.com"]
11
+ spec.description = %q{API to integrate POLi payment gateway -- http://www.polipayments.com}
12
+ spec.summary = %q{API to integrate POLi payment gateway -- http://www.polipayments.com}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "httparty"
22
+ spec.add_dependency "nokogiri"
23
+ spec.add_dependency "thor"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+
28
+ spec.add_development_dependency "rspec", "~> 2.6"
29
+ end
data/spec/poli_spec.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'poli'
2
+
3
+ describe Poli::POLi do
4
+
5
+ it "load config should initialize POLi" do
6
+ Poli::POLi.load_config
7
+ Poli::POLi.settings.empty?.should be_false
8
+ end
9
+
10
+ context "POLI API" do
11
+ before(:all) { Poli::POLi.load_config }
12
+
13
+ it "get_financialInstitutions" do
14
+ Poli::POLi.get_financialInstitutions.size.should be > 0
15
+ end
16
+
17
+ it "initiate and get transaction" do
18
+ transaction = Poli::POLi.initiate_transaction(1.00, "Test Ref", "119.225.58.230")
19
+ transaction.should_not be_nil
20
+
21
+ transaction2 = Poli::POLi.get_transaction(transaction["TransactionToken"])
22
+ transaction2.last.should_not be_nil
23
+ end
24
+
25
+ end
26
+
27
+ context "Test generator" do
28
+
29
+ it "poli version test" do
30
+ Poli::Runner.start(["-v"])
31
+ end
32
+
33
+ it "poli version test" do
34
+ Poli::Runner.start(["generate"])
35
+ end
36
+
37
+ end
38
+
39
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: poli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrew Feng
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: nokogiri
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: thor
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '1.3'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '1.3'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rake
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rspec
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: '2.6'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: '2.6'
110
+ description: API to integrate POLi payment gateway -- http://www.polipayments.com
111
+ email:
112
+ - mingliangfeng@gmail.com
113
+ executables:
114
+ - poli
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - bin/poli
124
+ - lib/poli.rb
125
+ - lib/poli/generators/config.rb
126
+ - lib/poli/generators/poli.yml
127
+ - lib/poli/poli.rb
128
+ - lib/poli/runner.rb
129
+ - lib/poli/version.rb
130
+ - poli.gemspec
131
+ - spec/poli_spec.rb
132
+ homepage: ''
133
+ licenses:
134
+ - MIT
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ! '>='
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ! '>='
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 1.8.21
154
+ signing_key:
155
+ specification_version: 3
156
+ summary: API to integrate POLi payment gateway -- http://www.polipayments.com
157
+ test_files:
158
+ - spec/poli_spec.rb