PageRenderTime 1348ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/adamlwalker/spree
Ruby | 82 lines | 67 code | 15 blank | 0 comment | 2 complexity | b95f26afa9a94941b0fe6b35e22a176b MD5 | raw file
  1. module Spree
  2. module Api
  3. module V1
  4. class PaymentsController < Spree::Api::BaseController
  5. before_action :find_order
  6. before_action :find_payment, only: [:update, :show, :authorize, :purchase, :capture, :void]
  7. def index
  8. @payments = @order.payments.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
  9. respond_with(@payments)
  10. end
  11. def new
  12. @payment_methods = Spree::PaymentMethod.available
  13. respond_with(@payment_methods)
  14. end
  15. def create
  16. @payment = @order.payments.build(payment_params)
  17. if @payment.save
  18. respond_with(@payment, status: 201, default_template: :show)
  19. else
  20. invalid_resource!(@payment)
  21. end
  22. end
  23. def update
  24. authorize! params[:action], @payment
  25. if !@payment.editable?
  26. render 'update_forbidden', status: 403
  27. elsif @payment.update_attributes(payment_params)
  28. respond_with(@payment, default_template: :show)
  29. else
  30. invalid_resource!(@payment)
  31. end
  32. end
  33. def show
  34. respond_with(@payment)
  35. end
  36. def authorize
  37. perform_payment_action(:authorize)
  38. end
  39. def capture
  40. perform_payment_action(:capture)
  41. end
  42. def purchase
  43. perform_payment_action(:purchase)
  44. end
  45. def void
  46. perform_payment_action(:void_transaction)
  47. end
  48. private
  49. def find_order
  50. @order = Spree::Order.friendly.find(order_id)
  51. authorize! :read, @order, order_token
  52. end
  53. def find_payment
  54. @payment = @order.payments.friendly.find(params[:id])
  55. end
  56. def perform_payment_action(action, *args)
  57. authorize! action, Payment
  58. @payment.send("#{action}!", *args)
  59. respond_with(@payment, default_template: :show)
  60. end
  61. def payment_params
  62. params.require(:payment).permit(permitted_payment_attributes)
  63. end
  64. end
  65. end
  66. end
  67. end