Sha256: bf47f664d366257cdcd5c19e4029440f03efdf2e5a514994f6649aa68c73a6b9

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module SolidusAfterpay
  class ExpressCallbacksController < SolidusAfterpay::BaseController
    def create
      authorize! :update, order, order_token

      unless SolidusAfterpay.update_order_attributes_service_class.call(
        order: order,
        afterpay_order_token: params[:token],
        payment_method: payment_method,
        request_env: request.headers.env
      )
        render(
          json: {
            error: I18n.t('solidus_afterpay.express_checkout.errors.unable_place_order'),
            errorCode: :internal_server_error
          },
          status: :internal_server_error
        )
        return
      end

      order.next!
      order.next!

      render json: { redirect_url: checkout_state_url(order.state) }, status: :ok
    end

    def update
      authorize! :update, order, order_token

      unless SolidusAfterpay::UpdateOrderAddressesService.call(order: order, address_params: params[:address])
        render json: { errorCode: :internal_server_error }, status: :internal_server_error
        return
      end

      order.next!
      # rubocop:disable Rails/SkipsModelValidations
      order.update_columns(email: nil) if order.email == SolidusAfterpay.configuration.dummy_email
      # rubocop:enable Rails/SkipsModelValidations

      render(
        json: {
          data: ::SolidusAfterpay.shipping_rate_builder_service_class.call(order: order)
        },
        status: :ok
      )
    end

    private

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

    def payment_method
      @payment_method ||= SolidusAfterpay::PaymentMethod.active.find(params[:payment_method_id])
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
solidus_afterpay-0.2.0 app/controllers/solidus_afterpay/express_callbacks_controller.rb