Class: Spree::Order

Inherits:
Base
  • Object
show all
Extended by:
DisplayMoney
Includes:
Metadata, Payments
Defined in:
app/models/spree/order.rb,
app/models/spree/order/payments.rb

Overview

The customers cart until completed, then acts as permanent record of the transaction.

‘Spree::Order` is the heart of the Solidus system, as it acts as the customer’s cart as they shop. Once an order is complete, it serves as the permanent record of their purchase. It has many responsibilities:

  • Records and validates attributes like ‘total` and relationships like

‘Spree::LineItem` as an ActiveRecord model.

  • Implements a customizable state machine to manage the lifecycle of an order.

  • Implements business logic to provide a single interface for quesitons like

‘checkout_allowed?` or `payment_required?`.

* Implements an interface for mutating the order with methods like

‘empty!` and `fulfill!`.

Defined Under Namespace

Modules: Payments Classes: CannotRebuildShipments, InsufficientStock, NumberGenerator

Constant Summary collapse

ORDER_NUMBER_LENGTH =
9
ORDER_NUMBER_LETTERS =
false
ORDER_NUMBER_PREFIX =
"R"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DisplayMoney

money_methods

Methods included from Payments

#authorize_payments!, #capture_payments!, #process_payments!, #unprocessed_payments

Methods inherited from Base

display_includes

Methods included from Core::Permalinks

#generate_permalink, #save_permalink

Instance Attribute Details

#coupon_codeObject

Returns the value of attribute coupon_code.



72
73
74
# File 'app/models/spree/order.rb', line 72

def coupon_code
  @coupon_code
end

#temporary_addressObject

Returns the value of attribute temporary_address.



73
74
75
# File 'app/models/spree/order.rb', line 73

def temporary_address
  @temporary_address
end

#temporary_payment_sourceObject

Returns the value of attribute temporary_payment_source.



75
76
77
# File 'app/models/spree/order.rb', line 75

def temporary_payment_source
  @temporary_payment_source
end

#use_billingObject

Returns the value of attribute use_billing.



145
146
147
# File 'app/models/spree/order.rb', line 145

def use_billing
  @use_billing
end

#use_shippingObject

Returns the value of attribute use_shipping.



146
147
148
# File 'app/models/spree/order.rb', line 146

def use_shipping
  @use_shipping
end

Class Method Details

.by_customer(customer) ⇒ Object



197
198
199
# File 'app/models/spree/order.rb', line 197

def self.by_customer(customer)
  joins(:user).where("#{Spree.user_class.table_name}.email" => customer)
end

.by_state(state) ⇒ Object



201
202
203
# File 'app/models/spree/order.rb', line 201

def self.by_state(state)
  where(state:)
end

.canceledObject



213
214
215
# File 'app/models/spree/order.rb', line 213

def self.canceled
  where(state: "canceled")
end

.completeObject



205
206
207
# File 'app/models/spree/order.rb', line 205

def self.complete
  where.not(completed_at: nil)
end

.find_by_param(value) ⇒ Object



157
158
159
# File 'app/models/spree/order.rb', line 157

def self.find_by_param(value)
  find_by number: value
end

.find_by_param!(value) ⇒ Object



161
162
163
# File 'app/models/spree/order.rb', line 161

def self.find_by_param!(value)
  find_by! number: value
end

.incompleteObject



209
210
211
# File 'app/models/spree/order.rb', line 209

def self.incomplete
  where(completed_at: nil)
end

.line_item_comparison_hooksObject



178
179
180
# File 'app/models/spree/order.rb', line 178

def line_item_comparison_hooks
  Spree::Config.line_item_comparison_hooks
end

.line_item_comparison_hooks=(value) ⇒ Object



172
173
174
# File 'app/models/spree/order.rb', line 172

def line_item_comparison_hooks=(value)
  Spree::Config.line_item_comparison_hooks = value.to_a
end

.not_canceledObject



217
218
219
# File 'app/models/spree/order.rb', line 217

def self.not_canceled
  where.not(state: "canceled")
end

.register_line_item_comparison_hook(hook) ⇒ Object



183
184
185
# File 'app/models/spree/order.rb', line 183

def register_line_item_comparison_hook(hook)
  Spree::Config.line_item_comparison_hooks << hook
end

Instance Method Details

#add_default_payment_from_walletObject



698
699
700
701
702
703
704
705
706
707
708
709
710
# File 'app/models/spree/order.rb', line 698

def add_default_payment_from_wallet
  builder = Spree::Config.default_payment_builder_class.new(self)

  if (payment = builder.build)
    payments << payment

    if bill_address.nil?
      # this is one of 2 places still using User#bill_address
      self.bill_address = payment.source.try(:address) ||
        user.bill_address
    end
  end
end

#add_payment_sources_to_walletObject



692
693
694
695
696
# File 'app/models/spree/order.rb', line 692

def add_payment_sources_to_wallet
  Spree::Config
    .add_payment_sources_to_wallet_class.new(self)
    .add_to_wallet
end

#add_store_credit_paymentsObject



585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# File 'app/models/spree/order.rb', line 585

def add_store_credit_payments
  return if user.nil?
  return if payments.store_credits.checkout.empty? && user.available_store_credit_total(currency:).zero?

  payments.store_credits.checkout.each(&:invalidate!)

  # this can happen when multiple payments are present, auto_capture is
  # turned off, and one of the payments fails when the user tries to
  # complete the order, which sends the order back to the 'payment' state.
  authorized_total = payments.pending.sum(:amount)

  remaining_total = outstanding_balance - authorized_total

  matching_store_credits = user.store_credits.where(currency:)

  if matching_store_credits.any?
    payment_method = Spree::PaymentMethod::StoreCredit.first
    sorter = Spree::Config.store_credit_prioritizer_class.new(matching_store_credits, self)

    sorter.call.each do |credit|
      break if remaining_total.zero?
      next if credit.amount_remaining.zero?

      amount_to_take = [credit.amount_remaining, remaining_total].min
      payments.create!(source: credit,
        payment_method:,
        amount: amount_to_take,
        state: "checkout",
        response_code: credit.generate_authorization_code)
      remaining_total -= amount_to_take
    end
  end

  other_payments = payments.checkout.not_store_credits
  if remaining_total.zero?
    other_payments.each(&:invalidate!)
  elsif other_payments.size == 1
    other_payments.first.update!(amount: remaining_total)
  end

  payments.reset

  if payments.where(state: %w[checkout pending completed]).sum(:amount) != total
    errors.add(:base, I18n.t("spree.store_credit.errors.unable_to_fund")) && (return false)
  end
end

#all_inventory_units_returned?Boolean

Returns:

  • (Boolean)


302
303
304
305
306
307
308
# File 'app/models/spree/order.rb', line 302

def all_inventory_units_returned?
  # Inventory units are transitioned to the "return" state through CustomerReturn and
  # ReturnItem instead of using Order#inventory_units, thus making the latter method
  # potentially return stale data. This situation requires to *reload* `inventory_units`
  # in order to pick-up the latest changes and make the check on `returned?` reliable.
  inventory_units.reload.all?(&:returned?)
end

#allow_cancel?Boolean

Returns:

  • (Boolean)


297
298
299
300
# File 'app/models/spree/order.rb', line 297

def allow_cancel?
  return false unless completed? && state != "canceled"
  shipment_state.nil? || %w[ready backorder pending].include?(shipment_state)
end

#amountObject

For compatiblity with Calculator::PriceSack



222
223
224
# File 'app/models/spree/order.rb', line 222

def amount
  line_items.sum(&:amount)
end

#approved?Boolean

Returns:

  • (Boolean)


565
566
567
# File 'app/models/spree/order.rb', line 565

def approved?
  !!approved_at
end

#assign_billing_to_shipping_addressObject



287
288
289
290
# File 'app/models/spree/order.rb', line 287

def assign_billing_to_shipping_address
  self.ship_address = bill_address if bill_address
  true
end

#assign_default_user_addressesObject

Note:

This doesn’t persist the change bill_address or ship_address

Assigns a default bill_address and ship_address to the order based on the associated user’s bill_address and ship_address.



674
675
676
677
678
679
680
681
682
683
684
# File 'app/models/spree/order.rb', line 674

def assign_default_user_addresses
  if user
    bill_address = user.bill_address
    ship_address = user.ship_address
    # this is one of 2 places still using User#bill_address
    self.bill_address ||= bill_address if bill_address.try!(:valid?)
    # Skip setting ship address if order doesn't have a delivery checkout step
    # to avoid triggering validations on shipping address
    self.ship_address ||= ship_address if ship_address.try!(:valid?) && checkout_steps.include?("delivery")
  end
end

#assign_shipping_to_billing_addressObject



292
293
294
295
# File 'app/models/spree/order.rb', line 292

def assign_shipping_to_billing_address
  self.bill_address = ship_address if ship_address
  true
end

#associate_user!(user, override_email = true) ⇒ Object

Associates the specified user with the order.



323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'app/models/spree/order.rb', line 323

def associate_user!(user, override_email = true)
  self.user = user
  attrs_to_set = {user_id: user.try(:id)}
  attrs_to_set[:email] = user.try(:email) if override_email
  attrs_to_set[:created_by_id] = user.try(:id) if created_by.blank?

  if persisted?
    # immediately persist the changes we just made, but don't use save since we might have an invalid address associated
    self.class.unscoped.where(id:).update_all(attrs_to_set)
  end

  assign_attributes(attrs_to_set)
end

#available_payment_methodsObject



432
433
434
435
436
437
438
# File 'app/models/spree/order.rb', line 432

def available_payment_methods
  @available_payment_methods ||= Spree::PaymentMethod
    .active
    .available_to_store(store)
    .available_to_users
    .order(:position)
end

#backordered?Boolean

Returns:

  • (Boolean)


268
269
270
# File 'app/models/spree/order.rb', line 268

def backordered?
  shipments.any?(&:backordered?)
end

#bill_address_attributes=(attributes) ⇒ Object



663
664
665
# File 'app/models/spree/order.rb', line 663

def bill_address_attributes=(attributes)
  self.bill_address = Spree::Address.immutable_merge(bill_address, attributes)
end

#billing_address_required?Boolean

Returns:

  • (Boolean)


500
501
502
# File 'app/models/spree/order.rb', line 500

def billing_address_required?
  Spree::Config.billing_address_required
end

#can_add_coupon?Boolean

Returns:

  • (Boolean)


478
479
480
# File 'app/models/spree/order.rb', line 478

def can_add_coupon?
  Spree::Config.promotions.coupon_code_handler_class.new(self).can_apply?
end

#can_approve?Boolean

Returns:

  • (Boolean)


569
570
571
# File 'app/models/spree/order.rb', line 569

def can_approve?
  !approved?
end

#can_ship?Boolean

Returns:

  • (Boolean)


407
408
409
# File 'app/models/spree/order.rb', line 407

def can_ship?
  complete? || resumed? || awaiting_return? || returned?
end

#canceled_by(user) ⇒ Object



558
559
560
561
562
563
# File 'app/models/spree/order.rb', line 558

def canceled_by(user)
  transaction do
    cancel!
    update_column(:canceler_id, user.id)
  end
end

#cancellationsObject



318
319
320
# File 'app/models/spree/order.rb', line 318

def cancellations
  @cancellations ||= Spree::Config.order_cancellations_class.new(self)
end

#check_shipments_and_restart_checkoutObject Also known as: ensure_updated_shipments

Clean shipments and make order back to address state (or to whatever state is set by restart_checkout_flow in case of state machine modifications)



525
526
527
528
529
530
531
# File 'app/models/spree/order.rb', line 525

def check_shipments_and_restart_checkout
  if !completed? && shipments.all?(&:pending?)
    shipments.destroy_all
    update_column(:shipment_total, 0)
    restart_checkout_flow
  end
end

#checkout_allowed?Boolean

Indicates whether or not the user is allowed to proceed to checkout. Currently this is implemented as a check for whether or not there is at least one LineItem in the Order. Feel free to override this logic in your own application if you require additional steps before allowing a checkout.

Returns:

  • (Boolean)


259
260
261
# File 'app/models/spree/order.rb', line 259

def checkout_allowed?
  line_items.count > 0
end

#completed?Boolean

Returns:

  • (Boolean)


251
252
253
# File 'app/models/spree/order.rb', line 251

def completed?
  completed_at.present?
end

#contains?(variant, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


345
346
347
# File 'app/models/spree/order.rb', line 345

def contains?(variant, options = {})
  find_line_item_by_variant(variant, options).present?
end

#contentsObject



310
311
312
# File 'app/models/spree/order.rb', line 310

def contents
  @contents ||= Spree::Config.order_contents_class.new(self)
end

#covered_by_store_credit?Boolean Also known as: covered_by_store_credit

Returns:

  • (Boolean)


632
633
634
635
# File 'app/models/spree/order.rb', line 632

def covered_by_store_credit?
  return false unless user
  user.available_store_credit_total(currency:) >= total
end

#create_proposed_shipmentsObject



504
505
506
507
508
509
510
511
512
513
# File 'app/models/spree/order.rb', line 504

def create_proposed_shipments
  if completed?
    raise CannotRebuildShipments.new(I18n.t("spree.cannot_rebuild_shipments_order_completed"))
  elsif shipments.any? { |shipment| !shipment.pending? }
    raise CannotRebuildShipments.new(I18n.t("spree.cannot_rebuild_shipments_shipments_not_pending"))
  else
    shipments.destroy_all
    shipments.push(*Spree::Config.stock.coordinator_class.new(self).shipments)
  end
end

#create_shipments_for_line_item(line_item) ⇒ Object



515
516
517
518
519
520
521
# File 'app/models/spree/order.rb', line 515

def create_shipments_for_line_item(line_item)
  units = Spree::Config.stock.inventory_unit_builder_class.new(self).missing_units_for_line_item(line_item)

  Spree::Config.stock.coordinator_class.new(self, units).shipments.each do |shipment|
    shipments << shipment
  end
end

#credit_cardsObject



411
412
413
414
# File 'app/models/spree/order.rb', line 411

def credit_cards
  credit_card_ids = payments.from_credit_card.pluck(:source_id).uniq
  Spree::CreditCard.where(id: credit_card_ids)
end

#currencyObject



239
240
241
# File 'app/models/spree/order.rb', line 239

def currency
  self[:currency] || Spree::Config[:currency]
end

#display_store_credit_remaining_after_captureObject



659
660
661
# File 'app/models/spree/order.rb', line 659

def display_store_credit_remaining_after_capture
  Spree::Money.new(total_available_store_credit - total_applicable_store_credit, {currency:})
end

#display_total_applicable_store_creditObject



655
656
657
# File 'app/models/spree/order.rb', line 655

def display_total_applicable_store_credit
  Spree::Money.new(-total_applicable_store_credit, {currency:})
end

#empty!Object



461
462
463
464
465
466
467
468
# File 'app/models/spree/order.rb', line 461

def empty!
  line_items.destroy_all
  adjustments.destroy_all
  shipments.destroy_all
  Spree::Bus.publish :order_emptied, order: self

  recalculate
end

#ensure_billing_addressObject



492
493
494
495
496
497
498
# File 'app/models/spree/order.rb', line 492

def ensure_billing_address
  return unless billing_address_required?
  return if bill_address&.valid?

  errors.add(:base, I18n.t("spree.bill_address_required"))
  false
end

#ensure_line_item_variants_are_not_deletedObject

Check to see if any line item variants are soft, deleted. If so add error and restart checkout.



447
448
449
450
451
452
453
454
455
# File 'app/models/spree/order.rb', line 447

def ensure_line_item_variants_are_not_deleted
  if line_items.any? { |li| li.variant.discarded? }
    errors.add(:base, I18n.t("spree.deleted_variants_present"))
    restart_checkout_flow
    false
  else
    true
  end
end

#ensure_shipping_addressObject



486
487
488
489
490
# File 'app/models/spree/order.rb', line 486

def ensure_shipping_address
  unless ship_address&.valid?
    errors.add(:base, I18n.t("spree.ship_address_required")) && (return false)
  end
end

#find_line_item_by_variant(variant, options = {}) ⇒ Object



354
355
356
357
358
359
# File 'app/models/spree/order.rb', line 354

def find_line_item_by_variant(variant, options = {})
  line_items.detect { |line_item|
    line_item.variant_id == variant.id &&
      line_item_options_match(line_item, options)
  }
end

#fulfill!Object



421
422
423
424
425
# File 'app/models/spree/order.rb', line 421

def fulfill!
  shipments.each { |shipment| shipment.update_state if shipment.persisted? }
  recalculator.recalculate_shipment_state
  save!
end

#generate_order_numberObject



337
338
339
# File 'app/models/spree/order.rb', line 337

def generate_order_number
  self.number ||= Spree::Config.order_number_generator.generate
end

Returns:

  • (Boolean)


577
578
579
# File 'app/models/spree/order.rb', line 577

def has_non_reimbursement_related_refunds?
  refunds.non_reimbursement.exists?
end

#insufficient_stock_linesObject



440
441
442
# File 'app/models/spree/order.rb', line 440

def insufficient_stock_lines
  line_items.select(&:insufficient_stock?)
end

#is_risky?Boolean

Returns:

  • (Boolean)


554
555
556
# File 'app/models/spree/order.rb', line 554

def is_risky?
  payments.risky.count > 0
end

#item_total_before_taxObject



226
227
228
# File 'app/models/spree/order.rb', line 226

def item_total_before_tax
  line_items.to_a.sum(&:total_before_tax)
end

#item_total_excluding_vatObject

Sum of all line item amounts pre-tax



235
236
237
# File 'app/models/spree/order.rb', line 235

def item_total_excluding_vat
  line_items.to_a.sum(&:total_excluding_vat)
end

#line_item_options_match(line_item, options) ⇒ Object

This method enables extensions to participate in the “Are these line items equal” decision.

When adding to cart, an extension would send something like: params=…

and would provide:

def product_customizations_match



370
371
372
373
374
375
376
# File 'app/models/spree/order.rb', line 370

def line_item_options_match(line_item, options)
  return true unless options

  Spree::Config.line_item_comparison_hooks.all? { |hook|
    send(hook, line_item, options)
  }
end

#merge!(*args) ⇒ Object



457
458
459
# File 'app/models/spree/order.rb', line 457

def merge!(*args)
  Spree::Config.order_merger_class.new(self).merge!(*args)
end

#nameObject



401
402
403
404
405
# File 'app/models/spree/order.rb', line 401

def name
  if (address = bill_address || ship_address)
    address.name
  end
end

#order_total_after_store_creditObject



643
644
645
# File 'app/models/spree/order.rb', line 643

def order_total_after_store_credit
  total - total_applicable_store_credit
end

#outstanding_balanceObject



382
383
384
385
386
387
388
389
390
391
# File 'app/models/spree/order.rb', line 382

def outstanding_balance
  # If reimbursement has happened add it back to total to prevent balance_due payment state
  # See: https://github.com/spree/spree/issues/6229

  if state == "canceled"
    -1 * payment_total
  else
    total - reimbursement_total - payment_total
  end
end

#outstanding_balance?Boolean

Returns:

  • (Boolean)


393
394
395
# File 'app/models/spree/order.rb', line 393

def outstanding_balance?
  outstanding_balance != 0
end

#paid?Boolean

Helper methods for checkout steps

Returns:

  • (Boolean)


428
429
430
# File 'app/models/spree/order.rb', line 428

def paid?
  %w[paid credit_owed].include?(payment_state)
end

#payment_required?Boolean

Is this a free order in which case the payment step should be skipped

Returns:

  • (Boolean)


264
265
266
# File 'app/models/spree/order.rb', line 264

def payment_required?
  total > 0
end

#payments_attributes=(attributes) ⇒ Object



720
721
722
723
# File 'app/models/spree/order.rb', line 720

def payments_attributes=(attributes)
  validate_payments_attributes(attributes)
  super
end

#persist_user_address!Object



686
687
688
689
690
# File 'app/models/spree/order.rb', line 686

def persist_user_address!
  if !temporary_address && user&.respond_to?(:persist_order_address) && bill_address_id
    user.persist_order_address(self)
  end
end

#quantityObject



573
574
575
# File 'app/models/spree/order.rb', line 573

def quantity
  line_items.sum(:quantity)
end

#quantity_of(variant, options = {}) ⇒ Object



349
350
351
352
# File 'app/models/spree/order.rb', line 349

def quantity_of(variant, options = {})
  line_item = find_line_item_by_variant(variant, options)
  line_item ? line_item.quantity : 0
end

#recalculatorObject Also known as: updater



281
282
283
# File 'app/models/spree/order.rb', line 281

def recalculator
  @recalculator ||= Spree::Config.order_recalculator_class.new(self)
end

#record_ip_address(ip_address) ⇒ Object



712
713
714
715
716
717
718
# File 'app/models/spree/order.rb', line 712

def record_ip_address(ip_address)
  if new_record?
    self.last_ip_address = ip_address
  elsif last_ip_address != ip_address
    update_column(:last_ip_address, ip_address)
  end
end

#refresh_shipment_ratesObject



546
547
548
# File 'app/models/spree/order.rb', line 546

def refresh_shipment_rates
  shipments.map(&:refresh_rates)
end

#refund_totalObject



397
398
399
# File 'app/models/spree/order.rb', line 397

def refund_total
  refunds.sum(&:amount)
end

#reimbursement_totalObject



378
379
380
# File 'app/models/spree/order.rb', line 378

def reimbursement_total
  reimbursements.sum(:total)
end

#restart_checkout_flowObject



536
537
538
539
540
541
542
543
544
# File 'app/models/spree/order.rb', line 536

def restart_checkout_flow
  return if state == "cart"

  update_columns(
    state: "cart",
    updated_at: Time.current
  )
  self.next if line_items.any?
end

#ship_address_attributes=(attributes) ⇒ Object



667
668
669
# File 'app/models/spree/order.rb', line 667

def ship_address_attributes=(attributes)
  self.ship_address = Spree::Address.immutable_merge(ship_address, attributes)
end

#shipment_total_before_taxObject



230
231
232
# File 'app/models/spree/order.rb', line 230

def shipment_total_before_tax
  shipments.to_a.sum(&:total_before_tax)
end

#shipped?Boolean

Returns:

  • (Boolean)


482
483
484
# File 'app/models/spree/order.rb', line 482

def shipped?
  %w[partial shipped].include?(shipment_state)
end

#shipped_shipmentsObject



341
342
343
# File 'app/models/spree/order.rb', line 341

def shipped_shipments
  shipments.shipped
end

#shippingObject



314
315
316
# File 'app/models/spree/order.rb', line 314

def shipping
  @shipping ||= Spree::Config.order_shipping_class.new(self)
end

#shipping_discountObject



243
244
245
# File 'app/models/spree/order.rb', line 243

def shipping_discount
  shipment_adjustments.credit.sum(:amount) * - 1
end

#shipping_eq_billing_address?Boolean

Returns:

  • (Boolean)


550
551
552
# File 'app/models/spree/order.rb', line 550

def shipping_eq_billing_address?
  bill_address == ship_address
end

#tax_addressObject

Returns the address for taxation based on configuration



273
274
275
276
277
278
279
# File 'app/models/spree/order.rb', line 273

def tax_address
  if Spree::Config[:tax_using_ship_address]
    ship_address
  else
    bill_address
  end || store&.default_cart_tax_location
end

#tax_totalObject



581
582
583
# File 'app/models/spree/order.rb', line 581

def tax_total
  additional_tax_total + included_tax_total
end

#to_paramObject



247
248
249
# File 'app/models/spree/order.rb', line 247

def to_param
  number
end

#total_applicable_store_creditObject



647
648
649
650
651
652
653
# File 'app/models/spree/order.rb', line 647

def total_applicable_store_credit
  if can_complete? || complete?
    valid_store_credit_payments.to_a.sum(&:amount)
  else
    [total, user.try(:available_store_credit_total, currency:) || 0.0].min
  end
end

#total_available_store_creditObject



638
639
640
641
# File 'app/models/spree/order.rb', line 638

def total_available_store_credit
  return 0.0 unless user
  user.available_store_credit_total(currency:)
end

#valid_credit_cardsObject



416
417
418
419
# File 'app/models/spree/order.rb', line 416

def valid_credit_cards
  credit_card_ids = payments.from_credit_card.valid.pluck(:source_id).uniq
  Spree::CreditCard.where(id: credit_card_ids)
end

#validate_payments_attributes(attributes) ⇒ Object



725
726
727
728
729
730
731
732
733
734
# File 'app/models/spree/order.rb', line 725

def validate_payments_attributes(attributes)
  attributes = Array(attributes)

  attributes.each do |payment_attributes|
    payment_method_id = payment_attributes[:payment_method_id]

    # raise RecordNotFound unless it is an allowed payment method
    available_payment_methods.find(payment_method_id) if payment_method_id
  end
end