Module: EffectiveOrders

Defined in:
lib/effective_orders.rb,
lib/effective_orders/engine.rb,
lib/effective_orders/version.rb,
lib/generators/effective_orders/install_generator.rb

Defined Under Namespace

Modules: Generators Classes: AlreadyPurchasedException, Engine, SoldOutException

Constant Summary collapse

PENDING =
'pending'.freeze
CONFIRMED =
'confirmed'.freeze
PURCHASED =
'purchased'.freeze
DECLINED =
'declined'.freeze
STATES =
{ PENDING => PENDING, CONFIRMED => CONFIRMED, PURCHASED => PURCHASED, DECLINED => DECLINED }
ACTIVE =

Subscription statuses (as per stripe)

'active'.freeze
PAST_DUE =
'past_due'.freeze
CANCELED =
'canceled'.freeze
STATUSES =
{ ACTIVE => ACTIVE, PAST_DUE => PAST_DUE, CANCELED => CANCELED }
VERSION =
'4.3.1'.freeze

Class Method Summary collapse

Class Method Details

.authorize!(controller, action, resource) ⇒ Object



86
87
88
# File 'lib/effective_orders.rb', line 86

def self.authorize!(controller, action, resource)
  raise Effective::AccessDenied.new('Access Denied', action, resource) unless authorized?(controller, action, resource)
end

.authorized?(controller, action, resource) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/effective_orders.rb', line 73

def self.authorized?(controller, action, resource)
  @_exceptions ||= [Effective::AccessDenied, (CanCan::AccessDenied if defined?(CanCan)), (Pundit::NotAuthorizedError if defined?(Pundit))].compact

  return !!authorization_method unless authorization_method.respond_to?(:call)
  controller = controller.controller if controller.respond_to?(:controller)

  begin
    !!(controller || self).instance_exec((controller || self), action, resource, &authorization_method)
  rescue *@_exceptions
    false
  end
end

.can_skip_checkout_step1?Boolean

Returns:

  • (Boolean)


159
160
161
162
163
164
165
166
# File 'lib/effective_orders.rb', line 159

def self.can_skip_checkout_step1?
  return false if require_billing_address
  return false if require_shipping_address
  return false if collect_note
  return false if terms_and_conditions
  return false if collect_user_fields.present?
  true
end

.cheque?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/effective_orders.rb', line 100

def self.cheque?
  cheque.kind_of?(Hash)
end

.free?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/effective_orders.rb', line 104

def self.free?
  free_enabled == true
end

.mark_as_paid?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/effective_orders.rb', line 108

def self.mark_as_paid?
  mark_as_paid_enabled == true
end

.moneris?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/effective_orders.rb', line 112

def self.moneris?
  moneris.kind_of?(Hash)
end

.payment_providersObject

The Effective::Order.payment_provider value must be in this collection



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/effective_orders.rb', line 145

def self.payment_providers
  [
    ('cheque' if cheque?),
    ('free' if free?),
    ('moneris' if moneris?),
    ('paypal' if paypal?),
    ('pretend' if pretend?),
    ('stripe' if stripe?),
    ('credit card' if mark_as_paid?),
    ('other' if mark_as_paid?),
    'none'
  ].compact
end

.paypal?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/effective_orders.rb', line 116

def self.paypal?
  paypal.kind_of?(Hash)
end

.permitted_paramsObject



90
91
92
93
94
95
96
97
98
# File 'lib/effective_orders.rb', line 90

def self.permitted_params
  [
    :note, :terms_and_conditions, :confirmed_checkout,
    billing_address: EffectiveAddresses.permitted_params,
    shipping_address: EffectiveAddresses.permitted_params,
    user_attributes: (EffectiveOrders.collect_user_fields || []),
    subscripter: [:stripe_plan_id, :stripe_token]
  ]
end

.pretend?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/effective_orders.rb', line 120

def self.pretend?
  pretend_enabled == true
end

.refunds?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/effective_orders.rb', line 124

def self.refunds?
  refunds_enabled == true
end

.setup {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



69
70
71
# File 'lib/effective_orders.rb', line 69

def self.setup
  yield self
end

.single_payment_processor?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/effective_orders.rb', line 140

def self.single_payment_processor?
  [cheque?, moneris?, paypal?, stripe?].select { |enabled| enabled }.length == 1
end

.stripe?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/effective_orders.rb', line 128

def self.stripe?
  stripe.kind_of?(Hash)
end

.stripe_plansObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/effective_orders.rb', line 168

def self.stripe_plans
  return [] unless (stripe? && subscriptions?)

  @stripe_plans ||= (
    Rails.logger.info '[STRIPE] index plans'

    plans = begin
      Stripe::Plan.all
    rescue => e
      raise e if Rails.env.production?
      Rails.logger.info "[STRIPE ERROR]: #{e.message}"
      Rails.logger.info "[STRIPE ERROR]: effective_orders continuing with empty stripe plans. This would fail loudly in Rails.env.production."
      []
    end

    plans = plans.map do |plan|
      {
        id: plan.id,
        product_id: plan.product,
        name: plan.nickname,
        amount: plan.amount,
        currency: plan.currency,
        description: ("$#{'%0.2f' % (plan.amount / 100.0)}" + ' ' + plan.currency.upcase + '/' +  plan.interval.to_s),
        interval: plan.interval,
        interval_count: plan.interval_count
      }
    end.sort do |x, y|
      val ||= (x[:interval] <=> y[:interval])
      val = nil if val == 0

      val ||= (x[:amount] <=> y[:amount])
      val = nil if val == 0

      val ||= (x[:name] <=> y[:name])
      val = nil if val == 0

      val || (x[:id] <=> y[:id])
    end

    # Calculate savings for any yearly per user plans, based on their matching monthly plans
    plans.select { |plan| plan[:interval] == 'year' }.each do |yearly|
      monthly_name = yearly[:name].downcase.gsub('year', 'month')
      monthly = plans.find { |plan| plan[:interval] == 'month' && plan[:name].downcase == monthly_name }
      next unless monthly

      savings = (monthly[:amount].to_i * 12) - yearly[:amount].to_i
      next unless savings > 0

      yearly[:savings] = savings
    end

    plans
  )
end

.stripe_plans_collectionObject



223
224
225
# File 'lib/effective_orders.rb', line 223

def self.stripe_plans_collection
  stripe_plans.map { |plan| [plan[:name], plan[:id]] }
end

.subscriptions?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/effective_orders.rb', line 132

def self.subscriptions?
  subscriptions.kind_of?(Hash)
end

.trial?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/effective_orders.rb', line 136

def self.trial?
  trial.kind_of?(Hash)
end