Skip to content
lyang edited this page Mar 17, 2013 · 2 revisions

Attributes

Create

[
  :billing_day_of_month, :first_billing_date, :id, :merchant_account_id, :never_expires, 
  :number_of_billing_cycles, :payment_method_token, :plan_id, :price, :trial_duration,
  :trial_duration_unit, :trial_period, :options, :descriptor
]

Update

[
  :merchant_account_id, :never_expires, :number_of_billing_cycles,
  :payment_method_token, :plan_id, :price, :options
]

Readonly

[
  :balance, :billing_period_end_date, :billing_period_start_date, :current_billing_cycle, :days_past_due,
  :failure_count, :next_billing_date, :next_billing_period_amount, :paid_through_date, :status
]

Associations

AddOns (readonly collection)

subscription = BraintreeRails::Subscription.find('subscription_id')
subscription.add_ons # eager loaded collection

Discounts (readonly collection)

subscription = BraintreeRails::Subscription.find('subscription_id')
subscription.discounts # eager loaded collection

Transactions (readonly collection)

subscription = BraintreeRails::Subscription.find('subscription_id')
subscription.transactions # lazy loaded, readonly collection

CreditCard

subscription = BraintreeRails::Subscription.find('subscription_id')
subscription.credit_card # Loaded from payment_method_token

Plan

subscription = BraintreeRails::Subscription.find('subscription_id')
subscription.plan # Loaded from plan_id

Default Validations

[:id, :format => {:with => /\A[-_[:alnum:]]*\z/},  :exclusion => {:in => %w(all new)}],
[:billing_day_of_month, :numericality => { :only_integer => true }, :inclusion => {:in => [*(1..28), 31]}, :allow_nil => true, :if => :new_record?],
[:number_of_billing_cycles, :numericality => { :only_integer => true, :greater_than_or_equal_to  => 1 }, :allow_nil => true],
[:payment_method_token, :presence => true, :if => :new_record?],
[:plan_id, :presence => true, :if => :new_record?],
[:price, :numericality => true, :allow_nil => true],
[:trial_duration, :presence => true, :numericality => { :only_integer => true, :greater_than_or_equal_to => 1, :less_than_or_equal_to => 9999 }, :if => :trial_period],
[:trial_duration_unit, :presence => true, :inclusion => { :in => %w(day month) }, :if => :trial_period]

Additional validations

In addition to the above lists, there are also other logical validations

  • number_of_billing_cycles_must_be_greater_than_current_billing_cycle
        if number_of_billing_cycles.present? && current_billing_cycle.present?
          errors.add(:number_of_billing_cycles, "is too small.") if number_of_billing_cycles < current_billing_cycle
        end
  • first_billing_date_must_be_valid_future_date
        begin
          if new_record? && first_billing_date.present?
            errors.add(:first_billing_date, "cannot be in the past.") if DateTime.parse(first_billing_date.to_s) < Date.today
          end
        rescue ArgumentError
          errors.add(:first_billing_date, "is invalid.")
        end

Default Callbacks

(before|after)_validate
(before|around|after)_save
(before|around|after)_create
(before|around|after)_update
(before|around|after)_destroy

Convenient Methods

cancel

BraintreeRails::Subscription.cancel('subscription_id')
subscription.cancel

Supported REST APIs

Create

Find

Update

Delete

  • delete/destroy a subscription will cancel the subscription
Clone this wiki locally