zuora4r 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,124 @@
1
+ module ZUORA
2
+
3
+ endpoint_url = ARGV.shift
4
+ obj = Soap.new(endpoint_url)
5
+
6
+ # run ruby with -d to see SOAP wiredumps.
7
+ obj.wiredump_dev = STDERR if $DEBUG
8
+
9
+ # SYNOPSIS
10
+ # login(parameters)
11
+ #
12
+ # ARGS
13
+ # parameters Login - {http://api.zuora.com/}login
14
+ #
15
+ # RETURNS
16
+ # parameters LoginResponse - {http://api.zuora.com/}loginResponse
17
+ #
18
+ # RAISES
19
+ # fault LoginFault - {http://fault.api.zuora.com/}LoginFault
20
+ # fault UnexpectedErrorFault - {http://fault.api.zuora.com/}UnexpectedErrorFault
21
+ #
22
+ parameters = nil
23
+ puts obj.login(parameters)
24
+
25
+ # SYNOPSIS
26
+ # subscribe(parameters)
27
+ #
28
+ # ARGS
29
+ # parameters Subscribe - {http://api.zuora.com/}subscribe
30
+ #
31
+ # RETURNS
32
+ # parameters SubscribeResponse - {http://api.zuora.com/}subscribeResponse
33
+ #
34
+ # RAISES
35
+ # fault UnexpectedErrorFault - {http://fault.api.zuora.com/}UnexpectedErrorFault
36
+ #
37
+ parameters = nil
38
+ puts obj.subscribe(parameters)
39
+
40
+ # SYNOPSIS
41
+ # subscribeWithExistingAccount(parameters)
42
+ #
43
+ # ARGS
44
+ # parameters SubscribeWithExistingAccount - {http://api.zuora.com/}subscribeWithExistingAccount
45
+ #
46
+ # RETURNS
47
+ # parameters SubscribeResponse - {http://api.zuora.com/}subscribeResponse
48
+ #
49
+ # RAISES
50
+ # fault UnexpectedErrorFault - {http://fault.api.zuora.com/}UnexpectedErrorFault
51
+ #
52
+ parameters = nil
53
+ puts obj.subscribeWithExistingAccount(parameters)
54
+
55
+ # SYNOPSIS
56
+ # create(parameters)
57
+ #
58
+ # ARGS
59
+ # parameters Create - {http://api.zuora.com/}create
60
+ #
61
+ # RETURNS
62
+ # parameters CreateResponse - {http://api.zuora.com/}createResponse
63
+ #
64
+ # RAISES
65
+ # fault InvalidTypeFault - {http://fault.api.zuora.com/}InvalidTypeFault
66
+ # fault UnexpectedErrorFault - {http://fault.api.zuora.com/}UnexpectedErrorFault
67
+ #
68
+ parameters = nil
69
+ puts obj.create(parameters)
70
+
71
+ # SYNOPSIS
72
+ # update(parameters)
73
+ #
74
+ # ARGS
75
+ # parameters Update - {http://api.zuora.com/}update
76
+ #
77
+ # RETURNS
78
+ # parameters UpdateResponse - {http://api.zuora.com/}updateResponse
79
+ #
80
+ # RAISES
81
+ # fault InvalidTypeFault - {http://fault.api.zuora.com/}InvalidTypeFault
82
+ # fault UnexpectedErrorFault - {http://fault.api.zuora.com/}UnexpectedErrorFault
83
+ #
84
+ parameters = nil
85
+ puts obj.update(parameters)
86
+
87
+ # SYNOPSIS
88
+ # delete(parameters)
89
+ #
90
+ # ARGS
91
+ # parameters Delete - {http://api.zuora.com/}delete
92
+ #
93
+ # RETURNS
94
+ # parameters DeleteResponse - {http://api.zuora.com/}deleteResponse
95
+ #
96
+ # RAISES
97
+ # fault InvalidTypeFault - {http://fault.api.zuora.com/}InvalidTypeFault
98
+ # fault InvalidValueFault - {http://fault.api.zuora.com/}InvalidValueFault
99
+ # fault UnexpectedErrorFault - {http://fault.api.zuora.com/}UnexpectedErrorFault
100
+ #
101
+ parameters = nil
102
+ puts obj.delete(parameters)
103
+
104
+ # SYNOPSIS
105
+ # query(parameters)
106
+ #
107
+ # ARGS
108
+ # parameters Query - {http://api.zuora.com/}query
109
+ #
110
+ # RETURNS
111
+ # parameters QueryResponse - {http://api.zuora.com/}queryResponse
112
+ #
113
+ # RAISES
114
+ # fault MalformedQueryFault - {http://fault.api.zuora.com/}MalformedQueryFault
115
+ # fault InvalidQueryLocatorFault - {http://fault.api.zuora.com/}InvalidQueryLocatorFault
116
+ # fault UnexpectedErrorFault - {http://fault.api.zuora.com/}UnexpectedErrorFault
117
+ #
118
+ parameters = nil
119
+ puts obj.query(parameters)
120
+
121
+
122
+
123
+
124
+ end
data/lib/zuora/api.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'singleton'
2
+
3
+ require 'zuora/ZUORA'
4
+ require 'zuora/ZUORADriver'
5
+ require 'zuora/ZUORAMappingRegistry'
6
+
7
+ module Zuora
8
+ module Billing
9
+ class Api
10
+ include Singleton
11
+ def initialize
12
+ @driver = ZUORA::Soap.new
13
+ end
14
+
15
+ attr_reader :driver
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,135 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Copyright 2010 Ning
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+
18
+ # if you get this error:
19
+ #
20
+ # 'uninitialized constant SOAP::Mapping::EncodedRegistry (NameError)'
21
+ #
22
+ # you need to follow the bellow steps to work around it
23
+ #
24
+ # gem uninstall soap4r
25
+ # curl -O http://dev.ctor.org/download/soap4r-1.5.8.tar.gz
26
+ # tar zxvf soap4r-1.5.8.tar.gz
27
+ # cd soap4r-1.5.8
28
+ # sudo ruby install.rb
29
+
30
+ require 'zuora_interface'
31
+ require 'zuora/api'
32
+
33
+ class SOAP::Header::HandlerSet
34
+ def reset
35
+ @store = XSD::NamedElements.new
36
+ end
37
+
38
+ def set(header)
39
+ reset
40
+ add header
41
+ end
42
+ end
43
+
44
+
45
+ class ZuoraClient
46
+ PROD_URL = 'https://www.zuora.com/apps/services/a/21.0'
47
+ SANDBOX_URL = 'https://apisandbox.zuora.com/apps/services/a/23.0'
48
+
49
+ def initialize(username, password, url=PROD_URL)
50
+ $ZUORA_USER = username
51
+ $ZUORA_PASSWORD = password
52
+ $ZUORA_ENDPOINT = url
53
+
54
+ @client = ZuoraInterface.new
55
+ @client.session_start
56
+ end
57
+
58
+ def query(query_string)
59
+
60
+ query_string =~ /select\s+(.+)\s+from/i
61
+ fields = ($1.split /,\s+/).map do |f|
62
+ f.gsub!(/\b\w/) { $&.downcase }
63
+ end
64
+
65
+ begin
66
+ response = @client.query(query_string)
67
+ rescue Exception => e
68
+ puts e.message
69
+ end
70
+
71
+ result = []
72
+ if response && response.result && response.result.size > 0
73
+ response.result.records.each do |record|
74
+ row = {}
75
+ fields.each do |f|
76
+ row[f] = record.send(f)
77
+ end
78
+ result << row
79
+ end
80
+ end
81
+ result
82
+ end
83
+
84
+ def create(obj)
85
+ begin
86
+ response = @client.create(obj)
87
+ result = save_results_to_hash(response)
88
+ rescue Exception => e
89
+ puts e.message
90
+ end
91
+ result || []
92
+ end
93
+
94
+ def generate(obj)
95
+ begin
96
+ response = @client.generate(obj)
97
+ result = save_results_to_hash(response)
98
+ rescue Exception => e
99
+ puts e.message
100
+ end
101
+ result || []
102
+ end
103
+
104
+ def update(obj)
105
+ begin
106
+ response = @client.update(obj)
107
+ result = save_results_to_hash(response)
108
+ rescue Exception => e
109
+ puts e.message
110
+ end
111
+ result || []
112
+ end
113
+
114
+ private
115
+
116
+ def save_results_to_hash(save_results)
117
+ result = []
118
+ puts save_results.inspect
119
+ save_results.each do |record|
120
+ row = {:success => record.success}
121
+ if record.success
122
+ row[:id] = record.id
123
+ else
124
+ row[:errors] = []
125
+ record.errors.each do |error|
126
+ row[:errors] << {:message => error.message, :code => error.code}
127
+ end
128
+ end
129
+ result << row
130
+ end
131
+ result
132
+ end
133
+
134
+
135
+ end
@@ -0,0 +1,198 @@
1
+ require 'zuora/api'
2
+
3
+ class ZuoraInterface
4
+
5
+ def make_account(accountName)
6
+ acc = ZUORA::Account.new
7
+ acc.allowInvoiceEdit = 0
8
+ acc.name = accountName
9
+ acc.currency = "USD"
10
+ acc.autoPay = 0
11
+ acc.status = "Draft"
12
+ acc.paymentTerm = "Due Upon Receipt"
13
+ acc.batch = "Batch1"
14
+ acc.billCycleDay = "01"
15
+ return acc
16
+ end
17
+
18
+ def make_contact(accountId)
19
+ con = ZUORA::Contact.new
20
+ con.accountId = accountId
21
+ con.address1 = '4901 Morena Blvd';
22
+ con.city = 'San Diego';
23
+ con.country = 'United States';
24
+ con.firstName = 'Robert';
25
+ con.lastName = 'Smith';
26
+ con.postalCode = '92117';
27
+ con.state = 'Virginia';
28
+ con.workEmail = 'robert@smith.com';
29
+ return con
30
+ end
31
+
32
+ def make_payment_method(accountId)
33
+ pmt = ZUORA::PaymentMethod.new
34
+ pmt.accountId = accountId
35
+ pmt.creditCardAddress1 = '52 Vexford Lane';
36
+ pmt.creditCardCity = 'Anaheim';
37
+ pmt.creditCardCountry = 'United States';
38
+ pmt.creditCardExpirationMonth = '12';
39
+ pmt.creditCardExpirationYear = '2010';
40
+ pmt.creditCardHolderName = 'Firstly Lastly';
41
+ pmt.creditCardNumber = '4111111111111111';
42
+ pmt.creditCardPostalCode = '22042';
43
+ pmt.creditCardState = 'California';
44
+ pmt.creditCardType = 'Visa';
45
+ pmt.type = 'CreditCard';
46
+ return pmt
47
+ end
48
+
49
+ def create_active_account(name)
50
+
51
+ val = false
52
+ session_start
53
+ account = make_account(name)
54
+ result = create([account])
55
+ # puts result.first.inspect
56
+
57
+ if result.first.success
58
+
59
+ accountId = result.first.id
60
+ payment = make_payment_method(accountId)
61
+ result = create([payment])
62
+
63
+ if result.first.success
64
+
65
+ paymentId = result.first.id
66
+ con = make_contact(accountId)
67
+ result = create([con])
68
+
69
+ if result.first.success
70
+ conId = result.first.id
71
+
72
+ account.id = accountId
73
+ account.status = 'Active'
74
+ account.billToId = conId
75
+ account.soldToId = conId
76
+ result = update([account])
77
+
78
+ if result.first.success
79
+ val = account
80
+ else
81
+ add_errors(result)
82
+ end
83
+
84
+ else
85
+ add_errors(result)
86
+ end
87
+ else
88
+ add_errors(result)
89
+ end
90
+ else
91
+ add_errors(result)
92
+ end
93
+ return val
94
+ end
95
+
96
+ def query(query)
97
+ q = ZUORA::Query.new
98
+ q.queryString = query
99
+ results = @z.query(q)
100
+ return results
101
+ end
102
+
103
+ def create(objs)
104
+ return @z.create(objs)
105
+ end
106
+
107
+ def generate(objs)
108
+ return @z.generate(objs)
109
+ end
110
+
111
+ def update(objs)
112
+ return @z.update(objs)
113
+ end
114
+
115
+ def delete(type, ids)
116
+ d = ZUORA::Delete.new
117
+ d.type = type
118
+ d.ids = ids
119
+ return @z.delete(d)
120
+ end
121
+
122
+ def subscribe(sub)
123
+ return @z.subscribe(sub)
124
+ end
125
+
126
+ def get_object(query)
127
+ object = lookup(query)
128
+ unless object.result.size == 0
129
+ return object.result.records.first
130
+ else
131
+ return nil
132
+ end
133
+ end
134
+
135
+ def get_object_array(query)
136
+ object = lookup(query)
137
+ unless object.result.size == 0
138
+ return object.result.records
139
+ else
140
+ return []
141
+ end
142
+ end
143
+
144
+ def get_driver
145
+ @z ||= Zuora::Billing::Api.instance.driver
146
+ end
147
+
148
+ def driver
149
+ get_driver
150
+ end
151
+
152
+ def login
153
+ loginargs = ZUORA::Login.new
154
+ loginargs.username = $ZUORA_USER
155
+ loginargs.password = $ZUORA_PASSWORD
156
+ @session = ZUORA::SessionHeader.new
157
+ @session.session = @z.login(loginargs).result.session
158
+ end
159
+
160
+ def session_start
161
+ get_driver
162
+ session_cleanup
163
+ login
164
+ @z.headerhandler.set @session
165
+ @z.wiredump_dev = STDERR if $ZUORA_VERBOSE
166
+ end
167
+
168
+ def session_cleanup
169
+ @z.headerhandler.delete(@session) if @session
170
+ end
171
+ end
172
+
173
+ public
174
+
175
+ def run_tests
176
+
177
+ t = ZuoraInterface.new
178
+ t.session_start
179
+
180
+ # create active account
181
+ e = t.create_active_account("name" + String(Time.now.to_f))
182
+ puts "Created Account: " + e.id
183
+
184
+ # query it
185
+ r = t.query("SELECT Id, Name FROM Account WHERE Name = '#{e.name}' and status = 'Active'")
186
+ e = r.result.records[0]
187
+ puts "Queried Account: " + e.to_s
188
+
189
+ # delete it
190
+ r = t.delete("Account", [e.id])
191
+ puts "Deleted Account? " + r[0].success.to_s
192
+
193
+ t.session_cleanup
194
+
195
+ end
196
+
197
+ if __FILE__ == $0
198
+ end
data/zuora4r.gemspec ADDED
@@ -0,0 +1,58 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{zuora4r}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Cloocher"]
12
+ s.date = %q{2010-08-30}
13
+ s.description = %q{A client for Zuora API}
14
+ s.email = %q{gene@ning.com}
15
+ s.executables = ["zuora-query", "zuora-bill-run", "zuora-payment-run", "zq"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ "Rakefile",
22
+ "bin/zq",
23
+ "bin/zuora-bill-run",
24
+ "bin/zuora-payment-run",
25
+ "bin/zuora-query",
26
+ "lib/zuora/ZUORA.rb",
27
+ "lib/zuora/ZUORADriver.rb",
28
+ "lib/zuora/ZUORAMappingRegistry.rb",
29
+ "lib/zuora/ZuoraServiceClient.rb",
30
+ "lib/zuora/api.rb",
31
+ "lib/zuora_client.rb",
32
+ "lib/zuora_interface.rb",
33
+ "zuora4r.gemspec"
34
+ ]
35
+ s.homepage = %q{http://github.com/cloocher/zuora4r}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.requirements = ["none"]
39
+ s.rubygems_version = %q{1.3.7}
40
+ s.summary = %q{Zuora4r}
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_runtime_dependency(%q<soap4r>, [">= 1.5.8"])
48
+ s.add_runtime_dependency(%q<json_pure>, [">= 1.4.6"])
49
+ else
50
+ s.add_dependency(%q<soap4r>, [">= 1.5.8"])
51
+ s.add_dependency(%q<json_pure>, [">= 1.4.6"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<soap4r>, [">= 1.5.8"])
55
+ s.add_dependency(%q<json_pure>, [">= 1.4.6"])
56
+ end
57
+ end
58
+
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zuora4r
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Cloocher
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-30 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: soap4r
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 19
30
+ segments:
31
+ - 1
32
+ - 5
33
+ - 8
34
+ version: 1.5.8
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: json_pure
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 11
46
+ segments:
47
+ - 1
48
+ - 4
49
+ - 6
50
+ version: 1.4.6
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ description: A client for Zuora API
54
+ email: gene@ning.com
55
+ executables:
56
+ - zuora-query
57
+ - zuora-bill-run
58
+ - zuora-payment-run
59
+ - zq
60
+ extensions: []
61
+
62
+ extra_rdoc_files:
63
+ - LICENSE
64
+ - README.rdoc
65
+ files:
66
+ - Rakefile
67
+ - bin/zq
68
+ - bin/zuora-bill-run
69
+ - bin/zuora-payment-run
70
+ - bin/zuora-query
71
+ - lib/zuora/ZUORA.rb
72
+ - lib/zuora/ZUORADriver.rb
73
+ - lib/zuora/ZUORAMappingRegistry.rb
74
+ - lib/zuora/ZuoraServiceClient.rb
75
+ - lib/zuora/api.rb
76
+ - lib/zuora_client.rb
77
+ - lib/zuora_interface.rb
78
+ - zuora4r.gemspec
79
+ - LICENSE
80
+ - README.rdoc
81
+ has_rdoc: true
82
+ homepage: http://github.com/cloocher/zuora4r
83
+ licenses: []
84
+
85
+ post_install_message:
86
+ rdoc_options:
87
+ - --charset=UTF-8
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 3
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ requirements:
109
+ - none
110
+ rubyforge_project:
111
+ rubygems_version: 1.3.7
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: Zuora4r
115
+ test_files: []
116
+