Module: PaymentsHelper

Defined in:
app/helpers/payments_helper.rb

Overview

View helper: payments.

Instance Method Summary collapse

Instance Method Details

#payment_command_options(payment) ⇒ Object



41
42
43
# File 'app/helpers/payments_helper.rb', line 41

def payment_command_options(payment)
  payment_dropdown_actions(payment).reject { |a| a == :divider }
end

#payment_dropdown_actions(payment) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/helpers/payments_helper.rb', line 16

def payment_dropdown_actions(payment)
  [].tap do |opts|
    if [Payment::PAYPAL, Payment::CREDIT_CARD].include?(payment.category) && payment.total_captured < payment.amount
      opts << link_to(fa_icon('hand-holding-dollar', text: 'Capture Funds'), capture_funds_payment_path(payment), class: 'dropdown-item') if payment.authorized? || (payment.captured? && payment.supports_multicapture?)
    end
    opts << link_to(fa_icon('ban', text: 'Void'), void_order_payment_path(@order, payment), class: 'dropdown-item') if payment.can_be_voided?
    opts << link_to(fa_icon('check', text: 'Approve'), approve_payment_path(payment), class: 'dropdown-item') if payment.pending_release_authorization? && user_has_role?('accounting_rep')
    opts << :divider if opts.any?
    opts << link_to("Send Auth Receipt", send_receipt_order_payment_path(@order, payment), class: 'dropdown-item') if payment.category == Payment::CREDIT_CARD && (payment.authorized? || payment.captured?)
    opts << link_to("Capture Receipt", email_receipt_path(payment.receipts.last), class: 'dropdown-item') if payment.receipts.any?
    if payment.category == Payment::PAYPAL_INVOICE && @order.pending_release_authorization? && payment.authorized?
      opts << link_to("Check Invoice Status", check_invoice_status_order_payment_path(@order, payment), class: 'dropdown-item')
      opts << link_to("Resend Invoice", resend_paypal_invoice_order_payment_path(@order, payment), class: 'dropdown-item')
    end
    if payment.category == Payment::PAYPAL && payment.authorized?
      opts << link_to("Re-authorize", reauthorize_paypal_payment_path(payment), class: 'dropdown-item',
data: { turbo_confirm: 'If the transaction cannot be reauthorized, the current authorization will be voided. Are you sure you want to continue?' })
    end
    opts << link_to("View PO", upload_path(payment.po_upload), class: 'dropdown-item') if payment.category == Payment::PO && payment.po_upload.present?
    opts << link_to("Fraud Report", fraud_report_payment_path(payment), class: 'dropdown-item') if payment.fraud_report.present?
    opts << :divider
    opts << link_to("Edit", edit_order_payment_path(@order, payment), class: 'dropdown-item')
  end
end

#payment_state_color(payment) ⇒ String

Bootstrap contextual color for a payment state badge, kept in one place so
the hero badge and the multicapture sibling rows stay consistent.

Parameters:

Returns:

  • (String)


62
63
64
65
66
67
68
69
70
71
72
# File 'app/helpers/payments_helper.rb', line 62

def payment_state_color(payment)
  case payment.state
  when 'authorized'         then 'info'
  when 'captured'           then 'success'
  when 'declined'           then 'danger'
  when 'voided', 'expired'  then 'secondary'
  when 'partially_refunded', 'pending' then 'warning'
  when 'fully_refunded' then 'dark'
  else 'secondary'
  end
end

#payment_state_label(payment) ⇒ String

Badge label for a payment's lifecycle state, distinguishing a split
hand-off ("Superseded by split") from a real void so a soft-voided split
original doesn't read as a cancelled/released charge.

Parameters:

Returns:

  • (String)


51
52
53
54
55
# File 'app/helpers/payments_helper.rb', line 51

def payment_state_label(payment)
  return 'Superseded by split' if payment.superseded_by_split?

  payment.human_state_name.titleize
end

#payment_tab_options(payment, payment_options) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/helpers/payments_helper.rb', line 74

def payment_tab_options(payment, payment_options)
  hsh = {}

  if @incrementable_auths.present?
    hsh[:increment_authorization] = {
      title: 'Increment Authorization',
      partial: 'crm/payments/gateways/increment_authorization',
      locals: { incrementable_auths: @incrementable_auths, order: @order }
    }
  end

  if @customer&.paypal_vault_token_id.present?
    hsh[:'paypal-vault'] = {
      title: 'PayPal Vault',
      partial: 'crm/payments/gateways/paypal_vault',
      locals: { customer: @customer, order: @order }
    }
  end

  payment_options.each do |payment_option|
    hsh[payment_option.parameterize.to_sym] = {
                                   partial: 'payment_option',
                                   locals: {
                                     payment: payment,
                                     payment_option: payment_option,
                                     payment_options: payment_options,
                                     order: @order
                                    }
                                  }
  end
  hsh.merge!(payment_link: { partial: 'crm/payments/gateways/payment_link' })

  if current_user&.has_role?(%w[admin accounting_manager]) && @customer&.credit_card_vaults&.where(hidden: true)&.exists?
    hsh[:'saved-vaults'] = {
      title: 'Saved Vaults',
      partial: 'crm/payments/gateways/saved_vaults',
      locals: { customer: @customer, order: @order },
      tab_class: 'bg-warning text-dark'
    }
  end

  reorder_tabs_by_preference(hsh)
end

#preferred_tab_key(category, available_tabs) ⇒ Object



131
132
133
134
135
136
137
138
139
140
# File 'app/helpers/payments_helper.rb', line 131

def preferred_tab_key(category, available_tabs)
  case category
  when Payment::CREDIT_CARD then :'credit-card'
  when Payment::PAYPAL      then available_tabs.key?(:'paypal-vault') ? :'paypal-vault' : :'paypal-invoice'
  when Payment::PO          then :'purchase-order'
  when Payment::VPO         then :'verbal-purchase-order'
  when Payment::CHECK       then :check
  when Payment::WIRE        then :'wire-transfer'
  end
end

#reorder_tabs_by_preference(hsh) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/helpers/payments_helper.rb', line 118

def reorder_tabs_by_preference(hsh)
  return hsh if @payment_preference&.category.blank?

  preferred_key = preferred_tab_key(@payment_preference.category, hsh)
  return hsh unless preferred_key && hsh.key?(preferred_key)

  pinned = {}
  pinned[:increment_authorization] = hsh.delete(:increment_authorization) if hsh.key?(:increment_authorization)
  preferred_entry = hsh.delete(preferred_key)

  { **pinned, preferred_key => preferred_entry, **hsh }
end

#setup_for_payment(payment, category, order) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'app/helpers/payments_helper.rb', line 5

def setup_for_payment(payment, category, order)
  payment.tap do |pp|
    if category == Payment::ADV_REPL
      pp.amount = order.rma.credit_available if order.balance > order.rma.credit_available
    elsif category == Payment::RMA_CREDIT
      pp.amount = order.precreated_rma_credit_available if order.balance > order.precreated_rma_credit_available
    end
  end
  payment
end

#user_has_role?(role) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
# File 'app/helpers/payments_helper.rb', line 142

def user_has_role?(role)
  &.has_role?(role)&.to_b
end

#user_is_manager?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'app/helpers/payments_helper.rb', line 146

def user_is_manager?
  &.is_manager?&.to_b
end