Module: Kaui::KillbillHelper

Defined in:
app/helpers/kaui/killbill_helper.rb

Class Method Summary collapse

Class Method Details

.add_account_email(account_email, current_user = nil, reason = nil, comment = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'app/helpers/kaui/killbill_helper.rb', line 78

def self.(, current_user = nil, reason = nil, comment = nil)
   = Kaui::AccountEmail.camelize(.to_hash)
  call_killbill :post,
                "/1.0/kb/accounts/#{.}/emails",
                ActiveSupport::JSON.encode(, :root => false),
                :content_type => "application/json",
                "X-Killbill-CreatedBy" => current_user,
                "X-Killbill-Reason" => extract_reason_code(reason),
                "X-Killbill-Comment" => "#{comment}"
end

.add_payment_method(payment_method, current_user = nil, reason = nil, comment = nil) ⇒ Object



398
399
400
401
402
403
404
405
406
407
# File 'app/helpers/kaui/killbill_helper.rb', line 398

def self.add_payment_method(payment_method, current_user = nil, reason = nil, comment = nil)
  payment_method_data = Kaui::Refund.camelize(payment_method.to_hash)
    call_killbill :post,
                  "/1.0/kb/accounts/#{payment_method.}/paymentMethods?isDefault=#{payment_method.is_default}",
                  ActiveSupport::JSON.encode(payment_method_data, :root => false),
                  :content_type => :json,
                  "X-Killbill-CreatedBy" => current_user,
                  "X-Killbill-Reason" => extract_reason_code(reason),
                  "X-Killbill-Comment" => "#{comment}"
end

.add_tags_for_account(account_id, tags, current_user = nil, reason = nil, comment = nil) ⇒ Object



509
510
511
512
513
514
515
516
# File 'app/helpers/kaui/killbill_helper.rb', line 509

def self.(, tags, current_user = nil, reason = nil, comment = nil)
  call_killbill :post,
                "/1.0/kb/accounts/#{}/tags?" + RestClient::Payload.generate(:tagList => tags.join(",")).to_s,
                nil,
                "X-Killbill-CreatedBy" => current_user,
                "X-Killbill-Reason" => "#{reason}",
                "X-Killbill-Comment" => "#{comment}"
end

.adjust_invoice(invoice_item, current_user = nil, reason = nil, comment = nil) ⇒ Object



258
259
260
261
262
263
264
265
266
267
# File 'app/helpers/kaui/killbill_helper.rb', line 258

def self.adjust_invoice(invoice_item, current_user = nil, reason = nil, comment = nil)
  invoice_data = Kaui::InvoiceItem.camelize(invoice_item.to_hash)
  call_killbill :post,
                "/1.0/kb/invoices/#{invoice_item.invoice_id}",
                ActiveSupport::JSON.encode(invoice_data, :root => false),
                :content_type => :json,
                "X-Killbill-CreatedBy" => current_user,
                "X-Killbill-Reason" => "#{reason}",
                "X-Killbill-Comment" => "#{comment}"
end

.call_killbill(method, uri, *args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/helpers/kaui/killbill_helper.rb', line 4

def self.call_killbill(method, uri, *args)
  url = Kaui.killbill_finder.call + uri
  Rails.logger.info "Performing #{method} request to #{url}"
  begin
    response = RestClient.send(method.to_sym, url, *args)
    data = { :code => response.code }
    if response.code < 300 && response.body.present?
      if response.headers[:content_type] =~ /application\/json.*/
        data[:json] = JSON.parse(response.body)
      else
        data[:body] = response.body
      end
    end
    data
  rescue => e
    puts "#{$!}\n\t" + e.backtrace.join("\n\t")
    raise e
  end
end

.compute_previous_ctd(ctd, billing_period) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'app/helpers/kaui/killbill_helper.rb', line 218

def self.compute_previous_ctd(ctd, billing_period)
  return nil if ctd.nil? or billing_period.nil?

  ctd = DateTime.parse(ctd)
  billing_period = billing_period.upcase
  if billing_period == 'MONTHLY'
    ctd.prev_month(1)
  elsif billing_period == 'QUARTERLY'
    ctd.prev_month(3)
  elsif billing_period == 'ANNUAL'
    ctd.prev_month(12)
  else
    nil
  end
end

.create_charge(charge, requested_date, current_user = nil, reason = nil, comment = nil) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'app/helpers/kaui/killbill_helper.rb', line 269

def self.create_charge(charge, requested_date, current_user = nil, reason = nil, comment = nil)
  charge_data = Kaui::Charge.camelize(charge.to_hash)
  date_param = "?requestedDate=" + requested_date unless requested_date.blank?

  if charge.invoice_id.present?
    call_killbill :post,
                  "/1.0/kb/invoices/#{charge.invoice_id}/charges#{date_param}",
                  ActiveSupport::JSON.encode(charge_data, :root => false),
                  :content_type => "application/json",
                  "X-Killbill-CreatedBy" => current_user,
                  "X-Killbill-Reason" => extract_reason_code(reason),
                  "X-Killbill-Comment" => "#{comment}"
  else
    call_killbill :post,
                  "/1.0/kb/invoices/charges#{date_param}",
                  ActiveSupport::JSON.encode(charge_data, :root => false),
                  :content_type => "application/json",
                  "X-Killbill-CreatedBy" => current_user,
                  "X-Killbill-Reason" => extract_reason_code(reason),
                  "X-Killbill-Comment" => "#{comment}"
  end
end

.create_chargeback(chargeback, current_user = nil, reason = nil, comment = nil) ⇒ Object



442
443
444
445
446
447
448
449
450
451
452
# File 'app/helpers/kaui/killbill_helper.rb', line 442

def self.create_chargeback(chargeback, current_user = nil, reason = nil, comment = nil)
  chargeback_data = Kaui::Refund.camelize(chargeback.to_hash)

  call_killbill :post,
                "/1.0/kb/chargebacks",
                ActiveSupport::JSON.encode(chargeback_data, :root => false),
                :content_type => "application/json",
                "X-Killbill-CreatedBy" => current_user,
                "X-Killbill-Reason" => extract_reason_code(reason),
                "X-Killbill-Comment" => "#{comment}"
end

.create_credit(credit, current_user = nil, reason = nil, comment = nil) ⇒ Object

CREDIT ##############



456
457
458
459
460
461
462
463
464
465
# File 'app/helpers/kaui/killbill_helper.rb', line 456

def self.create_credit(credit, current_user = nil, reason = nil, comment = nil)
  credit_data = Kaui::Credit.camelize(credit.to_hash)
  call_killbill :post,
                "/1.0/kb/credits",
                ActiveSupport::JSON.encode(credit_data, :root => false),
                :content_type => "application/json",
                "X-Killbill-CreatedBy" => current_user,
                "X-Killbill-Reason" => extract_reason_code(reason),
                "X-Killbill-Comment" => "#{comment}"
end

.create_payment(payment, external, current_user = nil, reason = nil, comment = nil) ⇒ Object



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'app/helpers/kaui/killbill_helper.rb', line 344

def self.create_payment(payment, external, current_user = nil, reason = nil, comment = nil)
  payment_data = Kaui::Payment.camelize(payment.to_hash)

  if payment.invoice_id.present?
    # We should use different model for POST and GET, this seems fragile...
    payment_data.delete(:external)
    payment_data.delete(:refunds)
    payment_data.delete(:chargebacks)
    payment_data.delete(:audit_logs)
    call_killbill :post,
                   "/1.0/kb/invoices/#{payment.invoice_id}/payments?externalPayment=#{external}",
                   ActiveSupport::JSON.encode(payment_data, :root => false),
                   :content_type => "application/json",
                   "X-Killbill-CreatedBy" => current_user,
                   "X-Killbill-Reason" => extract_reason_code(reason),
                   "X-Killbill-Comment" => "#{comment}"
  end
end

.create_refund(payment_id, refund, current_user = nil, reason = nil, comment = nil) ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'app/helpers/kaui/killbill_helper.rb', line 421

def self.create_refund(payment_id, refund, current_user = nil, reason = nil, comment = nil)
  refund_data = Kaui::Refund.camelize(refund.to_hash)
  # We don't want to pass adjustment_type
  refund_data.delete(:adjustmentType)

  call_killbill :post,
                "/1.0/kb/payments/#{payment_id}/refunds",
                ActiveSupport::JSON.encode(refund_data, :root => false),
                :content_type => "application/json",
                "X-Killbill-CreatedBy" => current_user,
                "X-Killbill-Reason" => extract_reason_code(reason),
                "X-Killbill-Comment" => "#{comment}"
end

.create_subscription(subscription, current_user = nil, reason = nil, comment = nil) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
# File 'app/helpers/kaui/killbill_helper.rb', line 165

def self.create_subscription(subscription, current_user = nil, reason = nil, comment = nil)
  subscription_data = Kaui::Subscription.camelize(subscription.to_hash)
  # We don't want to pass events
  subscription_data.delete(:events)
  call_killbill :post,
                "/1.0/kb/subscriptions",
                ActiveSupport::JSON.encode(subscription_data, :root => false),
                :content_type => "application/json",
                "X-Killbill-CreatedBy" => current_user,
                "X-Killbill-Reason" => "#{reason}",
                "X-Killbill-Comment" => "#{comment}"
end

.create_tag_definition(tag_definition, current_user = nil, reason = nil, comment = nil) ⇒ Object



479
480
481
482
483
484
485
486
487
488
# File 'app/helpers/kaui/killbill_helper.rb', line 479

def self.create_tag_definition(tag_definition, current_user = nil, reason = nil, comment = nil)
  tag_definition_data = Kaui::TagDefinition.camelize(tag_definition.to_hash)
  call_killbill :post,
                "/1.0/kb/tagDefinitions",
                ActiveSupport::JSON.encode(tag_definition_data, :root => false),
                :content_type => "application/json",
                "X-Killbill-CreatedBy" => current_user,
                "X-Killbill-Reason" => extract_reason_code(reason),
                "X-Killbill-Comment" => "#{comment}"
end

.delete_cba(account_id, invoice_id, invoice_item_id, current_user = nil, reason = nil, comment = nil) ⇒ Object



292
293
294
295
296
297
298
# File 'app/helpers/kaui/killbill_helper.rb', line 292

def self.delete_cba(, invoice_id, invoice_item_id, current_user = nil, reason = nil, comment = nil)
  call_killbill :delete,
                "/1.0/kb/invoices/#{invoice_id}/#{invoice_item_id}/cba?accountId=#{}",
                "X-Killbill-CreatedBy" => current_user,
                "X-Killbill-Reason" => "#{reason}",
                "X-Killbill-Comment" => "#{comment}"
end

.delete_payment_method(payment_method_id, set_auto_pay_off = false, current_user = nil, reason = nil, comment = nil) ⇒ Object

PAYMENT METHOD ##############



365
366
367
368
369
370
371
372
# File 'app/helpers/kaui/killbill_helper.rb', line 365

def self.delete_payment_method(payment_method_id, set_auto_pay_off = false,  current_user = nil, reason = nil, comment = nil)
  set_auto_pay_off_param = "?deleteDefaultPmWithAutoPayOff=true" if set_auto_pay_off
  call_killbill :delete,
                "/1.0/kb/paymentMethods/#{payment_method_id}#{set_auto_pay_off_param}",
                "X-Killbill-CreatedBy" => current_user,
                "X-Killbill-Reason" => "#{reason}",
                "X-Killbill-Comment" => "#{comment}"
end

.delete_subscription(subscription_id, policy = nil, ctd = nil, billing_period = nil, current_user = nil, reason = nil, comment = nil) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
# File 'app/helpers/kaui/killbill_helper.rb', line 206

def self.delete_subscription(subscription_id, policy = nil, ctd = nil, billing_period = nil, current_user = nil, reason = nil, comment = nil)
  prev_ctd = compute_previous_ctd(ctd, billing_period)
  params = "?"
  params += "policy=#{policy}&" unless policy.blank?
  params += "requestedDate=#{prev_ctd.strftime('%Y-%m-%dT%H:%M:%S')}" unless prev_ctd.nil?
  call_killbill :delete,
                "/1.0/kb/subscriptions/#{subscription_id}#{params}",
                "X-Killbill-CreatedBy" => current_user,
                "X-Killbill-Reason" => "#{reason}",
                "X-Killbill-Comment" => "#{comment}"
end

.delete_tag_definition(tag_definition_id, current_user = nil, reason = nil, comment = nil) ⇒ Object



490
491
492
493
494
495
496
# File 'app/helpers/kaui/killbill_helper.rb', line 490

def self.delete_tag_definition(tag_definition_id, current_user = nil, reason = nil, comment = nil)
  call_killbill :delete,
                "/1.0/kb/tagDefinitions/#{tag_definition_id}",
                "X-Killbill-CreatedBy" => current_user,
                "X-Killbill-Reason" => "#{reason}",
                "X-Killbill-Comment" => "#{comment}"
end

.extract_reason_code(reason) ⇒ Object



34
35
36
# File 'app/helpers/kaui/killbill_helper.rb', line 34

def self.extract_reason_code(reason)
  reason_code = $1 if reason =~ /\s*(\d+).*/
end

.get_account(account_id, with_balance = false, with_balance_and_cba = false) ⇒ Object



58
59
60
61
# File 'app/helpers/kaui/killbill_helper.rb', line 58

def self.(, with_balance = false, with_balance_and_cba = false)
  data = call_killbill :get, "/1.0/kb/accounts/#{}?accountWithBalance=#{with_balance}&accountWithBalanceAndCBA=#{with_balance_and_cba}"
  process_response(data, :single) {|json| Kaui::Account.new(json) }
end

.get_account_by_bundle_id(bundle_id) ⇒ Object



68
69
70
71
# File 'app/helpers/kaui/killbill_helper.rb', line 68

def self.(bundle_id)
  bundle = get_bundle(bundle_id)
   = (bundle.)
end

.get_account_by_external_key(external_key, with_balance = false, with_balance_and_cba = false) ⇒ Object



63
64
65
66
# File 'app/helpers/kaui/killbill_helper.rb', line 63

def self.(external_key, with_balance = false, with_balance_and_cba = false)
  data = call_killbill :get, "/1.0/kb/accounts?externalKey=#{external_key}&accountWithBalance=#{with_balance}&accountWithBalanceAndCBA=#{with_balance_and_cba}"
  process_response(data, :single) {|json| Kaui::Account.new(json) }
end

.get_account_by_key(key, with_balance = false, with_balance_and_cba = false) ⇒ Object



44
45
46
47
48
49
50
51
# File 'app/helpers/kaui/killbill_helper.rb', line 44

def self.(key, with_balance = false, with_balance_and_cba = false)
  # support id (UUID) and external key search
  if key =~ /[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}/
    Kaui::KillbillHelper.(key, with_balance, with_balance_and_cba)
  else
    Kaui::KillbillHelper.(key, with_balance, with_balance_and_cba)
  end
end

.get_account_by_key_with_balance_and_cba(key) ⇒ Object

ACCOUNT ##############



40
41
42
# File 'app/helpers/kaui/killbill_helper.rb', line 40

def self.(key)
  self.(key, false, true)
end

.get_account_emails(account_id) ⇒ Object



73
74
75
76
# File 'app/helpers/kaui/killbill_helper.rb', line 73

def self.()
  data = call_killbill :get, "/1.0/kb/accounts/#{}/emails"
  process_response(data, :multiple) { |json| Kaui::AccountEmail.new(json) }
end

.get_account_timeline(account_id) ⇒ Object



53
54
55
56
# File 'app/helpers/kaui/killbill_helper.rb', line 53

def self.()
  data = call_killbill :get, "/1.0/kb/accounts/#{}/timeline?audit=MINIMAL"
  process_response(data, :single) {|json| Kaui::AccountTimeline.new(json) }
end

.get_accounts_created_over_timeObject

ANALYTICS ##############



555
556
557
558
# File 'app/helpers/kaui/killbill_helper.rb', line 555

def self.get_accounts_created_over_time
  data = call_killbill :get, "/1.0/kb/analytics/accountsCreatedOverTime"
  process_response(data, :single) { |json| Kaui::TimeSeriesData.new(json) }
end

.get_available_addons(base_product_name) ⇒ Object



307
308
309
310
311
312
# File 'app/helpers/kaui/killbill_helper.rb', line 307

def self.get_available_addons(base_product_name)
  data = call_killbill :get, "/1.0/kb/catalog/availableAddons?baseProductName=#{base_product_name}"
  if data.has_key?(:json)
    data[:json].inject({}) {|catalog_hash, item| catalog_hash.merge!(item["planName"] => item) }
  end
end

.get_available_base_plansObject



314
315
316
317
318
319
# File 'app/helpers/kaui/killbill_helper.rb', line 314

def self.get_available_base_plans()
  data = call_killbill :get, "/1.0/kb/catalog/availableBasePlans"
  if data.has_key?(:json)
    data[:json].inject({}) {|catalog_hash, item| catalog_hash.merge!(item["planName"] => item) }
  end
end

.get_bundle(bundle_id) ⇒ Object



129
130
131
132
# File 'app/helpers/kaui/killbill_helper.rb', line 129

def self.get_bundle(bundle_id)
  data = call_killbill :get, "/1.0/kb/bundles/#{bundle_id}"
  process_response(data, :single) {|json| Kaui::Bundle.new(json) }
end

.get_bundle_by_external_key(account_id, external_key) ⇒ Object



124
125
126
127
# File 'app/helpers/kaui/killbill_helper.rb', line 124

def self.get_bundle_by_external_key(, external_key)
  data = call_killbill :get, "/1.0/kb/accounts/#{}/bundles?externalKey=#{external_key}"
  process_response(data, :single) {|json| Kaui::Bundle.new(json) }
end

.get_bundle_by_key(key, account_id) ⇒ Object

BUNDLE ##############



110
111
112
113
114
115
116
117
# File 'app/helpers/kaui/killbill_helper.rb', line 110

def self.get_bundle_by_key(key, )
  # support id (UUID) and external key search
  if key =~ /[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}/
    @bundle = Kaui::KillbillHelper::get_bundle(key)
  else
    @bundle = Kaui::KillbillHelper::get_bundle_by_external_key(key, )
  end
end

.get_bundles(account_id) ⇒ Object



119
120
121
122
# File 'app/helpers/kaui/killbill_helper.rb', line 119

def self.get_bundles()
  data = call_killbill :get, "/1.0/kb/accounts/#{}/bundles"
  process_response(data, :multiple) {|json| Kaui::Bundle.new(json) }
end

.get_chargebacks_for_payment(payment_id) ⇒ Object

CHARGEBACK ##############



437
438
439
440
# File 'app/helpers/kaui/killbill_helper.rb', line 437

def self.get_chargebacks_for_payment(payment_id)
  data = call_killbill :get, "/1.0/kb/chargebacks/payments/#{payment_id}"
  process_response(data, :multiple) {|json| Kaui::Chargeback.new(json) }
end

.get_custom_fields_for_account(account_id) ⇒ Object

CUSTOM FIELDS ##############



541
542
543
544
# File 'app/helpers/kaui/killbill_helper.rb', line 541

def self.()
  data = call_killbill :get, "/1.0/kb/accounts/#{}/customFields"
  process_response(data, :multiple) { |json| Kaui::CustomField.new(json) }
end

.get_full_catalogObject

CATALOG ##############



302
303
304
305
# File 'app/helpers/kaui/killbill_helper.rb', line 302

def self.get_full_catalog
  data = call_killbill :get, "/1.0/kb/catalog/simpleCatalog"
  data[:json]
end

.get_invoice(invoice_id) ⇒ Object

INVOICE ##############



236
237
238
239
# File 'app/helpers/kaui/killbill_helper.rb', line 236

def self.get_invoice(invoice_id)
  data = call_killbill :get, "/1.0/kb/invoices/#{invoice_id}?withItems=true"
  process_response(data, :single) {|json| Kaui::Invoice.new(json) }
end

.get_invoice_html(invoice_id) ⇒ Object



253
254
255
256
# File 'app/helpers/kaui/killbill_helper.rb', line 253

def self.get_invoice_html(invoice_id)
  data = call_killbill :get, "/1.0/kb/invoices/#{invoice_id}/html"
  data[:body] if data.present?
end

.get_invoice_item(invoice_id, invoice_item_id) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
# File 'app/helpers/kaui/killbill_helper.rb', line 241

def self.get_invoice_item(invoice_id, invoice_item_id)
  # Find the item from the invoice
  # TODO add killbill-server API
  invoice = Kaui::KillbillHelper.get_invoice(invoice_id)
  if invoice.present? and invoice.items.present?
    invoice.items.each do |item|
      return item if item.invoice_item_id == invoice_item_id
    end
  end
  nil
end

.get_non_external_payment_methods(account_id) ⇒ Object



374
375
376
# File 'app/helpers/kaui/killbill_helper.rb', line 374

def self.get_non_external_payment_methods()
  self.get_payment_methods().reject { |x| x.plugin_name == '__EXTERNAL_PAYMENT__' }
end

.get_overdue_state_for_bundle(bundle_id) ⇒ Object

OVERDUE ##############



548
549
550
551
# File 'app/helpers/kaui/killbill_helper.rb', line 548

def self.get_overdue_state_for_bundle(bundle_id)
  data = call_killbill :get, "/1.0/kb/overdue/bundles/#{bundle_id}"
  process_response(data, :single) { |json| Kaui::OverdueState.new(json) }
end

.get_payment(payment_id) ⇒ Object

PAYMENT ##############



323
324
325
326
# File 'app/helpers/kaui/killbill_helper.rb', line 323

def self.get_payment(payment_id)
  data = call_killbill :get, "/1.0/kb/payments/#{payment_id}"
  process_response(data, :single) { |json| Kaui::Payment.new(json) }
end

.get_payment_method(payment_method_id) ⇒ Object



383
384
385
386
# File 'app/helpers/kaui/killbill_helper.rb', line 383

def self.get_payment_method(payment_method_id)
  data = call_killbill :get, "/1.0/kb/paymentMethods/#{payment_method_id}?withPluginInfo=true"
  process_response(data, :single) {|json| Kaui::PaymentMethod.new(json) }
end

.get_payment_methods(account_id) ⇒ Object



378
379
380
381
# File 'app/helpers/kaui/killbill_helper.rb', line 378

def self.get_payment_methods()
  data = call_killbill :get, "/1.0/kb/accounts/#{}/paymentMethods?withPluginInfo=true"
  process_response(data, :multiple) {|json| Kaui::PaymentMethod.new(json) }
end

.get_payments(invoice_id) ⇒ Object



328
329
330
331
332
# File 'app/helpers/kaui/killbill_helper.rb', line 328

def self.get_payments(invoice_id)
  data = call_killbill :get, "/1.0/kb/invoices/#{invoice_id}/payments"
  response_data = process_response(data, :multiple) {|json| Kaui::Payment.new(json) }
  return response_data
end

.get_refund(refund_id) ⇒ Object

REFUND ##############



411
412
413
414
# File 'app/helpers/kaui/killbill_helper.rb', line 411

def self.get_refund(refund_id)
  data = call_killbill :get, "/1.0/kb/refunds/#{refund_id}"
  process_response(data, :single) { |json| Kaui::Refund.new(json) }
end

.get_refunds_for_payment(payment_id) ⇒ Object



416
417
418
419
# File 'app/helpers/kaui/killbill_helper.rb', line 416

def self.get_refunds_for_payment(payment_id)
  data = call_killbill :get, "/1.0/kb/payments/#{payment_id}/refunds"
  process_response(data, :multiple) {|json| Kaui::Refund.new(json) }
end

.get_subscription(subscription_id) ⇒ Object



160
161
162
163
# File 'app/helpers/kaui/killbill_helper.rb', line 160

def self.get_subscription(subscription_id)
  data = call_killbill :get, "/1.0/kb/subscriptions/#{subscription_id}"
  process_response(data, :single) {|json| Kaui::Subscription.new(json) }
end

.get_subscriptions(account_id) ⇒ Object



151
152
153
154
155
156
157
158
# File 'app/helpers/kaui/killbill_helper.rb', line 151

def self.get_subscriptions()
  subscriptions = []
  bundles = get_bundles()
  bundles.each do |bundle|
    subscriptions += get_subscriptions_for_bundle(bundle.bundle_id)
  end
  subscriptions
end

.get_subscriptions_created_over_time(product_type, slug) ⇒ Object



560
561
562
563
# File 'app/helpers/kaui/killbill_helper.rb', line 560

def self.get_subscriptions_created_over_time(product_type, slug)
  data = call_killbill :get, "/1.0/kb/analytics/subscriptionsCreatedOverTime?productType=#{product_type}&slug=#{slug}"
  process_response(data, :single) { |json| Kaui::TimeSeriesData.new(json) }
end

.get_subscriptions_for_bundle(bundle_id) ⇒ Object

SUBSCRIPTION ##############



146
147
148
149
# File 'app/helpers/kaui/killbill_helper.rb', line 146

def self.get_subscriptions_for_bundle(bundle_id)
  data = call_killbill :get, "/1.0/kb/bundles/#{bundle_id}/subscriptions"
  process_response(data, :multiple) {|json| Kaui::Subscription.new(json) }
end

.get_tag_definition(tag_definition_id) ⇒ Object



474
475
476
477
# File 'app/helpers/kaui/killbill_helper.rb', line 474

def self.get_tag_definition(tag_definition_id)
  data = call_killbill :get, "/1.0/kb/tagDefinitions/#{tag_definition_id}"
  process_response(data, :single) { |json| Kaui::TagDefinition.new(json) }
end

.get_tag_definitionsObject

TAG ##############



469
470
471
472
# File 'app/helpers/kaui/killbill_helper.rb', line 469

def self.get_tag_definitions
  data = call_killbill :get, "/1.0/kb/tagDefinitions"
  process_response(data, :multiple) { |json| Kaui::TagDefinition.new(json) }
end

.get_tags_for_account(account_id) ⇒ Object



498
499
500
501
# File 'app/helpers/kaui/killbill_helper.rb', line 498

def self.()
  data = call_killbill :get, "/1.0/kb/accounts/#{}/tags"
  process_response(data, :multiple) { |json| Kaui::Tag.new(json) }
end

.get_tags_for_bundle(bundle_id) ⇒ Object



503
504
505
506
# File 'app/helpers/kaui/killbill_helper.rb', line 503

def self.get_tags_for_bundle(bundle_id)
  data = call_killbill :get, "/1.0/kb/bundles/#{bundle_id}/tags"
  return data[:json]
end

.pay_all_invoices(account_id, external = false, current_user = nil, reason = nil, comment = nil) ⇒ Object



334
335
336
337
338
339
340
341
342
# File 'app/helpers/kaui/killbill_helper.rb', line 334

def self.pay_all_invoices(, external = false, current_user = nil, reason = nil, comment = nil)
  call_killbill :post,
                 "/1.0/kb/invoices/payments?externalPayment=#{external}",
                 ActiveSupport::JSON.encode({:accountId => }, :root => false),
                 :content_type => "application/json",
                 "X-Killbill-CreatedBy" => current_user,
                 "X-Killbill-Reason" => extract_reason_code(reason),
                 "X-Killbill-Comment" => "#{comment}"
end

.process_response(response, arity, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'app/helpers/kaui/killbill_helper.rb', line 24

def self.process_response(response, arity, &block)
  if response.nil? || response[:json].nil?
    arity == :single ? nil : []
  elsif block_given?
    arity == :single ? yield(response[:json]) : response[:json].collect {|item| yield(item) }
  else
    response[:json]
  end
end

.reinstate_subscription(subscription_id, current_user = nil, reason = nil, comment = nil) ⇒ Object



196
197
198
199
200
201
202
203
204
# File 'app/helpers/kaui/killbill_helper.rb', line 196

def self.reinstate_subscription(subscription_id, current_user = nil, reason = nil, comment = nil)
  call_killbill :put,
                "/1.0/kb/subscriptions/#{subscription_id}/uncancel",
                "",
                :content_type => :json,
                "X-Killbill-CreatedBy" => current_user,
                "X-Killbill-Reason" => "#{reason}",
                "X-Killbill-Comment" => "#{comment}"
end

.remove_account_email(account_email, current_user = nil, reason = nil, comment = nil) ⇒ Object



89
90
91
92
93
94
95
# File 'app/helpers/kaui/killbill_helper.rb', line 89

def self.(, current_user = nil, reason = nil, comment = nil)
  call_killbill :delete,
                "/1.0/kb/accounts/#{.}/emails/#{.email}",
                "X-Killbill-CreatedBy" => current_user,
                "X-Killbill-Reason" => "#{reason}",
                "X-Killbill-Comment" => "#{comment}"
end

.remove_tags_for_account(account_id, tags, current_user = nil, reason = nil, comment = nil) ⇒ Object



518
519
520
521
522
523
524
525
# File 'app/helpers/kaui/killbill_helper.rb', line 518

def self.(, tags, current_user = nil, reason = nil, comment = nil)
  return if !tags.present? || tags.size == 0
  call_killbill :delete,
                "/1.0/kb/accounts/#{}/tags?" + RestClient::Payload.generate(:tagList => tags.join(",")).to_s,
                "X-Killbill-CreatedBy" => current_user,
                "X-Killbill-Reason" => "#{reason}",
                "X-Killbill-Comment" => "#{comment}"
end

.set_payment_method_as_default(account_id, payment_method_id, current_user = nil, reason = nil, comment = nil) ⇒ Object



388
389
390
391
392
393
394
395
396
# File 'app/helpers/kaui/killbill_helper.rb', line 388

def self.set_payment_method_as_default(, payment_method_id, current_user = nil, reason = nil, comment = nil)
  call_killbill :put,
                "/1.0/kb/accounts/#{}/paymentMethods/#{payment_method_id}/setDefault",
                "",
                :content_type => :json,
                "X-Killbill-CreatedBy" => current_user,
                "X-Killbill-Reason" => extract_reason_code(reason),
                "X-Killbill-Comment" => "#{comment}"
end

.set_tags_for_bundle(bundle_id, tags, current_user = nil, reason = nil, comment = nil) ⇒ Object



527
528
529
530
531
532
533
534
535
536
537
# File 'app/helpers/kaui/killbill_helper.rb', line 527

def self.set_tags_for_bundle(bundle_id, tags, current_user = nil, reason = nil, comment = nil)
  if tags.nil? || tags.empty?
  else
    call_killbill :post,
                  "/1.0/kb/bundles/#{bundle_id}/tags?" + RestClient::Payload.generate(:tag_list => tags.join(",")).to_s,
                  nil,
                  "X-Killbill-CreatedBy" => current_user,
                  "X-Killbill-Reason" => "#{reason}",
                  "X-Killbill-Comment" => "#{comment}"
  end
end

.transfer_bundle(bundle_id, new_account_id, cancel_immediately = false, transfer_addons = true, current_user = nil, reason = nil, comment = nil) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'app/helpers/kaui/killbill_helper.rb', line 134

def self.transfer_bundle(bundle_id, , cancel_immediately = false, transfer_addons = true, current_user = nil, reason = nil, comment = nil)
  call_killbill :put,
                "/1.0/kb/bundles/#{bundle_id}?cancelImmediately=#{cancel_immediately}&transferAddOn=#{transfer_addons}",
                ActiveSupport::JSON.encode("accountId" => ),
                :content_type => :json,
                "X-Killbill-CreatedBy" => current_user,
                "X-Killbill-Reason" => "#{reason}",
                "X-Killbill-Comment" => "#{comment}"
end

.update_email_notifications(account_id, is_notified, current_user = nil, reason = nil, comment = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'app/helpers/kaui/killbill_helper.rb', line 97

def self.update_email_notifications(, is_notified, current_user = nil, reason = nil, comment = nil)
  email_data = { :isNotifiedForInvoices => is_notified }
  call_killbill :put,
                "/1.0/kb/accounts/#{}/emailNotifications",
                ActiveSupport::JSON.encode(email_data, :root => false),
                :content_type => "application/json",
                "X-Killbill-CreatedBy" => current_user,
                "X-Killbill-Reason" => extract_reason_code(reason),
                "X-Killbill-Comment" => "#{comment}"
end

.update_subscription(subscription, requested_date = nil, policy = nil, current_user = nil, reason = nil, comment = nil) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/helpers/kaui/killbill_helper.rb', line 178

def self.update_subscription(subscription, requested_date = nil, policy = nil, current_user = nil, reason = nil, comment = nil)
  subscription_data = Kaui::Subscription.camelize(subscription.to_hash)

  params = "?"
  params = "#{params}requestedDate=#{requested_date.to_s}&" unless requested_date.blank?
  params = "#{params}policy=#{policy}" unless policy.blank?

  # We don't want to pass events
  subscription_data.delete(:events)
  call_killbill :put,
                "/1.0/kb/subscriptions/#{subscription.subscription_id}#{params}",
                ActiveSupport::JSON.encode(subscription_data, :root => false),
                :content_type => :json,
                "X-Killbill-CreatedBy" => current_user,
                "X-Killbill-Reason" => "#{reason}",
                "X-Killbill-Comment" => "#{comment}"
end