pg_recurrence 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem "crack"
7
+ gem "pg_serializer"
8
+ gem "pg_connection"
9
+ gem "pg_creditcard"
10
+ gem "mocha"
11
+
12
+ # Add dependencies to develop your gem here.
13
+ # Include everything needed to run rake, tests, features, etc.
14
+ group :development do
15
+ gem "bundler", "~> 1.0.0"
16
+ gem "jeweler", "~> 1.5.1"
17
+ gem "rcov", ">= 0"
18
+ end
19
+
20
+ group :test do
21
+ gem "mocha"
22
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,33 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ builder (3.0.0)
5
+ crack (0.1.8)
6
+ creditcard (1.0)
7
+ git (1.2.5)
8
+ jeweler (1.5.1)
9
+ bundler (~> 1.0.0)
10
+ git (>= 1.2.5)
11
+ rake
12
+ mocha (0.9.10)
13
+ rake
14
+ pg_connection (0.1.2)
15
+ pg_creditcard (0.2.1)
16
+ creditcard
17
+ pg_serializer (0.1.2)
18
+ builder
19
+ rake (0.8.7)
20
+ rcov (0.9.9)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ bundler (~> 1.0.0)
27
+ crack
28
+ jeweler (~> 1.5.1)
29
+ mocha
30
+ pg_connection
31
+ pg_creditcard
32
+ pg_serializer
33
+ rcov
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Simon Chiu
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.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = pg_recurrence
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to pg_recurrence
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Simon Chiu. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,65 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "pg_recurrence"
16
+ gem.homepage = "http://github.com/drchiu/pg_recurrence"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Unofficial Psigate recurring billing library for Ruby}
19
+ gem.description = %Q{Unofficial Psigate recurring billing library for Ruby}
20
+ gem.email = "skhchiu@gmail.com"
21
+ gem.authors = ["Simon Chiu"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ Rake::TestTask.new(:unit) do |test|
37
+ test.libs << 'lib' << 'test'
38
+ test.pattern = 'test/unit/test_*.rb'
39
+ test.verbose = true
40
+ end
41
+
42
+ Rake::TestTask.new(:remote) do |test|
43
+ test.libs << 'lib' << 'test'
44
+ test.pattern = 'test/remote/test_*.rb'
45
+ test.verbose = true
46
+ end
47
+
48
+ require 'rcov/rcovtask'
49
+ Rcov::RcovTask.new do |test|
50
+ test.libs << 'test'
51
+ test.pattern = 'test/**/test_*.rb'
52
+ test.verbose = true
53
+ end
54
+
55
+ require 'rake/rdoctask'
56
+ Rake::RDocTask.new do |rdoc|
57
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
58
+
59
+ rdoc.rdoc_dir = 'rdoc'
60
+ rdoc.title = "pg_recurrence #{version}"
61
+ rdoc.rdoc_files.include('README*')
62
+ rdoc.rdoc_files.include('lib/**/*.rb')
63
+ end
64
+
65
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,10 @@
1
+ require 'pg_creditcard'
2
+ require 'pg_connection'
3
+ require 'pg_serializer'
4
+ require 'crack'
5
+
6
+ require 'ruby_psigate/request'
7
+ require 'ruby_psigate/response'
8
+ require 'ruby_psigate/credential'
9
+ require 'ruby_psigate/account'
10
+ require 'ruby_psigate/charge'
@@ -0,0 +1,178 @@
1
+ module RubyPsigate
2
+ class Account < Request
3
+
4
+ attr_accessor :accountid, :name, :company, :address1, :address2, :city, :province, :postalcode, :country, :phone, :fax, :email, :comments, :status, :error
5
+ attr_accessor :returnmessage, :returncode, :action, :response
6
+ alias_method :state, :province
7
+ alias_method :state=, :province=
8
+ alias_method :zipcode, :postalcode
9
+ alias_method :zipcode=, :postalcode=
10
+ attr_reader :credit_card
11
+
12
+ def credit_card=(credit_card)
13
+ raise ArgumentError unless credit_card.is_a?(PgCreditcard)
14
+ @credit_card = credit_card
15
+ end
16
+
17
+ def initialize(attributes={})
18
+ attributes.each_pair do |attribute, value|
19
+ if self.respond_to?(attribute)
20
+ setter = "#{attribute}=".to_sym
21
+ self.send(setter, value)
22
+ end
23
+ end
24
+ super
25
+ end
26
+
27
+ def attributes=(attributes={})
28
+ attributes.each_pair do |attribute, value|
29
+ if self.respond_to?(attribute)
30
+ setter = "#{attribute}=".to_sym
31
+ self.send(setter, value)
32
+ end
33
+ end
34
+ end
35
+
36
+ def new_record?
37
+ status.nil? ? true : false
38
+ end
39
+
40
+ def save
41
+ begin
42
+ # Action
43
+ action = new_record? ? "AMA01" : "AMA02"
44
+ @request[:Request][:Action] = action
45
+
46
+ if new_record?
47
+ # Account Details
48
+ @request[:Request][:Account] = {}
49
+ %w( AccountID Name Company Address1 Address2 City Province Postalcode Country Phone Fax Email Comments ).each do |a|
50
+ value = self.send((a.downcase).to_sym)
51
+ @request[:Request][:Account][a.to_sym] = value if value
52
+ end
53
+
54
+ # Credit Card
55
+ if !credit_card.nil? && credit_card.valid?
56
+ @request[:Request][:Account][:CardInfo] = {}
57
+ %w( CardHolder CardNumber CardExpMonth CardExpYear ).each do |c|
58
+ value = credit_card.send((c.downcase).to_sym)
59
+ @request[:Request][:Account][:CardInfo][c.to_sym] = value if value
60
+ end
61
+ end
62
+ else # Is an update
63
+ # Set account ID
64
+ @request[:Request][:Condition] = {:AccountID => accountid}
65
+
66
+ @request[:Request][:Update] = {}
67
+ %w( Name Company Address1 Address2 City Province Postalcode Country Phone Fax Email Comments ).each do |a|
68
+ value = self.send((a.downcase).to_sym)
69
+ @request[:Request][:Update][a.to_sym] = value if value
70
+ end
71
+ end
72
+
73
+ # Creates parameters
74
+ params = RubyPsigate::Serializer.new(@request, :header => true).to_xml
75
+ connection = RubyPsigate::Connection.new(self.class.credential.endpoint)
76
+ response = connection.post(params)
77
+ response = Response.new(response)
78
+ self.response = response
79
+
80
+ if response.success? && (response.returncode == "RPA-0000" && new_record?) || (response.returncode == "RPA-0022" && !new_record?)
81
+ %w( accountid name company address1 address2 city province postalcode country phone fax comments ).each do |attribute|
82
+ self.send("#{attribute}=".to_sym, response.send(attribute.to_sym))
83
+ end
84
+ result = true
85
+ else
86
+ self.send(:error=, response.returnmessage)
87
+ result = false
88
+ end
89
+ rescue ConnectionError => e
90
+ result = false
91
+ end
92
+ result
93
+ end
94
+
95
+ def self.find(accountid)
96
+ begin
97
+ params = {
98
+ :Request => {
99
+ :CID => credential.cid,
100
+ :UserID => credential.userid,
101
+ :Password => credential.password,
102
+ :Action => "AMA05",
103
+ :Condition => { :AccountID => accountid }
104
+ }
105
+ }
106
+
107
+ @result = Request.new
108
+ @result.params = params
109
+ @result = @result.post
110
+
111
+ if @result.returncode == "RPA-0020"
112
+ # Adds basic attributes
113
+ attributes = {}
114
+ %w( AccountID Name Company Address1 Address2 City Province Postalcode Country Phone Fax Email Comments ).each do |attribute|
115
+ attributes[attribute.downcase.to_sym] = @result.send(attribute.downcase.to_sym)
116
+ end
117
+
118
+ attributes[:status] = @result.response["Account"]["Status"]
119
+
120
+ # Back end info
121
+ %w( returnmessage returncode action ).each do |info|
122
+ attributes[info.downcase.to_sym] = @result.send(info.downcase.to_sym)
123
+ end
124
+
125
+ # Adds credit card attribute
126
+ %w( status serialno cardholder cardnumber cardexpmonth cardexpyear cardtype ).each do |cc|
127
+ # TODO
128
+ end
129
+
130
+ @account = Account.new(attributes)
131
+ else
132
+ @account = nil
133
+ end
134
+ rescue ConnectionError => e
135
+ @account = nil
136
+ end
137
+ @account
138
+ end
139
+
140
+ def self.enable(accountid)
141
+ response = self.do(:action => "AMA08", :accountid => accountid, :success => "RPA-0046")
142
+ response
143
+ end
144
+
145
+ def self.disable(accountid)
146
+ response = self.do(:action => "AMA09", :accountid => accountid, :success => "RPA-0040")
147
+ response
148
+ end
149
+
150
+ private
151
+
152
+ def self.do(opts = {})
153
+ action = opts[:action]
154
+ accountid = opts[:accountid]
155
+ success = opts[:success]
156
+ begin
157
+ params = {
158
+ :Request => {
159
+ :CID => credential.cid,
160
+ :UserID => credential.userid,
161
+ :Password => credential.password,
162
+ :Action => action,
163
+ :Condition => { :AccountID => accountid }
164
+ }
165
+ }
166
+
167
+ result = Request.new
168
+ result.params = params
169
+ result = result.post
170
+ response = result.returncode == success ? true : false
171
+ rescue ConnectionError => e
172
+ response = false
173
+ end
174
+ response
175
+ end
176
+
177
+ end
178
+ end
@@ -0,0 +1,227 @@
1
+ module RubyPsigate
2
+ class Charge < Request
3
+
4
+ attr_accessor :accountid, :status, :productid, :quantity, :price, :interval, :rbtrigger, :starttime, :endtime, :response, :rbname, :rbcid
5
+
6
+ def self.serialno
7
+ @serialno
8
+ end
9
+
10
+ def self.serialno=(x)
11
+ @serialno=x
12
+ end
13
+
14
+ def self.find(rbcid)
15
+ begin
16
+ params = {
17
+ :Request => {
18
+ :CID => credential.cid,
19
+ :UserID => credential.userid,
20
+ :Password => credential.password,
21
+ :Action => "RBC00",
22
+ :Condition => { :RBCID => rbcid }
23
+ }
24
+ }
25
+
26
+ @result = Request.new
27
+ @result.params = params
28
+ @result = @result.post
29
+
30
+ if @result.returncode == "RRC-0060"
31
+ # Adds basic attributes
32
+ attributes = {}
33
+ %w( rbcid interval trigger status ).each do |info|
34
+ attributes[info.downcase.to_sym] = @result.send(info.downcase.to_sym)
35
+ end
36
+
37
+ attributes["starttime"] = @result.startdate
38
+ attributes["endtime"] = @result.enddate
39
+ attributes["response"] = @result.response
40
+
41
+ @charge = Charge.new(attributes)
42
+ else
43
+ @charge = nil
44
+ end
45
+ rescue ConnectionError => e
46
+ @charge = nil
47
+ end
48
+ @charge
49
+ end
50
+
51
+ def self.update(options={})
52
+ rbname = options[:rbname]
53
+ rbcid = options[:rbcid]
54
+ serialno = options[:serialno]
55
+ interval = options[:interval]
56
+ rbtrigger = options[:rbtrigger]
57
+ starttime = options[:starttime]
58
+ endtime = options[:endtime]
59
+
60
+ begin
61
+ params = {
62
+ :Request => {
63
+ :CID => credential.cid,
64
+ :UserID => credential.userid,
65
+ :Password => credential.password,
66
+ :Action => "RBC02",
67
+ :Condition => { :RBCID => rbcid },
68
+ :Update => {
69
+ :RBName => rbname,
70
+ :SerialNo => serialno,
71
+ :Interval => interval,
72
+ :RBTrigger => rbtrigger,
73
+ :StartTime => starttime,
74
+ :EndTime => endtime
75
+ }
76
+ }
77
+ }
78
+
79
+ @result = Request.new
80
+ @result.params = params
81
+ @result = @result.post
82
+
83
+ if @result.returncode = "RRC-0072"
84
+ result = true
85
+ else
86
+ result = false
87
+ end
88
+ rescue ConnectionError => e
89
+ result = false
90
+ end
91
+ result
92
+ end
93
+
94
+ def self.enable(rbcid)
95
+ toggle(rbcid, :on)
96
+ end
97
+
98
+ def self.disable(rbcid)
99
+ toggle(rbcid, :off)
100
+ end
101
+
102
+ def initialize(attributes={})
103
+ attributes.each_pair do |attribute, value|
104
+ if self.respond_to?(attribute)
105
+ setter = "#{attribute}=".to_sym
106
+ self.send(setter, value)
107
+ end
108
+ end
109
+ super
110
+ end
111
+
112
+ # Creates (adds) or Updates recurring charges
113
+ def save
114
+ raise ArgumentError, "StoreID is not specified in superclass" if self.class.storeid.nil?
115
+ begin
116
+ @request[:Request][:Action] = "RBC01"
117
+ @request[:Request][:Charge] = {
118
+ :RBName => rbname,
119
+ :StoreID => self.class.storeid,
120
+ :SerialNo => self.class.serialno,
121
+ :AccountID => accountid,
122
+ :Interval => interval,
123
+ :RBTrigger => rbtrigger,
124
+ :StartTime => starttime,
125
+ :EndTime => endtime,
126
+ :ItemInfo => {
127
+ :ProductID => productid,
128
+ :Quantity => quantity,
129
+ :Price => price
130
+ }
131
+ }
132
+
133
+ # Creates parameters
134
+ params = RubyPsigate::Serializer.new(@request, :header => true).to_xml
135
+ connection = RubyPsigate::Connection.new(self.class.credential.endpoint)
136
+ response = connection.post(params)
137
+ response = Response.new(response)
138
+ self.response = response
139
+
140
+ if response.success? && response.returncode == "RRC-0000"
141
+ self.rbcid = response.rbcid
142
+ result = true
143
+ else
144
+ result = false
145
+ end
146
+ rescue ConnectionError => e
147
+ result = false
148
+ end
149
+ result
150
+ end
151
+
152
+ # Immediately charges the credit card
153
+ def immediately
154
+ raise ArgumentError, "StoreID is not specified in superclass" if self.class.storeid.nil?
155
+ begin
156
+ @request[:Request][:Action] = "RBC99"
157
+ @request[:Request][:Charge] = {
158
+ :StoreID => self.class.storeid,
159
+ :SerialNo => self.class.serialno,
160
+ :AccountID => accountid,
161
+ :ItemInfo => {
162
+ :ProductID => productid,
163
+ :Quantity => quantity,
164
+ :Price => price
165
+ }
166
+ }
167
+
168
+ # Creates parameters
169
+ params = RubyPsigate::Serializer.new(@request, :header => true).to_xml
170
+ connection = RubyPsigate::Connection.new(self.class.credential.endpoint)
171
+ response = connection.post(params)
172
+ response = Response.new(response)
173
+ self.response = response
174
+
175
+ if response.success? && response.returncode == "PSI-0000"
176
+ result = true
177
+ else
178
+ result = false
179
+ end
180
+ rescue ConnectionError => e
181
+ result = false
182
+ end
183
+ result
184
+ end
185
+
186
+ private
187
+
188
+ # Toggles a charge to be active or inactive
189
+ def self.toggle(rbcid, set_action)
190
+ action = case set_action
191
+ when :on
192
+ "RBC08"
193
+ when :off
194
+ "RBC09"
195
+ end
196
+
197
+ begin
198
+ params = {
199
+ :Request => {
200
+ :CID => credential.cid,
201
+ :UserID => credential.userid,
202
+ :Password => credential.password,
203
+ :Action => action,
204
+ :Condition => { :RBCID => rbcid }
205
+ }
206
+ }
207
+
208
+ @result = Request.new
209
+ @result.params = params
210
+ @result = @result.post
211
+
212
+ if set_action==:on && @result.returncode == "RRC-0190"
213
+ result = true
214
+ elsif set_action==:off && @result.returncode == "RRC-0090"
215
+ result = true
216
+ else
217
+ result = false
218
+ end
219
+
220
+ rescue ConnectionError => e
221
+ result = false
222
+ end
223
+ result
224
+ end
225
+
226
+ end
227
+ end