Sha256: d808a943bd870401e6e7fd8ebbf13d48c93d101f3caf92e33ae32c144229f315

Contents?: true

Size: 1.07 KB

Versions: 9

Compression:

Stored size: 1.07 KB

Contents

module Affirm
  module Charge
    extend self

    def authorize(token)
      respond Client.request(:post, "charges", checkout_token: token)
    end

    def find(charge_id)
      respond Client.request(:get, "charges/#{charge_id}")
    end

    def capture(charge_id)
      respond Client.request(:post, "charges/#{charge_id}/capture")
    end

    def void(charge_id)
      respond Client.request(:post, "charges/#{charge_id}/void")
    end

    def refund(charge_id, amount:)
      respond Client.request(:post, "charges/#{charge_id}/refund", amount: amount)
    end

    def update(charge_id, **updates)
      respond Client.request(:post, "charges/#{charge_id}/update", updates)
    end

    private

    def respond(response)
      return FailureResult.new(response) unless response.success?

      type  = response.body["type"]
      klass = case type
      when *%w(capture void refund update)
        Object.const_get("Affirm::Responses::#{type.capitalize}")
      else
        Affirm::Responses::Auth
      end

      SuccessResult.new(klass.new(response.body))
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
affirm-ruby-1.2.0 lib/affirm/charge.rb
affirm-ruby-1.1.3 lib/affirm/charge.rb
affirm-ruby-1.1.2 lib/affirm/charge.rb
affirm-ruby-1.1.1 lib/affirm/charge.rb
affirm-ruby-1.1.0 lib/affirm/charge.rb
affirm-ruby-1.0.2 lib/affirm/charge.rb
affirm-ruby-1.0.1 lib/affirm/charge.rb
affirm-ruby-1.0.0 lib/affirm/charge.rb
affirm-ruby-0.0.1 lib/affirm/charge.rb