Sha256: 4351ce82af1681b368699c440d3d6dfdaf648a00c0c67b3abefc09dd6b4204e2

Contents?: true

Size: 1.08 KB

Versions: 12

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module DefraRubyMocks
  class WorldpayPaymentService < BaseService
    def run(merchant_code:, xml:)
      check_config

      @merchant_code = merchant_code
      @order_code = extract_order_code(xml)

      {
        merchant_code: @merchant_code,
        order_code: @order_code,
        id: generate_id,
        url: generate_url
      }
    end

    private

    def check_config
      domain = DefraRubyMocks.configuration.worldpay_domain

      raise InvalidConfigError, :worldpay_domain if domain.blank?
    end

    def extract_order_code(xml)
      order = xml.at_xpath("//order")
      order.attribute("orderCode").text
    end

    def generate_id
      # Worldpay seems to generate 10 digit numbers for all its ID's. So we
      # replicate that here with this.
      # https://stackoverflow.com/a/31043825
      rand(1e9...1e10).to_i.to_s
    end

    def generate_url
      "#{base_url}?OrderKey=#{@merchant_code}%5E#{@order_code}"
    end

    def base_url
      File.join(DefraRubyMocks.configuration.worldpay_domain, "/worldpay/dispatcher")
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
defra_ruby_mocks-2.3.3 app/services/defra_ruby_mocks/worldpay_payment_service.rb
defra_ruby_mocks-2.3.2 app/services/defra_ruby_mocks/worldpay_payment_service.rb
defra_ruby_mocks-2.3.1 app/services/defra_ruby_mocks/worldpay_payment_service.rb
defra_ruby_mocks-2.3.0 app/services/defra_ruby_mocks/worldpay_payment_service.rb
defra_ruby_mocks-2.2.0 app/services/defra_ruby_mocks/worldpay_payment_service.rb
defra_ruby_mocks-2.1.0 app/services/defra_ruby_mocks/worldpay_payment_service.rb
defra_ruby_mocks-2.0.0 app/services/defra_ruby_mocks/worldpay_payment_service.rb
defra_ruby_mocks-1.5.0 app/services/defra_ruby_mocks/worldpay_payment_service.rb
defra_ruby_mocks-1.4.1 app/services/defra_ruby_mocks/worldpay_payment_service.rb
defra_ruby_mocks-1.4.0 app/services/defra_ruby_mocks/worldpay_payment_service.rb
defra_ruby_mocks-1.3.0 app/services/defra_ruby_mocks/worldpay_payment_service.rb
defra_ruby_mocks-1.2.0 app/services/defra_ruby_mocks/worldpay_payment_service.rb