Class: Kaui::PaymentMethodsController

Inherits:
EngineController show all
Defined in:
app/controllers/kaui/payment_methods_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

#createObject



10
11
12
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
# File 'app/controllers/kaui/payment_methods_controller.rb', line 10

def create
  @payment_method             = Kaui::PaymentMethod.new(params[:payment_method].delete_if { |_key, value| value.blank? })
  # Transform "1" into boolean
  @payment_method.is_default  = @payment_method.is_default == '1'
  # Sensible default
  @payment_method.plugin_name ||= Kaui.creditcard_plugin_name.call

  # Instance variables needed in case of failure
  @card_type                  = params[:card_type]
  @card_holder_name           = params[:card_holder_name]
  @expiration_year            = params[:expiration_year]
  @expiration_month           = params[:expiration_month]
  @credit_card_number         = params[:credit_card_number]
  @verification_value         = params[:verification_value]
  @address1                   = params[:address1]
  @address2                   = params[:address2]
  @city                       = params[:city]
  @postal_code                = params[:postal_code]
  @state                      = params[:state]
  @country                    = params[:country]

  # Magic from lib/killbill/helpers/active_merchant/payment_plugin.rb
  @payment_method.plugin_info = {
    'type' => 'CreditCard',
    'ccType' => @card_type,
    'ccFirstName' => @card_holder_name,
    'ccLastName' => @card_holder_name,
    'ccExpirationMonth' => @expiration_month,
    'ccExpirationYear' => @expiration_year,
    'ccNumber' => @credit_card_number,
    'ccVerificationValue' => @verification_value,
    'address1' => @address1,
    'address2' => @address2,
    'city' => @city,
    'country' => @country,
    'zip' => @postal_code,
    'state' => @state
  }

  @plugin_properties = begin
    params[:plugin_properties].values.reject { |item| (item['value'].blank? || item['key'].blank?) }
  rescue StandardError
    @plugin_properties = nil
  end
  if @plugin_properties.blank?
    # In case of error, we want the view to receive nil, not [], so that at least the first row is populated (required to make the JS work)
    # See https://github.com/killbill/killbill-admin-ui/issues/258
    @plugin_properties = nil
  else
    @plugin_properties.map! do |property|
      KillBillClient::Model::PluginPropertyAttributes.new(property)
    end
  end

  begin
    @payment_method = @payment_method.create(@payment_method.is_default, current_user.kb_username, params[:reason], params[:comment],
                                             @plugin_properties.blank? ? options_for_klient : { pluginProperty: @plugin_properties }.merge(options_for_klient))
    redirect_to kaui_engine.(@payment_method.), notice: 'Payment method was successfully created'
  rescue StandardError => e
    flash.now[:error] = "Error while creating payment method: #{as_string(e)}"
    render action: :new
  end
end

#destroyObject



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

def destroy
  payment_method_id = params[:id]

  payment_method = Kaui::PaymentMethod.find_by_id(payment_method_id, false, options_for_klient)
  begin
    Kaui::PaymentMethod.destroy(payment_method_id, params[:set_auto_pay_off], false, current_user.kb_username, params[:reason], params[:comment], options_for_klient)
    redirect_to kaui_engine.(payment_method.), notice: "Payment method #{payment_method_id} successfully deleted"
  rescue StandardError => e
    flash[:error] = "Error while deleting payment method #{payment_method_id}: #{as_string(e)}"
    redirect_to kaui_engine.(payment_method.)
  end
end

#newObject



5
6
7
8
# File 'app/controllers/kaui/payment_methods_controller.rb', line 5

def new
  @payment_method = Kaui::PaymentMethod.new(account_id: params[:account_id],
                                            plugin_name: params[:plugin_name] || Kaui.creditcard_plugin_name.call)
end

#refreshObject



110
111
112
113
# File 'app/controllers/kaui/payment_methods_controller.rb', line 110

def refresh
  Kaui::PaymentMethod.refresh(params.require(:account_id), current_user.kb_username, params[:reason], params[:comment], options_for_klient)
  redirect_to kaui_engine.(params.require(:account_id)), notice: 'Payment methods successfully refreshed'
end

#restful_showObject



91
92
93
94
# File 'app/controllers/kaui/payment_methods_controller.rb', line 91

def restful_show
  payment_method = Kaui::PaymentMethod.find_by_id(params.require(:id), false, options_for_klient)
  redirect_to kaui_engine.(payment_method.)
end

#showObject



87
88
89
# File 'app/controllers/kaui/payment_methods_controller.rb', line 87

def show
  restful_show
end

#validate_external_keyObject



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

def validate_external_key
  json_response do
    external_key = params.require(:external_key)

    begin
      payment_methods = Kaui::PaymentMethod.find_by_external_key(external_key, false, false, 'NONE', options_for_klient)
    rescue KillBillClient::API::NotFound
      payment_methods = nil
    end

    { is_found: !payment_methods.nil? }
  end
end