PageRenderTime 1650ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/api/app/controllers/spree/api/payments_controller.rb

https://github.com/tomz/spree
Ruby | 94 lines | 77 code | 17 blank | 0 comment | 3 complexity | 493992dba7a26c2e4fc46d75866482af MD5 | raw file
Possible License(s): BSD-3-Clause
  1. module Spree
  2. module Api
  3. class PaymentsController < Spree::Api::BaseController
  4. before_filter :find_order
  5. before_filter :find_payment, only: [:update, :show, :authorize, :purchase, :capture, :void, :credit]
  6. def index
  7. @payments = @order.payments.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
  8. respond_with(@payments)
  9. end
  10. def new
  11. @payment_methods = Spree::PaymentMethod.available
  12. respond_with(@payment_method)
  13. end
  14. def create
  15. @payment = @order.payments.build(payment_params)
  16. if @payment.save
  17. respond_with(@payment, status: 201, default_template: :show)
  18. else
  19. invalid_resource!(@payment)
  20. end
  21. end
  22. def update
  23. authorize! params[:action], @payment
  24. if ! @payment.pending?
  25. render 'update_forbidden', status: 403
  26. elsif @payment.update_attributes(payment_params)
  27. respond_with(@payment, default_template: :show)
  28. else
  29. invalid_resource!(@payment)
  30. end
  31. end
  32. def show
  33. respond_with(@payment)
  34. end
  35. def authorize
  36. perform_payment_action(:authorize)
  37. end
  38. def capture
  39. perform_payment_action(:capture)
  40. end
  41. def purchase
  42. perform_payment_action(:purchase)
  43. end
  44. def void
  45. perform_payment_action(:void_transaction)
  46. end
  47. def credit
  48. if params[:amount].to_f > @payment.credit_allowed
  49. render 'credit_over_limit', status: 422
  50. else
  51. perform_payment_action(:credit, params[:amount])
  52. end
  53. end
  54. private
  55. def find_order
  56. @order = Spree::Order.find_by(number: order_id)
  57. authorize! :read, @order
  58. end
  59. def find_payment
  60. @payment = @order.payments.find(params[:id])
  61. end
  62. def perform_payment_action(action, *args)
  63. authorize! action, Payment
  64. begin
  65. @payment.send("#{action}!", *args)
  66. respond_with(@payment, :default_template => :show)
  67. rescue Spree::Core::GatewayError => e
  68. @error = e.message
  69. render 'spree/api/errors/gateway_error', status: 422
  70. end
  71. end
  72. def payment_params
  73. params.require(:payment).permit(permitted_payment_attributes)
  74. end
  75. end
  76. end
  77. end