fadendaten/six_saferpay

View on GitHub
lib/six_saferpay/api/six_secure_pay_gate_offer/requests/create_offer.rb

Summary

Maintainability
A
25 mins
Test Coverage
module SixSaferpay
  module SixSecurePayGateOffer
    class CreateOffer

      attr_accessor(:payment,
                    :expiration_date,
                    :config_set,
                    :payer,
                    :billing_address_form,
                    :register_alias,
                   )

      def initialize(payment: ,
                     expiration_date: ,
                     config_set: nil,
                     payer: ,
                     billing_address_form: nil,
                     register_alias: nil
                    )

        @payment = SixSaferpay::Payment.new(**payment.to_h) if payment
        @expiration_date = expiration_date
        @config_set = config_set
        @payer = SixSaferpay::Payer.new(**payer.to_h) if payer
        @billing_address_form =
          SixSaferpay::BillingAddressForm.new(**billing_address_form) if billing_address_form
        @register_alias = SixSaferpay::RegisterAlias.new(**register_alias.to_h) if register_alias
      end

      def to_hash
        hash = Hash.new
        hash.merge!(payment: @payment.to_h) if @payment
        hash.merge!(expiration_date: @expiration_date) if @expiration_date
        hash.merge!(config_set: @config_set) if @config_set
        hash.merge!(payer: @payer.to_h) if @payer
        hash.merge!(billing_address_form: @billing_address_form.to_h) if @billing_address_form
        hash.merge!(register_alias: @register_alias.to_h) if @register_alias
        hash
      end
      alias_method :to_h, :to_hash

      def to_json
        to_hash.to_json
      end

      def url
        "/rest/customers/#{customer_id}/terminals/#{terminal_id}/spg-offers"
      end

      def response_class
        SixSaferpay::SixSecurePayGateOffer::CreateOfferResponse
      end

      private

      def customer_id
        SixSaferpay.config.customer_id
      end

      def terminal_id
        SixSaferpay.config.terminal_id
      end
    end
  end
end