From b511090ced5b05b6bd35e6ab644e73ac6fd9d0e2 Mon Sep 17 00:00:00 2001 From: Scott Latham Date: Fri, 19 Oct 2018 09:46:07 -0700 Subject: [PATCH 1/8] Load Clover URL from ENV before default --- .gitignore | 1 + lib/cloverrb/client.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 8eb3b06..1c59284 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ /pkg/ /spec/reports/ /tmp/ +.idea/ # rspec failure tracking .rspec_status diff --git a/lib/cloverrb/client.rb b/lib/cloverrb/client.rb index b3b066a..577f89f 100644 --- a/lib/cloverrb/client.rb +++ b/lib/cloverrb/client.rb @@ -1,6 +1,6 @@ module Cloverrb class Client - BASE_URL = "https://api.clover.com/v3" + BASE_URL = ENV.fetch("CLOVER_API_URL") { "https://api.clover.com/v3" } AUTH_URL = "https://clover.com/oauth/token" def get(token, path) From e56847b606cc0f2fa9603859099146aa05bd6f81 Mon Sep 17 00:00:00 2001 From: Scott Latham Date: Mon, 22 Oct 2018 09:38:54 -0700 Subject: [PATCH 2/8] Add Cloverrb::Payment and add find method to Order, Payment --- lib/cloverrb.rb | 1 + lib/cloverrb/order.rb | 6 ++++++ lib/cloverrb/payment.rb | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 lib/cloverrb/payment.rb diff --git a/lib/cloverrb.rb b/lib/cloverrb.rb index ac341d2..5ef6b2e 100644 --- a/lib/cloverrb.rb +++ b/lib/cloverrb.rb @@ -9,6 +9,7 @@ require_relative "cloverrb/line_item" require_relative "cloverrb/address" require_relative "cloverrb/properties" +require_relative "cloverrb/payment" module Cloverrb # Your code goes here... diff --git a/lib/cloverrb/order.rb b/lib/cloverrb/order.rb index dd2b956..deaa875 100644 --- a/lib/cloverrb/order.rb +++ b/lib/cloverrb/order.rb @@ -16,6 +16,12 @@ def all(merchant_id, options = {}) get(@token, url) end + def find(merchant_id, order_id) + url = "/merchants/#{merchant_id}/orders/#{order_id}" + + get(@token, url) + end + def self.total(line_items) items = line_items["elements"] items.inject(0) { |sum, item| sum + item["price"] } diff --git a/lib/cloverrb/payment.rb b/lib/cloverrb/payment.rb new file mode 100644 index 0000000..cf74743 --- /dev/null +++ b/lib/cloverrb/payment.rb @@ -0,0 +1,18 @@ +module Cloverrb + class Payment < Client + def initialize(token) + @token = token + end + + def all(merchant_id) + url = "/merchants/#{merchant_id}/payments" + get(@token, url) + end + + def find(merchant_id, payment_id) + url = "/merchants/#{merchant_id}/payments/#{payment_id}" + + get(@token, url) + end + end +end \ No newline at end of file From 7d033a33ac576a9070df9cb3abdebf4c2ea4024d Mon Sep 17 00:00:00 2001 From: Scott Latham Date: Tue, 23 Oct 2018 10:20:31 -0700 Subject: [PATCH 3/8] Add Cloverrb::Discount and redesign gem interface --- lib/cloverrb.rb | 1 + lib/cloverrb/address.rb | 11 +++-------- lib/cloverrb/client.rb | 12 ++++++------ lib/cloverrb/discount.rb | 8 ++++++++ lib/cloverrb/employee.rb | 17 ++++++----------- lib/cloverrb/line_item.rb | 12 +++--------- lib/cloverrb/merchant.rb | 8 ++------ lib/cloverrb/order.rb | 18 +++++++----------- lib/cloverrb/payment.rb | 13 ++++--------- lib/cloverrb/properties.rb | 11 +++-------- 10 files changed, 43 insertions(+), 68 deletions(-) create mode 100644 lib/cloverrb/discount.rb diff --git a/lib/cloverrb.rb b/lib/cloverrb.rb index 5ef6b2e..7ba2fe6 100644 --- a/lib/cloverrb.rb +++ b/lib/cloverrb.rb @@ -10,6 +10,7 @@ require_relative "cloverrb/address" require_relative "cloverrb/properties" require_relative "cloverrb/payment" +require_relative "cloverrb/discount" module Cloverrb # Your code goes here... diff --git a/lib/cloverrb/address.rb b/lib/cloverrb/address.rb index 7db218e..71ca583 100644 --- a/lib/cloverrb/address.rb +++ b/lib/cloverrb/address.rb @@ -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 \ No newline at end of file diff --git a/lib/cloverrb/client.rb b/lib/cloverrb/client.rb index 577f89f..733f27d 100644 --- a/lib/cloverrb/client.rb +++ b/lib/cloverrb/client.rb @@ -1,13 +1,13 @@ module Cloverrb class Client - BASE_URL = ENV.fetch("CLOVER_API_URL") { "https://api.clover.com/v3" } - AUTH_URL = "https://clover.com/oauth/token" + BASE_URL = ENV.fetch("CLOVER_BASE_URL") { "https://api.clover.com/v3" } + AUTH_URL = ENV.fetch("CLOVER_AUTH_URL") { "https://clover.com/oauth/token" } - def get(token, path) + def self.get(token, path) HTTParty.get(BASE_URL + path, headers: build_headers(token)).parsed_response end - def post(token, path, body) + def self.post(token, path, body) HTTParty.post( BASE_URL + path, headers: build_headers(token), @@ -15,7 +15,7 @@ def post(token, path, body) ).parsed_response end - def put(token, path, body) + def self.put(token, path, body) HTTParty.put( BASE_URL + path, headers: build_headers(token), @@ -30,7 +30,7 @@ def self.generate_access_token(client_id, code, app_secret) private - def build_headers(token) + def self.build_headers(token) { "Authorization" => "Bearer #{token}"} end diff --git a/lib/cloverrb/discount.rb b/lib/cloverrb/discount.rb new file mode 100644 index 0000000..ab640e5 --- /dev/null +++ b/lib/cloverrb/discount.rb @@ -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 \ No newline at end of file diff --git a/lib/cloverrb/employee.rb b/lib/cloverrb/employee.rb index e71c2e7..6aba24a 100644 --- a/lib/cloverrb/employee.rb +++ b/lib/cloverrb/employee.rb @@ -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 \ No newline at end of file diff --git a/lib/cloverrb/line_item.rb b/lib/cloverrb/line_item.rb index 3eb1b92..d2156b8 100644 --- a/lib/cloverrb/line_item.rb +++ b/lib/cloverrb/line_item.rb @@ -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 \ No newline at end of file diff --git a/lib/cloverrb/merchant.rb b/lib/cloverrb/merchant.rb index 97d1d15..7c01b4b 100644 --- a/lib/cloverrb/merchant.rb +++ b/lib/cloverrb/merchant.rb @@ -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 \ No newline at end of file diff --git a/lib/cloverrb/order.rb b/lib/cloverrb/order.rb index deaa875..70753b0 100644 --- a/lib/cloverrb/order.rb +++ b/lib/cloverrb/order.rb @@ -1,10 +1,6 @@ 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 = [] @@ -13,13 +9,13 @@ def all(merchant_id, options = {}) filters << "filter=state=#{options[:state]}" if has_state?(options) url += filters.join("&") - get(@token, url) + get(token, url) end - def find(merchant_id, order_id) + def self.find(merchant_id, token, order_id) url = "/merchants/#{merchant_id}/orders/#{order_id}" - get(@token, url) + get(token, url) end def self.total(line_items) @@ -27,15 +23,15 @@ def self.total(line_items) 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 diff --git a/lib/cloverrb/payment.rb b/lib/cloverrb/payment.rb index cf74743..e14b68d 100644 --- a/lib/cloverrb/payment.rb +++ b/lib/cloverrb/payment.rb @@ -1,18 +1,13 @@ module Cloverrb class Payment < Client - def initialize(token) - @token = token - end - - def all(merchant_id) + def self.all(merchant_id, token) url = "/merchants/#{merchant_id}/payments" - get(@token, url) + get(token, url) end - def find(merchant_id, payment_id) + def self.find(merchant_id, token, payment_id) url = "/merchants/#{merchant_id}/payments/#{payment_id}" - - get(@token, url) + get(token, url) end end end \ No newline at end of file diff --git a/lib/cloverrb/properties.rb b/lib/cloverrb/properties.rb index 877f7c4..067ca76 100644 --- a/lib/cloverrb/properties.rb +++ b/lib/cloverrb/properties.rb @@ -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 \ No newline at end of file From d49df8fb652a6287ddeae6c2ff340c24fe8edeef Mon Sep 17 00:00:00 2001 From: Scott Latham Date: Tue, 23 Oct 2018 15:08:38 -0700 Subject: [PATCH 4/8] Fetch nested lineItems/discounts when fetching order --- lib/cloverrb/order.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/cloverrb/order.rb b/lib/cloverrb/order.rb index 70753b0..6f6ebcb 100644 --- a/lib/cloverrb/order.rb +++ b/lib/cloverrb/order.rb @@ -1,9 +1,9 @@ module Cloverrb class Order < Client def self.all(merchant_id, token, options = {}) - url = "/merchants/#{merchant_id}/orders?" + url = "/merchants/#{merchant_id}/orders?expand" - filters = [] + filters = %w(expand=lineItems expand=lineItems.discounts 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) @@ -13,7 +13,7 @@ def self.all(merchant_id, token, options = {}) end def self.find(merchant_id, token, order_id) - url = "/merchants/#{merchant_id}/orders/#{order_id}" + url = "/merchants/#{merchant_id}/orders/#{order_id}?expand=lineItems&expand=lineItems.discounts&expand=discounts" get(token, url) end From fe658da8bdfa0a8779dff1c7b61beeb7ced60ea7 Mon Sep 17 00:00:00 2001 From: Scott Latham Date: Tue, 23 Oct 2018 16:32:05 -0700 Subject: [PATCH 5/8] Strip clover URL ENV variables down to host and protocol --- lib/cloverrb/client.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/cloverrb/client.rb b/lib/cloverrb/client.rb index 733f27d..907efbf 100644 --- a/lib/cloverrb/client.rb +++ b/lib/cloverrb/client.rb @@ -1,15 +1,15 @@ module Cloverrb class Client - BASE_URL = ENV.fetch("CLOVER_BASE_URL") { "https://api.clover.com/v3" } - AUTH_URL = ENV.fetch("CLOVER_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://clover.com" } def self.get(token, path) - HTTParty.get(BASE_URL + path, headers: build_headers(token)).parsed_response + HTTParty.get("#{BASE_URL}/v3" + path, headers: build_headers(token)).parsed_response end def self.post(token, path, body) HTTParty.post( - BASE_URL + path, + "#{BASE_URL}/v3" + path, headers: build_headers(token), query: body ).parsed_response @@ -17,7 +17,7 @@ def self.post(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 @@ -25,7 +25,7 @@ def self.put(token, path, body) 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 From 00cad8ccf66756fb455bba45fc48977cc85f9b6e Mon Sep 17 00:00:00 2001 From: Scott Latham Date: Mon, 12 Nov 2018 15:53:13 -0800 Subject: [PATCH 6/8] Small fixes --- lib/cloverrb/client.rb | 2 +- lib/cloverrb/order.rb | 4 ++-- lib/cloverrb/payment.rb | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/cloverrb/client.rb b/lib/cloverrb/client.rb index 907efbf..217afc7 100644 --- a/lib/cloverrb/client.rb +++ b/lib/cloverrb/client.rb @@ -1,7 +1,7 @@ module Cloverrb class Client BASE_URL = ENV.fetch("CLOVER_BASE_URL") { "https://api.clover.com" } - AUTH_URL = ENV.fetch("CLOVER_AUTH_URL") { "https://clover.com" } + AUTH_URL = ENV.fetch("CLOVER_AUTH_URL") { "https://www.clover.com" } def self.get(token, path) HTTParty.get("#{BASE_URL}/v3" + path, headers: build_headers(token)).parsed_response diff --git a/lib/cloverrb/order.rb b/lib/cloverrb/order.rb index 6f6ebcb..61fb467 100644 --- a/lib/cloverrb/order.rb +++ b/lib/cloverrb/order.rb @@ -1,9 +1,9 @@ module Cloverrb class Order < Client def self.all(merchant_id, token, options = {}) - url = "/merchants/#{merchant_id}/orders?expand" + url = "/merchants/#{merchant_id}/orders?" - filters = %w(expand=lineItems expand=lineItems.discounts discounts) + 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) diff --git a/lib/cloverrb/payment.rb b/lib/cloverrb/payment.rb index e14b68d..1ab6180 100644 --- a/lib/cloverrb/payment.rb +++ b/lib/cloverrb/payment.rb @@ -1,12 +1,12 @@ module Cloverrb class Payment < Client def self.all(merchant_id, token) - url = "/merchants/#{merchant_id}/payments" + url = "/merchants/#{merchant_id}/payments?expand=refunds" get(token, url) end def self.find(merchant_id, token, payment_id) - url = "/merchants/#{merchant_id}/payments/#{payment_id}" + url = "/merchants/#{merchant_id}/payments/#{payment_id}?expand=refunds" get(token, url) end end From 4b0f799fd99db46c65e7e8508435379ace625041 Mon Sep 17 00:00:00 2001 From: Scott Latham Date: Mon, 12 Nov 2018 16:47:31 -0800 Subject: [PATCH 7/8] Expand more payment fields --- lib/cloverrb/payment.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cloverrb/payment.rb b/lib/cloverrb/payment.rb index 1ab6180..4efa065 100644 --- a/lib/cloverrb/payment.rb +++ b/lib/cloverrb/payment.rb @@ -1,12 +1,12 @@ module Cloverrb class Payment < Client def self.all(merchant_id, token) - url = "/merchants/#{merchant_id}/payments?expand=refunds" + url = "/merchants/#{merchant_id}/payments?expand=refunds&expand=cardTransaction&expand=tender&expand=transactionInfo" get(token, url) end def self.find(merchant_id, token, payment_id) - url = "/merchants/#{merchant_id}/payments/#{payment_id}?expand=refunds" + url = "/merchants/#{merchant_id}/payments/#{payment_id}?expand=refunds&expand=cardTransaction&expand=tender&expand=transactionInfo" get(token, url) end end From 543ea7670a21d64438214f4cd2245cd1688774db Mon Sep 17 00:00:00 2001 From: kannan Date: Wed, 1 Apr 2020 15:48:27 -0600 Subject: [PATCH 8/8] Added options to the all method. default for options. changed createdAt to createdTime added self. handle limit and offset from the options. --- lib/cloverrb/payment.rb | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/cloverrb/payment.rb b/lib/cloverrb/payment.rb index 4efa065..0366986 100644 --- a/lib/cloverrb/payment.rb +++ b/lib/cloverrb/payment.rb @@ -1,7 +1,9 @@ module Cloverrb class Payment < Client - def self.all(merchant_id, token) + 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 @@ -9,5 +11,29 @@ 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 \ No newline at end of file