Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle finding payments for given dates and with pagination. #1

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/pkg/
/spec/reports/
/tmp/
.idea/

# rspec failure tracking
.rspec_status
2 changes: 2 additions & 0 deletions lib/cloverrb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
require_relative "cloverrb/line_item"
require_relative "cloverrb/address"
require_relative "cloverrb/properties"
require_relative "cloverrb/payment"
require_relative "cloverrb/discount"

module Cloverrb
# Your code goes here...
Expand Down
11 changes: 3 additions & 8 deletions lib/cloverrb/address.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
module Cloverrb
class Address < Client
def initialize(token, merchant_code)
@token = token
@merchant_code = merchant_code
end

def fetch
url = "/merchants/#{@merchant_code}/address"
get(@token, url)
def self.fetch(merchant_id, token)
url = "/merchants/#{merchant_id}/address"
get(token, url)
end
end
end
20 changes: 10 additions & 10 deletions lib/cloverrb/client.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
module Cloverrb
class Client
BASE_URL = "https://api.clover.com/v3"
AUTH_URL = "https://clover.com/oauth/token"
BASE_URL = ENV.fetch("CLOVER_BASE_URL") { "https://api.clover.com" }
AUTH_URL = ENV.fetch("CLOVER_AUTH_URL") { "https://www.clover.com" }

def get(token, path)
HTTParty.get(BASE_URL + path, headers: build_headers(token)).parsed_response
def self.get(token, path)
HTTParty.get("#{BASE_URL}/v3" + path, headers: build_headers(token)).parsed_response
end

def post(token, path, body)
def self.post(token, path, body)
HTTParty.post(
BASE_URL + path,
"#{BASE_URL}/v3" + path,
headers: build_headers(token),
query: body
).parsed_response
end

def put(token, path, body)
def self.put(token, path, body)
HTTParty.put(
BASE_URL + path,
"#{BASE_URL}/v3" + path,
headers: build_headers(token),
query: body
).parsed_response
end

def self.generate_access_token(client_id, code, app_secret)
query = build_query(client_id, code, app_secret)
HTTParty.get(AUTH_URL, query: query)
HTTParty.get("#{AUTH_URL}/oauth/token", query: query)
end

private

def build_headers(token)
def self.build_headers(token)
{ "Authorization" => "Bearer #{token}"}
end

Expand Down
8 changes: 8 additions & 0 deletions lib/cloverrb/discount.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Cloverrb
class Discount < Client
def self.all(merchant_id, token, order_id)
url = "/merchants/#{merchant_id}/orders/#{order_id}/discounts"
get(token, url)
end
end
end
17 changes: 6 additions & 11 deletions lib/cloverrb/employee.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
module Cloverrb
class Employee < Client
def initialize(token, merchant_code)
@token = token
@merchant_code = merchant_code
end

def all(role=nil)
url = "/merchants/#{@merchant_code}/employees"
def self.all(merchant_id, token, role=nil)
url = "/merchants/#{merchant_id}/employees"
url += "?filter=role=#{role}" if role
get(@token, url)
get(token, url)
end

def find(employee_id)
url = "/merchants/#{@merchant_code}/employees/#{employee_id}"
get(@token, url)
def self.find(merchant_id, token, employee_id)
url = "/merchants/#{merchant_id}/employees/#{employee_id}"
get(token, url)
end
end
end
12 changes: 3 additions & 9 deletions lib/cloverrb/line_item.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
module Cloverrb
class LineItem < Client
def initialize(token, order, merchant)
@token = token
@order = order
@merchant = merchant
end

def all
url = "/merchants/#{@merchant}/orders/#{@order}/line_items"
get(@token, url)
def self.all(merchant_id, token, order_id)
url = "/merchants/#{merchant_id}/orders/#{order_id}/line_items"
get(token, url)
end
end
end
8 changes: 2 additions & 6 deletions lib/cloverrb/merchant.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
module Cloverrb
class Merchant < Client
def initialize(token)
@token = token
end

def find(merchant_id)
def self.find(merchant_id, token)
url = "/merchants/#{merchant_id}?expand=owner"
get(@token, url)
get(token, url)
end
end
end
22 changes: 12 additions & 10 deletions lib/cloverrb/order.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
module Cloverrb
class Order < Client
def initialize(token)
@token = token
end

def all(merchant_id, options = {})
def self.all(merchant_id, token, options = {})
url = "/merchants/#{merchant_id}/orders?"

filters = []
filters = %w(expand=lineItems expand=lineItems.discounts expand=discounts)
filters << "filter=createdTime>=#{options[:start_date]}" if has_start_date?(options)
filters << "filter=createdTime<=#{options[:end_date]}" if has_end_date?(options)
filters << "filter=state=#{options[:state]}" if has_state?(options)
url += filters.join("&")

get(@token, url)
get(token, url)
end

def self.find(merchant_id, token, order_id)
url = "/merchants/#{merchant_id}/orders/#{order_id}?expand=lineItems&expand=lineItems.discounts&expand=discounts"

get(token, url)
end

def self.total(line_items)
items = line_items["elements"]
items.inject(0) { |sum, item| sum + item["price"] }
end

def has_start_date?(options)
def self.has_start_date?(options)
options[:start_date]
end

def has_end_date?(options)
def self.has_end_date?(options)
options[:end_date]
end

def has_state?(options)
def self.has_state?(options)
options[:state]
end
end
Expand Down
39 changes: 39 additions & 0 deletions lib/cloverrb/payment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Cloverrb
class Payment < Client
def self.all(merchant_id, token, options = {})
url = "/merchants/#{merchant_id}/payments?expand=refunds&expand=cardTransaction&expand=tender&expand=transactionInfo"
url += add_date_filters(options)
url += add_limit_and_offset(options)
get(token, url)
end

def self.find(merchant_id, token, payment_id)
url = "/merchants/#{merchant_id}/payments/#{payment_id}?expand=refunds&expand=cardTransaction&expand=tender&expand=transactionInfo"
get(token, url)
end

private

def self.add_date_filters(options)
filter = ""
if options["startDateTime"].present?
filter += "&filter=createdTime>=#{options['startDateTime']}"
end
if options["endDateTime"].present?
filter += "&filter=createdTime<=#{options['endDateTime']}"
end
filter
end

def self.add_limit_and_offset(options)
filter = ""
if options["limit"].present?
filter += "&limit=#{options['limit']}"
end
if options["offset"].present?
filter += "&offset=#{options['offset']}"
end
filter
end
end
end
11 changes: 3 additions & 8 deletions lib/cloverrb/properties.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
module Cloverrb
class Properties < Client
def initialize(token, merchant_code)
@token = token
@merchant_code = merchant_code
end

def fetch
url = "/merchants/#{@merchant_code}/properties"
get(@token, url)
def self.fetch(merchant_id, token)
url = "/merchants/#{merchant_id}/properties"
get(token, url)
end
end
end