Sha256: 3dc516978b05486113ebc5da37e252788c1d0c7ba4e359136bfce0625fe55a67

Contents?: true

Size: 1.9 KB

Versions: 10

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

module SolidusBolt
  module Transactions
    class AuthorizeService < SolidusBolt::Transactions::BaseService
      attr_reader :order, :create_bolt_account, :credit_card, :auto_capture, :repeat

      def initialize(order:, create_bolt_account:, credit_card:, payment_method:, auto_capture: false, repeat: false)
        @order = order
        @create_bolt_account = create_bolt_account
        @credit_card = credit_card
        @auto_capture = auto_capture
        @repeat = repeat
        super
      end

      def call
        authorize
      end

      private

      def authorize
        url = "#{api_base_url}/#{api_version}/merchant/transactions/authorize"
        handle_result(
          HTTParty.post(
            url, headers: headers, body: (repeat ? repeat_authorization_body : new_authorization_body).to_json
          )
        )
      end

      def new_authorization_body
        body.merge(
          credit_card: credit_card,
          division_id: '', # used to be defined, now needs to stay empty for call to work
          shipping_address: shipping_address,
          create_bolt_account: create_bolt_account
        )
      end

      def repeat_authorization_body
        body.merge(
          credit_card_id: credit_card[:id]
        )
      end

      def body
        {
          auto_capture: auto_capture,
          cart: order.bolt_cart,
          source: 'direct_payments',
          user_identifier: order.bolt_user_identifier,
          user_identity: order.bolt_user_identity
        }
      end

      def shipping_address
        order.shipping_address.yield_self do |address|
          address.bolt_address(order.email)
        end
      end

      def headers
        {
          'Content-Type' => 'application/json',
          'X-Nonce' => generate_nonce,
          'X-Publishable-Key' => publishable_key
        }.merge(authentication_header)
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
solidus_bolt-0.7.2 app/services/solidus_bolt/transactions/authorize_service.rb
solidus_bolt-0.7.1 app/services/solidus_bolt/transactions/authorize_service.rb
solidus_bolt-0.7.0 app/services/solidus_bolt/transactions/authorize_service.rb
solidus_bolt-0.6.0 app/services/solidus_bolt/transactions/authorize_service.rb
solidus_bolt-0.5.0 app/services/solidus_bolt/transactions/authorize_service.rb
solidus_bolt-0.4.0 app/services/solidus_bolt/transactions/authorize_service.rb
solidus_bolt-0.3.0 app/services/solidus_bolt/transactions/authorize_service.rb
solidus_bolt-0.2.0 app/services/solidus_bolt/transactions/authorize_service.rb
solidus_bolt-0.1.0 app/services/solidus_bolt/transactions/authorize_service.rb
solidus_bolt-0.0.1 app/services/solidus_bolt/transactions/authorize_service.rb