workarea-gift_cards 3.4.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (166) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +20 -0
  3. data/.eslintignore +2 -0
  4. data/.eslintrc +24 -0
  5. data/.github/ISSUE_TEMPLATE/bug_report.md +37 -0
  6. data/.github/ISSUE_TEMPLATE/documentation-request.md +17 -0
  7. data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  8. data/.gitignore +15 -0
  9. data/.scss-lint.yml +188 -0
  10. data/.yardopts +1 -0
  11. data/CHANGELOG.md +485 -0
  12. data/CODE_OF_CONDUCT.md +3 -0
  13. data/CONTRIBUTING.md +3 -0
  14. data/Gemfile +7 -0
  15. data/LICENSE +52 -0
  16. data/README.md +119 -0
  17. data/Rakefile +53 -0
  18. data/app/assets/images/workarea/admin/payment_icons/gift_card.svg +1 -0
  19. data/app/assets/images/workarea/storefront/payment_icons/gift_card.svg +1 -0
  20. data/app/controllers/workarea/admin/data_file_exports_controller.decorator +28 -0
  21. data/app/controllers/workarea/admin/payment_gift_cards_controller.rb +78 -0
  22. data/app/controllers/workarea/api/admin/payment_gift_cards_controller.rb +49 -0
  23. data/app/controllers/workarea/api/storefront/checkouts_controller.decorator +10 -0
  24. data/app/controllers/workarea/api/storefront/gift_cards_controller.rb +26 -0
  25. data/app/controllers/workarea/storefront/checkouts_controller.decorator +24 -0
  26. data/app/controllers/workarea/storefront/gift_cards_controller.rb +24 -0
  27. data/app/mailers/workarea/storefront/gift_card_mailer.rb +28 -0
  28. data/app/models/workarea/catalog/customizations/gift_card.rb +9 -0
  29. data/app/models/workarea/catalog/product.decorator +11 -0
  30. data/app/models/workarea/order/item.decorator +7 -0
  31. data/app/models/workarea/payment/authorize/gift_card.rb +43 -0
  32. data/app/models/workarea/payment/capture/gift_card.rb +25 -0
  33. data/app/models/workarea/payment/gift_card/redemption.rb +23 -0
  34. data/app/models/workarea/payment/gift_card.rb +186 -0
  35. data/app/models/workarea/payment/purchase/gift_card.rb +7 -0
  36. data/app/models/workarea/payment/refund/gift_card.rb +35 -0
  37. data/app/models/workarea/payment/tender/gift_card.rb +30 -0
  38. data/app/models/workarea/payment.decorator +19 -0
  39. data/app/models/workarea/search/admin/order.decorator +9 -0
  40. data/app/models/workarea/search/admin/payment_gift_card.rb +46 -0
  41. data/app/queries/workarea/admin_redemptions_export.rb +84 -0
  42. data/app/queries/workarea/order_item_details.decorator +11 -0
  43. data/app/queries/workarea/search/admin_gift_cards.rb +20 -0
  44. data/app/seeds/workarea/gift_card_seeds.rb +44 -0
  45. data/app/services/workarea/checkout/steps/gift_card.rb +49 -0
  46. data/app/view_models/workarea/admin/payment_gift_card_view_model.rb +11 -0
  47. data/app/view_models/workarea/storefront/checkout/payment_view_model.decorator +32 -0
  48. data/app/view_models/workarea/storefront/checkout/summary_view_model.decorator +11 -0
  49. data/app/view_models/workarea/storefront/gift_card_order_pricing.rb +36 -0
  50. data/app/view_models/workarea/storefront/order_view_model.decorator +27 -0
  51. data/app/view_models/workarea/storefront/product_templates/gift_card_view_model.rb +4 -0
  52. data/app/views/workarea/admin/activities/_payment_gift_card_create.html.haml +12 -0
  53. data/app/views/workarea/admin/activities/_payment_gift_card_destroy.html.haml +10 -0
  54. data/app/views/workarea/admin/activities/_payment_gift_card_update.html.haml +14 -0
  55. data/app/views/workarea/admin/orders/tenders/_gift_card.html.haml +5 -0
  56. data/app/views/workarea/admin/payment_gift_cards/_aux_navigation.html.haml +4 -0
  57. data/app/views/workarea/admin/payment_gift_cards/_cards.html.haml +74 -0
  58. data/app/views/workarea/admin/payment_gift_cards/_menu.html.haml +1 -0
  59. data/app/views/workarea/admin/payment_gift_cards/_summary.html.haml +11 -0
  60. data/app/views/workarea/admin/payment_gift_cards/edit.html.haml +37 -0
  61. data/app/views/workarea/admin/payment_gift_cards/index.html.haml +95 -0
  62. data/app/views/workarea/admin/payment_gift_cards/new.html.haml +43 -0
  63. data/app/views/workarea/admin/payment_gift_cards/redemptions.html.haml +29 -0
  64. data/app/views/workarea/admin/payment_gift_cards/show.html.haml +19 -0
  65. data/app/views/workarea/api/storefront/checkouts/steps/_gift_card.json.jbuilder +6 -0
  66. data/app/views/workarea/api/storefront/gift_cards/balance.json.jbuilder +3 -0
  67. data/app/views/workarea/api/storefront/orders/tenders/_gift_card.json.jbuilder +3 -0
  68. data/app/views/workarea/storefront/checkouts/_gift_card_error.html.haml +6 -0
  69. data/app/views/workarea/storefront/checkouts/_gift_card_payment.html.haml +28 -0
  70. data/app/views/workarea/storefront/checkouts/_gift_card_summary.html.haml +6 -0
  71. data/app/views/workarea/storefront/gift_card_mailer/created.html.haml +15 -0
  72. data/app/views/workarea/storefront/gift_cards/index.html.haml +17 -0
  73. data/app/views/workarea/storefront/order_mailer/_gift_card_summary.html.haml +5 -0
  74. data/app/views/workarea/storefront/order_mailer/tenders/_gift_card.html.haml +5 -0
  75. data/app/views/workarea/storefront/orders/_gift_card_summary.html.haml +6 -0
  76. data/app/views/workarea/storefront/orders/tenders/_gift_card.html.haml +9 -0
  77. data/app/views/workarea/storefront/products/templates/_gift_card.html.haml +77 -0
  78. data/app/workers/workarea/create_ordered_gift_cards.rb +47 -0
  79. data/app/workers/workarea/log_gift_card_redemption.rb +32 -0
  80. data/app/workers/workarea/send_gift_card_notifications.rb +20 -0
  81. data/bin/rails +18 -0
  82. data/config/initializers/append_points.rb +37 -0
  83. data/config/initializers/configuration.rb +8 -0
  84. data/config/initializers/jump_to_navigation.rb +3 -0
  85. data/config/locales/en.yml +82 -0
  86. data/config/routes.rb +31 -0
  87. data/lib/workarea/gift_cards/engine.rb +19 -0
  88. data/lib/workarea/gift_cards/version.rb +5 -0
  89. data/lib/workarea/gift_cards.rb +11 -0
  90. data/lib/workarea/mailer_previews/storefront/gift_card_mailer_preview.rb +18 -0
  91. data/test/documentation/workarea/api/admin/gift_cards_documentation_test.rb +82 -0
  92. data/test/documentation/workarea/api/storefront/gift_cards_documentation_test.rb +107 -0
  93. data/test/dummy/Rakefile +6 -0
  94. data/test/dummy/app/assets/config/manifest.js +4 -0
  95. data/test/dummy/app/assets/images/.keep +0 -0
  96. data/test/dummy/app/assets/javascripts/application.js +13 -0
  97. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  98. data/test/dummy/app/controllers/application_controller.rb +3 -0
  99. data/test/dummy/app/controllers/concerns/.keep +0 -0
  100. data/test/dummy/app/helpers/application_helper.rb +2 -0
  101. data/test/dummy/app/jobs/application_job.rb +2 -0
  102. data/test/dummy/app/mailers/application_mailer.rb +4 -0
  103. data/test/dummy/app/models/concerns/.keep +0 -0
  104. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  105. data/test/dummy/app/views/layouts/mailer.html.erb +13 -0
  106. data/test/dummy/app/views/layouts/mailer.text.erb +1 -0
  107. data/test/dummy/bin/bundle +3 -0
  108. data/test/dummy/bin/rails +4 -0
  109. data/test/dummy/bin/rake +4 -0
  110. data/test/dummy/bin/setup +34 -0
  111. data/test/dummy/bin/update +29 -0
  112. data/test/dummy/config/application.rb +24 -0
  113. data/test/dummy/config/boot.rb +5 -0
  114. data/test/dummy/config/cable.yml +9 -0
  115. data/test/dummy/config/environment.rb +5 -0
  116. data/test/dummy/config/environments/development.rb +54 -0
  117. data/test/dummy/config/environments/production.rb +86 -0
  118. data/test/dummy/config/environments/test.rb +43 -0
  119. data/test/dummy/config/initializers/application_controller_renderer.rb +6 -0
  120. data/test/dummy/config/initializers/assets.rb +11 -0
  121. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  122. data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
  123. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  124. data/test/dummy/config/initializers/inflections.rb +16 -0
  125. data/test/dummy/config/initializers/mime_types.rb +4 -0
  126. data/test/dummy/config/initializers/new_framework_defaults.rb +21 -0
  127. data/test/dummy/config/initializers/session_store.rb +3 -0
  128. data/test/dummy/config/initializers/workarea.rb +5 -0
  129. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  130. data/test/dummy/config/locales/en.yml +23 -0
  131. data/test/dummy/config/puma.rb +47 -0
  132. data/test/dummy/config/routes.rb +6 -0
  133. data/test/dummy/config/secrets.yml +22 -0
  134. data/test/dummy/config/spring.rb +6 -0
  135. data/test/dummy/config.ru +5 -0
  136. data/test/dummy/lib/assets/.keep +0 -0
  137. data/test/dummy/log/.keep +0 -0
  138. data/test/dummy/public/404.html +67 -0
  139. data/test/dummy/public/422.html +67 -0
  140. data/test/dummy/public/500.html +66 -0
  141. data/test/dummy/public/apple-touch-icon-precomposed.png +0 -0
  142. data/test/dummy/public/apple-touch-icon.png +0 -0
  143. data/test/dummy/public/favicon.ico +0 -0
  144. data/test/factories/gift_cards.rb +27 -0
  145. data/test/integration/workarea/admin/gift_card_integration_test.rb +53 -0
  146. data/test/integration/workarea/admin/gift_card_redemptions_export_integration_test.rb +104 -0
  147. data/test/integration/workarea/api/admin/gift_card_integration_test.rb +57 -0
  148. data/test/integration/workarea/api/storefront/balance_integration_test.rb +40 -0
  149. data/test/integration/workarea/api/storefront/checkout_integration_test.rb +161 -0
  150. data/test/integration/workarea/storefront/gift_card_integration_test.rb +222 -0
  151. data/test/models/workarea/catalog/gift_card_product_test.rb +15 -0
  152. data/test/models/workarea/gift_card_payment_test.rb +12 -0
  153. data/test/models/workarea/payment/gift_card_test.rb +127 -0
  154. data/test/models/workarea/payment/purchase/gift_card_test.rb +52 -0
  155. data/test/models/workarea/payment/refund/gift_card_test.rb +19 -0
  156. data/test/queries/workarea/admin_redemptions_export_test.rb +52 -0
  157. data/test/queries/workarea/gift_card_order_item_details_test.rb +11 -0
  158. data/test/services/workarea/checkout/steps/gift_card_test.rb +87 -0
  159. data/test/system/workarea/admin/gift_cards_system_test.rb +89 -0
  160. data/test/system/workarea/storefront/gift_cards_system_test.rb +400 -0
  161. data/test/test_helper.rb +10 -0
  162. data/test/view_models/workarea/storefront/checkout/gift_card_payment_view_model_test.rb +65 -0
  163. data/test/workers/workarea/create_ordered_gift_cards_test.rb +39 -0
  164. data/test/workers/workarea/log_gift_card_redemption_test.rb +19 -0
  165. data/workarea-gift_cards.gemspec +21 -0
  166. metadata +227 -0
@@ -0,0 +1,57 @@
1
+ require 'test_helper'
2
+
3
+ module Workarea
4
+ if Plugin.installed?(:api)
5
+ module Api
6
+ module Admin
7
+ class GiftCardIntegrationTest < IntegrationTest
8
+ include Workarea::Admin::IntegrationTest
9
+ setup :set_sample_attributes
10
+
11
+ def set_sample_attributes
12
+ @sample_attributes = create_gift_card.as_json.except('_id')
13
+ end
14
+
15
+ def test_lists_gift_cards
16
+ gift_cards = [create_gift_card(to: 'test@email.com'), create_gift_card(to: 'foo@weblinc.com')]
17
+ get admin_api.payment_gift_cards_path
18
+ result = JSON.parse(response.body)['gift_cards']
19
+
20
+ assert_equal(3, result.length)
21
+ assert_equal(gift_cards.second, Payment::GiftCard.new(result.first))
22
+ assert_equal(gift_cards.first, Payment::GiftCard.new(result.second))
23
+ end
24
+
25
+ def test_creates_gift_cards
26
+ assert_difference 'Payment::GiftCard.count', 1 do
27
+ post admin_api.payment_gift_cards_path,
28
+ params: { gift_card: @sample_attributes.merge('to' => 'test@email.com').except('token') }
29
+ end
30
+ end
31
+
32
+ def test_shows_gift_cards
33
+ gift_card = create_gift_card(to: 'foo@weblinc.com')
34
+ get admin_api.payment_gift_card_path(gift_card.id)
35
+ result = JSON.parse(response.body)['gift_card']
36
+ assert_equal(gift_card, Payment::GiftCard.new(result))
37
+ end
38
+
39
+ def test_updates_gift_cards
40
+ gift_card = create_gift_card(to: 'foo@weblinc.com')
41
+ patch admin_api.payment_gift_card_path(gift_card.id),
42
+ params: { gift_card: { to: 'bar@weblinc.com' } }
43
+
44
+ assert_equal('bar@weblinc.com', gift_card.reload.to)
45
+ end
46
+
47
+ def test_destroys_gift_cards
48
+ gift_card = create_gift_card(to: 'foo@weblinc.com')
49
+ assert_difference 'Payment::GiftCard.count', -1 do
50
+ delete admin_api.payment_gift_card_path(gift_card.id)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ module Workarea
4
+ if Plugin.installed?(:api)
5
+ module Api
6
+ module Storefront
7
+ class BalanceIntegrationTest < Workarea::IntegrationTest
8
+ def test_balance_lookup
9
+ gift_card = create_gift_card(
10
+ to: 'bcrouse@weblinc.com',
11
+ amount: 5.to_m
12
+ )
13
+
14
+ get storefront_api.gift_cards_balance_path,
15
+ params: { email: 'foo@weblinc.com', token: gift_card.token }
16
+
17
+ refute(response.ok?)
18
+ assert_equal(404, response.status)
19
+
20
+ get storefront_api.gift_cards_balance_path,
21
+ params: { email: 'bcrouse@weblinc.com', token: 'foo' }
22
+
23
+ refute(response.ok?)
24
+ assert_equal(404, response.status)
25
+
26
+ get storefront_api.gift_cards_balance_path,
27
+ params: { email: 'bcrouse@weblinc.com', token: gift_card.token }
28
+
29
+ assert(response.ok?)
30
+ results = JSON.parse(response.body)
31
+
32
+ assert_equal(gift_card.to, results['to'])
33
+ assert_equal(gift_card.token, results['token'])
34
+ assert_equal(gift_card.balance.cents, results['balance']['cents'])
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,161 @@
1
+ require 'test_helper'
2
+
3
+ module Workarea
4
+ if Plugin.installed?(:api)
5
+ module Api
6
+ module Storefront
7
+ class CheckoutIntegrationTest < Workarea::IntegrationTest
8
+ setup :tax, :product, :shipping_service, :gift_card
9
+
10
+ def tax
11
+ @tax ||= create_tax_category(
12
+ name: 'Sales Tax',
13
+ code: '001',
14
+ rates: [{ percentage: 0.07, country: 'US', region: 'PA' }]
15
+ )
16
+ end
17
+
18
+ def product
19
+ @product ||= create_product(
20
+ name: 'Integration Product',
21
+ variants: [
22
+ { sku: 'SKU1',
23
+ regular: 6.to_m,
24
+ tax_code: '001',
25
+ on_sale: true,
26
+ sale: 5.to_m }
27
+ ]
28
+ )
29
+ end
30
+
31
+ def shipping_service
32
+ @shipping_service ||= create_shipping_service(
33
+ name: 'Ground',
34
+ tax_code: '001',
35
+ rates: [{ price: 7.to_m }]
36
+ )
37
+ end
38
+
39
+ def gift_card
40
+ @gift_card ||= create_gift_card(amount: 5.to_m)
41
+ end
42
+
43
+ def complete_checkout
44
+ post storefront_api.carts_path
45
+ order_id = JSON.parse(response.body)['id']
46
+
47
+ post storefront_api.cart_items_path(order_id),
48
+ params: {
49
+ product_id: product.id,
50
+ sku: product.skus.first,
51
+ quantity: 2
52
+ }
53
+
54
+ patch storefront_api.checkout_path(order_id),
55
+ params: {
56
+ email: 'bcrouse@workarea.com',
57
+ billing_address: {
58
+ first_name: 'Ben',
59
+ last_name: 'Crouse',
60
+ street: '12 N. 3rd St.',
61
+ city: 'Philadelphia',
62
+ region: 'PA',
63
+ postal_code: '19106',
64
+ country: 'US',
65
+ phone_number: '2159251800'
66
+ },
67
+ shipping_address: {
68
+ first_name: 'Ben',
69
+ last_name: 'Crouse',
70
+ street: '22 S. 3rd St.',
71
+ city: 'Philadelphia',
72
+ region: 'PA',
73
+ postal_code: '19106',
74
+ country: 'US',
75
+ phone_number: '2159251800'
76
+ }
77
+ }
78
+
79
+ patch storefront_api.add_gift_card_checkout_path(order_id),
80
+ params: { gift_card_number: gift_card.token }
81
+
82
+ post storefront_api.complete_checkout_path(order_id),
83
+ params: {
84
+ payment: 'new_card',
85
+ credit_card: {
86
+ number: '1',
87
+ month: '1',
88
+ year: Time.now.year + 1,
89
+ cvv: '999'
90
+ }
91
+ }
92
+ end
93
+
94
+ def test_saves_order_info
95
+ complete_checkout
96
+
97
+ order = Order.first
98
+
99
+ assert_equal('bcrouse@workarea.com', order.email)
100
+ assert(order.placed_at.present?)
101
+ assert_equal(1, order.items.length)
102
+
103
+ assert_equal(product.id, order.items.first.product_id)
104
+ assert_equal(product.skus.first, order.items.first.sku)
105
+ assert_equal(2, order.items.first.quantity)
106
+
107
+ assert_equal(10.to_m, order.items.first.total_price)
108
+ assert_equal(1, order.items.first.price_adjustments.length)
109
+ assert_equal(10.to_m, order.items.first.price_adjustments.first.amount)
110
+
111
+ assert(order.items.first.on_sale?)
112
+ assert(order.items.first.product_attributes.present?)
113
+
114
+ payment = Payment.find(order.id)
115
+ assert_equal('Ben', payment.first_name)
116
+ assert_equal('Crouse', payment.last_name)
117
+ assert_equal('Ben', payment.address.first_name)
118
+ assert_equal('Crouse', payment.address.last_name)
119
+ assert_equal('12 N. 3rd St.', payment.address.street)
120
+ assert_equal('Philadelphia', payment.address.city)
121
+ assert_equal('PA', payment.address.region)
122
+ assert_equal('19106', payment.address.postal_code)
123
+ assert_equal('US', payment.address.country.alpha2)
124
+ assert_equal('2159251800', payment.address.phone_number)
125
+
126
+ assert_equal('Test Card', payment.credit_card.issuer)
127
+ assert_equal('XXXX-XXXX-XXXX-1', payment.credit_card.display_number)
128
+ assert_equal(1, payment.credit_card.month)
129
+ assert(payment.credit_card.year.present?)
130
+ assert_equal(13.19.to_m, payment.credit_card.amount)
131
+ assert(payment.credit_card.token.present?)
132
+ assert(payment.credit_card.saved_card_id.blank?)
133
+
134
+ assert_equal(1, payment.credit_card.transactions.length)
135
+ assert(payment.credit_card.transactions.first.response.present?)
136
+ assert_equal(13.19.to_m, payment.credit_card.transactions.first.amount)
137
+
138
+ assert_equal(gift_card.token, payment.gift_card.number)
139
+ assert_equal(5.to_m, payment.gift_card.amount)
140
+ assert_equal(1, payment.gift_card.transactions.length)
141
+ assert_equal(5.to_m, payment.gift_card.transactions.first.amount)
142
+ end
143
+
144
+ def test_reduces_balance_on_gift_card
145
+ complete_checkout
146
+
147
+ gift_card.reload
148
+ assert_equal(0.to_m, gift_card.balance)
149
+ assert_equal(5.to_m, gift_card.amount)
150
+ assert_equal(5.to_m, gift_card.used)
151
+
152
+ order = Order.first
153
+ redemption = gift_card.redemptions.first
154
+ assert_equal(order.placed_at, redemption.redeemed_at)
155
+ assert_equal(5.to_m, redemption.amount)
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,222 @@
1
+ require 'test_helper'
2
+
3
+ module Workarea
4
+ module Storefront
5
+ class GiftCardIntegrationTest < Workarea::IntegrationTest
6
+ setup :tax, :product, :shipping_service, :gift_card
7
+
8
+ def tax
9
+ @tax ||= create_tax_category(
10
+ name: 'Sales Tax',
11
+ code: '001',
12
+ rates: [{ percentage: 0.07, country: 'US', region: 'PA' }]
13
+ )
14
+ end
15
+
16
+ def product
17
+ @product ||= create_product(
18
+ name: 'Integration Product',
19
+ variants: [
20
+ { sku: 'SKU1',
21
+ regular: 6.to_m,
22
+ tax_code: '001',
23
+ on_sale: true,
24
+ sale: 5.to_m }
25
+ ]
26
+ )
27
+ end
28
+
29
+ def shipping_service
30
+ @shipping_service ||= create_shipping_service(
31
+ name: 'Ground',
32
+ tax_code: '001',
33
+ rates: [{ price: 7.to_m }]
34
+ )
35
+ end
36
+
37
+ def gift_card
38
+ @gift_card ||= create_gift_card(amount: 5.to_m)
39
+ end
40
+
41
+ def complete_checkout
42
+ post storefront.cart_items_path,
43
+ params: {
44
+ product_id: product.id,
45
+ sku: product.skus.first,
46
+ quantity: 2
47
+ }
48
+
49
+ patch storefront.checkout_addresses_path,
50
+ params: {
51
+ email: 'bcrouse@workarea.com',
52
+ billing_address: {
53
+ first_name: 'Ben',
54
+ last_name: 'Crouse',
55
+ street: '12 N. 3rd St.',
56
+ city: 'Philadelphia',
57
+ region: 'PA',
58
+ postal_code: '19106',
59
+ country: 'US',
60
+ phone_number: '2159251800'
61
+ },
62
+ shipping_address: {
63
+ first_name: 'Ben',
64
+ last_name: 'Crouse',
65
+ street: '22 S. 3rd St.',
66
+ city: 'Philadelphia',
67
+ region: 'PA',
68
+ postal_code: '19106',
69
+ country: 'US',
70
+ phone_number: '2159251800'
71
+ }
72
+ }
73
+
74
+ patch storefront.checkout_add_gift_card_path,
75
+ params: { gift_card_number: gift_card.token }
76
+
77
+ patch storefront.checkout_place_order_path,
78
+ params: {
79
+ payment: 'new_card',
80
+ credit_card: {
81
+ number: '1',
82
+ month: 1,
83
+ year: 2020,
84
+ cvv: '999'
85
+ }
86
+ }
87
+ end
88
+
89
+ def test_saves_order_info
90
+ complete_checkout
91
+
92
+ order = Order.first
93
+
94
+ assert_equal('bcrouse@workarea.com', order.email)
95
+ assert(order.placed_at.present?)
96
+ assert_equal(1, order.items.length)
97
+
98
+ assert_equal(product.id, order.items.first.product_id)
99
+ assert_equal(product.skus.first, order.items.first.sku)
100
+ assert_equal(2, order.items.first.quantity)
101
+
102
+ assert_equal(10.to_m, order.items.first.total_price)
103
+ assert_equal(1, order.items.first.price_adjustments.length)
104
+ assert_equal(10.to_m, order.items.first.price_adjustments.first.amount)
105
+
106
+ assert(order.items.first.on_sale?)
107
+ assert(order.items.first.product_attributes.present?)
108
+
109
+ payment = Payment.find(order.id)
110
+ assert_equal('Ben', payment.first_name)
111
+ assert_equal('Crouse', payment.last_name)
112
+ assert_equal('Ben', payment.address.first_name)
113
+ assert_equal('Crouse', payment.address.last_name)
114
+ assert_equal('12 N. 3rd St.', payment.address.street)
115
+ assert_equal('Philadelphia', payment.address.city)
116
+ assert_equal('PA', payment.address.region)
117
+ assert_equal('19106', payment.address.postal_code)
118
+ assert_equal('US', payment.address.country.alpha2)
119
+ assert_equal('2159251800', payment.address.phone_number)
120
+
121
+ assert_equal('Test Card', payment.credit_card.issuer)
122
+ assert_equal('XXXX-XXXX-XXXX-1', payment.credit_card.display_number)
123
+ assert_equal(1, payment.credit_card.month)
124
+ assert_equal(2020, payment.credit_card.year)
125
+ assert_equal(13.19.to_m, payment.credit_card.amount)
126
+ assert(payment.credit_card.token.present?)
127
+ assert(payment.credit_card.saved_card_id.blank?)
128
+
129
+ assert_equal(1, payment.credit_card.transactions.length)
130
+ assert(payment.credit_card.transactions.first.response.present?)
131
+ assert_equal(13.19.to_m, payment.credit_card.transactions.first.amount)
132
+
133
+ assert_equal(gift_card.token, payment.gift_card.number)
134
+ assert_equal(5.to_m, payment.gift_card.amount)
135
+ assert_equal(1, payment.gift_card.transactions.length)
136
+ assert_equal(5.to_m, payment.gift_card.transactions.first.amount)
137
+ end
138
+
139
+ def test_reduces_balance_on_gift_card
140
+ complete_checkout
141
+
142
+ gift_card.reload
143
+ assert_equal(0.to_m, gift_card.balance)
144
+ assert_equal(5.to_m, gift_card.amount)
145
+ assert_equal(5.to_m, gift_card.used)
146
+
147
+ order = Order.first
148
+ redemption = gift_card.redemptions.first
149
+ assert_equal(order.placed_at, redemption.redeemed_at)
150
+ assert_equal(5.to_m, redemption.amount)
151
+ end
152
+
153
+ def test_generates_gift_card
154
+ product = create_product(
155
+ name: 'Gift Card',
156
+ gift_card: true,
157
+ digital: true,
158
+ customizations: 'gift_card',
159
+ variants: [{ sku: 'GIFTCARD', regular: 10.to_m }]
160
+ )
161
+
162
+ post storefront.cart_items_path,
163
+ params: {
164
+ product_id: product.id,
165
+ sku: product.skus.first,
166
+ email: 'recipient@workarea.com',
167
+ from: 'from@workarea.com',
168
+ message: 'this is a message',
169
+ quantity: 1
170
+ }
171
+
172
+ patch storefront.checkout_addresses_path,
173
+ params: {
174
+ email: 'bcrouse@workarea.com',
175
+ billing_address: {
176
+ first_name: 'Ben',
177
+ last_name: 'Crouse',
178
+ street: '12 N. 3rd St.',
179
+ city: 'Philadelphia',
180
+ region: 'PA',
181
+ postal_code: '19106',
182
+ country: 'US',
183
+ phone_number: '2159251800'
184
+ }
185
+ }
186
+
187
+ patch storefront.checkout_place_order_path,
188
+ params: {
189
+ payment: 'new_card',
190
+ credit_card: {
191
+ number: '1',
192
+ month: 1,
193
+ year: 2020,
194
+ cvv: '999'
195
+ }
196
+ }
197
+
198
+ assert_equal(2, Payment::GiftCard.count)
199
+
200
+ card = Payment::GiftCard.desc(:created_at).first
201
+ assert_equal(10.to_m, card.amount)
202
+ assert_equal(0.to_m, card.used)
203
+ assert_equal(10.to_m, card.balance)
204
+ assert(card.order_id.present?)
205
+
206
+ assert_equal(2, ActionMailer::Base.deliveries.length)
207
+
208
+ assert(card.purchased?)
209
+
210
+ gift_card_email = ActionMailer::Base.deliveries.detect do |email|
211
+ assert_includes(email.to, 'recipient@workarea.com')
212
+ end
213
+
214
+ gift_card_email.parts.each do |part|
215
+ assert_includes(part.body, card.token)
216
+ assert_includes(part.body, 'from@workarea.com')
217
+ assert_includes(part.body, 'this is a message')
218
+ end
219
+ end
220
+ end
221
+ end
222
+ end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ module Workarea
4
+ module Catalog
5
+ class GiftCardProductTest < TestCase
6
+ def test_gift_card?
7
+ product = Catalog::Product.new(template: 'gift_card')
8
+ assert(product.gift_card?)
9
+
10
+ product = Catalog::Product.new(gift_card: true)
11
+ assert(product.gift_card?)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ require 'test_helper'
2
+
3
+ module Workarea
4
+ class GiftCardPaymentTest < TestCase
5
+ def test_set_gift_card
6
+ payment = create_payment
7
+
8
+ payment.set_gift_card(number: '1234')
9
+ assert_equal('1234', payment.gift_card.number)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,127 @@
1
+ require 'test_helper'
2
+
3
+ module Workarea
4
+ class Payment
5
+ class GiftCardTest < TestCase
6
+ def test_not_expired
7
+ expired = Workarea::Payment::GiftCard.create!(amount: 10.to_m, expires_at: Time.now - 1.day)
8
+ not_expired = Workarea::Payment::GiftCard.create!(amount: 10.to_m, expires_at: Time.now + 1.day)
9
+ no_date = Workarea::Payment::GiftCard.create!(amount: 10.to_m, expires_at: '')
10
+
11
+ results = Workarea::Payment::GiftCard.not_expired.to_a
12
+ assert_includes(results, not_expired)
13
+ assert_includes(results, no_date)
14
+ refute_includes(results, expired)
15
+ end
16
+
17
+ def test_search_by_token_or_recipient
18
+ one = Workarea::Payment::GiftCard.create!(amount: 10.to_m, token: '123', to: 'zac@workarea.com')
19
+ two = Workarea::Payment::GiftCard.create!(amount: 10.to_m, token: '456', to: 'owen@workarea.com')
20
+
21
+ results = Workarea::Payment::GiftCard
22
+ .search_by_token_or_recipient('zac@workarea.com')
23
+
24
+ assert_includes(results, one)
25
+ refute_includes(results, two)
26
+ end
27
+
28
+ def test_purchase
29
+ card = Workarea::Payment::GiftCard.create!(amount: 10.to_m)
30
+
31
+ card.purchase(500)
32
+ card.reload
33
+ assert_equal(10.to_m, card.amount)
34
+ assert_equal(5.to_m, card.balance)
35
+ assert_equal(5.to_m, card.used)
36
+
37
+ assert_raise(Payment::InsufficientFunds) do
38
+ card.purchase(5000)
39
+ end
40
+ end
41
+
42
+ def test_refund_class_method
43
+ card = Workarea::Payment::GiftCard.create!(amount: 10.to_m)
44
+
45
+ Workarea::Payment::GiftCard.refund(card.token, 500)
46
+ card.reload
47
+
48
+ assert_equal(10.to_m, card.amount)
49
+ assert_equal(15.to_m, card.balance)
50
+ assert_equal(-5.to_m, card.used)
51
+ end
52
+
53
+ def test_refund_instance_method
54
+ card = Workarea::Payment::GiftCard.create!(amount: 10.to_m)
55
+
56
+ card.refund(500)
57
+ card.reload
58
+ assert_equal(10.to_m, card.amount)
59
+ assert_equal(15.to_m, card.balance)
60
+ assert_equal(-5.to_m, card.used)
61
+ end
62
+
63
+ def test_find_by_token_and_email
64
+ card = Workarea::Payment::GiftCard.create!(
65
+ amount: 10.to_m,
66
+ token: '123',
67
+ to: 'test@workarea.com'
68
+ )
69
+
70
+ assert_equal(
71
+ card,
72
+ Workarea::Payment::GiftCard.find_by_token_and_email(
73
+ '123',
74
+ 'test@workarea.com'
75
+ )
76
+ )
77
+
78
+ assert_nil(
79
+ Workarea::Payment::GiftCard.find_by_token_and_email(
80
+ '123',
81
+ 'noone@workarea.com'
82
+ )
83
+ )
84
+
85
+ assert_nil(
86
+ Workarea::Payment::GiftCard.find_by_token_and_email(
87
+ '321',
88
+ 'test@workarea.com'
89
+ )
90
+ )
91
+ end
92
+
93
+ def test_to=
94
+ card =
95
+ Workarea::Payment::GiftCard.create!(
96
+ amount: 10.to_m,
97
+ token: 'TESTTOKEN',
98
+ to: 'TEST@WORKAREA.COM'
99
+ )
100
+
101
+ assert_equal('test@workarea.com', card.to)
102
+ assert_equal('testtoken', card.token)
103
+
104
+ card.to = nil
105
+ assert_nil(card.to)
106
+ end
107
+
108
+ def test_last_redeemed_at
109
+ card =
110
+ Workarea::Payment::GiftCard.create!(
111
+ amount: 2.to_m,
112
+ token: '123',
113
+ to: 'test@workarea.com'
114
+ )
115
+
116
+ assert_nil(card.last_redeemed_at)
117
+
118
+ one_day_ago = 1.day.ago
119
+ two_days_ago = 2.days.ago
120
+ card.redemptions.create!(amount: 1.to_m, redeemed_at: one_day_ago)
121
+ card.redemptions.create!(amount: 1.to_m, redeemed_at: two_days_ago)
122
+
123
+ assert_equal(one_day_ago.to_i, card.last_redeemed_at.to_i)
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+
3
+ module Workarea
4
+ class Payment
5
+ module Purchase
6
+ class GiftCardTest < TestCase
7
+ setup :gift_card
8
+
9
+ def gift_card
10
+ @gift_card ||= Payment::GiftCard.create!(token: 'token', amount: 5.to_m)
11
+ end
12
+
13
+ def payment
14
+ @payment ||= create_payment
15
+ end
16
+
17
+ def test_complete!
18
+ payment.set_gift_card(number: 'token')
19
+
20
+ transaction = payment.gift_card.build_transaction(amount: 10.to_m)
21
+
22
+ GiftCard.new(payment.gift_card, transaction).complete!
23
+ refute(transaction.success?)
24
+ assert(transaction.message.present?)
25
+
26
+ transaction = payment.gift_card.build_transaction(amount: 3.to_m)
27
+ purchase = GiftCard.new(payment.gift_card, transaction)
28
+ purchase.complete!
29
+
30
+ assert(transaction.success?)
31
+ assert_equal(3.to_m, transaction.amount)
32
+ end
33
+
34
+ def test_cancel!
35
+ payment.set_gift_card(number: 'token')
36
+ transaction = payment.gift_card.build_transaction(amount: 5.to_m)
37
+
38
+ purchase = GiftCard.new(payment.gift_card, transaction)
39
+ purchase.cancel!
40
+
41
+ refute(transaction.cancellation.present?)
42
+
43
+ purchase.complete!
44
+ assert(transaction.success?)
45
+
46
+ purchase.cancel!
47
+ assert(transaction.cancellation.present?)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end