class Workarea::Afterpay::Gateway

Attributes

merchant_id[R]
options[R]
secret_key[R]

Public Class Methods

new(merchant_id, secret_key, options = {}) click to toggle source
# File lib/workarea/afterpay/gateway.rb, line 8
def initialize(merchant_id, secret_key, options = {})
  @merchant_id = merchant_id
  @secret_key = secret_key
  @options = options
end

Public Instance Methods

authorize(token, order_id, request_id) click to toggle source
# File lib/workarea/afterpay/gateway.rb, line 37
def authorize(token, order_id, request_id)
  body = {
    token: token,
    request_id: request_id
  }
  response = connection.post do |req|
    req.url "/v2/payments/auth"
    req.body = body.to_json
  end

  Afterpay::Response.new(response)
end
capture(payment_id, amount, request_id) click to toggle source
# File lib/workarea/afterpay/gateway.rb, line 50
def capture(payment_id, amount, request_id)
  body = {
    amount: {
      amount: amount.to_f,
      currency: amount.currency.iso_code
    },
    request_id: request_id
  }
  response = connection.post do |req|
    req.url "/v2/payments/#{payment_id}/capture"
    req.body = body.to_json
  end

  Afterpay::Response.new(response)
end
create_order(order) click to toggle source
# File lib/workarea/afterpay/gateway.rb, line 29
def create_order(order)
  response = connection.post do |req|
    req.url "/v2/checkouts"
    req.body = order.to_json
  end
  Afterpay::Response.new(response)
end
get_configuration() click to toggle source
# File lib/workarea/afterpay/gateway.rb, line 14
def get_configuration
  response = connection.get do |req|
    req.url "/v2/configuration"
  end
  Afterpay::Response.new(response)
end
get_order(token) click to toggle source
# File lib/workarea/afterpay/gateway.rb, line 21
def get_order(token)
    response = connection.get do |req|
    req.url "/v2/checkouts/#{token}"
  end

  Afterpay::Response.new(response)
end
purchase(token, request_id) click to toggle source
# File lib/workarea/afterpay/gateway.rb, line 74
def purchase(token, request_id)
  body = {
    token: token,
    request_id: request_id
  }
  response = connection.post do |req|
    req.url "/v2/payments/capture"
    req.body = body.to_json
  end

  Afterpay::Response.new(response)
end
refund(afterpay_order_id, amount, request_id) click to toggle source
# File lib/workarea/afterpay/gateway.rb, line 87
def refund(afterpay_order_id, amount, request_id)
  body = {
    requestId: request_id,
    amount: {
      amount: amount.to_f,
      currency: amount.currency.iso_code
    }
  }

  response = connection.post do |req|
    req.url "/v2/payments/#{afterpay_order_id}/refund"
    req.body = body.to_json
  end
  Afterpay::Response.new(response)
end
void(payment_id) click to toggle source
# File lib/workarea/afterpay/gateway.rb, line 66
def void(payment_id)
  response = connection.post do |req|
    req.url "/v2/payments/#{payment_id}/void"
  end

  Afterpay::Response.new(response)
end

Private Instance Methods

au_endpoints() click to toggle source
# File lib/workarea/afterpay/gateway.rb, line 135
def au_endpoints
  {
    test_rest_endpoint: 'https://api-sandbox.afterpay.com',
    production_rest_endpoint: 'https://api.afterpay.com'
  }
end
auth_string() click to toggle source
# File lib/workarea/afterpay/gateway.rb, line 121
def auth_string
  @auth_string = Base64.strict_encode64("#{merchant_id}:#{secret_key}")
end
connection() click to toggle source
# File lib/workarea/afterpay/gateway.rb, line 105
def connection
  headers = {
    "Content-Type" => "application/json",
    "Accept"       => "application/json",
    "Authorization" => "Basic #{auth_string}",
    "User-Agent" => user_agent
  }

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

  Faraday.new(url: rest_endpoint, headers: headers, request: request_timeouts)
end
location() click to toggle source
# File lib/workarea/afterpay/gateway.rb, line 149
def location
  options[:location]
end
rest_endpoint() click to toggle source
# File lib/workarea/afterpay/gateway.rb, line 129
def rest_endpoint
  endpoint_hash = location == :us ? us_endpoints : au_endpoints

  test? ? endpoint_hash[:test_rest_endpoint] : endpoint_hash[:production_rest_endpoint]
end
test?() click to toggle source
# File lib/workarea/afterpay/gateway.rb, line 125
def test?
  (options.has_key?(:test) ? options[:test] : true)
end
us_endpoints() click to toggle source
# File lib/workarea/afterpay/gateway.rb, line 142
def us_endpoints
  {
    test_rest_endpoint: 'https://api.us-sandbox.afterpay.com',
    production_rest_endpoint: 'https://api.us.afterpay.com'
  }
end
user_agent() click to toggle source
# File lib/workarea/afterpay/gateway.rb, line 153
def user_agent
  "Afterpay Modile/1.0.0 (WORKAREA/#{Workarea::VERSION::STRING}; Merchant/#{merchant_id}) https://#{Workarea.config.host}"
end