Class: Kaui::PaymentsController

Inherits:
EngineController show all
Defined in:
app/controllers/kaui/payments_controller.rb

Constant Summary

Constants included from EngineControllerUtil

EngineControllerUtil::SIMPLE_PAGINATION_THRESHOLD

Instance Method Summary collapse

Methods inherited from EngineController

#check_for_redirect_to_tenant_screen, #current_ability, #current_user, #options_for_klient, #populate_account_details, #retrieve_allowed_users_for_current_user, #retrieve_tenants_for_current_user

Instance Method Details

#cancel_scheduled_paymentObject


120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/controllers/kaui/payments_controller.rb', line 120

def cancel_scheduled_payment
  begin
    payment_transaction = Kaui::Transaction.new
    payment_transaction.transaction_external_key = params.require(:transaction_external_key)

    payment_transaction.cancel_scheduled_payment(current_user.kb_username, params[:reason], params[:comment], options_for_klient)

    redirect_to kaui_engine.(params.require(:account_id), params.require(:id)), :notice => "Payment attempt retry successfully deleted"
  rescue => e
    flash[:error] = "Error deleting payment attempt retry: #{as_string(e)}"
    redirect_to kaui_engine.(params.require(:account_id))
  end
end

#createObject


86
87
88
89
90
91
92
# File 'app/controllers/kaui/payments_controller.rb', line 86

def create
  payment = Kaui::InvoicePayment.new(invoice_payment_params)
  external_payment = params[:external] == '1'
  payment.payment_method_id = nil if external_payment || payment.payment_method_id.blank?
  payment = payment.create(external_payment, current_user.kb_username, params[:reason], params[:comment], options_for_klient)
  redirect_to kaui_engine.(payment., payment.target_invoice_id), :notice => 'Payment created'
end

#indexObject


3
4
5
6
7
8
9
10
11
# File 'app/controllers/kaui/payments_controller.rb', line 3

def index
  @search_query = params[:q] || params[:account_id]

  @ordering = params[:ordering] || (@search_query.blank? ? 'desc' : 'asc')
  @offset = params[:offset] || 0
  @limit = params[:limit] || 50

  @max_nb_records = @search_query.blank? ? Kaui::Payment.list_or_search(nil, 0, 0, options_for_klient).pagination_max_nb_records : 0
end

#newObject


75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/kaui/payments_controller.rb', line 75

def new
  cached_options_for_klient = options_for_klient
  fetch_invoice = promise { Kaui::Invoice.find_by_id(params.require(:invoice_id), 'NONE', cached_options_for_klient) }
  fetch_payment_methods = promise { Kaui::PaymentMethod.(params.require(:account_id), false, cached_options_for_klient) }

  @invoice = wait(fetch_invoice)
  @payment_methods = wait(fetch_payment_methods)

  @payment = Kaui::InvoicePayment.new('accountId' => @account., 'targetInvoiceId' => @invoice.invoice_id, 'purchasedAmount' => @invoice.balance)
end

#paginationObject


13
14
15
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/kaui/payments_controller.rb', line 13

def pagination
  searcher = lambda do |search_key, offset, limit|
    if Kaui::Payment::TRANSACTION_STATUSES.include?(search_key)
      # Search is done by payment state on the server side, see http://docs.killbill.io/latest/userguide_payment.html#_payment_states
      payment_state = if %w(PLUGIN_FAILURE UNKNOWN).include?(search_key)
                        '_ERRORED'
                      elsif search_key == 'PAYMENT_FAILURE'
                        '_FAILED'
                      else
                        '_' + search_key
                      end
      payments = Kaui::Payment.list_or_search(payment_state, offset, limit, options_for_klient)
    else
       = Kaui::Account::find_by_id_or_key(search_key, false, false, options_for_klient) rescue nil
      if .nil?
        payments = Kaui::Payment.list_or_search(search_key, offset, limit, options_for_klient)
      else
        payments = .payments(options_for_klient).map! { |payment| Kaui::Payment.build_from_raw_payment(payment) }
      end
    end

    payments.each do |payment|
      created_date = nil
      payment.transactions.each do |transaction|
        transaction_date = Date.parse(transaction.effective_date)
        if created_date.nil? or transaction_date < created_date
          created_date = transaction_date
        end
      end
      payment.payment_date = created_date
    end

    payments
  end

  data_extractor = lambda do |payment, column|
    [
        payment.payment_number.to_i,
        payment.payment_date,
        payment.total_authed_amount_to_money,
        payment.paid_amount_to_money,
        payment.returned_amount_to_money,
        payment.transactions.empty? ? nil : payment.transactions[-1].status,
        payment.payment_external_key
    ][column]
  end

  formatter = lambda do |payment|
    [
        view_context.link_to(payment.payment_number, view_context.url_for(:controller => :payments, :action => :show, :account_id => payment., :id => payment.payment_id)),
        view_context.format_date(payment.payment_date, @account.time_zone),
        view_context.humanized_money_with_symbol(payment.total_authed_amount_to_money),
        view_context.humanized_money_with_symbol(payment.paid_amount_to_money),
        view_context.humanized_money_with_symbol(payment.returned_amount_to_money),
        payment.transactions.empty? ? nil : view_context.colored_transaction_status(payment.transactions[-1].status),
        payment.payment_external_key
    ]
  end

  paginate searcher, data_extractor, formatter
end

#restful_showObject


111
112
113
114
115
116
117
118
# File 'app/controllers/kaui/payments_controller.rb', line 111

def restful_show
  begin
    payment = Kaui::InvoicePayment.find_by_id(params.require(:id), false, true, options_for_klient)
  rescue KillBillClient::API::NotFound
    payment = Kaui::Payment.find_by_external_key(params.require(:id), false, true, options_for_klient)
  end
  redirect_to (payment., payment.payment_id)
end

#showObject


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/kaui/payments_controller.rb', line 94

def show
  cached_options_for_klient = options_for_klient

  invoice_payment = Kaui::InvoicePayment.find_safely_by_id(params.require(:id), cached_options_for_klient)
  @payment = Kaui::InvoicePayment.build_from_raw_payment(invoice_payment)

  fetch_payment_fields = promise {
    direct_payment = Kaui::Payment.new(:payment_id => @payment.payment_id)
    direct_payment.custom_fields('NONE', cached_options_for_klient).sort { |cf_a, cf_b| cf_a.name.downcase <=> cf_b.name.downcase }
  }
  # The payment method may have been deleted
  fetch_payment_method = promise { Kaui::PaymentMethod.find_safely_by_id(@payment.payment_method_id, cached_options_for_klient) }

  @custom_fields = wait(fetch_payment_fields)
  @payment_method = wait(fetch_payment_method)
end