levelup 0.9.4 → 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +32 -305
  3. data/Gemfile.lock +1 -1
  4. data/README.md +131 -38
  5. data/levelup.gemspec +1 -1
  6. data/lib/levelup.rb +4 -0
  7. data/lib/levelup/api.rb +1 -2
  8. data/lib/levelup/configuration.rb +1 -1
  9. data/lib/levelup/endpoints/access_tokens.rb +7 -10
  10. data/lib/levelup/endpoints/app_locations.rb +4 -6
  11. data/lib/levelup/endpoints/app_users.rb +1 -7
  12. data/lib/levelup/endpoints/credit_cards.rb +3 -4
  13. data/lib/levelup/endpoints/location_credit.rb +6 -8
  14. data/lib/levelup/endpoints/location_merchant_funded_credit.rb +23 -0
  15. data/lib/levelup/endpoints/location_orders.rb +8 -14
  16. data/lib/levelup/endpoints/merchant_funded_credits.rb +4 -6
  17. data/lib/levelup/endpoints/merchant_locations.rb +8 -13
  18. data/lib/levelup/endpoints/merchant_orders.rb +6 -8
  19. data/lib/levelup/endpoints/orders.rb +3 -3
  20. data/lib/levelup/endpoints/permissions_requests.rb +1 -2
  21. data/lib/levelup/endpoints/qr_codes.rb +3 -4
  22. data/lib/levelup/endpoints/specific_app.rb +1 -2
  23. data/lib/levelup/endpoints/specific_location.rb +13 -8
  24. data/lib/levelup/endpoints/specific_merchant.rb +4 -5
  25. data/lib/levelup/endpoints/specific_order.rb +4 -8
  26. data/lib/levelup/endpoints/specific_permissions_request.rb +3 -5
  27. data/lib/levelup/endpoints/user_addresses.rb +6 -7
  28. data/lib/levelup/endpoints/user_orders.rb +2 -3
  29. data/lib/levelup/endpoints/users.rb +5 -0
  30. data/lib/levelup/requests/authenticate_merchant.rb +1 -2
  31. data/lib/levelup/requests/create_address.rb +3 -4
  32. data/lib/levelup/requests/create_card.rb +2 -3
  33. data/lib/levelup/requests/create_order.rb +1 -2
  34. data/lib/levelup/requests/create_user.rb +4 -13
  35. data/lib/levelup/requests/get_location_credit.rb +1 -2
  36. data/lib/levelup/requests/get_location_merchant_funded_credit.rb +16 -0
  37. data/lib/levelup/requests/get_order.rb +1 -2
  38. data/lib/levelup/requests/give_merchant_credit.rb +1 -2
  39. data/lib/levelup/requests/list_addresses.rb +3 -4
  40. data/lib/levelup/requests/list_app_locations.rb +1 -2
  41. data/lib/levelup/requests/list_locations.rb +2 -3
  42. data/lib/levelup/requests/list_orders.rb +2 -3
  43. data/lib/levelup/requests/list_user_orders.rb +3 -4
  44. data/lib/levelup/requests/show_permissions_request.rb +1 -2
  45. data/lib/levelup/responses/success_paginated.rb +1 -2
  46. data/lib/levelup/templates/app_authenticated.rb +1 -3
  47. data/lib/levelup/templates/merchant_and_user_authenticated.rb +4 -9
  48. data/lib/levelup/templates/merchant_authenticated.rb +2 -5
  49. data/lib/levelup/utils/payment_calculator.rb +83 -0
  50. data/spec/endpoints/access_tokens_spec.rb +2 -4
  51. data/spec/endpoints/app_users_spec.rb +6 -22
  52. data/spec/endpoints/location_credit_spec.rb +1 -2
  53. data/spec/endpoints/location_merchant_funded_credit_spec.rb +12 -0
  54. data/spec/endpoints/location_orders_spec.rb +3 -4
  55. data/spec/endpoints/merchant_funded_credits_spec.rb +1 -2
  56. data/spec/endpoints/merchant_locations_spec.rb +4 -4
  57. data/spec/endpoints/qr_codes_spec.rb +1 -2
  58. data/spec/endpoints/specific_order_spec.rb +3 -4
  59. data/spec/endpoints/specific_permissions_request_spec.rb +1 -2
  60. data/spec/endpoints/user_addresses_spec.rb +1 -2
  61. data/spec/endpoints/user_orders_spec.rb +1 -2
  62. data/spec/endpoints/users_spec.rb +22 -0
  63. data/spec/{data_objects/requests → requests}/authenticate_app_spec.rb +0 -0
  64. data/spec/{data_objects/requests → requests}/authenticate_merchant_spec.rb +0 -0
  65. data/spec/{data_objects/requests → requests}/create_order_spec.rb +0 -0
  66. data/spec/{data_objects/requests → requests}/get_order_spec.rb +0 -0
  67. data/spec/{data_objects/requests → requests}/list_locations_spec.rb +0 -0
  68. data/spec/{data_objects/requests → requests}/list_orders_spec.rb +0 -0
  69. data/spec/{data_objects/requests → requests}/refund_order_spec.rb +0 -0
  70. data/spec/{data_objects/requests → requests}/request_permissions_spec.rb +1 -2
  71. data/spec/{data_objects/responses → responses}/error_spec.rb +0 -0
  72. data/spec/{data_objects/responses → responses}/response_spec.rb +0 -0
  73. data/spec/{data_objects/responses → responses}/success_paginated_spec.rb +1 -2
  74. data/spec/utils/payment_calculator/payment_calculator_spec.rb +164 -0
  75. metadata +36 -26
@@ -1,8 +1,7 @@
1
1
  module Levelup
2
2
  module Requests
3
- # Represents a request to list all orders made at a
4
- # specified location. This is a v14 request and should not be expected to
5
- # remain functional indefinitely.
3
+ # Represents a request to list all orders made at a specified location.
4
+ # This is a v14 request and should not be expected to remain functional indefinitely.
6
5
  class ListOrders < Base
7
6
  include Templates::MerchantAuthenticated
8
7
 
@@ -1,8 +1,7 @@
1
- module Levelup
1
+ module Levelup
2
2
  module Requests
3
- # Represents a request to create an address for a
4
- # specific user. For information about its parameters, see UserAddressData
5
- # and UserAuthenticated.
3
+ # Represents a request to create an address for a specific user.
4
+ # For information about its parameters, see UserAddressData and UserAuthenticated.
6
5
  # User access token must have the read_user_orders permission.
7
6
  class ListUserOrders < Base
8
7
  include Templates::UserAuthenticated
@@ -1,7 +1,6 @@
1
1
  module Levelup
2
2
  module Requests
3
- # Represents a request to show the status of a specified permissions
4
- # request.
3
+ # Represents a request to show the status of a specified permissions request.
5
4
  class ShowPermissionsRequest < Base
6
5
  include Templates::AppAuthenticated
7
6
 
@@ -1,7 +1,6 @@
1
1
  module Levelup
2
2
  module Responses
3
- # Class that encapsulates a successful response from a paginated endpoint
4
- # of the LevelUp API.
3
+ # Class that encapsulates a successful response from a paginated endpoint of the LevelUp API.
5
4
  class SuccessPaginated < Success
6
5
  attr_writer :next_page_request, :next_page
7
6
 
@@ -1,9 +1,7 @@
1
1
  module Levelup
2
2
  module Templates
3
3
  # A template to apply to any requests requiring app authentication.
4
- #
5
- # Authentication template - only apply one authentication template per
6
- # request.
4
+ # Authentication template - only apply one authentication template per request.
7
5
  module AppAuthenticated
8
6
  # An access token for an app.
9
7
  attr_accessor :app_access_token
@@ -1,16 +1,11 @@
1
1
  module Levelup
2
2
  module Templates
3
- # A template to apply to any requests requiring both merchant and user
4
- # authentication.
5
- #
6
- # Authentication template - only apply one authentication template per
7
- # request.
3
+ # A template to apply to any requests requiring both merchant and user authentication.
4
+ # Authentication template - only apply one authentication template per request.
8
5
  module MerchantAndUserAuthenticated
9
- # An access token for a merchant that has granted you
10
- # 'manage_merchant_orders' permissions.
6
+ # An access token for a merchant that has granted you 'manage_merchant_orders' permissions.
11
7
  attr_accessor :merchant_access_token
12
- # An access token for a user that has granted you 'create_orders'
13
- # permissions.
8
+ # An access token for a user that has granted you 'create_orders' permissions.
14
9
  attr_accessor :user_access_token
15
10
 
16
11
  def auth_type
@@ -3,12 +3,9 @@ module Levelup
3
3
  # A template to apply to any requests requiring v15 merchant authentication.
4
4
  # If your request requires v14 merchant authentication, include this module
5
5
  # and define auth_type as :merchant_v14.
6
- #
7
- # Authentication template - only apply one authentication template per
8
- # request.
6
+ # Authentication template - only apply one authentication template per request.
9
7
  module MerchantAuthenticated
10
- # An access token for a merchant that has granted you
11
- # 'manage_merchant_orders' permissions.
8
+ # An access token for a merchant that has granted you 'manage_merchant_orders' permissions.
12
9
  attr_accessor :merchant_access_token
13
10
 
14
11
  def auth_type
@@ -0,0 +1,83 @@
1
+ module Levelup
2
+ module Utils
3
+ # This is a tool to allow integrators to calculate various values to discount and tender
4
+ class PaymentCalculator
5
+ # @param discount_applied [Integer] The total LevelUp discount applied to the check.
6
+ # @param gift_card_credit_available [Integer] The total available amount of LevelUp gift card
7
+ # credit.
8
+ # @param spend_amount_returned_from_levelup [Integer] The amount of spend returned from the
9
+ # LevelUp API.
10
+ # @param tip_returned_from_levelup [Integer] The tip returned from the LevelUp API.
11
+ def initialize(discount_applied:, gift_card_credit_available:,
12
+ spend_amount_returned_from_levelup:, tip_returned_from_levelup:)
13
+ @discount_applied = discount_applied
14
+ @gift_card_credit_available = gift_card_credit_available
15
+ @spend_amount_returned_from_levelup = spend_amount_returned_from_levelup
16
+ @tip_returned_from_levelup = tip_returned_from_levelup
17
+ end
18
+
19
+ # Calculates the LevelUp discount to apply based on the arguments passed
20
+ # @param check_total_due_including_tax [Integer] Grand total of check balance in cents
21
+ # @param exempted_item_total [Integer] Combined total of exempted items in cents
22
+ # @param merchant_funded_credit_available [Integer] Total amount of merchant funded credit
23
+ # available in cents
24
+ # @param payment_amount_requested [Integer] Spend amount requested from LevelUp API order
25
+ # endpoint in cents
26
+ # @param tax_amount_due [Integer] Total amount of tax due on the check
27
+ def self.levelup_discount_to_apply(check_total_due_including_tax:, exempted_item_total:,
28
+ merchant_funded_credit_available:, payment_amount_requested:, tax_amount_due:)
29
+ total_due_without_tax = check_total_due_including_tax - tax_amount_due
30
+ max_discount = [payment_amount_requested, total_due_without_tax].min
31
+ max_discount_less_exemptions = [0, max_discount - exempted_item_total].max
32
+ [merchant_funded_credit_available, max_discount_less_exemptions].min
33
+ end
34
+
35
+ # Cent amount of LevelUp gift card payment to apply NOT including tip
36
+ def gift_card_payment_to_apply
37
+ [spend_amount_after_discount, @gift_card_credit_available].min
38
+ end
39
+
40
+ # Remaining balance in cents on LevelUp gift card after gift card payment has been applied
41
+ def gift_card_remaining_balance_after_payment
42
+ [0, @gift_card_credit_available - total_gift_card_payment_to_apply_including_tip].max
43
+ end
44
+
45
+ # Cent amount of LevelUp gift card tip to apply NOT including LevelUp gift card payment
46
+ def gift_card_tip_to_apply
47
+ total_gift_card_payment_to_apply_including_tip - gift_card_payment_to_apply
48
+ end
49
+
50
+ # Cent amount of LevelUp payment to apply NOT including the tip amount
51
+ def levelup_payment_to_apply
52
+ [0, spend_amount_after_discount - @gift_card_credit_available].max
53
+ end
54
+
55
+ # Cent amount of LevelUp tip to apply NOT including payment amount
56
+ def levelup_tip_to_apply
57
+ total_levelup_payment_to_apply_including_tip - levelup_payment_to_apply
58
+ end
59
+
60
+ # Total amount in cents of LevelUp payment to apply including both tip AND payment amounts
61
+ def total_levelup_payment_to_apply_including_tip
62
+ [0, total_amount_after_discount - @gift_card_credit_available].max
63
+ end
64
+
65
+ private
66
+
67
+ # Spend amount in cents after discount is subtracted
68
+ def spend_amount_after_discount
69
+ [0, @spend_amount_returned_from_levelup - @discount_applied].max
70
+ end
71
+
72
+ # Total amount in cents including tip after discount is subtracted
73
+ def total_amount_after_discount
74
+ spend_amount_after_discount + @tip_returned_from_levelup
75
+ end
76
+
77
+ # Total amount in cents of LevelUp gift card payment to apply including tip
78
+ def total_gift_card_payment_to_apply_including_tip
79
+ [total_amount_after_discount, @gift_card_credit_available].min
80
+ end
81
+ end
82
+ end
83
+ end
@@ -58,8 +58,7 @@ describe 'Levelup::Endpoints::AccessTokens', vcr: true do
58
58
  password: TestConfig.merchant_password
59
59
  )
60
60
 
61
- response = @test_client.access_tokens.
62
- create_for_merchant(merchant_auth_request)
61
+ response = @test_client.access_tokens.create_for_merchant(merchant_auth_request)
63
62
  expect(response).to be_success
64
63
  expect(response.token).to_not be_nil
65
64
  end
@@ -72,8 +71,7 @@ describe 'Levelup::Endpoints::AccessTokens', vcr: true do
72
71
  username: 'username_wrong',
73
72
  password: 'password1'
74
73
  )
75
- response = @test_client.access_tokens.
76
- create_for_merchant(merchant_auth_request)
74
+ response = @test_client.access_tokens.create_for_merchant(merchant_auth_request)
77
75
  expect(response).to_not be_success
78
76
  end
79
77
  end
@@ -11,9 +11,12 @@ describe 'Levelup::Endpoints::AppUsers', vcr: true do
11
11
  describe '#create' do
12
12
  it 'creates a user' do
13
13
  response = @test_client.apps.users.create(
14
- email: 'some@email.com',
15
- first_name: 'firstname',
16
- last_name: 'lastname',
14
+ api_key: TestConfig.api_key_valid,
15
+ user: {
16
+ email: 'some' + SecureRandom.hex(3) + '@example.com',
17
+ first_name: 'firstname',
18
+ last_name: 'lastname'
19
+ },
17
20
  permission_keynames: ['create_orders']
18
21
  )
19
22
 
@@ -21,23 +24,4 @@ describe 'Levelup::Endpoints::AppUsers', vcr: true do
21
24
  expect(response.user.last_name).to eq('lastname')
22
25
  end
23
26
  end
24
-
25
- describe '#get' do
26
- context 'with an invalid access token' do
27
- it 'returns an error response' do
28
- response = @test_client.apps.users.get('invalid')
29
- expect(response).to_not be_success
30
- end
31
- end
32
-
33
- context 'with a valid access token' do
34
- it 'returns the user\'s info' do
35
- response = @test_client.apps.users.get(
36
- TestConfig.user_token_with_read_info_perms)
37
-
38
- expect(response).to be_success
39
- expect(response.email).to_not be_nil
40
- end
41
- end
42
- end
43
27
  end
@@ -13,8 +13,7 @@ describe 'Levelup::Endpoints::LocationCredit', vcr: true do
13
13
 
14
14
  context 'with an invalid user token' do
15
15
  it 'returns an error response' do
16
- response = @test_client.locations(TestConfig.location_id).credit.
17
- get('1234')
16
+ response = @test_client.locations(TestConfig.location_id).credit.get('1234')
18
17
  expect(response).to_not be_success
19
18
  end
20
19
  end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Levelup::Endpoints::LocationMerchantFundedCredit', vcr: true do
4
+ describe '#get' do
5
+ it 'gets a users credit' do
6
+ response = @test_client.locations(TestConfig.location_id).merchant_funded_credit.
7
+ get(TestConfig.user_qr_code, TestConfig.merchant_token_with_manage_orders_perms)
8
+
9
+ expect(response).to be_success
10
+ end
11
+ end
12
+ end
@@ -16,8 +16,8 @@ describe 'Levelup::Endpoints::LocationOrders', vcr: true do
16
16
  describe '#list' do
17
17
  context 'with a valid location' do
18
18
  it 'returns a list of orders' do
19
- response = @test_client.locations(TestConfig.location_id).
20
- orders.list(@test_merchant_auth_token)
19
+ response = @test_client.locations(TestConfig.location_id).orders.
20
+ list(@test_merchant_auth_token)
21
21
  expect(response).to be_success
22
22
  expect(response.orders).to_not be_nil
23
23
  end
@@ -25,8 +25,7 @@ describe 'Levelup::Endpoints::LocationOrders', vcr: true do
25
25
 
26
26
  context 'with an invalid location' do
27
27
  it 'returns an error response' do
28
- response = @test_client.locations(-25).orders.
29
- list(@test_merchant_auth_token)
28
+ response = @test_client.locations(-25).orders.list(@test_merchant_auth_token)
30
29
  expect(response).to_not be_success
31
30
  end
32
31
  end
@@ -6,8 +6,7 @@ describe 'Levelup::Endpoints::MerchantFundedCredits', vcr: true do
6
6
  response = @test_client.merchant_funded_credits.give(
7
7
  email: 'pos@thelevelup.com',
8
8
  value_amount: 5,
9
- merchant_access_token: TestConfig.
10
- merchant_token_with_grant_credit_perms
9
+ merchant_access_token: TestConfig.merchant_token_with_grant_credit_perms
11
10
  )
12
11
 
13
12
  expect(response).to be_success
@@ -17,8 +17,8 @@ describe 'Levelup::Endpoints::MerchantLocations', vcr: true do
17
17
  describe '#list' do
18
18
  context 'with a valid merchant' do
19
19
  it 'returns a successful list-locations response' do
20
- response = @test_client.merchants(@test_merchant_id).locations.
21
- list(@test_merchant_auth_token)
20
+ response = @test_client.merchants(@test_merchant_id).locations.list(
21
+ @test_merchant_auth_token)
22
22
  expect(response).to be_success
23
23
  expect(response.locations).to_not be_nil
24
24
  end
@@ -26,8 +26,8 @@ describe 'Levelup::Endpoints::MerchantLocations', vcr: true do
26
26
 
27
27
  context 'with an invalid merchant' do
28
28
  it 'returns an error response' do
29
- response = @test_client.merchants(-25).locations.
30
- list(@test_merchant_auth_token)
29
+ response = @test_client.merchants(-25).locations.list(
30
+ @test_merchant_auth_token)
31
31
  expect(response).to_not be_success
32
32
  end
33
33
  end
@@ -3,8 +3,7 @@ require 'spec_helper'
3
3
  describe 'Levelup::Endpoints::QrCodes', vcr: true do
4
4
  describe '#get' do
5
5
  it 'retrieves a user\'s QR code' do
6
- response = @test_client.qr_codes.get(TestConfig.
7
- user_token_with_read_qr_perms)
6
+ response = @test_client.qr_codes.get(TestConfig.user_token_with_read_qr_perms)
8
7
  expect(response).to be_success
9
8
  expect(response.code).to_not be_nil
10
9
  end
@@ -4,8 +4,7 @@ describe 'Levelup::Endpoints::SpecificOrder', vcr: true do
4
4
  describe '#refund' do
5
5
  context 'with an invalid UUID' do
6
6
  it 'returns an error response' do
7
- response = @test_client.orders('0').
8
- refund(@merchant_token_with_create_privs)
7
+ response = @test_client.orders('0').refund(@merchant_token_with_create_privs)
9
8
  expect(response).to_not be_success
10
9
  end
11
10
  end
@@ -23,8 +22,8 @@ describe 'Levelup::Endpoints::SpecificOrder', vcr: true do
23
22
  )
24
23
  expect(order_response).to be_success
25
24
 
26
- refund_response = @test_client.orders(order_response.uuid).
27
- refund(TestConfig.merchant_token_with_manage_orders_perms)
25
+ refund_response = @test_client.orders(order_response.uuid).refund(
26
+ TestConfig.merchant_token_with_manage_orders_perms)
28
27
  expect(refund_response).to be_success
29
28
  expect(refund_response.uuid).to eq(order_response.uuid)
30
29
  end
@@ -15,8 +15,7 @@ describe 'Levelup::Endpoints::SpecificPermissionsRequest', vcr: true do
15
15
  permission_keynames: ['read_user_basic_info']
16
16
  )
17
17
 
18
- response = @test_client.apps.permissions_requests(perms_request.id).
19
- show
18
+ response = @test_client.apps.permissions_requests(perms_request.id).show
20
19
  expect(response).to be_success
21
20
  expect(response.permission_keynames).to eq(['read_user_basic_info'])
22
21
  end
@@ -33,8 +33,7 @@ describe 'Levelup::Endpoints::UserAddresses', vcr: true do
33
33
  TestConfig.user_token_with_manage_addresses_perms
34
34
  response = @test_client.user_addresses.create(test_request)
35
35
  expect(response).to be_success
36
- expect(response.street_address).
37
- to eq(@test_address_hash[:street_address])
36
+ expect(response.street_address).to eq(@test_address_hash[:street_address])
38
37
  end
39
38
  end
40
39
  end
@@ -3,8 +3,7 @@ require 'spec_helper'
3
3
  describe 'Levelup::Endpoints::UserOrders', vcr: true do
4
4
  describe '#list' do
5
5
  it 'returns a user\'s order history' do
6
- response = @test_client.users.orders.
7
- list(TestConfig.user_token_with_read_orders_perms)
6
+ response = @test_client.users.orders.list(TestConfig.user_token_with_read_orders_perms)
8
7
  expect(response).to be_success
9
8
  expect(response.orders).to_not be_nil
10
9
  end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Levelup::Endpoints::GetUsers', vcr: true do
4
+ describe '#get' do
5
+ context 'with an invalid access token' do
6
+ it 'returns an error response' do
7
+ response = @test_client.users.get('invalid')
8
+ expect(response).to_not be_success
9
+ end
10
+ end
11
+
12
+ context 'with a valid access token' do
13
+ it 'returns the user\'s info' do
14
+ response = @test_client.users.get(
15
+ TestConfig.user_token_with_read_info_perms)
16
+
17
+ expect(response).to be_success
18
+ expect(response.email).to_not be_nil
19
+ end
20
+ end
21
+ end
22
+ end
@@ -14,8 +14,7 @@ describe 'Levelup::Requests::RequestPermissions' do
14
14
 
15
15
  describe '#body' do
16
16
  it 'returns a hash wrapped in a permissions_request tag' do
17
- expect(@test_user_permissions_request.body[:permissions_request]).
18
- to_not be_nil
17
+ expect(@test_user_permissions_request.body[:permissions_request]).to eq({})
19
18
  end
20
19
  end
21
20
  end
@@ -35,8 +35,7 @@ describe 'Levelup::Responses::SuccessPaginated', vcr: true do
35
35
  describe '#next' do
36
36
  context 'with no next page' do
37
37
  it 'raises an InvalidRequest error' do
38
- expect { @no_next_response.next }.
39
- to raise_error(Levelup::Errors::InvalidRequest)
38
+ expect { @no_next_response.next }.to raise_error(Levelup::Errors::InvalidRequest)
40
39
  end
41
40
  end
42
41
 
@@ -0,0 +1,164 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Utils::PaymentCalculator::Calculate' do
4
+ describe '#calculate_levelup_discount_to_apply' do
5
+ context 'with merchant_funded_credit_available and no exemptions' do
6
+ it 'returns the proper amount of credit to apply' do
7
+ calculator = Levelup::Utils::PaymentCalculator.levelup_discount_to_apply(
8
+ check_total_due_including_tax: 110,
9
+ exempted_item_total: 0,
10
+ merchant_funded_credit_available: 50,
11
+ payment_amount_requested: 100,
12
+ tax_amount_due: 10
13
+ )
14
+ expect(calculator).to eq(50)
15
+ end
16
+ end
17
+
18
+ context 'with merchant_funded_credit_available with overriding exemptions' do
19
+ it 'returns the proper credit to apply minus exemptions' do
20
+ calculator = Levelup::Utils::PaymentCalculator.levelup_discount_to_apply(
21
+ check_total_due_including_tax: 110,
22
+ exempted_item_total: 80,
23
+ merchant_funded_credit_available: 50,
24
+ payment_amount_requested: 100,
25
+ tax_amount_due: 10
26
+ )
27
+ expect(calculator).to eq(20)
28
+ end
29
+ end
30
+
31
+ context 'with no merchant_funded_credit_available' do
32
+ it 'returns no credit to apply' do
33
+ calculator = Levelup::Utils::PaymentCalculator.levelup_discount_to_apply(
34
+ check_total_due_including_tax: 110,
35
+ exempted_item_total: 80,
36
+ merchant_funded_credit_available: 0,
37
+ payment_amount_requested: 100,
38
+ tax_amount_due: 10
39
+ )
40
+ expect(calculator).to eq(0)
41
+ end
42
+ end
43
+
44
+ context 'with more merchant_funded_credit available than amount due' do
45
+ it 'returns the max allowable mfc amount' do
46
+ calculator = Levelup::Utils::PaymentCalculator.levelup_discount_to_apply(
47
+ check_total_due_including_tax: 110,
48
+ exempted_item_total: 0,
49
+ merchant_funded_credit_available: 500,
50
+ payment_amount_requested: 100,
51
+ tax_amount_due: 10
52
+ )
53
+ expect(calculator).to eq(100)
54
+ end
55
+ end
56
+ end
57
+
58
+ describe '#calculate_tenders_and_tips' do
59
+ context 'with discount' do
60
+ it 'returns the proper tenders and values' do
61
+ calculator = Levelup::Utils::PaymentCalculator.new(
62
+ discount_applied: 1000,
63
+ gift_card_credit_available: 0,
64
+ spend_amount_returned_from_levelup: 1000,
65
+ tip_returned_from_levelup: 0
66
+ )
67
+ expect(calculator.gift_card_payment_to_apply).to be(0)
68
+ expect(calculator.gift_card_tip_to_apply).to be(0)
69
+ expect(calculator.levelup_payment_to_apply).to be(0)
70
+ expect(calculator.levelup_tip_to_apply).to be(0)
71
+ end
72
+ end
73
+
74
+ context 'with discount and gift card with tip' do
75
+ it 'returns the proper tenders and values' do
76
+ calculator = Levelup::Utils::PaymentCalculator.new(
77
+ discount_applied: 500,
78
+ gift_card_credit_available: 1000,
79
+ spend_amount_returned_from_levelup: 1000,
80
+ tip_returned_from_levelup: 500
81
+ )
82
+ expect(calculator.gift_card_payment_to_apply).to be(500)
83
+ expect(calculator.gift_card_tip_to_apply).to be(500)
84
+ expect(calculator.levelup_payment_to_apply).to be(0)
85
+ expect(calculator.levelup_tip_to_apply).to be(0)
86
+ end
87
+ end
88
+
89
+ context 'with discount and gift card and levelup tender' do
90
+ it 'returns the proper tenders and values' do
91
+ calculator = Levelup::Utils::PaymentCalculator.new(
92
+ discount_applied: 500,
93
+ gift_card_credit_available: 1000,
94
+ spend_amount_returned_from_levelup: 1000,
95
+ tip_returned_from_levelup: 500
96
+ )
97
+ expect(calculator.gift_card_payment_to_apply).to be(500)
98
+ expect(calculator.gift_card_tip_to_apply).to be(500)
99
+ expect(calculator.levelup_payment_to_apply).to be(0)
100
+ expect(calculator.levelup_tip_to_apply).to be(0)
101
+ end
102
+ end
103
+
104
+ context 'with discount, gift card, levelup tender and levelup tip' do
105
+ it 'returns the proper tenders and values' do
106
+ calculator = Levelup::Utils::PaymentCalculator.new(
107
+ discount_applied: 50,
108
+ gift_card_credit_available: 60,
109
+ spend_amount_returned_from_levelup: 150,
110
+ tip_returned_from_levelup: 50
111
+ )
112
+ expect(calculator.gift_card_payment_to_apply).to be(60)
113
+ expect(calculator.gift_card_tip_to_apply).to be(0)
114
+ expect(calculator.levelup_payment_to_apply).to be(40)
115
+ expect(calculator.levelup_tip_to_apply).to be(50)
116
+ end
117
+ end
118
+
119
+ context 'with gift card, levelup tender' do
120
+ it 'returns the proper tenders and values' do
121
+ calculator = Levelup::Utils::PaymentCalculator.new(
122
+ discount_applied: 0,
123
+ gift_card_credit_available: 500,
124
+ spend_amount_returned_from_levelup: 1000,
125
+ tip_returned_from_levelup: 0
126
+ )
127
+ expect(calculator.gift_card_payment_to_apply).to be(500)
128
+ expect(calculator.gift_card_tip_to_apply).to be(0)
129
+ expect(calculator.levelup_payment_to_apply).to be(500)
130
+ expect(calculator.levelup_tip_to_apply).to be(0)
131
+ end
132
+ end
133
+
134
+ context 'with gift card, levelup tender and levelup tip' do
135
+ it 'returns the proper tenders and values' do
136
+ calculator = Levelup::Utils::PaymentCalculator.new(
137
+ discount_applied: 0,
138
+ gift_card_credit_available: 500,
139
+ spend_amount_returned_from_levelup: 1000,
140
+ tip_returned_from_levelup: 500
141
+ )
142
+ expect(calculator.gift_card_payment_to_apply).to be(500)
143
+ expect(calculator.gift_card_tip_to_apply).to be(0)
144
+ expect(calculator.levelup_payment_to_apply).to be(500)
145
+ expect(calculator.levelup_tip_to_apply).to be(500)
146
+ end
147
+ end
148
+
149
+ context 'with levelup tender and levelup tip' do
150
+ it 'returns the proper tenders and values' do
151
+ calculator = Levelup::Utils::PaymentCalculator.new(
152
+ discount_applied: 500,
153
+ gift_card_credit_available: 0,
154
+ spend_amount_returned_from_levelup: 2000,
155
+ tip_returned_from_levelup: 500
156
+ )
157
+ expect(calculator.gift_card_payment_to_apply).to be(0)
158
+ expect(calculator.gift_card_tip_to_apply).to be(0)
159
+ expect(calculator.levelup_payment_to_apply).to be(1500)
160
+ expect(calculator.levelup_tip_to_apply).to be(500)
161
+ end
162
+ end
163
+ end
164
+ end