7
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
|
# File 'app/controllers/spree/cielo_debt_controller.rb', line 7
def create
if current_order.guest_token != params[:guest_token]
render status: 500, json: {error: Spree.t('errors.invalid_guest_token')} and return
end
if Spree::CieloConfig.debt_cards.include?(params[:source][:cc_type])
credit_card = Spree::CreditCard.new(credit_card_params)
if credit_card.save
payment_method = Spree::PaymentMethod.find params[:payment_method_id]
amount = current_order.display_total.money.cents
response = payment_method.create amount, credit_card
if response.has_key? :url_auth
credit_card.update_attributes(gateway_payment_profile_id: response[:tid])
render json: response
else
credit_card.destroy
render status: 500, json: response
end
else
errors = credit_card.errors.full_messages.join('<br/>')
render status: 500, json: {error: errors}
end
else
supported_cards = Spree::CieloConfig.debt_cards.collect { |i| "<li>#{Spree.t("cielo_#{i}")}</li>" }
message = "<label style='font-weight: normal;'>#{Spree.t('errors.cielo_card_fail')}<ul>#{supported_cards.join('')}</ul></label>"
render status: 500, inline: message.html_safe
end
end
|