active_zuora 1.3.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
@@ -0,0 +1,4 @@
1
+ module Zuora
2
+ class Account < ZObject
3
+ end
4
+ end
@@ -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,4 @@
1
+ module Zuora
2
+ class Contact < ZObject
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Zuora
2
+ class RatePlan < ZObject
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Zuora
2
+ class RatePlanData < ZObject
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Zuora
2
+ class SubscribeOptions < ZObject
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Zuora
2
+ class SubscribeRequest < ZObject
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Zuora
2
+ class SubscribeWithExistingAccountRequest < ZObject
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Zuora
2
+ class Subscription < ZObject
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Zuora
2
+ class SubscriptionData < ZObject
3
+ end
4
+ end
@@ -0,0 +1,52 @@
1
+ module Zuora
2
+ class ZObject < SimpleDelegator
3
+ def initialize(attributes={})
4
+ super(self.class.zobject_class.new).tap do |zobject|
5
+ attributes.each do |attr, value|
6
+ zobject.send("#{attr}=", value)
7
+ end
8
+ end
9
+ end
10
+
11
+ def to_zobject
12
+ __getobj__
13
+ end
14
+
15
+ def self.zobject_class
16
+ return @zobject_class if @zobject_class
17
+ klass_name = name.match(/(\w+)::(\w+)/)
18
+ @zobject_class = "#{klass_name[1].upcase}::#{klass_name[2]}".constantize
19
+ end
20
+
21
+ #TODO: This sucks attributes need to be clearly defined
22
+ def self.attribute_names
23
+ @attribute_names ||= zobject_class.instance_variable_get("@attributes").reject{|name| name == :fieldsToNull }
24
+ end
25
+
26
+ def self.where(conditions={})
27
+ zobjects = self.client.query("select #{self.attribute_names.join(", ")} from #{self.name.gsub(/Zuora::/,"")} where #{build_filter_statments(conditions)}")
28
+ zobjects.map{|zobject| self.new zobject }
29
+ end
30
+
31
+ def self.build_filter_statments(filter_statments)
32
+ filter_statments.map{|key, value|
33
+ value = "'#{value}'" if value.kind_of?(String)
34
+ "#{key} = #{value}"
35
+ }.join(" and ")
36
+ end
37
+
38
+ def self.find(id)
39
+ zobject = client.query("select #{attribute_names.join(", ")} from #{self.name.gsub(/Zuora::/,"")} where Id = '#{id}'").first
40
+ self.new zobject if zobject
41
+ end
42
+
43
+ def self.all
44
+ zobjects = client.query("select #{attribute_names.join(", ")} from #{self.name.gsub(/Zuora::/,"")}")
45
+ zobjects.map{|zobject| self.new zobject }
46
+ end
47
+
48
+ def self.client
49
+ @client ||= Zuora::Client.new
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,181 @@
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
+ require 'yaml'
33
+
34
+ class SOAP::Header::HandlerSet
35
+ def reset
36
+ @store = XSD::NamedElements.new
37
+ end
38
+
39
+ def set(header)
40
+ reset
41
+ add header
42
+ end
43
+ end
44
+
45
+ module Zuora
46
+ class Client
47
+ PROD_URL = 'https://www.zuora.com/apps/services/a/36.0'
48
+ SANDBOX_URL = 'https://apisandbox.zuora.com/apps/services/a/36.0'
49
+
50
+ def self.config
51
+ return @config_hash if @config_hash
52
+ @config_hash = YAML.load_file(Zuora::CONFIG_FILE) if File.exist?(Zuora::CONFIG_FILE)
53
+ @config_hash ||= {}
54
+ end
55
+
56
+ def self.parse_custom_fields
57
+ if self.custom_fields
58
+ self.custom_fields.each do |zobject, field_names|
59
+ fields = field_names.map { |e| "#{e.strip}__c" }
60
+ type_class = Object.const_get('ZUORA').const_get(zobject)
61
+ fields.each do |field|
62
+ custom_field = field.gsub(/^\w/) { |i| i.downcase }
63
+ type_class.send :attr_accessor, custom_field
64
+ end
65
+ end
66
+ end
67
+ custom_fields
68
+ end
69
+
70
+ def self.custom_fields
71
+ @custom_fields = YAML.load_file(File.dirname(__FILE__) + '/../custom_fields.yml')
72
+ end
73
+
74
+ def initialize(url=SANDBOX_URL)
75
+ $ZUORA_USER = self.class.config["username"]
76
+ $ZUORA_PASSWORD = self.class.config["password"]
77
+ $ZUORA_ENDPOINT = url
78
+
79
+ @client = ZuoraInterface.new
80
+
81
+ # add custom fields, if any
82
+ custom_fields = self.class.parse_custom_fields
83
+ @client.session_start(custom_fields)
84
+ end
85
+
86
+ def query(query_string)
87
+
88
+ query_string =~ /select\s+(.+)\s+from/i
89
+ fields = ($1.split /,\s+/).map do |f|
90
+ f.gsub!(/\b\w/) { $&.downcase }
91
+ end
92
+
93
+ begin
94
+ response = @client.query(query_string)
95
+ rescue Exception => e
96
+ puts e.message
97
+ end
98
+
99
+ result = []
100
+ if response && response.result && response.result.size > 0
101
+ response.result.records.each do |record|
102
+ row = {}
103
+ fields.each do |f|
104
+ row[f] = record.send(f)
105
+ end
106
+ result << row
107
+ end
108
+ end
109
+ result
110
+ end
111
+
112
+ def subscribe(obj)
113
+ begin
114
+ response = @client.subscribe(obj)
115
+ return response
116
+ rescue Exception => e
117
+ puts e.message
118
+ end
119
+ end
120
+
121
+ def create(obj)
122
+ begin
123
+ response = @client.create(obj)
124
+ result = save_results_to_hash(response)
125
+ rescue Exception => e
126
+ puts e.message
127
+ end
128
+ result || []
129
+ end
130
+
131
+ def generate(obj)
132
+ begin
133
+ response = @client.generate(obj)
134
+ result = save_results_to_hash(response)
135
+ rescue Exception => e
136
+ puts e.message
137
+ end
138
+ result || []
139
+ end
140
+
141
+ def update(obj)
142
+ begin
143
+ response = @client.update(obj)
144
+ result = save_results_to_hash(response)
145
+ rescue Exception => e
146
+ puts e.message
147
+ end
148
+ result || []
149
+ end
150
+
151
+ def delete(type, ids)
152
+ begin
153
+ response = @client.delete(type, ids)
154
+ result = save_results_to_hash(response)
155
+ rescue Exception => e
156
+ puts e.message
157
+ end
158
+ result || []
159
+ end
160
+
161
+ private
162
+
163
+ def save_results_to_hash(save_results)
164
+ result = []
165
+ save_results.each do |record|
166
+ row = {:success => record.success}
167
+ if record.success
168
+ row[:id] = record.id
169
+ else
170
+ row[:errors] = []
171
+ record.errors.each do |error|
172
+ row[:errors] << {:message => error.message, :code => error.code}
173
+ end
174
+ end
175
+ result << row
176
+ end
177
+ result
178
+ end
179
+ end
180
+
181
+ end
@@ -0,0 +1,199 @@
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(custom_fields={})
161
+ get_driver
162
+ @z.do_init(custom_fields)
163
+ session_cleanup
164
+ login
165
+ @z.headerhandler.set @session
166
+ @z.wiredump_dev = STDERR if $ZUORA_VERBOSE
167
+ end
168
+
169
+ def session_cleanup
170
+ @z.headerhandler.delete(@session) if @session
171
+ end
172
+ end
173
+
174
+ public
175
+
176
+ def run_tests
177
+
178
+ t = ZuoraInterface.new
179
+ t.session_start
180
+
181
+ # create active account
182
+ e = t.create_active_account("name" + String(Time.now.to_f))
183
+ puts "Created Account: " + e.id
184
+
185
+ # query it
186
+ r = t.query("SELECT Id, Name FROM Account WHERE Name = '#{e.name}' and status = 'Active'")
187
+ e = r.result.records[0]
188
+ puts "Queried Account: " + e.to_s
189
+
190
+ # delete it
191
+ r = t.delete("Account", [e.id])
192
+ puts "Deleted Account? " + r[0].success.to_s
193
+
194
+ t.session_cleanup
195
+
196
+ end
197
+
198
+ if __FILE__ == $0
199
+ end