Module: EffectiveOrders

Includes:
EffectiveGem
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: Engine, SoldOutException

Constant Summary collapse

ACTIVE =

Subscription statuses (as per stripe)

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

Class Method Summary collapse

Class Method Details

.admin_payment_providersObject

For the Admin Mark as Paid screen



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/effective_orders.rb', line 188

def self.admin_payment_providers
  [
    ('cheque' if mark_as_paid?),
    ('credit card' if mark_as_paid?),
    ('deluxe' if deluxe?),
    ('etransfer' if etransfer?),
    ('helcim' if helcim?),
    #('free' if free?),
    ('moneris' if moneris?),
    ('moneris_checkout' if moneris_checkout?),
    ('paypal' if paypal?),
    ('phone' if mark_as_paid?),
    #('pretend' if pretend?),
    #('refund' if refund?),
    ('stripe' if stripe?),
    ('other (non credit card)' if mark_as_paid?),
    'none'
  ].compact
end

.buyer_purchases_refund?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/effective_orders.rb', line 146

def self.buyer_purchases_refund?
  buyer_purchases_refund == true
end

.can_skip_checkout_step1?Boolean

Returns:

  • (Boolean)


261
262
263
264
265
266
267
# File 'lib/effective_orders.rb', line 261

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
  true
end

.cheque?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/effective_orders.rb', line 82

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

.config_keysObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/effective_orders.rb', line 16

def self.config_keys
  [
    :orders_table_name, :order_items_table_name, :carts_table_name, :cart_items_table_name,
    :customers_table_name, :subscriptions_table_name, :products_table_name, :item_names_table_name,
    :layout,
    :orders_collection_scope, :order_tax_rate_method,
    :obfuscate_order_ids, :use_item_names, :require_item_names,
    :use_effective_qb_sync, :use_effective_qb_online, 
    :tax_label,
    :billing_address, :shipping_address,
    :collect_note, :collect_note_required, :collect_note_message,
    :terms_and_conditions, :terms_and_conditions_label, :minimum_charge,
    :credit_card_surcharge_percent, :credit_card_surcharge_qb_item_name,

    # Organization mode
    :organization_enabled, :organization_class_name,

    # Mailer
    :mailer, :parent_mailer, :deliver_method, :mailer_layout, :mailer_sender, :mailer_admin, :mailer_subject,

    # Emails
    :send_order_receipt_to_admin, :send_order_receipt_to_buyer, 
    :send_order_declined_to_admin, :send_order_declined_to_buyer, 
    :send_payment_request_to_admin, :send_payment_request_to_buyer,
    :send_order_receipts_when_mark_as_paid, :send_order_receipts_when_free,
    :send_subscription_events,
    :send_subscription_trialing, :send_subscription_trial_expired,
    :send_refund_notification_to_admin,

    # Quickbooks Online
    :qb_online_sync_free_orders, :qb_online_customer_display_name_format,

    # Quickbooks Online Error Emails
    :send_qb_online_sync_error, :qb_online_sync_error_recipients,

    # Features
    :free_enabled, :mark_as_paid_enabled, :pretend_enabled, :pretend_message, :buyer_purchases_refund,

    # Payment processors. false or Hash
    :cheque, :deluxe, :deluxe_delayed, :etransfer, :helcim, :moneris, :moneris_checkout, :paypal, :phone, :refund, :stripe, :subscriptions, :trial
  ]
end

.credit_card_payment_providersObject



217
218
219
# File 'lib/effective_orders.rb', line 217

def self.credit_card_payment_providers
  ['credit card', 'deluxe', 'helcim', 'moneris', 'moneris_checkout', 'paypal', 'stripe']
end

.deferred?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/effective_orders.rb', line 102

def self.deferred?
  deferred_providers.present?
end

.deferred_providersObject

Should not include delayed providers



209
210
211
# File 'lib/effective_orders.rb', line 209

def self.deferred_providers
  [('cheque' if cheque?), ('etransfer' if etransfer?), ('phone' if phone?)].compact
end

.delayed?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/effective_orders.rb', line 106

def self.delayed?
  delayed_providers.present?
end

.delayed_providersObject



213
214
215
# File 'lib/effective_orders.rb', line 213

def self.delayed_providers
  [('deluxe_delayed' if deluxe_delayed?)].compact
end

.deluxe?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/effective_orders.rb', line 94

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

.deluxe_delayed?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/effective_orders.rb', line 98

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

.deluxe_script_urlObject



358
359
360
361
362
363
364
# File 'lib/effective_orders.rb', line 358

def self.deluxe_script_url
  case EffectiveOrders.deluxe.fetch(:environment)
  when 'production' then 'https://hostedpaymentform.deluxe.com/v2/deluxe.js'
  when 'sandbox' then 'https://hostedform2.deluxe.com/V2/deluxe.js'
  else raise('unexpected EffectiveOrders.deluxe :environment key. Please check your config/initializers/effective_orders.rb file')
  end
end

.etransfer?Boolean

Returns:

  • (Boolean)


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

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

.fee_saver?Boolean

Returns:

  • (Boolean)


253
254
255
# File 'lib/effective_orders.rb', line 253

def self.fee_saver?
  helcim? && helcim[:fee_saver] == true
end

.free?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/effective_orders.rb', line 90

def self.free?
  free_enabled == true
end

.helcim?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/effective_orders.rb', line 110

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

.helcim_script_urlObject



366
367
368
# File 'lib/effective_orders.rb', line 366

def self.helcim_script_url
  "https://secure.helcim.app/helcim-pay/services/start.js"
end

.mailer_classObject



257
258
259
# File 'lib/effective_orders.rb', line 257

def self.mailer_class
  mailer&.constantize || Effective::OrdersMailer
end

.mark_as_paid?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/effective_orders.rb', line 114

def self.mark_as_paid?
  mark_as_paid_enabled == true
end

.moneris?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/effective_orders.rb', line 118

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

.moneris_checkout?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/effective_orders.rb', line 122

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

.moneris_checkout_script_urlObject



370
371
372
373
374
375
376
# File 'lib/effective_orders.rb', line 370

def self.moneris_checkout_script_url
  case EffectiveOrders.moneris_checkout.fetch(:environment)
  when 'prod' then 'https://gateway.moneris.com/chktv2/js/chkt_v2.00.js'
  when 'qa' then 'https://gatewayt.moneris.com/chktv2/js/chkt_v2.00.js'
  else raise('unexpected EffectiveOrders.moneris_checkout :environment key. Please check your config/initializers/effective_orders.rb file')
  end
end

.moneris_request_urlObject



378
379
380
381
382
383
384
# File 'lib/effective_orders.rb', line 378

def self.moneris_request_url
  case EffectiveOrders.moneris_checkout.fetch(:environment)
  when 'prod' then 'https://gateway.moneris.com/chktv2/request/request.php'
  when 'qa' then 'https://gatewayt.moneris.com/chktv2/request/request.php'
  else raise('unexpected EffectiveOrders.moneris_checkout :environment key. Please check your config/initializers/effective_orders.rb file')
  end
end

.no_refund?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/effective_orders.rb', line 142

def self.no_refund?
  !refund?
end

.OrganizationObject



74
75
76
77
78
79
80
# File 'lib/effective_orders.rb', line 74

def self.Organization
  klass = organization_class_name&.constantize
  klass ||= (EffectiveMemberships.Organization if defined?(EffectiveMemberships))
  raise('Please set the effective_orders config.organization_class_name') if klass.blank?

  klass
end

.organization_enabled?Boolean

Returns:

  • (Boolean)


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

def self.organization_enabled?
  organization_enabled == true
end

.payment_providersObject

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



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/effective_orders.rb', line 167

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

.payment_restriction(payment_provider, order) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/effective_orders.rb', line 269

def self.payment_restriction(payment_provider, order)
  provider = try(payment_provider)
  raise("Unexpected payment provider: #{payment_provider || 'nil'}") unless provider.kind_of?(Hash)

  if (price = provider[:min_price]).present? && order.total < provider[:min_price]
    return "is only available for invoices over $#{'%0.2f' % (price / 100.00)}"
  end

  if (price = provider[:max_price]).present? && order.total > provider[:max_price]
    return "is only available for invoices up to $#{'%0.2f' % (price / 100.00)}"
  end

  # No restrictions
  nil
end

.paypal?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/effective_orders.rb', line 126

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

.permitted_paramsObject



61
62
63
64
65
66
67
68
# File 'lib/effective_orders.rb', line 61

def self.permitted_params
  @permitted_params ||= [
    :cc, :note, :terms_and_conditions, :confirmed_checkout,
    billing_address: EffectiveAddresses.permitted_params,
    shipping_address: EffectiveAddresses.permitted_params,
    subscripter: [:stripe_plan_id, :stripe_token]
  ]
end

.phone?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/effective_orders.rb', line 130

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

.pretend?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/effective_orders.rb', line 134

def self.pretend?
  pretend_enabled == true
end

.qb_online?Boolean

Returns:

  • (Boolean)


225
226
227
# File 'lib/effective_orders.rb', line 225

def self.qb_online?
  use_effective_qb_online && defined?(EffectiveQbOnline)
end

.qb_online_sync_free_orders?Boolean

Returns:

  • (Boolean)


229
230
231
# File 'lib/effective_orders.rb', line 229

def self.qb_online_sync_free_orders?
  qb_online_sync_free_orders != false
end

.qb_sync?Boolean

Returns:

  • (Boolean)


221
222
223
# File 'lib/effective_orders.rb', line 221

def self.qb_sync?
  use_effective_qb_sync && defined?(EffectiveQbSync)
end

.quickbooks?Boolean

Returns:

  • (Boolean)


245
246
247
# File 'lib/effective_orders.rb', line 245

def self.quickbooks?
  use_effective_qb_sync || use_effective_qb_online
end

.refund?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/effective_orders.rb', line 138

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

.require_item_names?Boolean

Returns:

  • (Boolean)


237
238
239
# File 'lib/effective_orders.rb', line 237

def self.require_item_names?
  require_item_names == true
end

.single_payment_processor?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/effective_orders.rb', line 162

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

.stripe?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/effective_orders.rb', line 150

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

.stripe_plansObject



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/effective_orders.rb', line 296

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

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

    plans = begin
      Stripe::Plan.respond_to?(:all) ? Stripe::Plan.all : Stripe::Plan.list
    rescue Exception => 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|
      description = ("$#{'%0.2f' % (plan.amount / 100.0)}" + ' ' + plan.currency.upcase + '/' +  plan.interval.to_s)

      {
        id: plan.id,
        product_id: plan.product,
        name: plan.nickname || description,
        description: description,
        amount: plan.amount,
        currency: plan.currency,
        interval: plan.interval,
        interval_count: plan.interval_count,
        trial_period_days: (plan.trial_period_days if plan.respond_to?(:trial_period_days))
      }
    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



354
355
356
# File 'lib/effective_orders.rb', line 354

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

.stripe_script_urlObject



386
387
388
# File 'lib/effective_orders.rb', line 386

def self.stripe_script_url
  "https://js.stripe.com/v3/"
end

.subscriptions?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/effective_orders.rb', line 154

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

.surcharge?Boolean

Returns:

  • (Boolean)


249
250
251
# File 'lib/effective_orders.rb', line 249

def self.surcharge?
  credit_card_surcharge_percent.to_f > 0.0
end

.tax_labelObject



241
242
243
# File 'lib/effective_orders.rb', line 241

def self.tax_label
  config[:tax_label].presence || "Tax"
end

.trial?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/effective_orders.rb', line 158

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

.use_item_names?Boolean

Returns:

  • (Boolean)


233
234
235
# File 'lib/effective_orders.rb', line 233

def self.use_item_names?
  use_item_names != false
end

.with_stripe(&block) ⇒ Object



285
286
287
288
289
290
291
292
293
294
# File 'lib/effective_orders.rb', line 285

def self.with_stripe(&block)
  raise('expected stripe to be enabled') unless stripe?

  begin
    ::Stripe.api_key = stripe[:secret_key]
    yield
  ensure
    ::Stripe.api_key = nil
  end
end