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



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

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 = params[:plugin_properties].values.select{ |item| !(item['value'].blank? || item['key'].blank?) } rescue @plugin_properties = nil
  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 => e
    flash.now[:error] = "Error while creating payment method: #{as_string(e)}"
    render :action => :new
  end
end

#destroyObject



68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/kaui/payment_methods_controller.rb', line 68

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 => e
    flash[:error] = "Error while deleting payment method #{payment_method_id}: #{as_string(e)}"
    redirect_to kaui_engine.(payment_method.)
  end
end

#newObject



3
4
5
6
# File 'app/controllers/kaui/payment_methods_controller.rb', line 3

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

#restful_showObject



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

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



81
82
83
# File 'app/controllers/kaui/payment_methods_controller.rb', line 81

def show
  restful_show
end

#validate_external_keyObject



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/kaui/payment_methods_controller.rb', line 90

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