Sha256: 7f5fdbeeb4eb4bed3d5162cb3671c55a8a7d1c01cb90de637a389e2093d938d3

Contents?: true

Size: 1.17 KB

Versions: 8

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

module SolidusPaypalCommercePlatform
  class PaymentsController < ::Spree::Api::BaseController
    before_action :load_order
    skip_before_action :authenticate_user

    def create
      authorize! :update, @order, order_token
      paypal_order_id = paypal_params[:paypal_order_id]

      if !paypal_order_id
        return redirect_to checkout_state_path(@order.state), notice: "Invalid order confirmation data passed in"
      end

      if @order.complete?
        return redirect_to spree.order_path(@order), notice: "Order is already in complete state"
      end

      source = SolidusPaypalCommercePlatform::PaymentSource.new(paypal_order_id: paypal_order_id)

      source.transaction do
        if source.save!
          @order.payments.create!(
            payment_method_id: paypal_params[:payment_method_id],
            source: source
          )

          render json: {}, status: :ok
        end
      end
    end

    private

    def paypal_params
      params.permit(:paypal_order_id, :order_id, :order_token, :payment_method_id)
    end

    def load_order
      @order = ::Spree::Order.find_by!(number: params[:order_id])
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
solidus_paypal_commerce_platform-0.3.2 app/controllers/solidus_paypal_commerce_platform/payments_controller.rb
solidus_paypal_commerce_platform-0.3.1 app/controllers/solidus_paypal_commerce_platform/payments_controller.rb
solidus_paypal_commerce_platform-0.3.0 app/controllers/solidus_paypal_commerce_platform/payments_controller.rb
solidus_paypal_commerce_platform-0.2.2 app/controllers/solidus_paypal_commerce_platform/payments_controller.rb
solidus_paypal_commerce_platform-0.2.1 app/controllers/solidus_paypal_commerce_platform/payments_controller.rb
solidus_paypal_commerce_platform-0.2.0 app/controllers/solidus_paypal_commerce_platform/payments_controller.rb
solidus_paypal_commerce_platform-0.1.0 app/controllers/solidus_paypal_commerce_platform/payments_controller.rb
solidus_paypal_commerce_platform-0.0.1 app/controllers/solidus_paypal_commerce_platform/payments_controller.rb