I'm working on a Rails application that has a lot of logic in the controller that should be in the model. In the app/controllers/customers_controller.rb#create
there is logic to create stripe cc info for the customer. I think this should be in the model.
How can I move this logic into the model and still handle any exceptions in the controller?
app/controllers/customers_controller.rb
def create
...
card = {
'object' => 'card', 'number' => card_number, 'exp_month' => card_expire_month, 'exp_year' => card_expire_year,
'cvc' => card_cvc, 'address_zip' => parms['zipcode'], 'name' => "#{parms['first_name']} #{parms['last_name']}",
'address_line1' => parms['street_address_1'], 'address_state' => parms['state'],
'address_line2' => parms['street_address_2'], 'address_city' => parms['city']
}
begin
resp = Stripe::Customer.create(
description: 'Customer for DoctorNow',
metadata: { customer_id: @customer.id },
email: @customer.email,
source: card)
@customer.assign_attributes(
stripe_customer_id: resp['id'],
stripe_card_id: resp['sources']['data'][0]['id'],
stripe_card_last4: resp['sources']['data'][0]['last4'],
stripe_card_type: resp['sources']['data'][0]['brand']
)
event('card_preauth', 'customer_id', @customer.id)
rescue Stripe::CardError => e
@err = stripe_error(e, 'creating_card_card_error', 0)
render :err_show, status: :payment_required, json: @err
event('card_preauth_failed', 'customer_id', @customer.id)
return
rescue Stripe::InvalidRequestError => e
@err = stripe_error(e, 'create_card_invalid', 0)
event('card_preauth_failed', 'customer_id', @customer.id)
render :err_show, status: :payment_required, json: @err
return
rescue Stripe::AuthenticationError => e
@err = stripe_error(e, 'create_card_authentication', 0)
event('card_preauth_failed', 'customer_id', @customer.id)
render :err_show, status: :payment_required, json: @err
return
rescue Stripe::APIConnectionError => e
@err = stripe_error(e, 'create_card_connect', 0)
event('card_preauth_failed', 'customer_id', @customer.id)
render :err_show, status: :payment_required, json: @err
return
rescue Stripe::StripeError => e
@err = stripe_error(e, 'create_card_stripe', 0)
event('card_preauth_failed', 'customer_id', @customer.id)
render :err_show, status: :payment_required, json: @err
return
rescue => e
@err = stripe_error(e, 'create_card_generic', 0)
render :err_show, status: :payment_required, json: @err
event('card_preauth_failed', 'customer_id', @customer.id)
return
end
...
end
Stripe::*Error
's be thrown from? Inside@customer.assign_attributes(...)
or fromevent(...)
? \$\endgroup\$Stripe::Customer.create
\$\endgroup\$