Sha256: 0208b779c16f8ebbd84ee49960380399143d4813b0af125c761315dcb2823430

Contents?: true

Size: 1.81 KB

Versions: 13

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

require "uri"
require "net/http"

class BactracsService
  attr_accessor :token

  def initialize
    @username = SolidusBactracs.configuration.authentication_username
    @password = SolidusBactracs.configuration.authentication_password
    @runner = SolidusBactracs::Api::RequestRunner.new
    # Authenticate account
    authenticate!
  end

  def rma_was_synced?(shipment)
    if @token.present?

      response =
        @runner.call(
          method: :post,
          path: "/webservices/rma/rmaservice.asmx?op=Read",
          params:
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n
              <soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n
              <soap:Body>
                <Read xmlns=\"http://bactracs.andlor.com/rmaservice\">\n
                  <sGuid>#{@token}</sGuid>\n
                  <sRMANumber>#{shipment.number}</sRMANumber>\n
                </Read>\n
              </soap:Body>\n
            </soap:Envelope>"
        )

      if response
        return response.dig("Envelope", "Body", "ReadResponse", "ReadResult", "oRMA").present? rescue false
      end
    end

    return false

  rescue => e
    Rails.logger.error({ message: e.message.to_s, file: "bactracs_service.rb" })

    return false
  end

  private

  def authenticate!

    response =
      @runner.call(
        method: :get,
        path: "/webservices/user/Authentication.asmx/Login?sUserName=#{@username}&sPassword=#{@password}"
      )

    if response.dig("AuthenticationResponse", "Result") == "true"
      @token = response.dig("AuthenticationResponse", "Message")
    end

  rescue => e
    Rails.logger.error({ message: "#{e.message}, file: bactracs_service.rb" })
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
solidus_bactracs-4.0.1 lib/bactracs_service.rb
solidus_bactracs-4.0.0 lib/bactracs_service.rb
solidus_bactracs-3.5.2 lib/bactracs_service.rb
solidus_bactracs-3.5.1 lib/bactracs_service.rb
solidus_bactracs-3.5.0 lib/bactracs_service.rb
solidus_bactracs-3.4.0 lib/bactracs_service.rb
solidus_bactracs-3.3.2 lib/bactracs_service.rb
solidus_bactracs-3.3.1 lib/bactracs_service.rb
solidus_bactracs-3.3.0 lib/bactracs_service.rb
solidus_bactracs-3.2.2 lib/bactracs_service.rb
solidus_bactracs-3.2.1 lib/bactracs_service.rb
solidus_bactracs-3.2.0 lib/bactracs_service.rb
solidus_bactracs-3.1.2 lib/bactracs_service.rb