Class: BillForward::Subscription

Inherits:
MutableEntity show all
Defined in:
lib/bill_forward/entities/subscription.rb

Overview

This entity exposes the following child entities via method_missing:

PricingComponentValue[] .pricingComponentValues PricingComponentValueChange[] .pricingComponentValueChanges

Instance Attribute Summary

Attributes inherited from BillingEntity

#_client

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MutableEntity

#delete, #initialize, #save

Methods inherited from InsertableEntity

create, #initialize

Methods inherited from BillingEntity

#[], #[]=, #_dump, _load, build_entity, build_entity_array, #camel_case_lower, get_all, get_by_id, #initialize, #method_missing, request_ambiguous, request_first_heterotyped, request_many_heterotyped, #serialize, #serialize_field, singleton_client, #state_params, #to_json, #to_ordered_hash, #to_s, #to_unordered_hash

Constructor Details

This class inherits a constructor from BillForward::MutableEntity

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class BillForward::BillingEntity

Class Method Details

.add_charge(id, request_object = {}, custom_client = nil) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bill_forward/entities/subscription.rb', line 30

def add_charge(id, request_object = {}, custom_client = nil)
  raise ArgumentError.new("id cannot be nil") if id.nil?

  endpoint = sprintf('%s/charge',
                     ERB::Util.url_encode(id)
  )

  request_entity = BillForward::GenericEntity.new(
      request_object
  )

  self.request_many_heterotyped(BillForward::SubscriptionCharge, 'post', endpoint, request_entity, nil, custom_client)
end

.get_by_account_id(id, query_params = {}, custom_client = nil) ⇒ Object

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
# File 'lib/bill_forward/entities/subscription.rb', line 10

def (id, query_params = {}, custom_client = nil)
  raise ArgumentError.new("id cannot be nil") if id.nil?

  endpoint = sprintf('account/%s',
    ERB::Util.url_encode(id)
    )

  self.request_many('get', endpoint, query_params, custom_client)
end

.get_by_version_id(id, query_params = {}, custom_client = nil) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
# File 'lib/bill_forward/entities/subscription.rb', line 20

def get_by_version_id(id, query_params = {}, custom_client = nil)
  raise ArgumentError.new("id cannot be nil") if id.nil?

  endpoint = sprintf('version/%s',
    ERB::Util.url_encode(id)
    )

  self.request_first('get', endpoint, query_params, custom_client)
end

Instance Method Details

#activateObject



63
64
65
66
67
# File 'lib/bill_forward/entities/subscription.rb', line 63

def activate
  set_state_param('state', 'AwaitingPayment')
  response = save
  response
end

#add_charge(request_object = {}, custom_client = nil) ⇒ Object



45
46
47
# File 'lib/bill_forward/entities/subscription.rb', line 45

def add_charge(request_object = {}, custom_client = nil)
  self.class.add_charge(self.id, request_object, custom_client)
end

#add_payment_method(id) ⇒ Object

Raises:

  • (ArgumentError)


69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bill_forward/entities/subscription.rb', line 69

def add_payment_method(id)
  raise ArgumentError.new("id cannot be nil") if id.nil?

  endpoint = sprintf('%s/payment-methods',
      ERB::Util.url_encode(self.id)
      )

  request_entity = BillForward::GenericEntity.new({
    'id' => id
    })

  self.class.request_first('post', endpoint, request_entity, nil, custom_client)
end

#get_payment_methods(query_params = {}, custom_client = nil) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/bill_forward/entities/subscription.rb', line 94

def get_payment_methods(query_params = {}, custom_client = nil)
  endpoint = sprintf('%s/payment-methods',
      ERB::Util.url_encode(self.id)
      )

  self.class.request_many_heterotyped(BillForward::PaymentMethod, 'get', endpoint, query_params, custom_client)
end

#migrate_plan(names_to_values = Hash.new, new_plan_id = nil, invoicing_type = 'Aggregated', new_plan = nil) ⇒ self

Migrates subscription to new plan, with PricingComponentValue values corresponding to named PricingComponents. This works only for ‘arrears’ or ‘in advance’ pricing components.

Note: pricing component mapping is not yet implemented, so currently the ‘names_to_values’ parameter is ignored.

Parameters:

  • names_to_values (hash) (defaults to: Hash.new)

    The map of pricing component names to numerical values (‘Bandwidth usage’ => 102)

  • new_plan_id (string) (defaults to: nil)

    ID of the plan to migrate to.

  • invoicing_type (ENUM{'Immediate', 'Aggregated'}) (defaults to: 'Aggregated')

    (Default: ‘Aggregated’) Subscription-charge invoicing type <Immediate>: Generate invoice straight away with this charge applied, <Aggregated>: Add this charge to next invoice

  • new_plan (ProductRatePlan) (defaults to: nil)

    (Alternative parameter to avoid extra API request) The plan to migrate to.

Returns:

  • (self)

    The created Entity



115
116
117
118
119
120
121
122
# File 'lib/bill_forward/entities/subscription.rb', line 115

def migrate_plan(names_to_values = Hash.new, new_plan_id = nil, invoicing_type = 'Aggregated', new_plan = nil)
  # Until pricing component mapping is implemented, we don't to fetch the full plan
  # if new_plan.nil? do
  #   new_plan = BillForward::ProductRatePlan::get_by_id(new_plan_id)
  # end

  migrate_plan_simple(new_plan_id, invoicing_type)
end

#productObject



56
57
58
59
60
61
# File 'lib/bill_forward/entities/subscription.rb', line 56

def product
  if (super.nil?)
    self.product = BillForward::Product::get_by_id self.productID
  end
  super
end

#productRatePlanObject



49
50
51
52
53
54
# File 'lib/bill_forward/entities/subscription.rb', line 49

def productRatePlan
  if (super.nil?)
    self.productRatePlan = BillForward::ProductRatePlan::get_by_id self.productRatePlanID
  end
  super
end

#remove_payment_method(id) ⇒ Object

Raises:

  • (ArgumentError)


83
84
85
86
87
88
89
90
91
92
# File 'lib/bill_forward/entities/subscription.rb', line 83

def remove_payment_method(id)
  raise ArgumentError.new("id cannot be nil") if id.nil?

  endpoint = sprintf('%s/payment-methods/%s',
      ERB::Util.url_encode(self.id),
      ERB::Util.url_encode(id)
      )

  self.class.request_first('delete', endpoint, nil, custom_client)
end