class Workarea::Affirm::Gateway

Attributes

private_key[R]
public_key[R]
test[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/workarea/affirm/gateway.rb, line 6
def initialize(options = {})
  @public_key = options[:public_key]
  @private_key = options[:private_key]
  @test = options[:test]
end

Public Instance Methods

authorize(token, order_id) click to toggle source
# File lib/workarea/affirm/gateway.rb, line 20
def authorize(token, order_id)
  body = {
    checkout_token: token,
    order_id: order_id
  }
  response = connection.post do |req|
    req.url "/api/v2/charges"
    req.body = body.to_json
  end

  Affirm::Response.new(response)
end
capture(charge_id, amount, order_id) click to toggle source
# File lib/workarea/affirm/gateway.rb, line 33
def capture(charge_id, amount, order_id)
  body = {
    amount:  amount.cents,
    order_id: order_id
  }
  response = connection.post do |req|
    req.url "api/v2/charges/#{charge_id}/capture"
    req.body = body.to_json
  end

  Affirm::Response.new(response)
end
get_checkout(token) click to toggle source
# File lib/workarea/affirm/gateway.rb, line 12
def get_checkout(token)
    response = connection.get do |req|
    req.url "/api/v2/checkout/#{token}"
  end

  Affirm::Response.new(response)
end
refund(charge_id, amount) click to toggle source
# File lib/workarea/affirm/gateway.rb, line 46
def refund(charge_id, amount)
  body = {
    amount:  amount.cents,
    type: "refund"
  }

  response = connection.post do |req|
    req.url "/api/v2/charges/#{charge_id}/refund"
    req.body = body.to_json
  end

  Affirm::Response.new(response)
end
void(charge_id) click to toggle source
# File lib/workarea/affirm/gateway.rb, line 60
def void(charge_id)
  response = connection.post do |req|
    req.url "/api/v2/charges/#{charge_id}/void"
  end

  Affirm::Response.new(response)
end

Private Instance Methods

connection() click to toggle source
# File lib/workarea/affirm/gateway.rb, line 70
def connection
  headers = {
    "Content-Type" => "application/json",
    "Accept"       => "application/json",
    "Authorization" => "Basic #{encoded_credentials}",
  }

  request_timeouts = {
    timeout: Workarea.config.affirm[:api_timeout],
    open_timeout: Workarea.config.affirm[:open_timeout]
  }

  conn = Faraday.new(url: rest_endpoint, headers: headers, request: request_timeouts)
  conn.basic_auth(public_key, private_key)
  conn
end
encoded_credentials() click to toggle source
# File lib/workarea/affirm/gateway.rb, line 87
def encoded_credentials
  Base64.encode64("#{public_key}:#{private_key}") \
    .gsub(/\n/, '').strip
end
rest_endpoint() click to toggle source
# File lib/workarea/affirm/gateway.rb, line 96
def rest_endpoint
  if test?
    "https://sandbox.affirm.com"
  else
    "https://api.affirm.com"
  end
end
test?() click to toggle source
# File lib/workarea/affirm/gateway.rb, line 92
def test?
  test
end