workarea-gift_cards 3.4.10 → 4.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +5 -5
  2. data/{.eslintrc → .eslintrc.json} +12 -1
  3. data/.github/workflows/ci.yml +10 -8
  4. data/.rubocop.yml +2 -197
  5. data/.stylelintrc.json +8 -0
  6. data/CHANGELOG.md +40 -20
  7. data/Gemfile +3 -5
  8. data/README.md +25 -1
  9. data/app/controllers/workarea/api/admin/gift_cards_controller.rb +309 -0
  10. data/app/controllers/workarea/api/storefront/checkouts_controller.decorator +6 -0
  11. data/app/controllers/workarea/api/storefront/gift_cards_controller.rb +2 -8
  12. data/app/controllers/workarea/storefront/checkout/gift_cards_controller.rb +32 -0
  13. data/app/controllers/workarea/storefront/gift_cards_controller.rb +15 -16
  14. data/app/models/workarea/checkout/steps/gift_card.rb +72 -0
  15. data/app/models/workarea/fulfillment/policies/create_gift_card.rb +30 -0
  16. data/app/models/workarea/payment.decorator +25 -7
  17. data/app/models/workarea/payment/authorize/gift_card.rb +7 -20
  18. data/app/models/workarea/payment/capture/gift_card.rb +5 -7
  19. data/app/models/workarea/payment/gift_card.rb +1 -1
  20. data/app/models/workarea/payment/gift_card_operation.rb +9 -0
  21. data/app/models/workarea/payment/purchase/gift_card.rb +24 -1
  22. data/app/models/workarea/payment/refund/gift_card.rb +9 -14
  23. data/app/models/workarea/payment/tender/gift_card.rb +3 -10
  24. data/app/models/workarea/search/admin/order.decorator +2 -4
  25. data/app/seeds/workarea/gift_card_seeds.rb +7 -7
  26. data/app/view_models/workarea/storefront/gift_card_order_pricing.rb +19 -20
  27. data/app/view_models/workarea/storefront/order_view_model.decorator +2 -22
  28. data/app/views/workarea/admin/payment_gift_cards/_menu.html.haml +2 -1
  29. data/app/views/workarea/storefront/checkouts/_gift_card_error.html.haml +8 -6
  30. data/app/views/workarea/storefront/checkouts/_gift_card_payment.html.haml +20 -12
  31. data/app/views/workarea/storefront/gift_card_mailer/created.html.haml +1 -1
  32. data/app/views/workarea/storefront/order_mailer/_gift_card_summary.html.haml +6 -5
  33. data/app/views/workarea/storefront/orders/_gift_card_summary.html.haml +2 -2
  34. data/app/views/workarea/storefront/products/templates/_gift_card.html.haml +5 -5
  35. data/app/workers/workarea/log_gift_card_redemption.rb +16 -9
  36. data/config/initializers/configuration.rb +7 -4
  37. data/config/initializers/fields.rb +14 -0
  38. data/config/locales/en.yml +14 -4
  39. data/config/routes.rb +9 -2
  40. data/lib/workarea/gift_cards.rb +9 -0
  41. data/lib/workarea/gift_cards/gateway.rb +128 -0
  42. data/lib/workarea/gift_cards/version.rb +1 -1
  43. data/test/documentation/workarea/api/admin/gift_cards_documentation_test.rb +24 -10
  44. data/test/documentation/workarea/api/storefront/gift_cards_documentation_test.rb +57 -50
  45. data/test/dummy/config/initializers/session_store.rb +1 -1
  46. data/test/integration/workarea/api/admin/gift_card_integration_test.rb +13 -5
  47. data/test/integration/workarea/api/storefront/checkout_gift_cards_integration_test.rb +120 -0
  48. data/test/integration/workarea/api/storefront/{balance_integration_test.rb → gift_cards_integration_test.rb} +1 -1
  49. data/test/integration/workarea/storefront/checkout_gift_cards_integration_test.rb +116 -0
  50. data/test/integration/workarea/storefront/gift_cards_integration_test.rb +48 -0
  51. data/test/integration/workarea/storefront/purchase_gift_cards_integration_test.rb +55 -0
  52. data/test/lib/workarea/gift_cards/gateway_test.rb +131 -0
  53. data/test/models/workarea/checkout/steps/gift_card_test.rb +104 -0
  54. data/test/models/workarea/fulfillment/policies/create_gift_card_test.rb +47 -0
  55. data/test/models/workarea/gift_card_payment_test.rb +15 -3
  56. data/test/models/workarea/payment/purchase/gift_card_test.rb +11 -8
  57. data/test/queries/workarea/admin_redemptions_export_test.rb +31 -35
  58. data/test/system/workarea/storefront/gift_cards_system_test.rb +8 -2
  59. data/test/view_models/workarea/storefront/gift_card_order_pricing_test.rb +113 -0
  60. data/test/workers/workarea/log_gift_card_redemption_test.rb +1 -1
  61. data/workarea-gift_cards.gemspec +4 -4
  62. metadata +32 -25
  63. data/.eslintignore +0 -2
  64. data/.scss-lint.yml +0 -188
  65. data/app/controllers/workarea/api/admin/payment_gift_cards_controller.rb +0 -49
  66. data/app/controllers/workarea/storefront/checkouts_controller.decorator +0 -24
  67. data/app/services/workarea/checkout/steps/gift_card.rb +0 -49
  68. data/app/view_models/workarea/storefront/checkout/payment_view_model.decorator +0 -32
  69. data/app/view_models/workarea/storefront/checkout/summary_view_model.decorator +0 -11
  70. data/app/workers/workarea/create_ordered_gift_cards.rb +0 -47
  71. data/config/initializers/jump_to_navigation.rb +0 -3
  72. data/test/integration/workarea/api/storefront/checkout_integration_test.rb +0 -161
  73. data/test/integration/workarea/storefront/gift_card_integration_test.rb +0 -226
  74. data/test/services/workarea/checkout/steps/gift_card_test.rb +0 -87
  75. data/test/view_models/workarea/storefront/checkout/gift_card_payment_view_model_test.rb +0 -65
  76. data/test/workers/workarea/create_ordered_gift_cards_test.rb +0 -39
@@ -0,0 +1,48 @@
1
+ require 'test_helper'
2
+
3
+ module Workarea
4
+ module Storefront
5
+ class GiftCardsIntegrationTest < Workarea::IntegrationTest
6
+ def test_balance_lookup
7
+ gift_card = create_gift_card(to: 'test@workarea.com', amount: 12.to_m)
8
+
9
+ get storefront.gift_cards_lookup_path,
10
+ params: {
11
+ email: gift_card.to,
12
+ token: gift_card.token
13
+ }
14
+
15
+ assert_redirected_to(storefront.gift_cards_balance_path)
16
+ assert_equal(
17
+ t(
18
+ 'workarea.storefront.flash_messages.gift_card_balance',
19
+ balance: gift_card.balance.format
20
+ ),
21
+ flash[:success]
22
+ )
23
+
24
+ get storefront.gift_cards_lookup_path,
25
+ params: {
26
+ email: 'foo@workarea.com',
27
+ token: gift_card.token
28
+ }
29
+
30
+ assert_equal(
31
+ t('workarea.storefront.flash_messages.gift_card_not_found'),
32
+ flash[:error]
33
+ )
34
+
35
+ get storefront.gift_cards_lookup_path,
36
+ params: {
37
+ email: gift_card.to,
38
+ token: '125'
39
+ }
40
+
41
+ assert_equal(
42
+ t('workarea.storefront.flash_messages.gift_card_not_found'),
43
+ flash[:error]
44
+ )
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,55 @@
1
+ require 'test_helper'
2
+
3
+ module Workarea
4
+ module Storefront
5
+ class PurchaseGiftCardsIntegrationTest < Workarea::IntegrationTest
6
+ include Storefront::IntegrationTest
7
+
8
+ def test_generates_gift_card
9
+ pass && (return) unless Workarea::GiftCards.uses_system_cards?
10
+
11
+ product = create_product(
12
+ name: 'Gift Card',
13
+ gift_card: true,
14
+ customizations: 'gift_card',
15
+ variants: [{ sku: 'GIFTCARD', regular: 10.to_m }]
16
+ )
17
+
18
+ create_fulfillment_sku(id: 'GIFTCARD', policy: :create_gift_card)
19
+
20
+ post storefront.cart_items_path,
21
+ params: {
22
+ product_id: product.id,
23
+ sku: product.skus.first,
24
+ email: 'recipient@workarea.com',
25
+ from: 'from@workarea.com',
26
+ message: 'this is a message',
27
+ quantity: 1
28
+ }
29
+
30
+ complete_checkout
31
+
32
+ assert_equal(1, Payment::GiftCard.count)
33
+
34
+ card = Payment::GiftCard.first
35
+ assert_equal(10.to_m, card.amount)
36
+ assert_equal(0.to_m, card.used)
37
+ assert_equal(10.to_m, card.balance)
38
+ assert(card.order_id.present?)
39
+ assert(card.purchased?)
40
+
41
+ assert_equal(2, ActionMailer::Base.deliveries.length)
42
+
43
+ gift_card_email = ActionMailer::Base.deliveries.detect do |email|
44
+ assert_includes(email.to, 'recipient@workarea.com')
45
+ end
46
+
47
+ gift_card_email.parts.each do |part|
48
+ assert_includes(part.body, card.token)
49
+ assert_includes(part.body, 'from@workarea.com')
50
+ assert_includes(part.body, 'this is a message')
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,131 @@
1
+ require 'test_helper'
2
+
3
+ module Workarea
4
+ module GiftCards
5
+ class GatewayTest < TestCase
6
+ setup :gift_card
7
+
8
+ # Override this to instantiate an object that models a gift card
9
+ # on the remote system.
10
+ def gift_card
11
+ @gift_card ||= create_gift_card(amount: 10.to_m)
12
+ end
13
+
14
+ # The tender object used on an order that will be passed into each
15
+ # gateway method call. Override this to provide your own
16
+ # information.
17
+ #
18
+ # @return [Payment::Tender::GiftCard]
19
+ def tender
20
+ @tender ||= Payment::Tender::GiftCard.new(
21
+ number: gift_card.token,
22
+ amount: 5.to_m
23
+ )
24
+ end
25
+
26
+ # Override this in your test to instantiate a gateway for use in
27
+ # tests. The class name of the gateway is used to generate the
28
+ # folder name for the VCR cassettes used within this test.
29
+ def gateway
30
+ Gateway.new
31
+ end
32
+
33
+ # Use the class name of the gateway to generate a directory in
34
+ # `test/vcr_cassettes` where all cassettes for the custom gateway
35
+ # will go. This doesn't need to be overridden unless you need to
36
+ # customize the gateway cassette directory name.
37
+ #
38
+ # @return [String]
39
+ def gateway_cassette_name
40
+ gateway.class.name.systemize
41
+ end
42
+
43
+ def test_authorize
44
+ VCR.use_cassette "#{gateway_cassette_name}/authorize" do
45
+ response = gateway.authorize(200, tender)
46
+
47
+ assert(response.success?)
48
+ assert(response.message.present?)
49
+
50
+ gift_card.reload
51
+ assert_equal(8.to_m, gift_card.balance)
52
+ assert_equal(2.to_m, gift_card.used)
53
+ end
54
+ end
55
+
56
+ def test_cancel
57
+ VCR.use_cassette "#{gateway_cassette_name}/cancel" do
58
+ response = gateway.authorize(200, tender)
59
+ assert(response.success?)
60
+
61
+ response = gateway.cancel(200, tender)
62
+
63
+ assert(response.success?)
64
+ assert(response.message.present?)
65
+
66
+ gift_card.reload
67
+ assert_equal(10.to_m, gift_card.balance)
68
+ assert_equal(0.to_m, gift_card.used)
69
+ end
70
+ end
71
+
72
+ def test_capture
73
+ VCR.use_cassette "#{gateway_cassette_name}/capture" do
74
+ assert(gateway.capture(200, tender).success?)
75
+ assert_equal(10.to_m, gift_card.reload.balance)
76
+ end
77
+ end
78
+
79
+ def test_purchase
80
+ VCR.use_cassette "#{gateway_cassette_name}/purchase" do
81
+ response = gateway.purchase(200, tender)
82
+
83
+ assert(response.success?)
84
+ assert(response.message.present?)
85
+
86
+ gift_card.reload
87
+ assert_equal(8.to_m, gift_card.balance)
88
+ assert_equal(2.to_m, gift_card.used)
89
+ end
90
+ end
91
+
92
+ def test_refund
93
+ VCR.use_cassette "#{gateway_cassette_name}/refund" do
94
+ response = gateway.authorize(200, tender)
95
+ assert(response.success?)
96
+
97
+ response = gateway.refund(200, tender)
98
+
99
+ assert(response.success?)
100
+ assert(response.message.present?)
101
+
102
+ gift_card.reload
103
+ assert_equal(10.to_m, gift_card.balance)
104
+ assert_equal(0.to_m, gift_card.used)
105
+ end
106
+ end
107
+
108
+ def test_balance
109
+ VCR.use_cassette "#{gateway_cassette_name}/balance" do
110
+ balance = gateway.balance(gift_card.number)
111
+ assert_equal(10.to_m, balance)
112
+
113
+ gateway.authorize(300, tender)
114
+
115
+ balance = gateway.balance(gift_card.number)
116
+ assert_equal(7.to_m, balance)
117
+ end
118
+ end
119
+
120
+ def test_lookup
121
+ VCR.use_cassette "#{gateway_cassette_name}/lookup" do
122
+ lookup = gateway.lookup(email: gift_card.to, token: gift_card.token)
123
+ assert_equal(gift_card.id, lookup.id)
124
+
125
+ lookup = gateway.lookup(email: 'foo', token: gift_card.token)
126
+ assert_nil(lookup)
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,104 @@
1
+ require 'test_helper'
2
+
3
+ module Workarea
4
+ class Checkout
5
+ module Steps
6
+ class GiftCardTest < TestCase
7
+ setup :setup_checkout
8
+
9
+ def setup_checkout
10
+ create_shipping_service(name: 'Ground', rates: [{ price: 7.to_m }])
11
+
12
+ product = create_product(
13
+ name: 'Integration Product',
14
+ variants: [{ sku: 'SKU1', regular: 5.to_m }]
15
+ )
16
+
17
+ @order = create_order.tap do |order|
18
+ order.add_item(
19
+ product_id: product.id,
20
+ sku: product.skus.first,
21
+ quantity: 2
22
+ )
23
+ end
24
+
25
+ @checkout = Checkout.new(@order)
26
+ @checkout.update(
27
+ email: 'test@workarea.com',
28
+ billing_address: factory_defaults(:billing_address),
29
+ shipping_address: factory_defaults(:shipping_address),
30
+ shipping_service: 'Ground'
31
+ )
32
+
33
+ @payment = @checkout.payment
34
+ end
35
+
36
+ def test_update
37
+ card_one = create_gift_card(amount: 10.to_m)
38
+ card_two = create_gift_card(amount: 20.to_m)
39
+
40
+ step = Checkout::Steps::GiftCard.new(@checkout)
41
+ assert(step.update(gift_card_number: " #{card_one.number} "))
42
+
43
+ @payment.reload
44
+ assert_equal(1, @payment.gift_cards.count)
45
+
46
+ tender = @payment.gift_cards.first
47
+ assert_equal(card_one.token, tender.number)
48
+ assert_equal(10.to_m, tender.amount)
49
+
50
+ @order.reload
51
+ assert_equal(17.to_m, @order.total_price)
52
+
53
+ step = Checkout::Steps::GiftCard.new(@checkout)
54
+ assert(step.update(gift_card_number: card_two.number))
55
+
56
+ @payment.reload
57
+ assert_equal(2, @payment.gift_cards.count)
58
+
59
+ tender = @payment.gift_cards.first
60
+ assert_equal(card_one.token, tender.number)
61
+ assert_equal(10.to_m, tender.amount)
62
+
63
+ tender = @payment.gift_cards.last
64
+ assert_equal(card_two.token, tender.number)
65
+ assert_equal(7.to_m, tender.amount)
66
+ end
67
+
68
+ def test_remove
69
+ card_one = create_gift_card(amount: 10.to_m)
70
+ card_two = create_gift_card(amount: 20.to_m)
71
+
72
+ step = Checkout::Steps::GiftCard.new(@checkout)
73
+ step.update(gift_card_number: card_one.token)
74
+ step.update(gift_card_number: card_two.token)
75
+
76
+ assert_equal(2, @payment.reload.gift_cards.count)
77
+ assert_equal(10.to_m, @payment.gift_cards.first.amount)
78
+ assert_equal(7.to_m, @payment.gift_cards.last.amount)
79
+
80
+ step.remove(@payment.gift_cards.first)
81
+ assert_equal(1, @payment.reload.gift_cards.count)
82
+ assert_equal(card_two.token, @payment.gift_cards.first.number)
83
+ assert_equal(17.to_m, @payment.gift_cards.first.amount)
84
+ end
85
+
86
+ def test_complete?
87
+ card = create_gift_card(amount: 10.to_m)
88
+ step = Checkout::Steps::GiftCard.new(@checkout)
89
+
90
+ assert(step.complete?)
91
+
92
+ @payment.add_gift_card(number: card.number)
93
+ assert(step.complete?)
94
+
95
+ @payment.add_gift_card(number: '')
96
+ refute(step.complete?)
97
+
98
+ @payment.reload.add_gift_card(number: '1234')
99
+ refute(step.complete?)
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,47 @@
1
+ require 'test_helper'
2
+
3
+ module Workarea
4
+ class Fulfillment
5
+ module Policies
6
+ class CreateGiftCardTest < TestCase
7
+ def test_process
8
+ order = create_order
9
+ order.add_item(
10
+ product_id: 'PROD',
11
+ sku: 'SKU',
12
+ gift_card: true,
13
+ fulfillment: 'create_gift_card',
14
+ customizations: {
15
+ 'email' => 'bob@workarea.com',
16
+ 'from' => 'system@workarea.com',
17
+ 'message' => 'Hello'
18
+ },
19
+ price_adjustments: [{ amount: 10.to_m }]
20
+ )
21
+
22
+ fulfillment = Fulfillment.new(
23
+ id: order.id,
24
+ items: [{ order_item_id: order.items.first.id, quantity: 1 }]
25
+ )
26
+
27
+ sku = create_fulfillment_sku(id: 'SKU', policy: :create_gift_card)
28
+ policy = CreateGiftCard.new(sku)
29
+
30
+ policy.process(order_item: order.items.first, fulfillment: fulfillment)
31
+
32
+ card = Payment::GiftCard.first
33
+ assert(card.present?)
34
+ assert(card.notify)
35
+ assert(card.purchased)
36
+ assert_equal('bob@workarea.com', card.to)
37
+ assert_equal('system@workarea.com', card.from)
38
+ assert_equal('Hello', card.message)
39
+ assert_equal(order.id, card.order_id)
40
+ assert_equal(10.to_m, card.amount)
41
+
42
+ assert_equal(1, fulfillment.items.first.quantity_shipped)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -2,11 +2,23 @@ require 'test_helper'
2
2
 
3
3
  module Workarea
4
4
  class GiftCardPaymentTest < TestCase
5
- def test_set_gift_card
5
+ def test_add_gift_card
6
+ Workarea.config.max_gift_cards_per_order = 2
7
+
6
8
  payment = create_payment
7
9
 
8
- payment.set_gift_card(number: '1234')
9
- assert_equal('1234', payment.gift_card.number)
10
+ assert(payment.add_gift_card(number: '1234'))
11
+ assert_equal(1, payment.gift_cards.count)
12
+ assert_equal('1234', payment.gift_cards.first.number)
13
+
14
+ assert(payment.add_gift_card(number: '1234'))
15
+ assert_equal(1, payment.gift_cards.count)
16
+
17
+ assert(payment.add_gift_card(number: '3456'))
18
+ assert_equal(2, payment.gift_cards.count)
19
+
20
+ refute(payment.add_gift_card(number: '7890'))
21
+ assert_equal(2, payment.gift_cards.count)
10
22
  end
11
23
  end
12
24
  end
@@ -15,16 +15,17 @@ module Workarea
15
15
  end
16
16
 
17
17
  def test_complete!
18
- payment.set_gift_card(number: 'token')
18
+ payment.add_gift_card(number: 'token')
19
+ gift_card = payment.gift_cards.first
19
20
 
20
- transaction = payment.gift_card.build_transaction(amount: 10.to_m)
21
+ transaction = gift_card.build_transaction(amount: 10.to_m)
21
22
 
22
- GiftCard.new(payment.gift_card, transaction).complete!
23
+ GiftCard.new(gift_card, transaction).complete!
23
24
  refute(transaction.success?)
24
25
  assert(transaction.message.present?)
25
26
 
26
- transaction = payment.gift_card.build_transaction(amount: 3.to_m)
27
- purchase = GiftCard.new(payment.gift_card, transaction)
27
+ transaction = gift_card.build_transaction(amount: 3.to_m)
28
+ purchase = GiftCard.new(gift_card, transaction)
28
29
  purchase.complete!
29
30
 
30
31
  assert(transaction.success?)
@@ -32,10 +33,12 @@ module Workarea
32
33
  end
33
34
 
34
35
  def test_cancel!
35
- payment.set_gift_card(number: 'token')
36
- transaction = payment.gift_card.build_transaction(amount: 5.to_m)
36
+ payment.add_gift_card(number: 'token')
37
+ gift_card = payment.gift_cards.first
37
38
 
38
- purchase = GiftCard.new(payment.gift_card, transaction)
39
+ transaction = gift_card.build_transaction(amount: 5.to_m)
40
+
41
+ purchase = GiftCard.new(gift_card, transaction)
39
42
  purchase.cancel!
40
43
 
41
44
  refute(transaction.cancellation.present?)