Sha256: 3b0f7f56b060d2eef4528282f4bcec07177bf132d7985a5d0fb78e97db133737

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

require 'time'

module Tikkie
  module Api
    module Resources
      # Resource for a Refund.
      class Refund < Base
        STATUS_PENDING = "PENDING"
        STATUS_PAID = "PAID"

        attr_reader :payment_request_token, :payment_token

        def initialize(config, options = {})
          @payment_request_token = options.delete(:payment_request_token)
          @payment_token = options.delete(:payment_token)
          @refund_token = options.delete(:refund_token)
          super(config, options)
        end

        def refund_token
          @refund_token || body[:refundToken]
        end

        def amount
          Tikkie::Api::Amount.from_cents(body[:amountInCents]).to_d
        end

        def description
          body[:description]
        end

        def reference_id
          body[:referenceId]
        end

        def created_at
          Time.parse(body[:createdDateTime]) if body[:createdDateTime]
        end

        def status
          body[:status]
        end

        def pending?
          status == STATUS_PENDING
        end

        def paid?
          status == STATUS_PAID
        end

        private

        def load_resource
          request.get("paymentrequests/#{payment_request_token}/payments/#{payment_token}/refunds/#{refund_token}", options)
        end

        def create_resource(attributes)
          params = { description: attributes.fetch(:description) }
          amount = Tikkie::Api::Amount.new(attributes.fetch(:amount))
          params[:amountInCents] = amount.to_cents
          params[:referenceId] = attributes[:reference_id] if attributes.key?(:reference_id)

          request.post("paymentrequests/#{payment_request_token}/payments/#{payment_token}/refunds", options, params)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tikkie-api-2.0.0 lib/tikkie/api/resources/refund.rb