Class: Spree::Api::V3::Store::Carts::PaymentsController

Inherits:
BaseController show all
Includes:
CartResolvable
Defined in:
app/controllers/spree/api/v3/store/carts/payments_controller.rb

Constant Summary

Constants inherited from BaseController

BaseController::RATE_LIMIT_RESPONSE

Constants included from Idempotent

Idempotent::IDEMPOTENCY_HEADER, Idempotent::IDEMPOTENCY_TTL, Idempotent::MAX_KEY_LENGTH, Idempotent::MUTATING_METHODS

Constants included from ErrorHandler

ErrorHandler::ERROR_CODES

Constants included from JwtAuthentication

JwtAuthentication::JWT_AUDIENCE_ADMIN, JwtAuthentication::JWT_AUDIENCE_STORE, JwtAuthentication::JWT_ISSUER, JwtAuthentication::USER_TYPE_ADMIN, JwtAuthentication::USER_TYPE_CUSTOMER

Instance Method Summary collapse

Methods included from ApiKeyAuthentication

#authenticate_api_key!, #authenticate_secret_key!

Methods included from JwtAuthentication

#authenticate_user, #require_authentication!

Instance Method Details

#createObject

POST /api/v3/store/carts/:cart_id/payments Creates a payment for non-session payment methods (e.g. Check, Cash on Delivery, Bank Transfer)



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
# File 'app/controllers/spree/api/v3/store/carts/payments_controller.rb', line 13

def create
  payment_method = current_store.payment_methods.find_by_prefix_id!(params[:payment_method_id])

  if payment_method.session_required?
    return render_error(
      code: 'payment_session_required',
      message: Spree.t('api.v3.payments.session_required'),
      status: :unprocessable_content
    )
  end

  unless payment_method.available_for_order?(@cart)
    return render_error(
      code: 'payment_method_unavailable',
      message: Spree.t('api.v3.payments.method_unavailable'),
      status: :unprocessable_content
    )
  end

  amount = params[:amount].presence || @cart.total_minus_store_credits

  @payment = @cart.payments.build(
    payment_method: payment_method,
    amount: amount,
    metadata: params[:metadata].present? ? params[:metadata].to_unsafe_h : {}
  )

  if @payment.save
    render json: Spree.api.payment_serializer.new(@payment, params: serializer_params).to_h, status: :created
  else
    render_errors(@payment.errors)
  end
end