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

PENDING =

Order states

'pending'
CONFIRMED =

New orders are created in a pending state

'confirmed'
DEFERRED =

Once the order has passed checkout step 1

'deferred'
PURCHASED =

Deferred providers. cheque, etransfer or phone was selected.

'purchased'
DECLINED =

Purchased by provider

'declined'
ABANDONED =

Declined by provider

'abandoned'
STATES =

Not set by this gem. Can be set outside it.

{
  PENDING => PENDING,
  CONFIRMED => CONFIRMED,
  DEFERRED => DEFERRED,
  PURCHASED => PURCHASED,
  DECLINED => DECLINED,
  ABANDONED => ABANDONED
}
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.2.1'.freeze

Class Method Summary collapse

Class Method Details

.admin_payment_providersObject

For the Admin Mark as Paid screen



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/effective_orders.rb', line 157

def self.admin_payment_providers
  [
    ('cheque' if mark_as_paid?),
    ('credit card' if mark_as_paid?),
    ('etransfer' if etransfer?),
    #('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)


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

def self.buyer_purchases_refund?
  buyer_purchases_refund == true
end

.can_skip_checkout_step1?Boolean

Returns:

  • (Boolean)


199
200
201
202
203
204
205
# File 'lib/effective_orders.rb', line 199

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)


73
74
75
# File 'lib/effective_orders.rb', line 73

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

.config_keysObject



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
58
59
60
# File 'lib/effective_orders.rb', line 32

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,
    :layout,
    :orders_collection_scope, :order_tax_rate_method,
    :obfuscate_order_ids, :use_effective_qb_sync, :use_effective_qb_online,
    :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,

    # 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_payment_request_to_buyer, :send_pending_order_invoice_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,

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

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

.credit_card_payment_providersObject



179
180
181
# File 'lib/effective_orders.rb', line 179

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

.deferred?Boolean

Returns:

  • (Boolean)


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

def self.deferred?
  deferred_providers.present?
end

.deferred_providersObject



175
176
177
# File 'lib/effective_orders.rb', line 175

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

.etransfer?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/effective_orders.rb', line 77

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

.free?Boolean

Returns:

  • (Boolean)


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

def self.free?
  free_enabled == true
end

.gem_pathObject



298
299
300
# File 'lib/effective_orders.rb', line 298

def self.gem_path
  __dir__.chomp('/lib')
end

.mailer_classObject



195
196
197
# File 'lib/effective_orders.rb', line 195

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

.mark_as_paid?Boolean

Returns:

  • (Boolean)


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

def self.mark_as_paid?
  mark_as_paid_enabled == true
end

.moneris?Boolean

Returns:

  • (Boolean)


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

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

.moneris_checkout?Boolean

Returns:

  • (Boolean)


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

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

.moneris_checkout_script_urlObject



280
281
282
283
284
285
286
# File 'lib/effective_orders.rb', line 280

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

.moneris_request_urlObject



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

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

.payment_providersObject

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



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/effective_orders.rb', line 138

def self.payment_providers
  [
    ('cheque' if cheque?),
    ('credit card' if mark_as_paid?),
    ('etransfer' if etransfer?),
    ('free' if free?),
    ('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

.paypal?Boolean

Returns:

  • (Boolean)


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

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

.permitted_paramsObject



64
65
66
67
68
69
70
71
# File 'lib/effective_orders.rb', line 64

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)


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

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

.pretend?Boolean

Returns:

  • (Boolean)


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

def self.pretend?
  pretend_enabled == true
end

.qb_online?Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/effective_orders.rb', line 187

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

.qb_sync?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/effective_orders.rb', line 183

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

.refund?Boolean

Returns:

  • (Boolean)


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

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

.single_payment_processor?Boolean

Returns:

  • (Boolean)


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

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

.stripe?Boolean

Returns:

  • (Boolean)


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

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

.stripe_plansObject



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/effective_orders.rb', line 218

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 => 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



276
277
278
# File 'lib/effective_orders.rb', line 276

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

.subscriptions?Boolean

Returns:

  • (Boolean)


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

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

.surcharge?Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/effective_orders.rb', line 191

def self.surcharge?
  credit_card_surcharge_percent.to_f > 0.0
end

.trial?Boolean

Returns:

  • (Boolean)


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

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

.with_stripe(&block) ⇒ Object



207
208
209
210
211
212
213
214
215
216
# File 'lib/effective_orders.rb', line 207

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